返回市场
舒适UI_MCP

舒适UI_MCP

作者:ericwanghp11 星标更新:2025-05-31

项目介绍

ComfyUI_MCP Server(ComfyUI 的 ModelContextProtocol 服务端 | ModelContextProtocol Server for ComfyUI)

项目介绍 | Project Introduction

ComfyUI_MCP Server 是一个松散耦合、可扩展、基于配置的 ModelContextProtocol 服务器,专为 ComfyUI 设计。支持自定义工作流程中的 MCP 服务(工具),如 txt2img 和 img2img。每个 MCP 服务(工具)的参数和行为可以通过 JSON 初始化和 MCP 工具装饰器模块灵活扩展,使其适用于人工智能绘图、推理等场景的自动化和集成。

视频演示 | Video Demo

<details open> <summary>🎬 ComfyUI_MCP Server 功能演示 Demo(YouTube)</summary> <p align="center"> <a href="https://youtu.be/vXZOBZ_FsA8" target="_blank"> <img src="https://img.youtube.com/vi/vXZOBZ_FsA8/0.jpg" alt="ComfyUI_MCP Server Demo" width="480"> </a> </p> </details>

目录结构

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)

安装&配置&运行 | Installation&Configuration&Run

python install.py # 安装(检查项2~3)& 配置(检查项1) 

run_mcp.bat # 在Windows上运行
./run_mcp.sh # 在Linux上运行

  • 默认以流式HTTP模式运行
  • 自动登录 tools/ 自动注册目录下的所有工具模块 tools/ 目录

ComfyUI_SCP 工具自动注册和API扩展机制 | Tool Auto Registration&API Extension

1. 添加自定义MCP工具和方法实现 | Add Custom MCP Tools and Method Implementation

例如,扩展一个新的工具只需要:

  • 添加 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"![image]({url})" for url in image_urls]
        return "\n".join(markdown_images)
  • 调用ComfyUI在线工作流HTTP API获取结果。
  • 所有参数均可省略,默认值取自对应的Json tools/txt2img_api.json 文件。
  • 返回Markdown格式的图片链接,可以直接用于文档或前端显示。
  • 进一步实现上述功能并将其封装到相应的MCP服务(工具)中。

2. MCP工具自动注册

  • 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:
        ...

3. 驱动参数配置 | Config Driven Parameters

  • 每个MCP服务(工具)的参数签名、类型和注释可以通过同名的JSON(如 tools/txt2img_api.json)配置生成。从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀。
  • 所有参数都是可选的;如果未提供,则默认值来自模板。支持递归种子随机化、模型白名单、批处理大小限制等业务规则。
  • 新增的MCP服务(工具)的JSON模板需要从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀,放置在tools目录中。

示例片段 tools/txt2img_api.json| 示例片段:

{
  "5": {
    "inputs": {
      "width": 512,
      "height": 512,
      "batch_size": 1
    },
    "class_type": "EmptyLatentImage"
  }
}

4. 资源注册&管理

支持通过 @mcp.resource("info//ckpt") 装饰器注册资源型API,实现模型等资源的自动发现和管理。

示例 | Example:

@mcp.resource("info//ckpt")
async def list_checkpoints() -> list:
    # 返回所有可用的ckpt模型列表
    ...

使用MCP Inspector调试 | Debug with MCP Inspector

MCP Inspector 是官方推荐的交互式开发者工具,可用于测试和调试MCP服务器。

快速开始 Inspector | Quick Start Inspector

run_mcp.bat --inspector # 在Windows上运行
./run_mcp.sh --inspector # 在Linux上运行
  • Inspector 可连接本地MCP服务,支持工具、资源、提示词等的全面互动测试。
  • 您可以在Inspector的Tools面板中查看所有已自动注册的API工具,测试参数,查看返回结果,调试异常。

对于更多使用详情,请参阅 官方 Inspector 指南 | See more in the official Inspector guide


常见问题 | FAQ

  • ComfyUI未启动或地址错误 请检查 config.ini 配置
  • 依赖未安装 请先执行 uv pip install -r pyproject.toml
  • 新添加的工具未生效 确认已实现 register_xxx_tool(mcp) 并放置在 tools/ 目录
  • 参数验证失败 请检查JSON模板与实际参数类型和范围是否一致。从被调用的ComfyUI实例的自定义工作流中导出同名API,并加上_api后缀,放置在 tools/ 目录
  • Inspector无法连接 确认MCP服务已启动且端口未被占用
  • 日志未生成或无输出 请检查 config.inilogging 配置项,确保 log_path 路径存在且具有写权限。
  • MCP服务传输模式切换无效 请确认 config.initransport 参数已正确修改并重启服务。

贡献&扩展

欢迎提交PR或issue来改进更多的API工具和业务逻辑。 为了集成更多的模型、参数或业务规则,只需添加/修改工具模块和JSON配置,无需侵入主流程。


待优化 | To Do

  • 参数类型自动推断和验证增强:支持更智能的参数类型推断和严格的验证,提高健壮性。
  • 更全面的错误处理和日志记录:增加详细的错误日志、异常追踪和用户友好的提示。
  • 热加载工具/配置:支持工具目录和配置文件的热加载,无需重启服务即可生效。
  • 多ComfyUI后台支持:支持同时连接和调度多个ComfyUI后台实例,实现负载均衡或多模型推理。
  • 多媒体数据交互支持:支持图像、声音、视频等多媒体数据的输入、输出和处理。
  • 更全面地整合ComfyUI HTTP API。
  • 单元测试和CI改进:补充更多的单元测试案例,集成持续集成(CI)流程,确保代码质量。