图像生成服务器

一个基于ComfyUI的HTTP图像生成服务,提供文本到图像的转换功能,支持远程调用和流式传输。

  • by cameroncking
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 cameroncking/simple-comfy-remote-mcp

使用说明

简单的ComfyUI MCP服务器

公开ComfyUI映像的最小模型上下文协议(MCP)服务器 通过HTTP生成。

概述

该服务器提供了一个简单的MCP接口,用于使用以下命令生成文本到图像 ComfyUI。它可以返回与Open WebUI兼容的图像内容或纯文本 下载URL,具体取决于您调用的工具。

用例

已经发布了其他ComfyUI图像生成MCP;然而,他们 在客户端本地运行。此MCP提供了一个HTTP端点,这使得 对于通过网络提供图像生成的情况 多个用户。通过将MCP集中到远程服务器上,系统管理员 可以控制工作流UUID和节点参数。这允许 随着新模型的出现,工作流将在服务器端进行更新,而无需 客户端更新。

特性

  • generate_image Open WebUI兼容图像结果工具
  • 遗产 generate_image_url_from_prompt 基于URL的客户端工具
  • 基于提示的简单图像生成
  • 可流式HTTP传输
  • 无状态操作
  • 自动将提示注入到ComfyUI工作流中

先决条件

  • Node.js 20+
  • 正在运行ComfyUI实例
  • 使用ComfyUI工作流 CLIPTextEncode 节点

安装

npm install

配置

创建一个 .env 文件或设置以下环境变量:

# Required: URL of your ComfyUI instance
COMFYUI_URL=http://localhost:8188

# Required: Local file path to ComfyUI workflow JSON file
WORKFLOW_PATH=./workflow.json

# Optional: Port for MCP HTTP server (default: 3000)
MCP_PORT=3000

# Optional: Public URL for image downloads (default: http://localhost:3000)
PUBLIC_URL=http://localhost:3000

# Optional: Node ID for prompt injection (auto-detects first CLIPTextEncode if not set)
COMFYUI_INPUT_NODE_ID=

# Optional: Field name for prompt injection (default: text)
COMFYUI_INPUT_FIELD_NAME=

# Optional: Node ID for image output (auto-detects first SaveImage if not set)
COMFYUI_OUTPUT_NODE_ID=

# Optional: Field name for image output (default: images)
COMFYUI_OUTPUT_FIELD_NAME=

建筑

npm run build

跑步

npm start

服务器将于启动 http://localhost:3000/mcp.

码头工人

使用Docker Compose(推荐)

  1. 复制示例环境文件:
cp .env.example .env
  1. 编辑 .env 使用您的ComfyUI配置:
COMFYUI_URL=http://localhost:8188
WORKFLOW_PATH=./workflow.json
MCP_PORT=3000
PUBLIC_URL=http://localhost:3000
  1. 构建并启动容器:
docker-compose up -d

服务器将在以下时间可用 http://localhost:3000/mcp.

注: COMFYUI_URL 用途 host.docker.internal 在docker-compose.yml中访问主机上运行的ComfyUI。

直接使用Docker

  1. 塑造形象:
docker build -t comfy-mcp .
  1. 运行容器:
docker run -d \
  -p 3000:3000 \
  -e COMFYUI_URL=http://host.docker.internal:8188 \
  -e WORKFLOW_PATH=/app/workflow.json \
  -e MCP_PORT=3000 \
  -e PUBLIC_URL=http://localhost:3000 \
  -v $(pwd)/workflow.json:/app/workflow.json \
  -v $(pwd)/public/images:/app/public/images \
  --name comfy-mcp-server \
  comfy-mcp

用法

从MCP客户端连接

配置您的MCP客户端以连接到服务器:

对于HTTP传输:

Endpoint: http://localhost:3000/mcp
Transport: Streamable HTTP

示例:使用Open WebUI工具

连接后,您可以拨打 generate_image 工具:

{
  "name": "generate_image",
  "arguments": {
    "prompt": "A serene mountain landscape at sunset with a lake reflection"
  }
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "Generated image successfully."
    },
    {
      "type": "image",
      "mimeType": "image/png",
      "data": "<base64 image data>"
    }
  ]
}

Open WebUI的MCP集成吸收了 type: "image" 内容项和 将生成的图像附加到聊天中。

示例:使用传统URL工具

对于需要URL的客户端,原始 generate_image_url_from_prompt 工具仍然可用:

{
  "name": "generate_image_url_from_prompt",
  "arguments": {
    "prompt": "A serene mountain landscape at sunset with a lake reflection"
  }
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "http://localhost:3000/images/..."
    }
  ]
}

工作流程要求

ComfyUI工作流必须包括:

  • A. CLIPTextEncode 正提示的节点(或指定 COMFYUI_INPUT_NODE_ID)
  • 至少一个SaveImage节点(或指定 COMFYUI_OUTPUT_NODE_ID)

自动检测: 默认情况下,服务器会自动找到第一个 CLIPTextEncode 节点用于提示注入,第一个SaveImage节点用于 输出。对于具有多个此类节点或不同节点类型的工作流,请使用 可选的环境变量,用于指定要使用的节点和字段。

OpenCode示例

在 ~/.config/opencode/config.json:

{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    "image-generator": {
      "template": "User Request: $ARGUMENTS\n\nYou are an image generation specialist. When a user requests an image:\n\n1. Transform their request into a detailed, effective image prompt that will produce high-quality results\n2. Use the `image-generator_generate_image_url_from_prompt` tool to generate the image\n3. Return the response with:\n   - The actual prompt you used (in clear text)\n   - The image embedded as markdown: \n![image description](<image_url>)\n   - The raw image URL (for interfaces that don't render images)\n\nBe concise and direct. Always include all three elements: the prompt text, the markdown image, and the raw URL.\n\n## Example Output:\n\nUser: Draw me a cat\n\nYour response:\nPrompt: A playful orange tabby cat sitting on a windowsill, soft natural lighting, cozy home setting, digital art style, warm colors, detailed fur texture\n\n![orange tabby cat](https://example.com/images/generated-abc123.jpg)\n\nImage URL: https://example.com/images/generated-abc123.jpg",
      "description": "Generates an image"
    }
  },
  "mcp": {
    "image-generator": {
      "type": "remote",
      "url": "http://127.0.0.1:3000/mcp",
      "enabled": true
    }
  }
}

错误处理

错误 描述
ComfyUI server unavailable 无法在配置的URL连接到ComfyUI
Workflow not found 指定路径中不存在工作流文件
No CLIPTextEncode node found 工作流没有 CLIPTextEncode 类节点
Image generation timed out 生成时间超过5分钟
Workflow did not produce an image output 未生成图像

故障排除

服务器无法启动

  • 验证 COMFYUI_URL 和 WORKFLOW_PATH 已设置
  • 确保ComfyUI正在运行,并且可以在配置的URL上访问

工具返回“找不到工作流”

  • 检查一下 WORKFLOW_PATH 指向有效的工作流JSON文件
  • 确保工作流程JSON包含 CLIPTextEncode 节点和 SaveImage 节点

工具返回“未找到CLIPTextEncode节点”

  • 您的工作流程必须包括 CLIPTextEncode 类节点
  • 这通常是接收肯定文本提示的节点

世代超时

  • 检查ComfyUI是否有足够的GPU资源
  • 尝试降低工作流程的复杂性(步骤、分辨率等)
  • 默认超时为5分钟;必要时进行调整

发展

# Build and run in development mode
npm run dev

许可证

ISC