Grok Imagine图像MCP服务器
](https://www.npmjs.com/package/grok-imagine-image-mcp-server) 
用于xAI的Grok Imagine Image API的MCP(模型上下文协议)服务器。支持从文本提示生成图像和编辑现有图像。
npx快速入门
# Set your API key
export XAI_API_KEY="xai-your-api-key"
# Run the server
npx grok-imagine-image-mcp-server特性
- 图像生成:根据文本提示生成新图像
- 图像编辑:使用提示编辑现有图像(仅限grok想象图像)
- 批处理:通过CLI处理多个图像
- 各种纵横比(1:1、4:3、16:9等)
- 分辨率:1k(标准)
- 一次生成多个图像(最多10个)
- MCP响应中的可选缩略图预览
支持的型号
| 型号 | 价格 | 图像编辑 | 备注 |
|---|---|---|---|
grok-imagine-image | 0.02美元/张 | ✅ (+0.002美元/输入) | 推荐,默认 |
需求
- Node.js 18.0.0或更高版本
- xAI API密钥(从 console.x.ai)
安装
选项1:npx(推荐)
npx grok-imagine-image-mcp-server选项2:全局安装
npm install -g grok-imagine-image-mcp-server
grok-imagine-image-mcp-server配置
环境变量
| 变量 | 必填 | 描述 |
|---|---|---|
XAI_API_KEY | 是 | 您的xAI API密钥 |
DEBUG | 否 | 设置为 true 用于调试日志记录 |
OUTPUT_DIR | 否 | 图像的默认输出目录 |
XAI_IMAGE_THUMBNAIL | 否 | 设置为 true 包含缩略图 |
Claude桌面配置
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"grok-imagine-image": {
"command": "npx",
"args": ["-y", "grok-imagine-image-mcp-server"],
"env": {
"XAI_API_KEY": "xai-your-api-key-here"
}
}
}
}工具
generate_image
根据文本提示生成图像。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 要生成的图像的文本描述 |
output_path | string | 否 | 输出文件路径(默认:generated_image.jpg) |
model | string | 否 | 要使用的模型(默认:grok imagine image) |
n | number | 否 | 图像数量(1-10,默认值:1) |
aspect_ratio | string | 否 | 纵横比(默认:1:1) |
resolution | string | 否 | 分辨率(默认值:1k) |
return_base64 | boolean | 否 | 返回base64数据(默认值:false) |
include_thumbnail | boolean | 否 | 包含缩略图(默认值:false) |
edit_image
编辑现有图像(仅限grok想象图像)。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 所需编辑的描述 |
image_path | string | 否\* | 源图像文件的路径 |
image_base64 | string | 否\* | Base64编码源图像 |
image_url | string | 否\* | 源图像的URL |
output_path | string | 否 | 输出文件路径(默认:edited_image.jpg) |
n | number | 否 | 图像数量(1-10,默认值:1) |
resolution | string | 否 | 分辨率(默认值:1k)。从输入图像中自动检测纵横比。 |
\*其中之一 image_path, image_base64,或 image_url 是必需的
备注edit_image工具使用源图像作为参考,并重新生成整个图像。它保持了整体构图和风格,但不是像素级的修复。最适合进行样式转换、主题替换和场景修改。
批处理CLI
使用批处理CLI一次处理多个映像:
# Run batch with config file
npx grok-imagine-image-batch batch.json
# Estimate cost only
npx grok-imagine-image-batch batch.json --estimate-only
# Custom output directory
npx grok-imagine-image-batch batch.json --output-dir ./images --format jsonCLI选项
| 选项 | 描述 | 默认值 |
|---|---|---|
| `--output-dir | ||
| ` | 覆盖输出目录 | 来自配置 |
--format | 输出格式 | text |
--timeout | 超时(毫秒) | 600000 |
--max-concurrent | 最大并发作业数(1-10) | 2 |
--estimate-only | 在不执行的情况下估算成本 | - |
--allow-any-path | 允许任何输出路径(CI/CD) | - |
批处理配置文件
{
"jobs": [
{
"prompt": "A beautiful sunset over mountains",
"output_path": "sunset.jpg",
"aspect_ratio": "16:9"
},
{
"prompt": "Change to nighttime scene",
"image_path": "input.jpg",
"output_path": "edited.jpg"
}
],
"output_dir": "./output",
"max_concurrent": 3,
"default_model": "grok-imagine-image",
"retry_policy": {
"max_retries": 2,
"retry_delay_ms": 1000
}
}看 examples/ 目录以获取更多配置示例。
支持的纵横比
| 纵横比 | 用例 |
|---|---|
1:1 | Square,社交媒体配置文件(默认) |
3:4 / 4:3 | 标准肖像/风景 |
9:16 / 16:9 | 智能手机/宽屏 |
支持的决议
| 解决方案 | 描述 |
|---|---|
1k | 标准分辨率(1024x1024,默认) |
用法示例
# Image generation
Generate an image of a sunset over mountains with aspect ratio 16:9
# Image editing
Change the background of this image to spaceAPI 参考
| 函数 | 端点 |
|---|---|
| 图像生成 | https://api.x.ai/v1/images/generations |
| 图像编辑 | https://api.x.ai/v1/images/edits |
发展
git clone https://github.com/ex-takashima/grok-imagine-image-mcp-server.git
cd grok-imagine-image-mcp-server
npm install
npm run build
npm start许可证
麻省理工学院
作者
高岛俊二
