comfyui-mcp-server
by: joenorton
comfyui mcp server
📌Overview
Purpose: To provide a lightweight server that allows AI agents to programmatically generate images through ComfyUI using the Model Context Protocol (MCP) via WebSockets.
Overview: The ComfyUI MCP Server facilitates seamless communication between AI agents and ComfyUI for image generation, leveraging the MCP protocol to allow for a flexible and dynamic image generation workflow.
Key Features:
-
Flexible Workflow Selection: Supports various workflow formats (e.g.,
basic_api_test.json
), allowing users to choose different image generation processes easily. -
Dynamic Parameters: Users can customize requests with adjustable parameters such as
prompt
,width
,height
, andmodel
, providing great flexibility for image generation. -
Image URL Response: The server returns generated image URLs directly from ComfyUI, simplifying the process of accessing and using generated images.
ComfyUI MCP Server
A lightweight Python-based MCP (Model Context Protocol) server that interfaces with a local ComfyUI instance to generate images programmatically via AI agent requests.
Overview
This project enables AI agents to send image generation requests to ComfyUI using the MCP protocol over WebSocket. It supports:
- Flexible workflow selection (e.g.,
basic_api_test.json
) - Dynamic parameters:
prompt
,width
,height
, andmodel
- Returns image URLs served by ComfyUI
Prerequisites
- Python 3.10+
- ComfyUI: Installed and running locally (e.g., on
localhost:8188
) - Dependencies:
requests
,websockets
,mcp
(install via pip)
Setup
-
Clone the Repository
git clone <your-repo-url> cd comfyui-mcp-server
-
Install Dependencies
pip install requests websockets mcp
-
Start ComfyUI
- Install ComfyUI (see ComfyUI docs)
- Run it on port 8188:
cd <ComfyUI_dir> python main.py --port 8188
-
Prepare Workflows
- Place API-format workflow files (e.g.,
basic_api_test.json
) in theworkflows/
directory - Export workflows from ComfyUI’s UI with “Save (API Format)” (enable dev mode in settings)
- Place API-format workflow files (e.g.,
Usage
-
Run the MCP Server
python server.py
Listens on
ws://localhost:9000
-
Test with the Client
python client.py
Sends a sample request:
"a dog wearing sunglasses"
with512x512
resolution usingsd_xl_base_1.0.safetensors
.Example output:
{ "image_url": "http://localhost:8188/view?filename=ComfyUI_00001_.png&subfolder=&type=output" }
-
Custom Requests
Modifyclient.py
’spayload
to change parameters such asprompt
,width
,height
,workflow_id
, ormodel
.Example payload parameters:
"params": json.dumps({ "prompt": "a cat in space", "width": 768, "height": 768, "workflow_id": "basic_api_test", "model": "v1-5-pruned-emaonly.ckpt" })
Project Structure
server.py
: MCP server with WebSocket transport and lifecycle supportcomfyui_client.py
: Interfaces with ComfyUI’s API, handles workflow queuingclient.py
: Test client for sending MCP requestsworkflows/
: Directory for API-format workflow JSON files
Notes
- Ensure your chosen
model
(e.g.,v1-5-pruned-emaonly.ckpt
) exists in<ComfyUI_dir>/models/checkpoints/
- The MCP SDK lacks native WebSocket transport; this project uses a custom implementation
- For custom workflows, adjust node IDs in
comfyui_client.py
’sDEFAULT_MAPPING
if needed
Contributing
Feel free to submit issues or pull requests to enhance flexibility, such as dynamic node mapping or progress streaming.
License
Apache License