下拉链接MCP(模型上下文协议)服务器
一款基于FastAPI的智能助手,与Droplink电子商务平台集成,通过OpenAI的助手API提供人工智能产品管理功能,并生成高级人工智能图像。
🚀 特性
- 人工智能驱动的产品管理:通过自然语言对话创建、列出和管理Droplinked产品
- 🎨 AI模型生成:
- 使用OpenAI的GPT-Image-1模型生成专业的AI增强产品模型 - 自动服装检测-不同姿势/设置的模型上显示的服装 - 经济高效的发电(3个专业模型约0.033美元) - 时尚与一般产品的智能提示
- 📦 完整的产品管理:
- 获取产品详细信息:查看全面的产品信息,包括定价、媒体和设置 - 更新产品:修改产品字段,如标题、描述、价格、收藏、标签等 - 删除产品:删除带有安全确认的产品 - 产品发现:列出并搜索您的产品目录
- 增强的图像上传流程:
- 通过直观的工作流程上传多张产品图片 - 在产品创建过程中逐步添加图像 - 首次上传图像后提供AI模型选项 - 清除选项以继续或添加更多图像 - 上传单张图片后不会过早创建产品
- 简化工作流程:单一功能方法,具有明确的基于行动的步骤
- 收集管理:浏览产品并将其组织到收藏中
- 安全认证:使用Droplinked API进行基于JWT的身份验证
- 交互式Web界面:具有拖放图像上传功能的现代聊天用户界面
- OpenAI助手集成:利用OpenAI的助手API进行智能响应
- 最终确认流程:在创建产品之前,始终需要明确的用户确认
- 实时通信:通过FastAPI端点获得类似WebSocket的体验
- 🔧 稳健的错误处理:通过自动重试和全面的日志记录增强错误处理
- 📊 改进的API响应处理:更好地处理各种Droplink API响应格式
📋 先决条件
- Python 3.8+
- OpenAI API帐户和API密钥
- Droplink帐户和API访问
- 现代网络浏览器
🛠️ 安装
- 克隆存储库
git clone
cd droplinked_mcp- 创建并激活虚拟环境
python -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate- 安装依赖项
pip install -r requirements.txt⚙️ 配置
- 创建环境文件
创建一个 .env 项目根目录中的文件,包含以下变量:
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
ASSISTANT_ID=your_openai_assistant_id_here
# Droplinked Configuration (Optional - for test user workaround)
STATIC_DROPLINKED_JWT=your_static_jwt_for_testing- OpenAI助手设置
- 在OpenAI仪表板中创建OpenAI助手 - 使用以下工具配置助手: - list_my_droplinked_products:列出用户的产品 - create_new_droplinked_product:创建新产品(有图像支持) - get_droplinked_shop_collections:获取产品系列 - handle_image_upload_response:处理上传的图像 - 将助理ID复制到您的 .env 文件
- Droplink API配置
- 应用程序使用Droplinked API基础URL: https://apiv3.droplinked.com - 图片上传使用Droplinked上传服务: https://tools.droplinked.com/upload - 身份验证是通过 /auth/login/basic 端点 - 测试用户解决方法可用于 mathofdynamic@gmail.com
- AI模型配置
- 使用OpenAI的GPT-Image-1模型生成专业模型 - GPT-4V自动服装检测 - 成本结构: - 低质量:每张图片0.011美元 - 中等质量:每张图片0.042美元 - 高品质:每张图片0.167美元 - 默认设置:低质量以实现成本效益(3个模型约0.033美元) - 智能提示区分服装和一般产品
🚀 运行应用程序
- 启动服务器
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- 访问应用程序
- 打开浏览器并导航到 http://localhost:8000 - 您将看到Droplinked MCP Assistant的聊天界面
- 认证
- 访问 http://localhost:8000/login 使用您的Droplinked凭据进行身份验证 - JWT令牌将用于后续API调用
📁 项目结构
droplinked_mcp/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point
│ ├── core/
│ │ └── config.py # Configuration settings
│ ├── models/ # Pydantic models (currently empty)
│ ├── routers/
│ │ └── chatbot_router.py # Chat API endpoints
│ └── services/
│ ├── auth_service.py # Authentication logic
│ ├── droplinked_api_service.py # Droplinked API integration
│ ├── ai_mockup_service.py # AI image generation service
│ └── nlu_service.py # Natural language understanding
├── openai_config/ # OpenAI Assistant configuration
│ ├── assistant_system_instructions.md
│ ├── updated_function_definition.json
│ └── simplified_instructions.md
├── static/
│ ├── script.js # Frontend JavaScript
│ └── style.css # Frontend styling
├── templates/
│ ├── index.html # Main chat interface
│ └── login.html # Login page
├── tests/ # Test files
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
└── README.md # This file🔧 API终点
认证
GET /login-登录页面POST /auth/mcp/login-对用户进行身份验证并获取JWT令牌
聊天界面
GET /-主聊天界面POST /chatbot/message-向AI助手发送消息
图片上传
POST /upload/image-上传产品图片(需要身份验证)
健康检查
GET /health-应用程序运行状况
🤖 可用的AI工具
OpenAI Assistant使用简化的单功能方法:
管理_推广_产品 (主要功能)
通过动作参数处理所有产品创建步骤的综合功能:
- 行动:
create_product-数据收集阶段
- 收集所需信息:标题、描述、收藏、价格、数量、重量 - 指导用户逐步收集数据
- 行动:
upload_images-图像处理阶段
- 处理上传的产品图片 - 支持每个产品上传多张图片 - 提供AI模型生成选项
- 行动:
generate_ai_mockups-AI增强阶段
- 使用GPT-Image-1生成3个专业AI增强模型 - 自动检测服装项目并将其显示在模特身上 - 创建工作室、生活方式和高级时尚照片 - 成本:3个高质量模型约0.033美元
- 行动:
show_summary-确认阶段
- 创建前显示完整的产品摘要 - 显示所有收集的数据,包括图像 - 需要明确的用户确认
- 行动:
confirm_creation-最终创建阶段
- 在Droplinked上实际创建产品 - 仅在用户看到并确认摘要后执行
支持功能
- list_my_droplinked_products -按页码列出用户的现有产品
- get_droplinked_shop_collections -检索可用的产品集合
产品管理功能
- 管理产品运营 -获取、更新或删除产品的统一功能(推荐)
- 行动: get_product -按ID检索详细的产品信息 - 行动: update_product -修改产品字段(标题、描述、价格、收藏、标签等) - 行动: delete_product -删除带有安全确认的产品
- get_product_by_id -获取特定产品的详细信息
- update_product -使用粒度控制更新特定产品字段
- 删除产品 -删除产品(需要明确确认)
💬 使用示例
使用AI模型生成创建产品
User: "I want to create a new product"
Assistant: "I'll help you create a new product. What is the title (name) of the product?"
User: "Vintage Denim Jacket"
Assistant: "What is the description for this product?"
User: "Classic vintage-style denim jacket with distressed details"
... (continues with guided setup including collection selection)
Assistant: "Would you like to upload product images? Please use the image upload feature in the chat interface, or type 'skip' to continue without images."
User: [Uploads first image using the camera button]
Assistant: "Great! I've uploaded your first image. You now have 1 image for your product.
🎨 **AI Mockup Option**: I can generate 3 professional AI-enhanced mockups from your image (cost: ~$0.033). For clothing items, I'll show them on models in different settings!
Would you like to:
1. **Generate AI mockups** (recommended for better sales)
2. Upload more images manually
3. Continue with just your original image
Type 'generate mockups', 'upload more', or 'continue' to proceed."
User: "generate mockups"
Assistant: "🎉 Successfully generated 3 AI mockups! You now have 4 images total (1 original + 3 AI-enhanced). The AI has created professional versions showing your product in different styles and settings."
User: "create it"
Assistant: "Here's a summary of your product:
- Title: Vintage Denim Jacket
- Description: Classic vintage-style denim jacket with distressed details
- Collection: Fashion
- Price: $89.99
- Quantity: 25
- Weight: 1.2
- Images: 4 image(s) uploaded
1. original_jacket.jpg
2. ai_studio_model.png (AI-generated)
3. ai_lifestyle_photo.png (AI-generated)
4. ai_premium_fashion.png (AI-generated)
Would you like to:
1. Create the product as is
2. Add more images
3. Make changes to the details
Please type 'create', 'add images', or tell me what you'd like to change."
User: "create"
Assistant: "Product 'Vintage Denim Jacket' created successfully with 4 professional images!"产品列表
User: "Show me my products"
Assistant: [Lists all products with details]
User: "Show me the first 5 products"
Assistant: [Lists first 5 products]管理收藏
User: "What collections do I have?"
Assistant: [Lists available collections]上传产品图片
User: "I want to create a new product"
Assistant: [Guides through product creation process]
Assistant: "Would you like to upload images for this product?"
User: [Clicks the camera button (📷) in the chat interface]
User: [Selects multiple image files or drags and drops them]
Assistant: "I've received 3 images for your product. Processing..."
Assistant: "Images uploaded successfully! Continuing with product creation..."AI模型生成
User: "I want to create a product called 'Summer Dress'"
Assistant: [Guides through product creation]
User: [Uploads dress image]
Assistant: "🎨 AI Mockup Option available! Generate 3 professional mockups?"
User: "yes"
Assistant: "✅ Generated 3 AI mockups showing the dress on models in studio, lifestyle, and premium settings!"获取产品信息
User: "Show me details for my product with ID 64f1a2b3c4d5e6f7g8h9i0j1"
Assistant: "📦 **Product: Vintage Denim Jacket**
- **ID**: 64f1a2b3c4d5e6f7g8h9i0j1
- **Description**: Classic vintage-style denim jacket with distressed details
- **Price Range**: $79.99 - $89.99
- **Collection**: Fashion
- **Type**: NORMAL
- **Images**: 4 uploaded
- **Status**: PUBLISHED
- **Affiliate**: Yes (15% commission)"
User: "What products do I have?"
Assistant: [Lists products with titles and IDs, then asks which one to view in detail]更新产品
User: "I want to update the price of my denim jacket to $95"
Assistant: "I'll help you update your product. First, let me get the current details..."
Assistant: [Shows current product information]
Assistant: "I can see your Vintage Denim Jacket is currently priced at $89.99. Would you like me to update it to $95.00?"
User: "Yes, update it"
Assistant: "✅ **Product Updated Successfully**
- **Product**: Vintage Denim Jacket (64f1a2b3c4d5e6f7g8h9i0j1)
- **Updated Fields**: price
- **Changes Applied**: Price updated from $89.99 to $95.00"
User: "Change the description of product 64f1a2b3c4d5e6f7g8h9i0j1 to 'Premium vintage denim jacket with authentic distressed finish'"
Assistant: [Gets current product, shows current description, then updates it]
Assistant: "✅ Description updated successfully!"删除产品
User: "Delete product 64f1a2b3c4d5e6f7g8h9i0j1"
Assistant: "⚠️ **Deletion Confirmation Required**
I found this product to delete:
📦 **Product: Vintage Denim Jacket**
- **ID**: 64f1a2b3c4d5e6f7g8h9i0j1
- **Price Range**: $95.00
- **Images**: 4 uploaded
- **Status**: PUBLISHED
**This action is irreversible.** Are you sure you want to permanently delete this product?"
User: "Yes, delete it"
Assistant: "✅ Product 'Vintage Denim Jacket' (64f1a2b3c4d5e6f7g8h9i0j1) has been permanently deleted from your store."产品发现和管理
User: "Show me all my products in the Fashion collection"
Assistant: [Lists products filtered by collection]
User: "Which of my products can be affiliated?"
Assistant: [Shows products with affiliate settings enabled]
User: "Update all my products to allow affiliate marketing with 20% commission"
Assistant: [Guides through bulk update process with confirmations]图像上传功能:
- 支持多种图像格式(JPEG、PNG、GIF、WebP)
- 拖放界面
- 实时上传进度指标
- 图像预览功能
- 最大文件大小:每张图像10MB
- 如果不需要,可以跳过图像上传
AI模型功能:
- GPT-4V自动服装检测
- 使用GPT-Image-1生成专业模型
- 模特在不同姿势/环境中展示的时尚单品
- 经济高效:3张专业图片约0.033美元
- 不同产品类型的智能提示
- 与产品创建工作流程无缝集成
## 🔒 Security Features
- **JWT Authentication**: Secure token-based authentication with Droplinked
- **Session Management**: In-memory session storage (suitable for development)
- **Error Handling**: Comprehensive error handling with detailed logging
- **Input Validation**: Pydantic models for request validation
## 🧪 Development
### Running TestsRun all tests
python -m pytest tests/
Run with coverage
python -m pytest tests/ --cov=app
### 开发模式
Run with auto-reload
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
### 调试
- 通过检查控制台输出启用调试日志记录
- 记录所有API调用和响应以进行故障排除
- OpenAI Assistant交互会被详细的日志跟踪
## 🚨 生产注意事项
在部署到生产环境之前,请考虑:
1. **会话存储**:用Redis或数据库替换内存中的会话存储
1. **环境变量**:使用适当的秘密管理
1. **超文本传输安全协议**:启用SSL/TLS加密
1. **速率限制**:实施API速率限制
1. **监控**:添加应用程序监控和日志记录
1. **数据库**:考虑会话和用户数据的持久存储
## 🐛 故障排除
### 常见问题
1. **OpenAI客户端未初始化**
- 确保 `OPENAI_API_KEY` 已设置 `.env`
- 验证API密钥是否有效
1. **未找到助理ID**
- 创建一个OpenAI助手并将ID添加到 `.env`
- 确保助手配置了所需的工具
1. **Droplinked身份验证失败**
- 检查您的Droplinked凭据
- 验证API终结点是否可访问
- 对于测试用户,确保 `STATIC_DROPLINKED_JWT` 已设置
1. **工具执行错误**
- 检查JWT令牌是否有效且未过期
- 验证Droplink API权限
- 查看控制台日志以了解详细的错误消息
1. **图像上传问题**
- 确保支持文件格式(JPEG、PNG、GIF、WebP)
- 检查文件大小是否低于10MB限制
- 验证身份验证令牌是否有效
- 检查浏览器控制台是否存在上传错误
- 确保Droplinked上传服务可访问
1. **AI模型生成问题**
- 验证OpenAI API密钥是否可以访问GPT-Image-1模型
- 检查是否支持图像格式(如果需要,会自动转换为JPEG)
- 确保有足够的OpenAI API信用用于图像生成
- 查看控制台日志,了解来自OpenAI API的详细错误消息
- 在人工智能处理之前,验证从URL下载图像是否成功
1. **产品更新错误(v2.1.0中已修复)**
- ✅ **已解决**: `'str' object has no attribute 'get'` 产品更新过程中出错
- ✅ **已解决**:更新现在可以正确处理Droplinked API中的字典和字符串SKU响应
- 如果您仍然看到更新错误,请确保您使用的是最新版本
1. **依赖性问题(在v2.1.0中修复)**
- ✅ **已解决**:枕头导入错误 `_imaging` 模块
- ✅ **已解决**: `jiter` 影响OpenAI客户端的依赖性问题
- **解决方案已应用**:重新安装枕头10.4.0和抖动器以保持兼容性
- 如果遇到依赖关系问题,请尝试:pip uninstall Pillow jiter -y pip install Pillow==10.4.0 jiter
1. **服务器启动问题**
- **PIL/\_imaging的导入错误**:运行 `pip uninstall Pillow -y && pip install Pillow==10.4.0`
- **没有名为“jiter.jiter”的模块**:运行 `pip uninstall jiter -y && pip install jiter`
- **OpenAI客户端故障**:确保OpenAI和jiter软件包都已正确安装
- 检查Python版本兼容性(需要Python 3.8+)
1. **成功操作后出现错误消息**
- ✅ **固定的**:实际成功但显示错误消息的产品更新
- ✅ **固定的**:导致操作后错误的SKU响应解析
- 操作现在可以正确报告成功/失败状态
### 调试步骤
1. **检查服务器状态**
# Verify Python processes are running Get-Process | Where-Object {$_.ProcessName -like "*python*"}
# Test server response (PowerShell) Invoke-WebRequest -Uri http://localhost:8000/ -UseBasicParsing
1. **审核日志**
- 检查控制台输出以获取详细的错误消息
- 查找带有DEBUG/ERROR前缀的行
- 记录API响应代码和错误详细信息
1. **验证依赖关系**
pip list | grep -E "(openai|pillow|jiter|fastapi)"
1. **重新启动新鲜**
# Stop server (Ctrl+C) # Reinstall problematic dependencies if needed pip uninstall Pillow jiter -y pip install Pillow==10.4.0 jiter # Restart server uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
## 📝 依赖项
- **快速API**:用于构建API的现代web框架
- **乌维科恩**:用于运行FastAPI应用程序的ASGI服务器
- **HTTPX**:用于带有重试逻辑的API调用的异步HTTP客户端
- **派丹蒂克**:使用Python类型注释进行数据验证
- **python dotenv**:环境变量管理
- **Jinja2**:用于HTML渲染的模板引擎
- **OpenAI**:官方OpenAI Python客户端(包括GPT-Image-1支持)
- **python多部分**:支持多部分表单数据(文件上传)
- **枕头**:用于格式转换和优化的图像处理(v10.4.0用于稳定性)
- **即时编译器**:OpenAI客户端所需的JSON解析库
## 🤝 贡献
1. 分叉存储库
1. 创建要素分支(`git checkout -b feature/amazing-feature`)
1. 提交您的更改(`git commit -m 'Add some amazing feature'`)
1. 推到分支(`git push origin feature/amazing-feature`)
1. 打开拉取请求
## 📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
## 🆘 支持
有关支持和问题:
- 检查上面的故障排除部分
- 查看控制台日志以了解详细的错误消息
- 确保所有环境变量都已正确配置
- 验证OpenAI Assistant设置和工具配置
## 🔄 版本历史记录
- **v2.1.0**:Bug修复和稳定性改进
- 🐛 **已修复产品更新错误**:已解决 `'str' object has no attribute 'get'` 产品更新过程中出错
- 🔧 **增强SKU响应处理**:使用混合SKU格式更好地处理Droplinked API响应
- 📦 **依赖修复**:解决了Pillow导入错误和抖动兼容性问题
- 🚀 **提高服务器稳定性**:更好的错误处理和恢复机制
- 🛠️ **增强调试**:更详细的日志记录和故障排除指南
- ✅ **准确的状态报告**:修复了成功操作后出现的错误消息
- 🔄 **稳健的请求处理**:添加了重试逻辑和更好的超时管理
- **v2.0.0版本**:AI模型生成和简化工作流程
- 🎨 **AI模型生成**:使用OpenAI GPT-Image-1的专业产品模型
- **智能服装检测**:基于模型的模型服装自动检测
- **单功能架构**:流线型 `manage_droplinked_product` 基于动作的工作流
- **增强的用户体验**:清晰的分步指导,有适当的确认流程
- **经济高效的人工智能**:生成3个专业模型,价格约为0.033美元
- **改进了错误处理**:更好的图像格式支持和转换
- **更新OpenAI集成**:最新的助手API,具有全面的函数定义
- **v1.1.0版本**:图像上传集成
- 添加了产品图片上传功能
- 与Droplinked的上传服务集成
- 通过图像支持增强产品创建流程
- 新增拖放图片上传界面
- **v1.0.0**:具有核心功能的初始版本
- OpenAI助手集成
- Droplink API集成
- 基于Web的聊天界面
- 产品管理工具