A full-featured implementation of the Model Context Protocol (MCP) for ComfyUI, enabling AI agents to generate and manipulate images using a standardized API.
- Introduction
- Features
- Installation
- Quick Start
- Usage Examples
- Project Structure
- API Reference
- Advanced Usage
- Troubleshooting
- Contributing
- License
ComfyUI MCP Server bridges the gap between AI agents and image generation by implementing the Model Context Protocol (MCP) for ComfyUI.
The Model Context Protocol is a standardized way for AI agents to discover and interact with tools through rich metadata and schema information. This implementation allows any AI assistant to easily generate and manipulate images using Stable Diffusion models through ComfyUI.
π What is MCP? The Model Context Protocol (MCP) standardizes how AI agents communicate with external tools, enabling consistent interfaces with rich metadata and capabilities descriptions.
-
Full MCP Protocol Implementation
- Tool discovery with metadata and schemas
- Session and context management
- Real-time communication
-
Rich Image Generation Capabilities
- Text-to-image (txt2img)
- Image-to-image transformation (img2img)
- Inpainting
- ControlNet support
- LoRA model integration
-
Advanced Infrastructure
- Job queue with priority support
- Progress tracking and real-time updates
- Authentication and rate limiting
- Session persistence
- Database backend for job history
- Python 3.10+
- ComfyUI installed and running
- 4GB+ RAM recommended
- CUDA-compatible GPU recommended (but not required)
# 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# 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 mcpMake sure to start ComfyUI first before running the MCP server:
cd /path/to/ComfyUI
python main.py --port 8188Verify ComfyUI is running by accessing http://localhost:8188 in your browser.
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.jsonRun the MCP client to verify everything is working:
# Check available tools
python mcp_client.py --action manifest
# Generate a test image
python mcp_client.py --action generate --prompt "a dog wearing sunglasses"Generate an image from a text prompt:
python mcp_client.py --action generate --prompt "a cat in space" --width 768 --height 512Transform an existing image based on a prompt:
python mcp_client.py --action img2img --prompt "watercolor painting" --image input.jpg --strength 0.75See what models are available to use:
python mcp_client.py --action list-modelsimport 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)
| Tool Name | Description | Key Parameters |
|---|---|---|
generate_image |
Generate image from text | prompt, width, height, negative_prompt, model |
img2img |
Transform an image based on text | prompt, image, strength, width, height |
get_job_status |
Check status of a job | job_id |
list_models |
List available models | - |
get_tools_manifest |
Get complete tools manifest | - |
The config.json file contains these key settings:
{
"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
}-
Edit
config.jsonto enable authentication:{ "enable_auth": true, "auth_config_path": "auth_config.json" } -
Create or modify
auth_config.json:{ "default_client": { "api_key": "YOUR_API_KEY_HERE", "permissions": ["*"], "rate_limit": { "rate": 5, "per": 60, "burst": 10 } } } -
Use the API key in client requests:
python mcp_client.py --api-key YOUR_API_KEY_HERE --action generate --prompt "test"
- Export a workflow from ComfyUI in API format (enable dev mode in settings)
- Save the JSON file to the
workflows/directory - Use it with the client:
python mcp_client.py --action generate --prompt "test" --workflow my_custom_workflow
Problem: Cannot connect to ComfyUI Solution: Ensure ComfyUI is running and accessible at the configured URL:
curl http://localhost:8188/system_statsProblem: MCP server won't start
Solution: Check logs in the logs directory and verify port 9000 is available:
lsof -i :9000 # On Linux/Mac
# or
netstat -ano | findstr :9000 # On WindowsProblem: Generation fails with workflow errors Solution:
- Check that ComfyUI has the required models installed
- Update the workflow JSON to match your ComfyUI installation
- Verify seed values are positive integers
- Check JSON encoding in workflow files
Problem: Timeout during image generation
Solution: Increase the timeout value in advanced_comfyui_client.py:
max_attempts = 300 # 5 minutesContributions are welcome! Here are some ways to get involved:
- Report bugs and request features by opening Issues
- Submit Pull Requests for bug fixes or enhancements
- Create new workflows for different generation tasks
- Improve documentation to help new users
Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Made with β€οΈ for AI and image generation