图像生成API服务

ComfyUI MCP Server是一个实现模型上下文协议(MCP)的全功能服务,为AI代理提供标准化的API来生成和操作图像。

  • by l2dnjsrud
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 l2dnjsrud/comfyui-mcp-server

使用说明

ComfyUI MCP服务器

ComfyUI的模型上下文协议(MCP)的全功能实现,使人工智能代理能够使用标准化的API生成和操作图像。

📋 目录

🌟 引言

ComfyUI MCP服务器 通过实现ComfyUI的模型上下文协议(MCP),弥合了AI代理和图像生成之间的差距。

模型上下文协议是人工智能代理通过丰富的元数据和模式信息发现工具并与之交互的标准化方式。此实现允许任何AI助手通过ComfyUI使用稳定扩散模型轻松生成和操纵图像。

🔍 什么是MCP? 模型上下文协议(MCP)规范了AI代理与外部工具的通信方式,实现了具有丰富元数据和功能描述的一致接口。

🚀 特性

  • 完整的MCP协议实施

    • 使用元数据和模式进行工具发现
    • 会话和上下文管理
    • 实时通信
  • 丰富的图像生成功能

    • 文本到图像(txt2img)
    • 图像到图像转换(img2img)
    • 修复
    • ControlNet支持
    • LoRA模型集成
  • 先进的基础设施

    • 具有优先级支持的作业队列
    • 进度跟踪和实时更新
    • 身份验证和速率限制
    • 会话持续
    • 作业历史数据库后端

💻 安装

先决条件

  • Python 3.10+
  • ComfyUI已安装并正在运行
  • 建议使用4GB+RAM
  • 建议使用CUDA兼容GPU(但不是必需的)

选项1:克隆并安装

# Clone the repository
git clone https://github.com/yourusername/comfyui-mcp-server.git
cd comfyui-mcp-server

# Install dependencies
pip install -r requirements.txt

# Run the installation script
python install.py --create-venv

选项2:手动设置

# Create and prepare project directory
mkdir comfyui-mcp-server
cd comfyui-mcp-server

# Copy the source files into this directory

# Install required packages
pip install websockets requests aiohttp aiohttp_cors aiohttp_session cryptography aiosqlite mcp

🏃‍♂️ 快速开始

第一步:启动ComfyUI

在运行MCP服务器之前,请确保先启动ComfyUI:

cd /path/to/ComfyUI
python main.py --port 8188

通过访问验证ComfyUI是否正在运行http://localhost:8188在您的浏览器中。

步骤2:启动MCP服务器

cd /path/to/comfyui-mcp-server

# Run with default settings
python mcp_integration.py

# Or run with custom config
python mcp_integration.py --config my_config.json

步骤3:测试连接

运行MCP客户端以验证一切正常:

# Check available tools
python mcp_client.py --action manifest

# Generate a test image
python mcp_client.py --action generate --prompt "a dog wearing sunglasses"

📝 使用示例

基本图像生成

从文本提示生成图像:

python mcp_client.py --action generate --prompt "a cat in space" --width 768 --height 512

图像到图像转换

根据提示转换现有图像:

python mcp_client.py --action img2img --prompt "watercolor painting" --image input.jpg --strength 0.75

列出可用型号

查看可使用的型号:

python mcp_client.py --action list-models

在Python脚本中使用

import asyncio
from mcp_client import MCPClient

async def generate_images():
    client = MCPClient()
    await client.connect()
    
    # Generate an image
    result = await client.generate_image(
        prompt="an astronaut riding a horse on mars",
        width=768,
        height=512
    )
    
    # Get job ID and wait for completion
    job_id = result["job_id"]
    await client.subscribe_to_job(job_id)
    final_status = await client.wait_for_job_completion(job_id)
    
    # Print result
    print(f"Generated image: {final_status['result']['image_url']}")
    
    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(generate_images())

📂 项目结构

comfyui-mcp-server/
├── mcp_protocol.py     # Core MCP protocol implementation
├── mcp_integration.py  # MCP server implementation
├── mcp_client.py       # MCP client
├── job_queue.py        # Job queue system
├── db_manager.py       # Database management
├── model_manager.py    # Model management
├── workflow_analyzer.py # Workflow analysis
├── auth.py             # Authentication & authorization
├── progress_tracker.py # Progress tracking
├── config.json         # Configuration file
├── workflows/          # Workflow definition files
│   ├── basic_api_test.json
│   ├── img2img.json
│   ├── inpaint.json
│   └── simple.json
├── data/               # Data storage folder (created on run)
└── logs/               # Log files (created on run)

📚 api参考

可用的MCP工具

工具名称 描述 关键参数
generate_image 从文本 提示、宽度、高度、negative_prompt、模型生成图像
img2img 根据文本 提示、图像、强度、宽度、高度转换图像
get_job_status 检查作业的状态 job_id
list_models 列出可用型号 -
get_tools_manifest 获取完整的工具清单 -

配置选项

这 config.json 文件包含以下关键设置:

{
  "comfyui_url": "http://localhost:8188",
  "comfyui_ws_url": "ws://localhost:8188/ws",
  "mcp_server_host": "localhost",
  "mcp_server_port": 9000,
  "enable_auth": false,
  "max_concurrent_jobs": 2
}

🔧 高级用法

设置身份验证

  1. 编辑 config.json 要启用身份验证,请执行以下操作:

    {
      "enable_auth": true,
      "auth_config_path": "auth_config.json"
    }
    
  2. 创建或修改 auth_config.json:

    {
      "default_client": {
        "api_key": "YOUR_API_KEY_HERE",
        "permissions": ["*"],
        "rate_limit": {
          "rate": 5,
          "per": 60,
          "burst": 10
        }
      }
    }
    
  3. 在客户端请求中使用API密钥:

    python mcp_client.py --api-key YOUR_API_KEY_HERE --action generate --prompt "test"
    

自定义工作流

  1. 以API格式从ComfyUI导出工作流(在设置中启用开发模式)
  2. 将JSON文件保存到 workflows/ 目录
  3. 与客户端一起使用:
    python mcp_client.py --action generate --prompt "test" --workflow my_custom_workflow
    

❓ 故障排除

连接问题

问题:无法连接到ComfyUI 解决方案:确保ComfyUI正在运行,并且可以在配置的URL上访问:

curl http://localhost:8188/system_stats

问题:MCP服务器无法启动 解决方案:检查日志 logs 目录并验证端口9000是否可用:

lsof -i :9000    # On Linux/Mac
# or
netstat -ano | findstr :9000    # On Windows

图像生成问题

问题:生成失败,出现工作流错误 解决方案:

  • 检查ComfyUI是否安装了所需的型号
  • 更新工作流程JSON以匹配您的ComfyUI安装
  • 验证种子值是否为正整数
  • 检查工作流文件中的JSON编码

问题:图像生成过程中超时 解决方案:增加中的超时值 advanced_comfyui_client.py:

max_attempts = 300  # 5 minutes

🤝 贡献

欢迎投稿!以下是一些参与方式:

  1. 报告错误和请求功能 通过打开问题
  2. 提交拉取请求 用于修复错误或增强功能
  3. 创建新工作流 用于不同的生成任务
  4. 改进文档 帮助新用户

请按照以下步骤操作:

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

📄 许可证

此项目根据Apache许可证2.0获得许可-请参阅 许可证 文件以获取详细信息。


Made with ❤️ for AI and image generation

Report Bug • Request Feature