ComfyUI_MCP Server 是一个松散耦合、可扩展、基于配置的 ModelContextProtocol 服务器,专为 ComfyUI 设计。支持自定义工作流程中的 MCP 服务(工具),如 txt2img 和 img2img。每个 MCP 服务(工具)的参数和行为可以通过 JSON 初始化和 MCP 工具装饰器模块灵活扩展,使其适用于人工智能绘图、推理等场景的自动化和集成。
mcp_server/
├── mcp_server/
│ ├── mcpserver.py # 主入口,自动注册 tools 目录下所有工具 | Main entry, auto-registers all tools in the tools directory
│ ├── tools/ # 工具模块与mcp tool配置(每个mcp tool一个py和json)| Tool modules and configs (one .py and .json per mcp tool)
│ │ ├── txt2img.py
│ │ ├── txt2img_api.json
│ │ ├── img2img.py
│ │ ├── img2img_api.json
│ │ ├── {xxxx}.py # 配合被调用的ComfyUI的工作流。可任意添加MCP工具配置,工具自动注册与API扩展机制。
│ │ ├── {xxxx}_api.json # 用于配合被调用的ComfyUI的工作流,允许添加MCP工具配置,具有自动注册和API扩展机制。
│ │ ├── ......
│ │ └── __init__.py
│ ├── utils.py # 配置、模板、随机种子等通用工具 | Utilities for config, templates, random seed, etc.
│ ├── config.ini # mcp服务与被调用的ComfyUI地址配置 | Service and ComfyUI address config
│ ├── logger.py & decorator.py # 日志系统 | logs sys
│ └── __init__.py
├── workflows/ # MCP工具可实现的ComfyUI工作流json,与tools/对应。 | The "ComfyUI workflow json" that the "MCP tool" can achieve. Corresponding to "tools/".
│ ├── txt2img.json
│ ├── img2img.json
│ ├── {xxxx}.json
│ ├── ......
├── pyproject.toml # 构建配置与依赖 | Build config and dependencies
└── README.md # 使用说明(本文件)| User guide (this file)
python install.py # 安装(检查项2~3)& 配置(检查项1)
run_mcp.bat # 在Windows上运行
./run_mcp.sh # 在Linux上运行
tools/ 自动注册目录下的所有工具模块 tools/ 目录例如,扩展一个新的工具只需要:
tools/myapi.py,实现 register_myapi_tool(mcp) 注册MCP服务(工具)tools/myapi_api.json 定义参数模板(从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀)要添加新的工具(例如 txt2img):
tools/myapi.py,实现 register_myapi_tool(mcp) 并注册MCP服务(工具)tools/myapi_api.json 来定义参数模板(从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀)文本转图像(txt2img)
# tools/txt2img.py
def register_txt2img_tool(mcp):
async def comfyui_txt2img_impl(prompt: str, pic_width: str, pic_height: str, negative_prompt: str, batch_size: str, model: str) -> str:
...
# 返回图片 Markdown 格式 | Returns image in Markdown format
...
markdown_images = [f"" for url in image_urls]
return "\n".join(markdown_images)
tools/txt2img_api.json 文件。mcpserver.py 将自动遍历 tools/ 目录下的所有 .py 文件(如 txt2img.py)并调用其 register_xxx_tool(mcp) 注册函数。register_xxx_tool(mcp) 并通过 @mcp.tool() 装饰器注册MCP服务(工具)。示例 | Example:
# tools/txt2img.py
def register_txt2img_tool(mcp):
@mcp.tool()
async def txt2img(prompt: str, pic_width: str = '512', ... ) -> str:
...
tools/txt2img_api.json)配置生成。从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀。_api后缀,放置在tools目录中。示例片段 tools/txt2img_api.json| 示例片段:
{
"5": {
"inputs": {
"width": 512,
"height": 512,
"batch_size": 1
},
"class_type": "EmptyLatentImage"
}
}
支持通过 @mcp.resource("info//ckpt") 装饰器注册资源型API,实现模型等资源的自动发现和管理。
示例 | Example:
@mcp.resource("info//ckpt")
async def list_checkpoints() -> list:
# 返回所有可用的ckpt模型列表
...
MCP Inspector 是官方推荐的交互式开发者工具,可用于测试和调试MCP服务器。
run_mcp.bat --inspector # 在Windows上运行
./run_mcp.sh --inspector # 在Linux上运行
对于更多使用详情,请参阅 官方 Inspector 指南 | See more in the official Inspector guide。
config.ini 配置uv pip install -r pyproject.tomlregister_xxx_tool(mcp) 并放置在 tools/ 目录_api后缀,放置在 tools/ 目录config.ini 中 logging 配置项,确保 log_path 路径存在且具有写权限。config.ini 中 transport 参数已正确修改并重启服务。欢迎提交PR或issue来改进更多的API工具和业务逻辑。 为了集成更多的模型、参数或业务规则,只需添加/修改工具模块和JSON配置,无需侵入主流程。