图像生成服务

基于OpenAI的gpt-image-1模型,提供图像生成和编辑功能的MCP服务器。

  • by IncomeStreamSurfer
  • Python
  • 本地部署
  • 15 星标
  • 更新于
在 GitHub 查看 IncomeStreamSurfer/chatgpt-native-image-gen-mcp

使用说明

OpenAI图像生成MCP服务器

该项目实现了一个MCP(模型上下文协议)服务器,该服务器提供使用OpenAI生成和编辑图像的工具 gpt-image-1 通过官方Python SDK建模。

特性

此MCP服务器提供以下工具:

  • generate_image:使用OpenAI生成图像 gpt-image-1 基于文本提示的模型并保存。

    • 输入架构:
      {
        "type": "object",
        "properties": {
          "prompt": { "type": "string", "description": "The text description of the desired image(s)." },
          "model": { "type": "string", "default": "gpt-image-1", "description": "The model to use (currently 'gpt-image-1')." },
          "n": { "type": ["integer", "null"], "default": 1, "description": "The number of images to generate (Default: 1)." },
          "size": { "type": ["string", "null"], "enum": ["1024x1024", "1536x1024", "1024x1536", "auto"], "default": "auto", "description": "Image dimensions ('1024x1024', '1536x1024', '1024x1536', 'auto'). Default: 'auto'." },
          "quality": { "type": ["string", "null"], "enum": ["low", "medium", "high", "auto"], "default": "auto", "description": "Rendering quality ('low', 'medium', 'high', 'auto'). Default: 'auto'." },
          "user": { "type": ["string", "null"], "default": null, "description": "An optional unique identifier representing your end-user." },
          "save_filename": { "type": ["string", "null"], "default": null, "description": "Optional filename (without extension). If None, a default name based on the prompt and timestamp is used." }
        },
        "required": ["prompt"]
      }
      
    • 输出: {"status": "success", "saved_path": "path/to/image.png"} 或错误字典。
  • edit_image:使用OpenAI编辑图像或创建变体 gpt-image-1 模型并保存。可以使用多个输入图像作为参考或使用蒙版进行修复。

    • 输入架构:
      {
        "type": "object",
        "properties": {
          "prompt": { "type": "string", "description": "The text description of the desired final image or edit." },
          "image_paths": { "type": "array", "items": { "type": "string" }, "description": "A list of file paths to the input image(s). Must be PNG. < 25MB." },
          "mask_path": { "type": ["string", "null"], "default": null, "description": "Optional file path to the mask image (PNG with alpha channel) for inpainting. Must be same size as input image(s). < 25MB." },
          "model": { "type": "string", "default": "gpt-image-1", "description": "The model to use (currently 'gpt-image-1')." },
          "n": { "type": ["integer", "null"], "default": 1, "description": "The number of images to generate (Default: 1)." },
          "size": { "type": ["string", "null"], "enum": ["1024x1024", "1536x1024", "1024x1536", "auto"], "default": "auto", "description": "Image dimensions ('1024x1024', '1536x1024', '1024x1536', 'auto'). Default: 'auto'." },
          "quality": { "type": ["string", "null"], "enum": ["low", "medium", "high", "auto"], "default": "auto", "description": "Rendering quality ('low', 'medium', 'high', 'auto'). Default: 'auto'." },
          "user": { "type": ["string", "null"], "default": null, "description": "An optional unique identifier representing your end-user." },
          "save_filename": { "type": ["string", "null"], "default": null, "description": "Optional filename (without extension). If None, a default name based on the prompt and timestamp is used." }
        },
        "required": ["prompt", "image_paths"]
      }
      
    • 输出: {"status": "success", "saved_path": "path/to/image.png"} 或错误字典。

先决条件

  • Python(建议使用3.8或更高版本)
  • pip(Python包安装程序)
  • OpenAI API密钥(直接在脚本中设置或通过 OPENAI_API_KEY 环境变量- 为了安全起见,强烈建议使用环境变量).
  • 能够管理和启动MCP服务器的MCP客户端环境(如Cline使用的环境)。

安装

  1. 克隆存储库:
    git clone https://github.com/IncomeStreamSurfer/chatgpt-native-image-gen-mcp.git
    cd chatgpt-native-image-gen-mcp
    
  2. 设置虚拟环境(推荐):
    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. 安装依赖项:
    pip install -r requirements.txt
    
  4. (可选但推荐)设置环境变量: 设置 OPENAI_API_KEY 使用OpenAI密钥替换环境变量,而不是在脚本中对其进行硬编码。如何设置取决于您的操作系统。

配置(适用于临床MCP客户端)

要使此服务器可供您的AI助手(如Cline)使用,请将其配置添加到MCP设置文件中(例如。, cline_mcp_settings.json).

查找 mcpServers 在设置文件中添加对象,并添加以下条目:

{
  "mcpServers": {
    // ... other server configurations ...

    "openai-image-gen-mcp": {
      "autoApprove": [
        "generate_image",
        "edit_image"
      ],
      "disabled": false,
      "timeout": 180, // Increased timeout for potentially long image generation
      "command": "python", // Or path to python executable if not in PATH
      "args": [
        // IMPORTANT: Replace this path with the actual absolute path
        // to the openai_image_mcp.py file on your system
        "C:/path/to/your/cloned/repo/chatgpt-native-image-gen-mcp/openai_image_mcp.py"
      ],
      "env": {
        // If using environment variables for the API key:
        // "OPENAI_API_KEY": "YOUR_API_KEY_HERE"
      },
      "transportType": "stdio"
    }

    // ... other server configurations ...
  }
}

重要提示: 替换 C:/path/to/your/cloned/repo/ 在计算机上克隆此存储库的正确绝对路径。确保路径分隔符适用于您的操作系统(例如,使用反斜杠 \ 在Windows上)。如果通过环境变量设置API键,则可以将其从脚本中删除,并可能将其添加到 env 如果您的MCP客户端支持,请点击此处。

运行服务器

您通常不需要手动运行服务器。MCP客户端(如Cline)将使用 command 和 args 当其工具之一首次被调用时,在配置文件中指定。

如果要手动测试它(确保已安装依赖项并且API密钥可用):

python openai_image_mcp.py

用法

AI助手使用 generate_image 和 edit_image 工具。图像保存在 ai-images 在以下位置创建的子目录 openai_image_mcp.py 脚本已定位。成功后,这些工具将返回保存图像的绝对路径。