This repository contains an MCP (Model Context Protocol) server for generating images using the ComfyUI API. It allows you to generate images from text prompts through an LLM interface.
- Generate images from text prompts using various ComfyUI workflows
 - Supports multiple aspect ratios (square, landscape, portrait)
 - Automatic image saving with timestamped filenames
 - Web-based image gallery for viewing generated images
 - Cross-platform compatibility (Windows, macOS, Linux)
 
- Python 3.8 or higher
 - ComfyUI with API enabled
 - Pre-configured ComfyUI workflows
 
- 
Clone this repository:
git clone https://github.com/bgreene2/comfyui-mcp-server.git cd comfyui-mcp-server - 
Install the required dependencies for the server:
pip install -r server_requirements.txt
 - 
If you plan to use the image hosting app, install its dependencies:
pip install -r image_host_requirements.txt
 
- In ComfyUI, create and configure your desired image generation workflow
 - Export your workflow for API usage (Save as "Workflow API format")
 - Copy the exported JSON file to the 
workflows/directory - Create a corresponding YAML configuration file with the same name (but with 
.yamlextension) 
Create a YAML file in the workflows/ directory with the same name as your workflow JSON file. This file should contain:
aspect_ratios:
  "16:9": [1280, 720]   # widest
  "4:3": [1152, 864]    # wide
  "1:1": [1024, 1024]   # square
  "3:4": [864, 1152]    # tall
  "9:16": [720, 1280]   # tallest
save_image_node: '9'          # ID of the Save Image node
image_size_nodes: ['27']      # List of node IDs for width/height settings
seed_nodes: ['31']            # List of node IDs for seed settings
prompt_nodes: ['45']          # List of node IDs for prompt settingsUpdate the node IDs to match those in your ComfyUI workflow.
You can customize the image dimensions for each aspect ratio in the YAML configuration file. If not specified, the following defaults will be used:
- 16:9 (widest): 1280×720
 - 4:3 (wide): 1152×864
 - 1:1 (square): 1024×1024
 - 3:4 (tall): 864×1152
 - 9:16 (tallest): 720×1280
 
Create a .env file based on env.example and set the required environment variables:
cp env.example .env
# Edit .env to match your setupIf you need to specify a custom working directory (where workflow files are located), uncomment and modify the WORKING_DIR line in your .env file.
Then run the server:
python /path/to/server.pyCOMFYUI_HOST: Hostname or IP address of ComfyUI API (e.g.,127.0.0.1)COMFYUI_PORT: Port number of ComfyUI API (e.g.,8188)OUTPUT_DIR: Directory where generated images will be savedIMAGE_APP_BASE_URL: URL where generated images will be served fromCOMFYUI_WORKFLOW_NAME: Name of the ComfyUI workflow (without extension)WORKING_DIR: (Optional) Directory where the server resides (parent directory of 'server.py' and 'workflows/'). Defaults to the current working directory.
All environment variables except WORKING_DIR are required and must be set.
The image hosting app provides a web-based gallery for viewing generated images:
python image_host.py --output-dir /path/to/outputThe gallery will be available at http://0.0.0.0:8081.
Create a .env file and set the required environment variables, then use this configuration:
{
  "mcpServers": {
    "image-generation": {
      "command": "python",
      "args": [
        "/path/to/server.py"
      ]
    }
  }
}Once the server is running, you can use the image_generate tool with the following parameters:
prompt: The text prompt for the image generationtitle: A short name (2-4 words) to title the imageaspect_ratio: Supported values:"widest"(16:9)"wide"(4:3)"square"(1:1)"tall"(3:4)"tallest"(9:16)
Example tool call:
{
  "tool": "image_generate",
  "params": {
    "prompt": "A beautiful sunset over the mountains",
    "title": "Mountain Sunset",
    "aspect_ratio": "wide"
  }
}comfyui-mcp-server/
├── server.py                 # Main MCP server implementation
├── image_host.py             # Image gallery web app
├── server_requirements.txt   # Dependencies for server.py
├── image_host_requirements.txt # Dependencies for image_host.py
├── workflows/                # Workflow configurations
│   ├── *.json               # ComfyUI workflow files
│   └── *.yaml               # Workflow configuration files
└── README.md
- Connection Issues: Ensure ComfyUI is running and the API is enabled
 - Workflow Errors: Verify that node IDs in the YAML configuration match those in your ComfyUI workflow
 - Permission Errors: Ensure the output directory is writable
 - Missing Dependencies: Run 
pip install -r server_requirements.txtto install all required packages