MCP图像服务器
用于Google Imagen API的MCP(模型上下文协议)服务器,能够使用Google最先进的Imagen模型生成文本到图像。
特性
- 文本到图像生成:使用Imagen 4.0模型从文本提示生成高质量图像
- 风格迁移:使用Imagen 3 Customization按照参考图像的样式生成图像
- 背景移除:使用rembg AI模型从图像中删除背景
- 自动裁剪:自动裁剪图像以删除透明或空边框,并支持批处理
- 多种型号:支持三种Imagen变体:
- imagen-4.0-generate-001 (默认)-标准质量和速度 - imagen-4.0-fast-generate-001 -更快的一代 - imagen-4.0-ultra-generate-001 -最高质量(仅单张图像)
- 灵活配置:
- 可定制的宽高比(1:1、3:4、4:3、9:16、16:9) - 批量生成(每个请求1-4张图像) - 支持透明度的PNG输出格式
- 身份验证选项:
- Google Cloud默认应用程序凭据 - Vertex AI或Gemini API
先决条件
安装
1.克隆存储库
git clone https://github.com/anton-proto/mcp-imagen.git
cd mcp-imagen/mcp-imagen-server2.用uv安装
# Install dependencies
uv sync
# Or install in development mode
uv sync --all-extras3.设置身份验证
选项A:使用Gemini API(建议快速启动)
- 从获取API密钥 谷歌AI工作室
- 设置API键:
export GOOGLE_API_KEY="your-api-key-here"选项B:使用Vertex AI(建议用于生产)
- 安装谷歌云SDK:
# For Debian/Ubuntu
curl https://sdk.cloud.google.com | bash
exec -l $SHELL- 使用Google Cloud进行身份验证:
gcloud auth application-default login- 设置您的项目:
export GOOGLE_CLOUD_PROJECT="your-project-id"
export USE_VERTEXAI="true"
# Optional: specify location (default: us-central1)
export GOOGLE_CLOUD_LOCATION="us-central1"用法
运行服务器
uv run mcp-imagen-server服务器将启动并通过stdio监听MCP请求。
与Claude Desktop集成
将此配置添加到您的Claude Desktop配置文件中:
位置:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 窗户:
%APPDATA%\Claude\claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
配置:
{
"mcpServers": {
"imagen": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/mcp-imagen-server",
"run",
"mcp-imagen-server"
],
"env": {
"GOOGLE_API_KEY": "your-api-key-here"
}
}
}
}对于顶点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"
}
}
}
}与其他MCP客户端集成
服务器实现了标准的MCP协议,可以与任何兼容MCP的客户端一起使用。
MCP工具
文本到图像
使用Google Imagen API从文本提示生成图像。
参数
| 参数 | 类型 | 必填 | 默认 | 说明 |
|---|---|---|---|---|
prompt | string | 是 | - | 要生成的图像的文本描述 |
output_dir | string | 是 | - | 应保存图像的目录的绝对路径 |
model | string | 否 | imagen-4.0-generate-001 | 要使用的Imagen模型(请参见 模型) |
sample_count | integer | 否 | 1 | 要生成的图像数量(1-4,对于超型号必须为1) |
aspect_ratio | string | 否 | 1:1 | 生成图像的纵横比(1:1、3:4、4:3、9:16、16:9) |
模型
- Image-4.0-Generate-001:质量和速度平衡的标准型号
- image-4.0-fast-generate-001:生产速度更快,质量更好
- Image-4.0-Ultra-Generate-001:最高质量,仅限单张图像(sample_count必须为1)
回应
返回一个文本响应,其中包含生成的PNG文件的路径:
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示例用法
在Claude Desktop或其他MCP客户端中:
Generate an image of "A serene mountain landscape at sunset with a lake reflecting the sky" and save it to /tmp/images/风格到形象
使用Imagen 3 Customization生成遵循参考图像样式的图像。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 要生成的图像内容的文本描述 |
style_image_path | string | 是 | 样式参考图像文件的绝对路径 |
style_description | string | 是 | 风格描述(例如,“水彩画风格”、“霓虹灯风格”和“马赛克风格”) |
output_dir | string | 是 | 应保存图像的目录的绝对路径 |
sample_count | integer | 否 | 要生成的图像数量(1-4)。默认值:1 |
回应
返回一个文本响应,其中包含生成的样式化PNG文件的路径。
示例用法
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/删除背景
使用rembg AI模型从图像中删除背景,生成具有透明背景的PNG。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
input_path | string | 是 | 输入图像文件的绝对路径 |
output_path | string | 否 | 保存输出图像的绝对路径。如果没有提供,将以“nobg\_”前缀保存在同一目录中 |
回应
返回一个文本响应,其中包含透明背景的输出图像的路径:
Successfully removed background from image:
Output: /path/to/output/nobg_image.png示例用法
Remove the background from /home/user/images/photo.png或者使用自定义输出路径:
Remove the background from /home/user/images/photo.png and save it to /home/user/outputs/transparent.png自动投运器
自动裁剪图像以删除透明或空白边框。支持单图像和批处理并行执行。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
input_paths | 字符串数组 | 是 | 要裁剪的输入图像文件的绝对路径列表 |
output_dir | string | 否 | 输出目录的绝对路径。如果没有提供,裁剪后的图像将与后缀为“\_cropped”的输入文件保存在同一目录中 |
padding | integer | 否 | 在裁剪内容周围添加作为填充的像素数。默认值:0 |
overwrite | boolean | 否 | 是否覆盖现有的输出文件。默认值:True。如果False和输出文件存在,则操作将失败并出现错误 |
回应
返回一个文本响应,其中包含处理摘要和裁剪图像的路径:
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如果任何图像无法处理,它们将单独列出:
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特性
- 并行处理:同时处理多个图像以获得更好的性能
- 批量支持:在一次调用中处理多个图像
- 灵活的输出:保存到特定目录或使用默认位置
- 填充控制:如果需要,在裁剪内容周围添加填充
- 透明度意识:自动检测并裁剪不透明像素周围
示例用法
单幅图像:
Autocrop the image at /home/user/images/logo.png具有输出目录的多个图像:
Autocrop these images: ["/home/user/images/logo1.png", "/home/user/images/logo2.png", "/home/user/images/logo3.png"] and save to /home/user/cropped/带衬垫:
Autocrop /home/user/images/logo.png with 10 pixels of padding and save to /home/user/output/禁用覆盖(防止覆盖):
Autocrop /home/user/images/logo.png and save to /home/user/output/ with overwrite disabled注意:默认情况下,该工具将覆盖现有的输出文件。集 overwrite=False 以防止意外覆盖并在输出文件已存在时引发错误。
发展
项目结构
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代码质量
该项目使用 颈毛 用于修剪和格式化:
# Check code
uv run ruff check src/
# Format code
uv run ruff format src/
# Auto-fix issues
uv run ruff check --fix src/运行测试
# Run tests (when implemented)
uv run pytest环境变量
| 变量 | 描述 | 必填 | 默认 |
|---|---|---|---|
GOOGLE_API_KEY | Gemini API的Google AI API密钥 | 适用于Gemini API | - |
USE_VERTEXAI | 设置为“true”以使用顶点AI | 否 | false |
GOOGLE_CLOUD_PROJECT | GCP项目ID | 顶点AI | - |
GOOGLE_CLOUD_LOCATION | GCP地区 | 否 | 以美元为中心1 |
故障排除
身份验证问题
问题: DefaultCredentialsError 或身份验证失败
解决方案:
- 对于Gemini API:确保
GOOGLE_API_KEY已设置 - 对于顶点AI:运行
gcloud auth application-default login - 验证您的项目是否已启用Vertex AI API
权限不足
问题:无法写入输出目录
解决方案:确保指定 output_dir 存在并且可写,或者服务器有创建它的权限
型号不可用
问题:找不到模型或拒绝访问
解决方案:
- 验证您的Google Cloud项目是否可以访问Imagen模型
- 检查您是否使用了受支持的型号名称
- 对于超型号,请确保
sample_count=1
许可证
MIT许可证-有关详细信息,请参阅许可证文件
贡献
欢迎投稿!请随时提交拉取请求。
