comfy-mcp-server
by: lalanikarim
A server using FastMCP framework to generate images based on prompts via a remote Comfy server.
📌Overview
Purpose: The Comfy MCP Server is designed to generate images based on prompts by leveraging a remote Comfy server through the FastMCP framework.
Overview: This server setup interacts with a Comfy server to facilitate image generation. It allows users to submit prompts via a defined workflow and receive the resulting images efficiently.
Key Features:
-
Remote Interaction: Connects seamlessly with a remote Comfy server to process image generation prompts.
-
Custom Workflow Support: Requires user-defined workflow files from Comfy UI, allowing for tailored image generation processes based on specific needs.
-
Flexible Output Options: Users can choose to receive generated images as URLs or file downloads, enhancing versatility for different applications.
-
Environment Configuration: The server supports various configurable environment variables, ensuring flexibility in connecting to different Comfy servers and workflows.
Comfy MCP Server
A server using FastMCP framework to generate images based on prompts via a remote Comfy server.
Overview
This script sets up a server using the FastMCP framework to generate images based on prompts using a specified workflow. It interacts with a remote Comfy server to submit prompts and retrieve generated images.
Prerequisites
- Install the uv package for Python.
- Export a workflow file from Comfy UI and set the environment variables accordingly.
Install the required packages for local development:
uvx mcp[cli]
Configuration
Set the following environment variables:
COMFY_URL
: Your Comfy server URL.COMFY_WORKFLOW_JSON_FILE
: Absolute path of the exported Comfy UI workflow JSON file.PROMPT_NODE_ID
: ID of the text prompt node.OUTPUT_NODE_ID
: ID of the output node with the final image.OUTPUT_MODE
: Eitherurl
orfile
for output preference.
If using an Ollama server, you can also set:
OLLAMA_API_BASE
: URL where Ollama is running.PROMPT_LLM
: Name of the model hosted on Ollama for prompt generation.
Example:
export COMFY_URL=http://your-comfy-server-url:port
export COMFY_WORKFLOW_JSON_FILE=/path/to/the/comfyui_workflow_export.json
export PROMPT_NODE_ID=6
export OUTPUT_NODE_ID=9
export OUTPUT_MODE=file
Usage
Launch Comfy MCP Server with:
uvx comfy-mcp-server
Example Configuration
{
"mcpServers": {
"Comfy MCP Server": {
"command": "/path/to/uvx",
"args": ["comfy-mcp-server"],
"env": {
"COMFY_URL": "http://your-comfy-server-url:port",
"COMFY_WORKFLOW_JSON_FILE": "/path/to/the/comfyui_workflow_export.json",
"PROMPT_NODE_ID": "6",
"OUTPUT_NODE_ID": "9",
"OUTPUT_MODE": "file"
}
}
}
}
Functionality
generate_image(prompt: str, ctx: Context) -> Image | str
Generates an image using a specified prompt by:
- Checking if all environment variables are set.
- Loading a prompt template from a JSON file.
- Submitting the prompt to the Comfy server.
- Polling for prompt processing status.
- Retrieving and returning the generated image.
generate_prompt(topic: str, ctx: Context) -> str
Generates an image generation prompt from a specified topic.
Dependencies
mcp
: FastMCP server setup.json
: Handling JSON data.urllib
: Making HTTP requests.time
: Adding delays in polling.os
: Accessing environment variables.langchain
: Simple LLM prompt chain for image generation.langchain-ollama
: Ollama specific modules for LangChain.
License
This project is licensed under the MIT License.