Content
# Image-Gen-Server
<div align="center">
<img src="images/logo_0.png" alt="Image-Gen-Server Logo" width="100%">
</div>
An image generation service based on Jimeng AI, specifically designed for integration with Cursor IDE. It receives text descriptions from Cursor, generates corresponding images, and provides functionality for downloading and saving images.
You can view the development process of this plugin on my website: [Developing an MCP Server Integrated with Cursor, Giving Cursor Wings!](https://aibook.ren/archives/mcp-server-for-cursor)
For more AI knowledge, see the AI Book (https://aibook.ren)
<div align="center">
<img src="images/example.png" alt="Image-Gen-Server Logo" width="100%">
</div>
## Features
- Perfect integration with Cursor IDE
- Supports text-to-image generation
- Automatically saves generated images
- Supports custom save paths
- Generates four images at once for more options
## Installation
1. Environment preparation. MCP is a relatively new technology, and the dependencies require newer versions.
- python 3.10+
- Install npm
- Install nodejs (versions v15 and v16 are not working; v20 has been verified to work in the development environment, others are unverified)
- Install with `pip install uv`
- If you want to debug, you also need to install this: `npm install -g @modelcontextprotocol/inspector@0.4.0`
2. Clone the project
```bash
git clone https://github.com/fengin/image-gen-server.git
cd image-gen-server
```
3. Install dependencies
```bash
pip install -r requirements.txt
pip install uv
```
4. Set the Jimeng Token and default image save path
Modify these two configurations in the `server.py` file
```bash
# API configuration
JIMENG_API_TOKEN = "057f7addf85dxxxxxxxxxxxxx" # Your session_id obtained from logging into Jimeng, supports multiple, separated by commas
IMG_SAVA_FOLDER = "D:/code/image-gen-server/images" # Default image save path
```
## Cursor Integration
<div align="center">
<img src="images/cursor_config.png" alt="Image-Gen-Server Logo" width="100%">
</div>
1. Open Cursor settings
- Click the settings icon in the lower left corner
- Select Features > MCP Servers
- Click "Add new MCP server"
2. Fill in the server configuration
- Name: `image-gen-server` (or any name you prefer)
- Type: `command`
- Command:
```bash
uv run --with fastmcp fastmcp run D:\code\image-gen-service\server.py
```
Note: Replace the path with your actual project path
- Windows example: `uv run --with fastmcp fastmcp run D:/code/image-gen-service/server.py`
- macOS/Linux example: `uv run --with fastmcp fastmcp run /Users/username/code/image-gen-server/server.py`
There are many issues with Windows paths, try various slashes for `D:/code/image-gen-server/server.py`
After filling in, a black window will pop up, and then you can ask Cursor to generate the images you need. Currently, the black window will keep running, and there is no way to resolve this issue yet.
## Usage
In Cursor, to generate an image, you need to prompt it to understand how to use the image tool in agent mode, and then directly state your image generation requirements and the save location.
## Obtaining Jimeng Token
1. Visit [Jimeng](https://jimeng.jianying.com/)
2. Log in to your account
3. Press F12 to open the developer tools
4. Find `sessionid` in Application > Cookies
5. Set the found `sessionid` in `server.py`'s `JIMENG_API_TOKEN`
## Tool Function Description
### generate_image
```python
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[types.TextContent | types.ImageContent | types.EmbeddedResource]:
"""Generate an image based on the text description
Args:
prompt: The text prompt description of the image
file_name: The filename of the generated image (without path; defaults to .jpg if no suffix is provided)
save_folder: The absolute directory for saving the image (optional; defaults to IMG_SAVA_FOLDER)
sample_strength: The fineness of the generated image (optional; range 0-1; defaults to 0.5)
width: The width of the generated image (optional; defaults to 1024)
height: The height of the generated image (optional; defaults to 1024)
Returns:
List: A JSON string containing the generation results
"""
```
### Technical Implementation
1. `server.py` uses fastmcp to implement the MCP server capabilities, providing services for Cursor/Claude.
2. `server.py` calls the `proxy.jimeng` module to interact with Jimeng AI in reverse.
The `proxy.jimeng` reverse module can also be installed separately and mainly provides the following features:
- Image generation (`generate_images`)
- Synchronous dialogue completion (`create_completion`)
- Streaming dialogue completion (`create_completion_stream`)
- Multi-account token support
- Comprehensive error handling
For more detailed information, please refer to `proxy/jimeng/README.md`.
### Usage Examples
```cmd
# In Cursor agent mode
# Example 1
Based on the project requirements you provided, help me generate a product logo and place it in the project directory under images.
# Example 2
Based on the project requirements, help me create the homepage of the website, with a banner image at the top.
```
## License
MIT License
Author: Ling Feng
## Troubleshooting
1. After configuration, a black window pops up and quickly disappears, and the tool status changes to "No tools found."
Reason: It did not start properly, generally due to the following reasons:
- Incorrect configuration command; check if the command is correct. Usually, it's due to an incorrect `server.py` path, or the path contains Chinese characters, or incorrect slashes.
- The required environment is not prepared.
- The terminal for running dependencies is incorrect. For example, on Windows, there are terminals like git bash, cmd, powershell, wsl, etc. Try these terminals. The default terminal for my Cursor configuration is cmd. If you encounter errors running in the corresponding terminal, it is usually due to the environment not being set up correctly; installing the environment should resolve it.
2. After running normally, if you want to see the call logs or debug, change the command to the following:
```
uv run --with fastmcp fastmcp dev D:/code/image-gen-service/server.py
```
Just change the last `run` to `dev`.
Or find a terminal and run the following command to enter debug mode:
```
fastmcp dev D:/code/image-gen-service/server.py
```
A debug address will be output: http://localhost:5173/, which you can open in a browser to access the MCP Inspector for debugging. For specific usage of MCP Inspector, please refer to the official documentation.