MCP Imagen Server

MCP Imagen Server

Enables text-to-image generation, style transfer, background removal, and automatic image cropping using Google's Imagen AI models through the Model Context Protocol.

Category
访问服务器

README

MCP Imagen Server

An MCP (Model Context Protocol) server for Google Imagen API, enabling text-to-image generation using Google's state-of-the-art Imagen models.

Features

  • Text-to-Image Generation: Generate high-quality images from text prompts using Imagen 4.0 models
  • Style Transfer: Generate images following the style of a reference image using Imagen 3 Customization
  • Background Removal: Remove backgrounds from images using rembg AI model
  • Auto-Crop: Automatically crop images to remove transparent or empty borders with batch processing support
  • Multiple Models: Support for three Imagen variants:
    • imagen-4.0-generate-001 (default) - Standard quality and speed
    • imagen-4.0-fast-generate-001 - Faster generation
    • imagen-4.0-ultra-generate-001 - Highest quality (single image only)
  • Flexible Configuration:
    • Customizable aspect ratios (1:1, 3:4, 4:3, 9:16, 16:9)
    • Batch generation (1-4 images per request)
    • PNG output format with transparency support
  • Authentication Options:
    • Google Cloud Default Application Credentials
    • Vertex AI or Gemini API

Prerequisites

  • Python 3.11 or later
  • uv for package management
  • Google Cloud credentials (see Authentication)

Installation

1. Clone the Repository

git clone https://github.com/anton-proto/mcp-imagen.git
cd mcp-imagen/mcp-imagen-server

2. Install with uv

# Install dependencies
uv sync

# Or install in development mode
uv sync --all-extras

3. Set Up Authentication

Option A: Using Gemini API (Recommended for quick start)

  1. Get an API key from Google AI Studio
  2. Set the API key:
    export GOOGLE_API_KEY="your-api-key-here"
    

Option B: Using Vertex AI (Recommended for production)

  1. Install Google Cloud SDK:

    # For Debian/Ubuntu
    curl https://sdk.cloud.google.com | bash
    exec -l $SHELL
    
  2. Authenticate with Google Cloud:

    gcloud auth application-default login
    
  3. Set your project:

    export GOOGLE_CLOUD_PROJECT="your-project-id"
    export USE_VERTEXAI="true"
    # Optional: specify location (default: us-central1)
    export GOOGLE_CLOUD_LOCATION="us-central1"
    

Usage

Running the Server

uv run mcp-imagen-server

The server will start and listen for MCP requests via stdio.

Integration with Claude Desktop

Add this configuration to your Claude Desktop config file:

Location:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

Configuration:

{
  "mcpServers": {
    "imagen": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/mcp-imagen-server",
        "run",
        "mcp-imagen-server"
      ],
      "env": {
        "GOOGLE_API_KEY": "your-api-key-here"
      }
    }
  }
}

For Vertex AI:

{
  "mcpServers": {
    "imagen": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/mcp-imagen-server",
        "run",
        "mcp-imagen-server"
      ],
      "env": {
        "USE_VERTEXAI": "true",
        "GOOGLE_CLOUD_PROJECT": "your-project-id",
        "GOOGLE_CLOUD_LOCATION": "us-central1"
      }
    }
  }
}

Integration with Other MCP Clients

The server implements the standard MCP protocol and can be used with any MCP-compatible client.

MCP Tools

text-to-image

Generates images from text prompts using Google Imagen API.

Parameters

Parameter Type Required Default Description
prompt string Yes - Text description of the image to generate
output_dir string Yes - Absolute path to directory where images should be saved
model string No imagen-4.0-generate-001 Imagen model to use (see Models)
sample_count integer No 1 Number of images to generate (1-4, must be 1 for ultra model)
aspect_ratio string No 1:1 Aspect ratio of generated images (1:1, 3:4, 4:3, 9:16, 16:9)

Models

  • imagen-4.0-generate-001: Standard model with balanced quality and speed
  • imagen-4.0-fast-generate-001: Faster generation with good quality
  • imagen-4.0-ultra-generate-001: Highest quality, single image only (sample_count must be 1)

Response

Returns a text response with paths to generated PNG files:

Successfully generated 2 image(s):
1. /path/to/output/A_serene_mountain_landscape_at_sunset_1.png
2. /path/to/output/A_serene_mountain_landscape_at_sunset_2.png

Example Usage

In Claude Desktop or other MCP client:

Generate an image of "A serene mountain landscape at sunset with a lake reflecting the sky" and save it to /tmp/images/

style-to-image

Generates images following the style of a reference image using Imagen 3 Customization.

Parameters

