ComfyUI MCP Server
Bridges ComfyUI's AI image generation workflows with game development, enabling dynamic generation of game assets like character portraits, item icons, and environment textures through natural language prompts.
README
ComfyUI MCP Server
AI-powered image generation for game development via ComfyUI and the Model Context Protocol
Overview
ComfyUI MCP Server is a Model Context Protocol (MCP) server that bridges ComfyUI's powerful workflow-based AI image generation with modern development workflows. Originally designed for Godot game development, it can be used with any MCP-compatible client to generate game assets, concept art, and visual content dynamically.
Key Features
- MCP Integration: Expose ComfyUI workflows as standardized MCP tools
- Python API Client: Full-featured async ComfyUI API client with type safety
- Workflow Templates: Pre-built templates for common game assets (characters, items, environments)
- Async Operations: Non-blocking generation with real-time progress updates via WebSockets
- Flexible Configuration: TOML files, environment variables, or Python code
- Type Safe: Full type hints with strict mypy validation
- Well Tested: Comprehensive test coverage with pytest
- Production Ready: Retry logic, error handling, and logging built-in
Use Cases
- Character Generation: NPC portraits, character sprites, concept art
- Item Icons: Unique item icons from text descriptions
- Environment Art: Background textures, tileable patterns, landscapes
- Dynamic Content: Procedural asset generation during gameplay
- Concept Art: Rapid visual prototyping and iteration
- Batch Processing: Generate multiple asset variations efficiently
Table of Contents
- Quick Start
- Installation
- Configuration
- Usage
- Workflow Templates
- Documentation
- Examples
- Development
- Troubleshooting
- Contributing
- License
Quick Start
Prerequisites
-
Python 3.10 or higher
python --version # Should be 3.10+ -
ComfyUI installed and running
- Download: ComfyUI GitHub
- Default URL:
http://localhost:8188 - Verify: Open
http://localhost:8188in your browser
-
Stable Diffusion models
- Download models and place in ComfyUI's
models/checkpoints/directory - Recommended: Stable Diffusion 1.5 or 2.1 for game assets
- Download models and place in ComfyUI's
Installation
# Clone the repository
git clone https://github.com/purlieu-studios/comfyui-mcp.git
cd comfyui-mcp
# Install the package
pip install -e .
# For development (includes testing and linting tools)
pip install -e ".[dev]"
# Verify installation
python -c "from comfyui_mcp import ComfyUIClient; print('Installation successful!')"
Basic Configuration
Option 1: Environment Variables (Recommended for getting started)
# Required
export COMFYUI_URL="http://localhost:8188"
# Optional
export COMFYUI_TIMEOUT="120.0"
export COMFYUI_OUTPUT_DIR="./generated_images"
Option 2: TOML Configuration File
Create comfyui.toml in your project root:
[comfyui]
url = "http://localhost:8188"
timeout = 120.0
output_dir = "./generated_images"
See docs/CONFIGURATION.md for comprehensive configuration options.
Your First Generation
Using the Python API
import asyncio
from comfyui_mcp import ComfyUIClient, ComfyUIConfig, WorkflowPrompt
async def generate_image():
# Configure the client
config = ComfyUIConfig(url="http://localhost:8188")
async with ComfyUIClient(config) as client:
# Check ComfyUI server health
if not await client.health_check():
print("ComfyUI server is not responding!")
return
# Create a simple workflow
workflow = WorkflowPrompt(
prompt={
"3": {
"class_type": "KSampler",
"inputs": {
"seed": 42,
"steps": 20,
"cfg": 7.0,
"sampler_name": "euler",
"scheduler": "normal",
"denoise": 1.0
}
}
}
)
# Submit and wait for completion
prompt_id = await client.submit_workflow(workflow)
print(f"Workflow submitted: {prompt_id}")
result = await client.wait_for_completion(
prompt_id=prompt_id,
poll_interval=1.0,
timeout=300.0
)
print(f"Generation complete! Result: {result}")
# Run the async function
asyncio.run(generate_image())
Using the MCP Server
1. Configure MCP Server
Add to your .mcp.json:
{
"mcpServers": {
"comfyui-mcp": {
"command": "python",
"args": ["-m", "comfyui_mcp.server"],
"env": {
"COMFYUI_URL": "http://localhost:8188",
"COMFYUI_OUTPUT_DIR": "./generated_images"
}
}
}
}
2. Use via Claude Code or MCP Client
# Via MCP client (example)
result = await mcp.call_tool(
"generate_image",
{
"template": "character-portrait",
"prompt": "fantasy elf warrior with detailed armor and glowing sword",
"width": 512,
"height": 512,
"steps": 25,
"seed": 12345
}
)
Installation
From Source (Development)
# Clone the repository
git clone https://github.com/purlieu-studios/comfyui-mcp.git
cd comfyui-mcp
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in editable mode with dev dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
From PyPI (Coming Soon)
pip install comfyui-mcp
Dependencies
Core:
- Python 3.10+
aiohttp- Async HTTP client for ComfyUI APIpydantic- Data validation and settings managementtomli- TOML configuration file parsingwebsockets- WebSocket support for real-time updates
Development:
pytest- Testing frameworkpytest-asyncio- Async test supportpytest-cov- Code coveragemypy- Static type checkingruff- Fast linting and formattingpre-commit- Git hooks for code quality
Configuration
ComfyUI MCP Server supports three configuration methods:
1. TOML Configuration File (Recommended for Projects)
Create comfyui.toml:
[comfyui]
# Required: ComfyUI server URL
url = "http://localhost:8188"
# Optional: API key for authentication (8+ characters)
# api_key = "your-api-key-here"
# Optional: Request timeout in seconds (1.0 - 3600.0)
timeout = 120.0
# Optional: Output directory for generated images
output_dir = "./generated_images"
Configuration file locations (searched in order):
./comfyui.toml(current directory)~/.config/comfyui/comfyui.toml(user config)/etc/comfyui/comfyui.toml(system-wide)
2. Environment Variables (Recommended for Deployment)
export COMFYUI_URL="http://localhost:8188"
export COMFYUI_API_KEY="your-api-key-min-8-chars"
export COMFYUI_TIMEOUT="120.0"
export COMFYUI_OUTPUT_DIR="./generated_images"
3. Python Code (Programmatic Configuration)
from comfyui_mcp import ComfyUIConfig, ComfyUIClient
# Direct instantiation
config = ComfyUIConfig(
url="http://localhost:8188",
api_key="your-api-key",
timeout=120.0,
output_dir="./generated_images"
)
# Load from environment
config = ComfyUIConfig.from_env()
# Use the config
async with ComfyUIClient(config) as client:
await client.health_check()
Configuration Priority
When multiple methods are used:
- Python code (highest priority)
- Environment variables
- TOML configuration file
- Default values (lowest priority)
Example Configuration Files
Complete example configuration files are available in examples/config/:
| File | Description | Use Case |
|---|---|---|
comfyui.minimal.toml |
Minimal configuration with only required settings | Quick start, beginners |
comfyui.example.toml |
Standard configuration with common options | General development |
comfyui.dev.toml |
Development environment settings | Local development |
comfyui.prod.toml |
Production environment settings | Production deployment |
comfyui.test.toml |
Testing and CI/CD configuration | Automated testing |
comfyui.docker.toml |
Docker and Kubernetes deployment | Containerized environments |
comfyui.advanced.toml |
Comprehensive reference with all options | Complete documentation |
.env.example |
Environment variables template | Docker, CI/CD |
Quick start:
# Copy minimal config to get started
cp examples/config/comfyui.minimal.toml comfyui.toml
# Or use a specific environment
cp examples/config/comfyui.dev.toml comfyui.toml
See docs/CONFIGURATION.md for comprehensive configuration documentation.
Usage
MCP Server Usage
The MCP server exposes ComfyUI functionality as standardized MCP tools.
Available MCP Tools
| Tool | Description |
|---|---|
generate_image |
Generate images using workflow templates |
list_workflows |
List available workflow templates |
get_workflow_status |
Check generation progress and status |
cancel_workflow |
Cancel a running workflow |
load_workflow |
Load and use a custom workflow file |
Example: Generate Image Tool
{
"tool": "generate_image",
"arguments": {
"template": "character-portrait",
"prompt": "cyberpunk hacker, neon lights, detailed face",
"negative_prompt": "blurry, low quality",
"width": 512,
"height": 768,
"steps": 30,
"cfg_scale": 7.5,
"seed": 42
}
}
Starting the MCP Server
# Via Python module
python -m comfyui_mcp.server
# Or with custom configuration
COMFYUI_URL="http://localhost:8188" python -m comfyui_mcp.server
Python API Client
Direct programmatic access to ComfyUI API.
Basic Usage
from comfyui_mcp import ComfyUIClient, ComfyUIConfig
async def main():
config = ComfyUIConfig(url="http://localhost:8188")
async with ComfyUIClient(config) as client:
# Health check
is_healthy = await client.health_check()
print(f"ComfyUI Status: {'Online' if is_healthy else 'Offline'}")
# Get system info
system_stats = await client.get_system_stats()
print(f"System: {system_stats}")
Submitting Workflows
from comfyui_mcp import WorkflowPrompt
async def generate():
config = ComfyUIConfig.from_env()
async with ComfyUIClient(config) as client:
workflow = WorkflowPrompt(
prompt={
"1": {
"class_type": "CheckpointLoaderSimple",
"inputs": {"ckpt_name": "sd_v1-5.safetensors"}
},
"2": {
"class_type": "CLIPTextEncode",
"inputs": {
"text": "beautiful landscape, mountains, sunset",
"clip": ["1", 0]
}
}
# ... more nodes
}
)
# Submit workflow
prompt_id = await client.submit_workflow(workflow)
# Wait for completion (with polling)
result = await client.wait_for_completion(
prompt_id=prompt_id,
poll_interval=2.0, # Check every 2 seconds
timeout=300.0 # Timeout after 5 minutes
)
return result
Queue Management
async def manage_queue():
async with ComfyUIClient(config) as client:
# Get queue status
queue = await client.get_queue()
print(f"Queue size: {queue['queue_running']}")
# Cancel specific workflow
await client.cancel_workflow(prompt_id="abc-123")
# Clear entire queue (careful!)
await client.interrupt()
Retrieving Generated Images
async def download_images(prompt_id: str):
async with ComfyUIClient(config) as client:
# Get workflow result
history = await client.get_history(prompt_id)
# Extract image filenames
for node_id, output in history["outputs"].items():
if "images" in output:
for image in output["images"]:
# Download image
image_data = await client.download_image(
filename=image["filename"],
subfolder=image.get("subfolder", ""),
folder_type=image.get("type", "output")
)
# Save to file
with open(f"./output/{image['filename']}", "wb") as f:
f.write(image_data)
Error Handling
from comfyui_mcp.exceptions import (
ComfyUIError,
ConnectionError,
WorkflowExecutionError,
TimeoutError
)
async def safe_generation():
try:
async with ComfyUIClient(config) as client:
result = await client.submit_workflow(workflow)
except ConnectionError as e:
print(f"Cannot connect to ComfyUI: {e}")
except WorkflowExecutionError as e:
print(f"Workflow failed: {e}")
except TimeoutError as e:
print(f"Generation timed out: {e}")
except ComfyUIError as e:
print(f"ComfyUI error: {e}")
See docs/API.md for complete API documentation.
Workflow Templates
Pre-built workflow templates for common game asset types.
Available Templates
| Template | Description | Size | Use Case |
|---|---|---|---|
character-portrait |
Character portraits and avatars | 512x512 | RPG character art, NPC portraits |
item-icon |
Centered item icons | 512x512 | Inventory items, UI icons |
environment-texture |
Tileable environment textures | 1024x1024 | Backgrounds, terrain textures |
pixel-art |
Upscaled pixel art style | 256x256 | Retro games, pixel art assets |
Using Templates
from comfyui_mcp import WorkflowTemplateManager
# Load template manager
manager = WorkflowTemplateManager(templates_dir="./workflows")
# List available templates
templates = manager.list_templates()
for template in templates:
print(f"{template.name}: {template.description}")
# Load and customize template
template = manager.load_template("character-portrait")
workflow = template.instantiate(
prompt="fantasy wizard with blue robes",
seed=12345,
steps=25
)
# Submit to ComfyUI
async with ComfyUIClient(config) as client:
prompt_id = await client.submit_workflow(workflow)
Creating Custom Templates
See docs/workflow-templates.md for template creation guide.
Documentation
Comprehensive documentation is available in the docs/ directory:
-
API Reference - Complete Python API documentation
- ComfyUIClient methods and parameters
- Pydantic models and data structures
- Exception handling
- Type hints and examples
-
Configuration Guide - Configuration options and best practices
- TOML file format and schema
- Environment variables
- Configuration priority
- Example configurations
-
ComfyUI API Integration - ComfyUI REST API patterns
- API endpoints and methods
- Workflow structure
- Queue management
- WebSocket integration
-
MCP Tool Usage - Complete MCP tool reference and usage guide
- All 5 MCP tools documented
- Integration patterns and examples
- Best practices and troubleshooting
-
Workflow Creation Tutorial - Comprehensive guide to creating ComfyUI workflows
- Understanding workflow structure and node system
- Creating workflows from scratch
- Parameter substitution and templates
- Advanced techniques (LoRA, ControlNet, batching)
- Integration with MCP server
- Complete examples for characters, items, and environments
-
Workflow Template System - Complete workflow template system documentation
- Template structure and file format
- Creating and using templates
- Built-in templates (character, item, environment, pixel art)
- Parameter substitution engine
- WorkflowTemplateManager usage
- Best practices and advanced topics
- Complete examples and troubleshooting
-
Godot Integration (coming soon) - Godot plugin and examples
Examples
Practical examples are available in the examples/ directory:
Example Projects
- Character Portrait Generation - Generate RPG character portraits
- Item Icon Batch Generation - Batch generate inventory icons
- Environment Textures - Create tileable background textures
- Real-time Godot Integration - Live generation in Godot engine
- Procedural Sprite Variation - Generate sprite variations
Running Examples
# Set configuration
export COMFYUI_URL="http://localhost:8188"
# Run character portrait example
python examples/character_portrait.py
# Run batch item icon generation
python examples/item_icons.py --count 10 --prompt "fantasy sword"
Development
Setup Development Environment
# Clone repository
git clone https://github.com/purlieu-studios/comfyui-mcp.git
cd comfyui-mcp
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install with development dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Running Tests
# Run all tests
pytest tests/ -v
# Run with coverage report
pytest tests/ -v --cov=comfyui_mcp --cov-report=term-missing
# Run specific test file
pytest tests/test_client.py -v
# Run tests matching pattern
pytest tests/ -v -k "test_workflow"
Code Quality Checks
# Type checking (strict mode)
mypy src/
# Linting
ruff check src/ tests/
# Format code
ruff format src/ tests/
# Run all quality checks
pre-commit run --all-files
Project Structure
comfyui-mcp/
├── src/
│ └── comfyui_mcp/ # Main package
│ ├── __init__.py # Public API exports
│ ├── server.py # MCP server implementation
│ ├── comfyui_client.py # ComfyUI API client
│ ├── models.py # Pydantic data models
│ ├── config.py # Configuration management
│ ├── exceptions.py # Custom exceptions
│ └── utils.py # Utility functions
├── tests/ # Test suite
│ ├── test_client.py # Client tests
│ ├── test_models.py # Model validation tests
│ ├── test_config.py # Configuration tests
│ └── fixtures/ # Test fixtures
├── examples/ # Example scripts
│ ├── character_portrait.py
│ ├── item_icons.py
│ └── godot/ # Godot integration
├── workflows/ # Workflow templates
│ ├── character-portrait.json
│ ├── item-icon.json
│ └── environment-texture.json
├── docs/ # Documentation
│ ├── API.md
│ ├── CONFIGURATION.md
│ └── COMFYUI_API.md
├── .github/workflows/ # CI/CD
│ └── ci.yml
├── pyproject.toml # Package configuration
├── README.md # This file
├── CLAUDE.md # AI assistant context
└── LICENSE # MIT License
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Quick contribution checklist:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
pytest tests/) - Run type checking (
mypy src/) - Run linting (
ruff check src/ tests/) - Format code (
ruff format src/ tests/) - Commit changes (
git commit -m 'feat: add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
Troubleshooting
Common Issues
ComfyUI Server Connection Failed
Problem: ConnectionError: Cannot connect to ComfyUI server at http://localhost:8188
Solutions:
- Verify ComfyUI is running: Open
http://localhost:8188in browser - Check URL configuration: Ensure
COMFYUI_URLis correct - Check firewall settings: Allow connections on port 8188
- Try explicit localhost: Use
http://127.0.0.1:8188instead ofhttp://localhost:8188
# Test connection
from comfyui_mcp import ComfyUIClient, ComfyUIConfig
async def test_connection():
config = ComfyUIConfig(url="http://127.0.0.1:8188")
async with ComfyUIClient(config) as client:
is_healthy = await client.health_check()
print(f"Connection: {'✓' if is_healthy else '✗'}")
Workflow Execution Timeout
Problem: TimeoutError: Workflow execution exceeded timeout of 120.0 seconds
Solutions:
-
Increase timeout in configuration:
[comfyui] timeout = 300.0 # 5 minutes -
Reduce workflow complexity (fewer steps, lower resolution)
-
Check ComfyUI server logs for errors
-
Ensure models are downloaded and accessible
Module Import Errors
Problem: ModuleNotFoundError: No module named 'comfyui_mcp'
Solutions:
- Reinstall package:
pip install -e . - Activate virtual environment:
source venv/bin/activate - Check Python path:
echo $PYTHONPATH
Type Checking Errors
Problem: mypy reports type errors
Solutions:
- Update type stubs:
pip install --upgrade types-all - Check mypy configuration in
pyproject.toml - Use
# type: ignorefor false positives (sparingly)
Debug Mode
Enable debug logging:
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Now all ComfyUI MCP operations will log debug information
Getting Help
- GitHub Issues: Report bugs or request features
- GitHub Discussions: Ask questions and share ideas
- Documentation: Read the docs
Roadmap
See GitHub Milestones for upcoming features.
Planned Features
-
Phase 1: Foundation (Current)
- ✅ ComfyUI API client
- ✅ MCP server implementation
- ✅ Basic workflow templates
- ✅ Configuration system
- ✅ Comprehensive documentation
-
Phase 2: Advanced Features (Next)
- [ ] WebSocket support for real-time progress
- [ ] Advanced workflow template system
- [ ] Image post-processing pipeline
- [ ] Generation caching
- [ ] Batch processing optimization
-
Phase 3: Godot Integration (Future)
- [ ] Godot GDScript helper library
- [ ] Godot plugin for ComfyUI integration
- [ ] Editor tools for workflow testing
- [ ] Asset pipeline integration
-
Phase 4: Production Enhancements (Future)
- [ ] Authentication and API key support
- [ ] Rate limiting and queue management
- [ ] Monitoring and metrics
- [ ] Docker containerization
- [ ] Kubernetes deployment support
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- ComfyUI - Powerful workflow-based Stable Diffusion UI by comfyanonymous
- Godot Engine - Open-source game engine
- Model Context Protocol - Universal AI integration standard by Anthropic
- Pydantic - Data validation using Python type hints
- Ruff - Fast Python linter and formatter
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: docs/
Status: Alpha - Active Development
Built with ❤️ by Purlieu Studios
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。