MCP HubMCP Hub
fengin

image-gen-server

by: fengin

一个能与Cursor集成的图片生成mcp server工具,实现调用即梦逆向接口

86created 07/02/2025
Visit
AI
Python

📌Overview

Purpose: To provide a seamless image generation service specifically designed for integration with Cursor IDE, enabling users to generate images from text descriptions.

Overview: The Image-Gen-Server is built on the JIMENG AI framework, allowing users to create images based on text prompts received from Cursor IDE. It offers functionality for generating images, along with options for saving and downloading them, enhancing the creative workflow within the IDE.

Key Features:

  • Seamless Integration with Cursor IDE: Directly interacts with the Cursor environment for efficient image generation.

  • Text-to-Image Conversion: Converts user-provided text descriptions into generated images, expanding creative possibilities.

  • Automatic Image Saving: Automatically saves generated images to a specified location, streamlining the workflow.

  • Customizable Save Path: Users can define their own image save locations, providing flexibility.

  • Multiple Image Generation: Capable of generating up to four images at once, offering users a variety of options to choose from.


Image-Gen-Server

基于即梦AI的图像生成服务,专门设计用于与Cursor IDE集成。它接收来自Cursor的文本描述,生成相应的图像,并提供图片下载和保存功能。

获取更多信息可访问开发一个MCP Server与Cursor集成AI全书

特性

  • 与Cursor IDE完美集成
  • 支持文本到图像的生成
  • 自动保存生成的图像
  • 支持自定义保存路径
  • 一次生成四张图,供更多选择

安装

  1. 环境准备:

    • python 3.10+
    • 安装npm
    • 安装nodejs(建议使用v20)
    • 安装 pip install uv
    • (可选)安装调试工具 npm install -g @modelcontextprotocol/inspector@0.4.0
  2. 克隆项目:

    git clone https://github.com/fengin/image-gen-server.git
    cd image-gen-server
    
  3. 安装依赖:

    pip install -r requirements.txt
    pip install uv
    
  4. 设置即梦Token和图片默认保存地址: 修改server.py文件中的以下配置:

    JIMENG_API_TOKEN = "你的即梦session_id" 
    IMG_SAVA_FOLDER = "D:/code/image-gen-server/images"  # 图片默认保存路径
    

Cursor集成

  1. 打开Cursor设置:

    • 点击左下角的设置图标
    • 选择 Features > MCP Servers
    • 点击 "Add new MCP server"
  2. 填写服务器配置:

    • Name: image-gen-server
    • Type: command
    • Command:
      uv run --with fastmcp fastmcp run D:\code\image-gen-service\server.py
      
    • Windows 示例: uv run --with fastmcp fastmcp run D:/code/image-gen-server/server.py
    • macOS/Linux 示例: uv run --with fastmcp fastmcp run /Users/username/code/image-gen-server/server.py

使用方法

在Cursor中,生成图片时需在agent模式下提示工具使用方法,然后输入生成的图片要求和保存位置。

获取即梦Token

  1. 访问 即梦
  2. 登录账号
  3. 打开开发者工具(F12),在Application > Cookies中找到sessionid
  4. 将找到的sessionid设置到server.pyJIMENG_API_TOKEN中。

工具函数说明

generate_image

async def generate_image(prompt: str, file_name: str, save_folder: str = None, sample_strength: float = 0.5, width: int = 1024, height: int = 1024) -> list:
    """根据文本描述生成图片
    
    Args:
        prompt: 图片的文本prompt描述
        file_name: 生成图片的文件名
        save_folder: 图片保存绝对地址目录(可选)
        sample_strength: 生成图片的精细度(可选)
        width: 生成图片的宽度(可选)
        height: 生成图片的高度(可选)

    Returns:
        List: 包含生成结果的JSON字符串
    """

技术实现

  1. server.py 采用了fastmcp实现了mcp server的能力,供Cursor使用。
  2. server.py调用的proxy.jimeng模块与即梦AI进行交互,支持图像生成、对话补全及多账号token等功能。

使用示例

# 在 cursor agent模式下
# 例子一
根据项目需求,生成一张产品logo,保存于项目目录images下。

# 例子二
根据项目需求,制作网站首页,头部应有banner图片。

许可证

MIT License
作者:凌封

故障排除

  1. 如果启动后黑窗口消失,工具状态变为No tools found,可能的原因包括:

    • 配置命令错误:检查server.py路径是否正确。
    • 环境未准备好。
    • 试用不同终端运行。
  2. 要查看调用日志或调试: 修改命令为:

    uv run --with fastmcp fastmcp dev D:/code/image-gen-service/server.py
    

    或使用调试模式运行:

    fastmcp dev D:/code/image-gen-service/server.py
    

输出的调试地址为 http://localhost:5173/,可用浏览器打开进行调试。