gemini-imggen
Enables image generation and transformation using Gemini API, returning file paths instead of base64 data to avoid token limit issues in Claude Code.
README
Gemini Image Generation MCP Server
A token-optimized MCP server that enables Gemini image generation in MCP clients by returning file paths instead of base64 data.
Why This Exists
Existing Gemini image generation MCP servers fail in Claude Code with MCP tool response exceeded token limit errors. They return base64-encoded image data (~2.4M tokens per image), exceeding Claude Code's 25,000 token limit.
This implementation solves the problem by saving images to disk and returning only file paths (~20 tokens) — a 120,000× reduction in token usage.
| Implementation | Response | Tokens | Result |
|---|---|---|---|
| Existing servers | Base64 data | 2.4M | ❌ Error |
| This server | File path | ~20 | ✅ Works |
Features
- Token-optimized: Returns file paths only (~20 tokens vs 2.4M)
- Two generation modes: Text-to-image and image-to-image transformation
- Claude Code compatible: Works within 25,000 token limit
- ISO 8601 UTC timestamps: Globally sortable filenames (
YYYYMMDDTHHMMSSZ.png) - Lightweight: Minimal dependencies
- Fast: uv-powered startup
- Simple: No build step required
Requirements
- Python 3.10+
- uv - Modern Python package manager (10-100× faster than pip)
- Gemini API key from Google AI Studio
Install uv
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Homebrew
brew install uv
# Verify installation
uv --version
Quick Start
# 1. Clone and navigate
git clone https://github.com/couhie/mcp-gemini-imggen.git
cd mcp-gemini-imggen
# 2. Configure settings
cp .env.example .env
# Edit .env and set:
# GEMINI_API_KEY - Your API key from Google AI Studio
# OUTPUT_DIR - Directory for generated images (e.g., ~/Pictures/ai)
# Directory will be created automatically if it doesn't exist
# 3. Add to Claude Code
claude mcp add -s user gemini-imggen uv -- --directory $(pwd) run mcp-gemini-imggen
Configuration
Claude Code CLI (Recommended)
claude mcp add -s user gemini-imggen uv -- --directory /absolute/path/to/mcp-gemini-imggen run mcp-gemini-imggen
Manual Setup
Add to ~/.claude.json:
{
"mcpServers": {
"gemini-imggen": {
"type": "stdio",
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/mcp-gemini-imggen",
"run",
"mcp-gemini-imggen"
],
"env": {}
}
}
}
Note: Use absolute paths, not ~ (e.g., /Users/yourname/dev/mcp-gemini-imggen)
Usage
Once configured, use the MCP tools in Claude Code:
Text-to-Image Generation
Generate a flat design style cute cat illustration
Image-to-Image Transformation
Transform /Users/name/Pictures/ai/20251015T120000Z.png: make the background blue
Note: You must provide the file path to an existing image. Common use cases:
- Modify previously generated images
- Transform images already saved on your system
- Chain transformations: generate → transform → transform again
The server will:
- Generate/transform the image using Gemini 2.5 Flash
- Save it to
$OUTPUT_DIR/YYYYMMDDTHHMMSSZ.png(ISO 8601 UTC format) - Return only the file path (~20 tokens)
Claude Code will automatically display the generated image.
Technical Details
Token Optimization
Base64-encoded responses cause token explosion:
- 1536×1536 PNG ≈ 1.4MB → Base64 ≈ 1.9MB (33% overhead)
- Token conversion: 1.9MB ÷ 4 chars/token ≈ 475,000 tokens
- Multiple images (4×): ~1,900,000 tokens
- JSON wrapper: +500,000 tokens
- Total: ~2,400,000 tokens (exceeds 25,000 limit)
Solution: Return file path instead of data
# ❌ Existing: 2.4M tokens
{"type": "image", "data": "iVBORw0KGgo...", "mimeType": "image/png"}
# ✅ This server: ~20 tokens
[{"type": "text", "text": "/Users/name/Pictures/ai/20251015T120000Z.png"}]
Troubleshooting
"uv: command not found"
Install uv first:
curl -LsSf https://astral.sh/uv/install.sh | sh
"GEMINI_API_KEY environment variable is required"
Get your API key from Google AI Studio and add to .env
"OUTPUT_DIR environment variable is required"
Set your desired output directory in .env (e.g., OUTPUT_DIR=~/Pictures/ai). The directory will be created automatically if it doesn't exist.
Images not generating
- Verify API key is valid at Google AI Studio
- Check API quota limits
- Verify OUTPUT_DIR path is valid (parent directories must be writable)
Contributing
Contributions are welcome! Please submit a Pull Request.
License
MIT License - see LICENSE for details.
Links
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。