Parameter Type Required Description
prompt string Yes Text description of the image content to generate
style_image_path string Yes Absolute path to the style reference image file
style_description string Yes Description of the style (e.g., "watercolor painting style", "neon sign style", "mosaic style")
output_dir string Yes Absolute path to directory where images should be saved
sample_count integer No Number of images to generate (1-4). Default: 1

Response

Returns a text response with paths to generated styled PNG files.

Example Usage

Generate an image of "A cat sitting on a windowsill" in the style of the image at /path/to/watercolor.png (watercolor painting style) and save it to /tmp/images/

remove-background

Removes the background from an image using the rembg AI model, producing a PNG with transparent background.

Parameters

Parameter Type Required Description
input_path string Yes Absolute path to the input image file
output_path string No Absolute path to save the output image. If not provided, will save with 'nobg_' prefix in same directory

Response

Returns a text response with the path to the output image with transparent background:

Successfully removed background from image:
Output: /path/to/output/nobg_image.png

Example Usage

Remove the background from /home/user/images/photo.png

Or with custom output path:

Remove the background from /home/user/images/photo.png and save it to /home/user/outputs/transparent.png

autocrop

Automatically crop images to remove transparent or empty borders. Supports both single image and batch processing with parallel execution.

Parameters

Parameter Type Required Description
input_paths array of strings Yes List of absolute paths to input image files to crop
output_dir string No Absolute path to output directory. If not provided, cropped images will be saved in the same directory as input files with '_cropped' suffix
padding integer No Number of pixels to add as padding around cropped content. Default: 0
overwrite boolean No Whether to overwrite existing output files. Default: True. If False and output file exists, the operation will fail with an error

Response

Returns a text response with processing summary and paths to cropped images:

Processed 3 image(s):
Successfully cropped: 3

Cropped images:
1. /output/dir/image1_cropped.png
2. /output/dir/image2_cropped.png
3. /output/dir/image3_cropped.png

If any images fail to process, they will be listed separately:

Processed 3 image(s):
Successfully cropped: 2

Cropped images:
1. /output/dir/image1_cropped.png
2. /output/dir/image2_cropped.png

Failed: 1
- image3.png: Error: Image appears to be completely transparent or empty - cannot autocrop

Features

  • Parallel Processing: Multiple images are processed concurrently for better performance
  • Batch Support: Process multiple images in a single call
  • Flexible Output: Save to a specific directory or use default location
  • Padding Control: Add padding around cropped content if needed
  • Transparency Aware: Automatically detects and crops around non-transparent pixels

Example Usage

Single image:

Autocrop the image at /home/user/images/logo.png

Multiple images with output directory:

Autocrop these images: ["/home/user/images/logo1.png", "/home/user/images/logo2.png", "/home/user/images/logo3.png"] and save to /home/user/cropped/

With padding:

Autocrop /home/user/images/logo.png with 10 pixels of padding and save to /home/user/output/

With overwrite disabled (prevent overwriting):

Autocrop /home/user/images/logo.png and save to /home/user/output/ with overwrite disabled

Note: By default, the tool will overwrite existing output files. Set overwrite=False to prevent accidental overwrites and raise an error if the output file already exists.

Development

Project Structure

mcp-imagen-server/
├── src/
│   └── mcp_imagen_server/
│       ├── __init__.py          # Package initialization
│       ├── imagen_client.py     # Imagen API client
│       └── server.py            # MCP server implementation
├── pyproject.toml               # Project configuration
├── README.md                    # This file
└── .python-version              # Python version

Code Quality

The project uses ruff for linting and formatting:

# Check code
uv run ruff check src/

# Format code
uv run ruff format src/

# Auto-fix issues
uv run ruff check --fix src/

Running Tests

# Run tests (when implemented)
uv run pytest

Environment Variables

Variable Description Required Default
GOOGLE_API_KEY Google AI API key for Gemini API For Gemini API -
USE_VERTEXAI Set to "true" to use Vertex AI No false
GOOGLE_CLOUD_PROJECT GCP project ID For Vertex AI -
GOOGLE_CLOUD_LOCATION GCP region No us-central1

Troubleshooting

Authentication Issues

Problem: DefaultCredentialsError or authentication failures

Solution:

  • For Gemini API: Ensure GOOGLE_API_KEY is set
  • For Vertex AI: Run gcloud auth application-default login
  • Verify your project has the Vertex AI API enabled

Permission Denied

Problem: Cannot write to output directory

Solution: Ensure the specified output_dir exists and is writable, or the server has permissions to create it

Model Not Available

Problem: Model not found or access denied

Solution:

  • Verify your Google Cloud project has access to Imagen models
  • Check that you're using a supported model name
  • For ultra model, ensure sample_count=1

License

MIT License - See LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

References

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选