OpenRouter Custom Image Generation MCP Server
Generates images using OpenRouter models and retrieves user-uploaded images from LibreChat database, enabling image generation and editing with reference images.
README
🖼️ MCP Image Generation Server for LibreChat
A custom Model Context Protocol (MCP) server that provides AI image generation capabilities to LibreChat via OpenRouter.
Model: Locked to
google/gemini-2.5-flash-imagefor all requests. To change the model, modify the source code intools.js.
📂 Project Structure
mcp-image-gen-custom/
├── index.js # Express app, SSE transport, MCP server + tool registration
├── tools.js # Tool handlers: get_user_images, generate_image, rate limiting
├── openrouter.js # OpenRouter API communication (chat completions with image modality)
├── files.js # MongoDB file record lookup + local file stream reader
├── db.js # MongoDB connection pool manager
├── Dockerfile # Container build definition
├── package.json
└── example/
├── docker-compose.override.example.yml # Docker service config example
├── librechat.example.yaml # librechat.yaml MCP config example
└── system_instructions.example.md # Agent system prompt template
🛠️ Tools Provided
1. get_user_images
Retrieves images uploaded by the current user in LibreChat, returning index aliases and file_ids for use as reference images.
| Argument | Type | Required | Description |
|---|---|---|---|
limit |
number | No | Max images to return. Default: 10 |
Example response:
Found 2 uploaded image(s):
1. [INDEX_1] file_id: "cab88c4e-aecb-4812-9fff-6c85aede6362" — mycar.png
2. [INDEX_2] file_id: "f6752381-3a6d-4b69-a92b-5555a9521069" — cat.png
Use EITHER the file_id OR the index number (e.g., '1', '2' or 'INDEX_1', 'INDEX_2') as the reference_image_url when calling generate_image.
2. generate_image
Generates an image from a text prompt. Supports Text-to-Image and Image-to-Image (editing) via reference images.
| Argument | Type | Required | Description |
|---|---|---|---|
prompt |
string | ✅ | Text description of the image to generate |
reference_image_url |
string | No | Index alias ("1", "INDEX_1") or local file_id of a single reference image |
reference_image_urls |
string[] | No | List of index aliases or file_ids for multiple reference images |
aspect_ratio |
string | No | Output image dimensions. See options below |
Aspect ratio options:
| Value | Shape | Common use |
|---|---|---|
1:1 |
Square | Profile pictures, feed posts |
16:9 |
Wide landscape | Banners, cover photos |
9:16 |
Tall portrait | Stories, Reels, TikTok |
4:5 |
Portrait | Feed posts (portrait) |
Note:
aspect_ratioonly controls image dimensions — it does not affect image style or content.
Note: Do NOT pass Google/OpenAI temporary
file-xxxxIDs intoreference_image_url. These are not stored in the local database. Always useget_user_imagesfirst to get valid index aliases or localfile_ids.
🔒 Per-User Rate Limiting
The server enforces configurable usage limits per user to prevent spam. Limits are tracked in MongoDB (mcp_image_gen_usage collection).
| Env Variable | Default | Description |
|---|---|---|
IMAGE_GEN_DAILY_LIMIT |
3 |
Max images per user per day (reset at midnight local time) |
IMAGE_GEN_COOLDOWN_SEC |
30 |
Min seconds between generations per user |
To reset limits manually (all users):
docker exec chat-mongodb mongosh LibreChat --eval "db.mcp_image_gen_usage.deleteMany({})"
To reset limits for a specific user:
docker exec chat-mongodb mongosh LibreChat --eval "db.mcp_image_gen_usage.deleteMany({ userId: ObjectId('<userId>') })"
To view current usage:
docker exec chat-mongodb mongosh LibreChat --eval "db.mcp_image_gen_usage.aggregate([{ \$group: { _id: '\$userId', count: { \$sum: 1 } } }]).forEach(r => print(r._id, '->', r.count))"
⚙️ How Index Resolution Works
The LLM agent only sees image bytes in chat history — it does not know the database file_id. This server resolves short index aliases (e.g., "1" or "INDEX_1") to the correct local file_id automatically.
sequenceDiagram
actor User
participant Agent as LLM Agent
participant MCP as MCP Server
participant DB as MongoDB
User->>Agent: "Edit this image" (uploads car.png)
Agent->>MCP: get_user_images()
MCP->>DB: Query user's latest images
DB-->>MCP: [car.png → file_id: "abc-123"]
MCP-->>Agent: "1. [INDEX_1] file_id: 'abc-123' — car.png"
Agent->>MCP: generate_image(prompt="make it red", reference_image_url="INDEX_1")
Note over MCP: Resolves "INDEX_1" → "abc-123"
MCP->>DB: Fetch file "abc-123" from disk
MCP-->>User: Returns edited image ✅
🚀 Installation & Deployment
Prerequisites
- LibreChat running via Docker Compose
- An OpenRouter API key
Step 1: Place this folder
Clone or copy this mcp-image-gen-custom/ directory into the root of your LibreChat repository:
LibreChat/
├── mcp-image-gen-custom/ ← here
├── api/
├── docker-compose.yml
└── ...
Step 2: Configure Environment Variables
Add the following to your .env file at the LibreChat root:
# Required
OPENROUTER_KEY=your_openrouter_api_key_here
# Optional — image generation rate limits
IMAGE_GEN_DAILY_LIMIT=3
IMAGE_GEN_COOLDOWN_SEC=30
Step 3: Add the MCP service to Docker Compose
Add the following to your docker-compose.override.yml (create it if it doesn't exist):
services:
api:
volumes:
- type: bind
source: ./librechat.yaml
target: /app/librechat.yaml
mcp-image-gen:
build: ./mcp-image-gen-custom
restart: always
environment:
NODE_ENV: ${NODE_ENV:-production}
PORT: 3013
OPENROUTER_KEY: ${OPENROUTER_KEY}
OPENROUTER_API_KEY: ${OPENROUTER_KEY}
MONGO_URI: mongodb://mongodb:27017/LibreChat
IMAGE_GEN_DAILY_LIMIT: ${IMAGE_GEN_DAILY_LIMIT:-3}
IMAGE_GEN_COOLDOWN_SEC: ${IMAGE_GEN_COOLDOWN_SEC:-30}
volumes:
- ./uploads:/app/uploads # shared with LibreChat api container
- ./images:/app/images # shared with LibreChat api container
expose:
- "3013"
networks:
- default
See
example/docker-compose.override.example.ymlfor a complete example.
Step 4: Register the MCP server in librechat.yaml
Append the following to your librechat.yaml:
mcpSettings:
allowedDomains:
- 'mcp-image-gen'
allowedAddresses:
- 'mcp-image-gen:3013'
mcpServers:
openrouter-imager:
type: sse
url: http://mcp-image-gen:3013/sse
headers:
x-user-id: '{{LIBRECHAT_USER_ID}}'
See
example/librechat.example.yamlfor a complete example.
Step 5: Build and start
# Build and start all services
docker compose up -d --build
# Restart the LibreChat API to reload librechat.yaml
docker compose restart api
Step 6: Configure the Agent in LibreChat UI
- Go to LibreChat → Agents and create a new agent (or edit an existing one).
- Enable the
openrouter-imagerMCP tool. - Paste the system prompt from
example/system_instructions.example.mdinto the Instructions field.
🔧 Changing the Model
The model is hardcoded to google/gemini-2.5-flash-image in tools.js:
const selectedModel = "google/gemini-2.5-flash-image";
To use a different model, edit this line directly and rebuild the container:
docker compose up -d --build mcp-image-gen
📋 Useful Commands
# View live MCP server logs
docker logs librechat-mcp-image-gen-1 -f
# Rebuild and restart MCP server only
docker compose up -d --build mcp-image-gen
# Reset all image generation usage limits
docker exec chat-mongodb mongosh LibreChat --eval "db.mcp_image_gen_usage.deleteMany({})"
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。