OpenRouter Custom Image Generation MCP Server
Allows retrieval of user-uploaded images stored in a MongoDB database used by LibreChat, and provides image generation capabilities via OpenRouter models.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@OpenRouter Custom Image Generation MCP ServerEdit my uploaded image of a car to make it red"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
š¼ļø 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 templateRelated MCP server: OpenRouter Image MCP Server
š ļø 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 |
| number | No | Max images to return. Default: |
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 |
| string | ā | Text description of the image to generate |
| string | No | Index alias ( |
| string[] | No | List of index aliases or |
| string | No | Output image dimensions. See options below |
Aspect ratio options:
Value | Shape | Common use |
| Square | Profile pictures, feed posts |
| Wide landscape | Banners, cover photos |
| Tall portrait | Stories, Reels, TikTok |
| 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 |
|
| Max images per user per day (reset at midnight local time) |
|
| 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=30Step 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:
- defaultSee
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 apiStep 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({})"This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseBqualityDmaintenanceEnables AI-powered image generation using Azure DALL-E 3 and FLUX models with intelligent automatic model selection. Generates stunning photorealistic or creative images directly within LibreChat through simple text prompts.Last updated142MIT
- -licenseAquality-maintenanceEnables generating and editing images using OpenRouter's API with Gemini 2.5 Flash Image model. Supports custom aspect ratios, iterative editing, and reference images for style transfer.Last updated698
- AlicenseBqualityDmaintenanceEnables image generation via OpenRouter API, supporting models like Gemini 2.5 Flash Image Preview with options to save files locally.Last updated21Do What The F*ck You Want To Public
- Alicense-qualityDmaintenanceEnables chat and image analysis through OpenRouter.ai models. Supports text chat, image generation, and analysis with multiple images and custom questions.Last updated245Apache 2.0
Related MCP Connectors
Image, video, audio, face-swap, talking avatars and chat across 300+ AI models, one balance.
Generate logos, social posts, app screenshots, comic panels & visual-novel assets from prompts.
Generate images, video, and audio with Glif's media-generation agent
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/nguyen1oc/mcp-librechat-image-generation-via-OP'
If you have feedback or need assistance with the MCP directory API, please join our Discord server