comfyui-mcp-server
If you are the rightful owner of comfyui-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
ComfyUI MCP Server is a full-featured implementation of the Model Context Protocol (MCP) for ComfyUI, enabling AI agents to generate and manipulate images using a standardized API.
ComfyUI MCP Server
A full-featured implementation of the Model Context Protocol (MCP) for ComfyUI, enabling AI agents to generate and manipulate images using a standardized API.
๐ Table of Contents
- Introduction
- Features
- Installation
- Quick Start
- Usage Examples
- Project Structure
- API Reference
- Advanced Usage
- Troubleshooting
- Contributing
- License
๐ Introduction
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.
๐ Features
-
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
๐ป Installation
Prerequisites
- Python 3.10+
- ComfyUI installed and running
- 4GB+ RAM recommended
- CUDA-compatible GPU recommended (but not required)
Option 1: Clone and Install
# 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
Option 2: Manual Setup
# 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 mcp
๐โโ๏ธ Quick Start
Step 1: Start ComfyUI
Make sure to start ComfyUI first before running the MCP server:
cd /path/to/ComfyUI
python main.py --port 8188
Verify ComfyUI is running by accessing http://localhost:8188 in your browser.
Step 2: Start the MCP Server
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.json
Step 3: Test the Connection
Run 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"
๐ Usage Examples
Basic Image Generation
Generate an image from a text prompt:
python mcp_client.py --action generate --prompt "a cat in space" --width 768 --height 512
Image-to-Image Transformation
Transform an existing image based on a prompt:
python mcp_client.py --action img2img --prompt "watercolor painting" --image input.jpg --strength 0.75
List Available Models
See what models are available to use:
python mcp_client.py --action list-models
Using in Python Scripts
import 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())
๐ Project Structure
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)
๐ API Reference
Available MCP Tools
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 | - |
Configuration Options
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
}
๐ง Advanced Usage
Setting Up Authentication
-
Edit
config.json
to 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"
Customizing Workflows
- 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
โ Troubleshooting
Connection Issues
Problem: Cannot connect to ComfyUI Solution: Ensure ComfyUI is running and accessible at the configured URL:
curl http://localhost:8188/system_stats
Problem: 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 Windows
Image Generation Issues
Problem: 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 minutes
๐ค Contributing
Contributions 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
๐ License
This project is licensed under the Apache License 2.0 - see the file for details.
Made with โค๏ธ for AI and image generation