基于Model Context Protocol (MCP)的ComfyUI图像生成服务,通过API调用本地ComfyUI实例生成图片,实现自然语言生图自由
Project description
ComfyUI MCP 服务
这是一个基于Model Context Protocol (MCP)的ComfyUI图像生成服务,通过API调用本地ComfyUI实例生成图片。
功能特性
- 通过MCP协议提供图像生成服务,实现自然语言生图自由
- 支持动态替换工作流中的提示词和尺寸等参数
- 自动加载workflows目录下的工作流文件作为资源
新增功能记录
- [2025-05-06] 把项目目录src/hh修改成src/hh_mcp_comfyui,增加uvx构建方式
- [2025-04-26] 增加图生图和移除背景样例工作流及支持图生图工具
- [2025-04-20] 加入文生图生成工具
效果
-
Cherry Studio中使用效果
-
Cline中使用效果
安装依赖
1. 确保已安装Python 3.13+
2. 使用uv管理Python环境:
- 安装uv:
# On macOS and Linux.
$ curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows.
$ powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# 更新uv(非必要操作):
$ uv self update
- 初始化项目环境:
# Clone the repository.
$ git clone https://github.com/zjf2671/hh-mcp-comfyui.git
$ cd hh-mcp-comfyui
# Initialized venv
$ uv venv
# Activate the virtual environment.
$ .venv\Scripts\activate
# Install dependencies.
$ uv lock
Resolved 30 packages in 1ms
# sync dependencies.
$ uv sync
Resolved 30 packages in 2.54s
Audited 29 package in 0.02ms
- 样例工作流copy到指定工作流目录:
# 进入项目目录
$ cd hh-mcp-comfyui
# 创建工作流没有这个目录执行下面进行创建(这个目录位置不能动)
$ mkdir src\hh_mcp_comfyui\workflows
#复制样例工作的工作流目录
$ cp .\example\workflows\* .\src\hh_mcp_comfyui\workflows\
测试运行服务
- uv方式
$ uv --directory 你本地安装目录/hh-mcp-comfyui run hh-mcp-comfyui
INFO:__main__:Scanning for workflows in: D:\cygitproject\hh-mcp-comfyui\src\hh_mcp_comfyui\workflows
INFO:__main__:Registered resource: workflow://i2image_bizyair_sdxl -> i2image_bizyair_sdxl.json
INFO:__main__:Registered resource: workflow://t2image_bizyair_flux -> t2image_bizyair_flux.json
INFO:__main__:Registered resource: workflow://t2image_sd1.5 -> t2image_sd1.5.json
INFO:__main__:Starting ComfyUI MCP Server...
- uvx方式
$ uvx hh-mcp-comfyui
INFO:hh_mcp_comfyui.server:Scanning for workflows in: C:\Users\tianw\AppData\Local\uv\cache\archive-v0\dp4MTo0f1qL0DdYF_BYCL\Lib\site-packages\hh_mcp_comfyui\workflows
INFO:hh_mcp_comfyui.server:Starting ComfyUI MCP Server...
出现上面的信息表示服务启动成功
使用方法
必须确保本地ComfyUI实例正在运行(默认地址: http://127.0.0.1:8188) ComfyUI安装地址
a、uv MCP服务配置方法
- Cherry Studio、Cline、Cursor等客户端的使用方式
{
"mcpServers": {
"hh-mcp-comfyui": {
"command": "uv",
"args": [
"--directory",
"项目绝对路径(例如:D:/hh-mcp-comfyui)",
"run",
"hh-mcp-comfyui"
]
}
}
}
b、uvx MCP服务配置方法
- Cherry Studio、Cline、Cursor等客户端的使用方式
{
"mcpServers": {
"hh-mcp-comfyui": {
"command": "uvx",
"args": [
"hh-mcp-comfyui"
]
}
}
}
测试
可以使用MCP Inspector测试服务端工具:
- uv方式
npx @modelcontextprotocol/inspector uv --directory 你本地安装目录/hh-mcp-comfyui run hh-mcp-comfyui
- uvx方式
npx @modelcontextprotocol/inspector uvx hh-mcp-comfyui
然后点击连接如图即可调试:
扩展
添加新工作流
- 将工作流JSON文件放入
src/hh_mcp_comfyui/workflows目录中(注意:如果是使用的uvx方式启动的话,请找你本地对应的安装目录添加,可以使用如下图方式找到你的目录的位置)
- 重启服务自动加载新工作流
自定义参数
修改server.py中的generate_image工具定义来添加新参数
项目结构
.
├── .gitignore
├── .python-version
├── pyproject.toml
├── README.md
├── uv.lock
├── example/ # 示例工作流目录
│ └── workflows/
│ ├── i2image_bizyair_sdxl.json
│ ├── t2image_bizyair_flux.json
│ ├── i2image_cogview4.json
│ └── t2image_sd1.5.json
├── src/ # 源代码目录
│ └── hh_mcp_comfyui/
│ ├── comfyui_client.py # ComfyUI客户端实现
│ ├── server.py # MCP服务主文件
│ └── workflows/ # 工作流文件目录
使用注意事项(针对没有用过comfyui的特别注意)
- 默认工作流为
t2image_bizyair_flux - 图片尺寸默认为1024x1024
- 服务启动时会自动加载workflows目录下的所有JSON工作流文件
- 如果你使用的是本项目中的样例工作流需要在comfyui中下载个插件,详细操作请查看:样例工作流插件安装教程
- 如果使用你本地的comfyui工作流的话,先要保证你的工作流能在comfyui正常运行,然后需要导出(API)的JSON格式,并放入src/workflows目录中
贡献
- Fork项目
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开Pull Request
如有问题可以到公众号中联系我:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hh_mcp_comfyui-2025.4.20.2.tar.gz.
File metadata
- Download URL: hh_mcp_comfyui-2025.4.20.2.tar.gz
- Upload date:
- Size: 1.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a5b25c7769076498c0c1cbcb8ed22c9794c423779092862bc6d8b0e46ae9e9f
|
|
| MD5 |
34a4484fd32e35ab69ef1613d0f31002
|
|
| BLAKE2b-256 |
a658007bca093c15b02f26e680453e4d5fcfb3d59f4335691a5224b43cf5fa1e
|
File details
Details for the file hh_mcp_comfyui-2025.4.20.2-py3-none-any.whl.
File metadata
- Download URL: hh_mcp_comfyui-2025.4.20.2-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46631cda6d16069911f7d4573d9d6d5e61b6e429506cb758666d45649db5e498
|
|
| MD5 |
5c2112999f85b7e07b9adbb9ba32a342
|
|
| BLAKE2b-256 |
340a48d662e5405c281061160591ad00f40606afc49e85fbfdf14dc7f0753553
|