Gemini Image Generation MCP Server
Provides image generation capabilities using Google's Gemini 2.0 Flash Preview model through the MCP protocol, enabling AI assistants to generate high-quality images from text prompts.
README
Gemini Image Generation MCP Server
A Model Context Protocol (MCP) server that provides image generation capabilities using Google's Gemini 2.0 Flash Preview model. This server allows AI assistants to generate high-quality images from text prompts through the MCP protocol.
Features
- Image Generation: Generate images from text prompts using Gemini 2.0 Flash Preview
- Multiple Output Formats: Support for PNG, JPEG, and other image formats
- File Management: Automatic saving of generated images with organized file naming
- Base64 Support: Handle image data in base64 format for easy integration
- Status Monitoring: Check API connection status and model information
- Prompt Templates: Pre-built prompts for optimized image generation
Prerequisites
- Python 3.9 or higher
- Google AI API key (Gemini API access)
- uv package manager (recommended) or pip
Installation
Using uv (Recommended)
- Clone or download this repository:
git clone <repository-url>
cd image-generation-gemini-mcp
- Install dependencies:
uv sync
Using pip
- Install dependencies:
pip install -r requirements.txt
Setup
1. Get Google AI API Key
- Visit Google AI Studio
- Create a new API key
- Copy the API key for use in the next step
2. Set Environment Variable
Set your Gemini API key as an environment variable:
Windows (PowerShell):
$env:GEMINI_API_KEY="your-api-key-here"
Windows (Command Prompt):
set GEMINI_API_KEY=your-api-key-here
macOS/Linux:
export GEMINI_API_KEY="your-api-key-here"
For permanent setup, add the environment variable to your system's environment variables or shell profile.
Usage
Running the Server
Development Mode
To test the server locally:
uv run server.py
MCP Integration Mode
To run as an MCP server:
uv run server.py stdio
Integration with MCP Clients
Claude Desktop Integration
- Add the server to your Claude Desktop configuration file:
Windows: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"gemini-image-generator": {
"command": "uv",
"args": ["run", "server.py", "stdio"],
"cwd": "C:\\path\\to\\image-generation-gemini-mcp",
"env": {
"GEMINI_API_KEY": "your-api-key-here"
}
}
}
}
- Restart Claude Desktop
- The image generation tools will be available in your conversations
Other MCP Clients
For other MCP-compatible clients, configure them to run:
uv run server.py stdio
With the working directory set to this project folder and the GEMINI_API_KEY environment variable configured.
Available Tools
generate_image
Generate an image from a text prompt.
Parameters:
prompt(string): Text description of the image to generateoutput_dir(string, optional): Directory to save the image (default: "./generated_images")
Returns:
success(boolean): Whether generation was successfulmessage(string): Status message or error descriptionimage_data(string): Base64 encoded image data (if successful)mime_type(string): MIME type of the generated imagefile_extension(string): File extension for the image
save_image_from_base64
Save a base64 encoded image to a file.
Parameters:
image_data(string): Base64 encoded image datafilename(string): Name for the output file (include extension)output_dir(string, optional): Directory to save the image
Returns:
success(string): "true" or "false"message(string): Status messagefile_path(string): Path to saved file (if successful)
Available Resources
gemini://api-status
Check the status of the Gemini API connection.
gemini://model-info
Get information about the Gemini image generation model capabilities.
Available Prompts
image_generation_prompt
Generate a detailed prompt optimized for image generation.
Parameters:
subject(string): Main subject or object to generatestyle(string, optional): Art style (default: "photorealistic")mood(string, optional): Mood or atmosphere (default: "neutral")
Example Usage
Once integrated with an MCP client like Claude Desktop, you can:
-
Generate an image:
Please generate an image of a sunset over mountains -
Use specific styles:
Create a cartoon-style image of a friendly robot -
Check API status:
Can you check the status of the Gemini API? -
Get model information:
What are the capabilities of the image generation model?
File Structure
image-generation-gemini-mcp/
├── server.py # Main MCP server implementation
├── requirements.txt # Python dependencies
├── pyproject.toml # Project configuration
├── README.md # This file
├── docs/ # Documentation files
│ ├── development-guidelines.md
│ ├── mcp-info.md
│ └── mcp-python-sdk-readme.md
└── generated_images/ # Default output directory (created automatically)
Troubleshooting
Common Issues
-
"GEMINI_API_KEY environment variable is required"
- Ensure you've set the
GEMINI_API_KEYenvironment variable - Verify the API key is valid and has access to Gemini API
- Ensure you've set the
-
"Error connecting to Gemini API"
- Check your internet connection
- Verify your API key is correct and active
- Ensure you have access to the Gemini 2.0 Flash Preview model
-
"No image was generated"
- Try rephrasing your prompt
- Ensure your prompt is descriptive and clear
- Check if there are any content policy restrictions
-
Permission errors when saving files
- Ensure the output directory is writable
- Check file system permissions
Getting Help
- Check the MCP documentation
- Review the Google AI documentation
- Ensure all dependencies are properly installed
Development
Code Quality
This project follows the MCP development guidelines:
- Formatting:
uv run ruff format . - Linting:
uv run ruff check . - Type checking:
uv run pyright
Testing
Run tests with:
uv run pytest
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please follow the MCP development guidelines and ensure all code is properly formatted and type-checked before submitting.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。