image-router-mcp 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -150
- package/README.zh-TW.md +281 -0
- package/dist/index.js +5 -4
- package/dist/providers/google.js +8 -2
- package/dist/providers/openai.js +14 -3
- package/dist/providers/xai.js +10 -1
- package/dist/schemas.d.ts +34 -0
- package/dist/schemas.js +10 -5
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -3,46 +3,80 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/image-router-mcp)
|
|
4
4
|
[](https://www.npmjs.com/package/image-router-mcp)
|
|
5
5
|
|
|
6
|
-
English | [繁體中文](
|
|
6
|
+
English | [繁體中文](./README.zh-TW.md)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
An open-source MCP server that routes image generation and editing to xAI/Grok, Google Gemini/Nano Banana, and OpenAI.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Run it directly from npm with `npx`. You only need an API key for the provider you want to use.
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
14
|
+
- Three focused MCP tools for xAI, Google, and OpenAI.
|
|
15
|
+
- Text-to-image plus local reference image and image-editing workflows.
|
|
16
|
+
- Flexible model IDs, so compatible new models are not blocked by a fixed allowlist.
|
|
17
|
+
- File output by default, avoiding large base64 payloads over stdio.
|
|
18
|
+
- Optional inline image content for MCP clients that can display it.
|
|
19
19
|
|
|
20
|
-
## Tools
|
|
20
|
+
## Tools and default models
|
|
21
21
|
|
|
22
|
-
| Tool | Provider | Supports |
|
|
23
|
-
| --- | --- | --- |
|
|
24
|
-
| `
|
|
25
|
-
| `google_generate_image` | Google Gemini/Nano Banana | Text-to-image and local reference/edit images through the Interactions API
|
|
26
|
-
| `
|
|
22
|
+
| Tool | Provider | Built-in default model | Supports |
|
|
23
|
+
| --- | --- | --- | --- |
|
|
24
|
+
| `openai_generate_image` | OpenAI | `gpt-image-2.5-sunburst` | Text-to-image and local reference/edit images, including `xhigh`/`max` quality |
|
|
25
|
+
| `google_generate_image` | Google Gemini/Nano Banana | `gemini-3.1-flash-image` | Text-to-image and local reference/edit images through the Interactions API |
|
|
26
|
+
| `xai_generate_image` | xAI/Grok | `grok-imagine-image-2.0` | Text-to-image and edits with up to 5 local input images |
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
Model IDs are examples and defaults, not an allowlist. Other compatible model IDs can be supplied through the `model` argument.
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
### Model update notes (2026-09-09)
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
- OpenAI: Sunburst is the default for editing precision. Choose `gpt-image-2.5-flare` for everyday generation, or explicitly set `gpt-image-2` to retain the previous model. `xhigh` and `max` require GPT Image 2.5 or a compatible newer model. [Official guide](https://developers.openai.com/api/docs/guides/image-generation)
|
|
33
|
+
- Google: `gemini-3.1-flash-lite-image` (Nano Banana 2 Lite) is an optional faster, lower-cost model, limited to `1K` output. The default remains `gemini-3.1-flash-image`. [Official guide](https://ai.google.dev/gemini-api/docs/image-generation)
|
|
34
|
+
- xAI: Imagine Image 2.0 replaces the previous built-in default. The old `grok-imagine-image-quality` slug is scheduled to redirect to 2.0 at `low` quality on November 2, 2026. With 2.0, omitted `quality` uses provider `auto`, currently `low` for generation and `medium` for edits. Pin `quality` to control this cost/quality choice. Environment model pins still take precedence. [Migration guide](https://docs.x.ai/developers/migration/imagine-image-quality-nov-2)
|
|
35
|
+
|
|
36
|
+
Model access depends on your provider account. No automatic fallback to another paid model is performed.
|
|
37
|
+
|
|
38
|
+
## Prerequisites
|
|
39
|
+
|
|
40
|
+
- Node.js 20 or later, including `npm` and `npx`.
|
|
41
|
+
- An MCP client that supports local stdio servers, such as Codex, Claude Desktop, or Cursor.
|
|
42
|
+
- At least one provider API key:
|
|
43
|
+
- `OPENAI_API_KEY` for OpenAI
|
|
44
|
+
- `GOOGLE_API_KEY` or `GEMINI_API_KEY` for Google
|
|
45
|
+
- `XAI_API_KEY` for xAI
|
|
46
|
+
|
|
47
|
+
Provider API usage may incur charges. Set only the keys for providers you intend to use.
|
|
48
|
+
|
|
49
|
+
> [!WARNING]
|
|
50
|
+
> Never commit real API keys. Prefer user-level config for settings that contain secrets, or forward keys from your existing environment when the MCP client supports it.
|
|
37
51
|
|
|
38
52
|
## Quick start
|
|
39
53
|
|
|
54
|
+
The examples below configure OpenAI only. Replace or add the environment variables listed in [Configuration](#configuration) to use Google or xAI.
|
|
55
|
+
|
|
56
|
+
### Codex
|
|
57
|
+
|
|
58
|
+
Add the server from the command line:
|
|
59
|
+
|
|
40
60
|
```bash
|
|
41
|
-
|
|
42
|
-
npm run build
|
|
61
|
+
codex mcp add image-router-mcp --env OPENAI_API_KEY=your_openai_key -- npx -y image-router-mcp
|
|
43
62
|
```
|
|
44
63
|
|
|
45
|
-
|
|
64
|
+
Or add it to `~/.codex/config.toml` for all projects, or `.codex/config.toml` for one trusted project:
|
|
65
|
+
|
|
66
|
+
```toml
|
|
67
|
+
[mcp_servers.image-router-mcp]
|
|
68
|
+
command = "npx"
|
|
69
|
+
args = ["-y", "image-router-mcp"]
|
|
70
|
+
|
|
71
|
+
[mcp_servers.image-router-mcp.env]
|
|
72
|
+
OPENAI_API_KEY = "your_openai_key"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Restart Codex after editing the file. Use `/mcp` to confirm that `image-router-mcp` and its three tools are available.
|
|
76
|
+
|
|
77
|
+
### Claude Desktop
|
|
78
|
+
|
|
79
|
+
Open **Settings > Developer > Edit Config** and add this server to `claude_desktop_config.json`:
|
|
46
80
|
|
|
47
81
|
```json
|
|
48
82
|
{
|
|
@@ -51,8 +85,6 @@ For MCP clients, run the published package with `npx`:
|
|
|
51
85
|
"command": "npx",
|
|
52
86
|
"args": ["-y", "image-router-mcp"],
|
|
53
87
|
"env": {
|
|
54
|
-
"XAI_API_KEY": "your_xai_key",
|
|
55
|
-
"GOOGLE_API_KEY": "your_google_key",
|
|
56
88
|
"OPENAI_API_KEY": "your_openai_key"
|
|
57
89
|
}
|
|
58
90
|
}
|
|
@@ -60,164 +92,138 @@ For MCP clients, run the published package with `npx`:
|
|
|
60
92
|
}
|
|
61
93
|
```
|
|
62
94
|
|
|
63
|
-
|
|
64
|
-
> Set only the API keys for the providers you use. Keys are checked lazily when each tool is called.
|
|
95
|
+
Default config locations:
|
|
65
96
|
|
|
66
|
-
|
|
97
|
+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
98
|
+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
67
99
|
|
|
68
|
-
|
|
69
|
-
| --- | --- | --- |
|
|
70
|
-
| `XAI_API_KEY` | For xAI tool | xAI API key. |
|
|
71
|
-
| `GOOGLE_API_KEY` or `GEMINI_API_KEY` | For Google tool | Google Gemini API key. |
|
|
72
|
-
| `OPENAI_API_KEY` | For OpenAI tool | OpenAI API key. |
|
|
73
|
-
| `DEFAULT_XAI_IMAGE_MODEL` | No | Overrides the default xAI image model. |
|
|
74
|
-
| `DEFAULT_GOOGLE_IMAGE_MODEL` | No | Overrides the default Google image model. |
|
|
75
|
-
| `DEFAULT_OPENAI_IMAGE_MODEL` | No | Sets the default OpenAI image model when `model` is omitted. |
|
|
76
|
-
| `DEFAULT_OUTPUT_DIR` | No | Directory for saved images. Defaults to `./generated-images`. |
|
|
100
|
+
Completely quit and reopen Claude Desktop after saving the file.
|
|
77
101
|
|
|
78
|
-
|
|
102
|
+
### Cursor
|
|
79
103
|
|
|
80
|
-
|
|
104
|
+
Add the same JSON configuration shown above to one of these locations:
|
|
81
105
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"tool": "openai_generate_image",
|
|
85
|
-
"arguments": {
|
|
86
|
-
"prompt": "A cinematic product photo of a transparent mechanical keyboard on a marble desk",
|
|
87
|
-
"model": "gpt-image-2",
|
|
88
|
-
"size": "1024x1024",
|
|
89
|
-
"return_mode": "file",
|
|
90
|
-
"output_path": "./generated-images"
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
```
|
|
106
|
+
- Global: `~/.cursor/mcp.json`
|
|
107
|
+
- Project only: `.cursor/mcp.json`
|
|
94
108
|
|
|
95
|
-
|
|
109
|
+
Open **Cursor Settings > Tools & MCP** to confirm that the server and tools are enabled.
|
|
96
110
|
|
|
97
|
-
|
|
98
|
-
{
|
|
99
|
-
"tool": "google_generate_image",
|
|
100
|
-
"arguments": {
|
|
101
|
-
"prompt": "Turn this sketch into a polished mobile app hero illustration",
|
|
102
|
-
"model": "gemini-3.1-flash-image",
|
|
103
|
-
"input_images": ["./assets/sketch.png"],
|
|
104
|
-
"aspect_ratio": "16:9",
|
|
105
|
-
"image_size": "512",
|
|
106
|
-
"return_mode": "both"
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
```
|
|
111
|
+
## Generate your first image
|
|
110
112
|
|
|
111
|
-
|
|
113
|
+
After the server is connected, ask your MCP client:
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
| --- | --- |
|
|
115
|
-
| `file` | Default. Saves images to disk and returns file paths. |
|
|
116
|
-
| `inline` | Returns MCP `image` content blocks, capped by `inline_max_bytes`. |
|
|
117
|
-
| `both` | Saves images to disk and also returns inline image content. |
|
|
115
|
+
> Use `openai_generate_image` to create a 1024x1024 cinematic product photo of a transparent mechanical keyboard on a marble desk. Save it to `/absolute/path/to/generated-images` and return the saved file path.
|
|
118
116
|
|
|
119
|
-
|
|
117
|
+
To edit an existing image:
|
|
120
118
|
|
|
121
|
-
|
|
119
|
+
> Use `google_generate_image` with `/absolute/path/to/sketch.png` as the input image. Turn it into a polished 16:9 mobile app hero illustration and save the result to `/absolute/path/to/generated-images`.
|
|
122
120
|
|
|
123
|
-
|
|
124
|
-
npm install
|
|
125
|
-
npm run dev
|
|
126
|
-
npm run ci
|
|
127
|
-
```
|
|
121
|
+
Use absolute paths in prompts and tool calls. Relative paths depend on the working directory chosen by the MCP client.
|
|
128
122
|
|
|
129
|
-
|
|
123
|
+
## Paths and image output
|
|
130
124
|
|
|
131
|
-
|
|
125
|
+
- `input_images` paths are resolved from the MCP server process working directory.
|
|
126
|
+
- `output_path` can be either a file path or a directory.
|
|
127
|
+
- An existing directory, or a new path without a file extension, is treated as a directory.
|
|
128
|
+
- A path with a file extension is treated as the requested output filename. Multiple results receive numbered suffixes.
|
|
129
|
+
- When `output_path` is omitted, files are saved under `DEFAULT_OUTPUT_DIR`, or `./generated-images` relative to the MCP server working directory.
|
|
130
|
+
- Use absolute input and output paths for predictable behavior across clients.
|
|
131
|
+
- For portable local-image use across providers, use PNG, JPG/JPEG, or WebP files. The Google and xAI loaders accept these extensions.
|
|
132
|
+
- xAI Imagine Image 2.0 accepts at most 5 `input_images`. Older model IDs retain the 3-image limit.
|
|
132
133
|
|
|
133
|
-
|
|
134
|
+
### Return modes
|
|
134
135
|
|
|
135
|
-
|
|
136
|
+
| Mode | Behavior |
|
|
137
|
+
| --- | --- |
|
|
138
|
+
| `file` | Default. Saves images to disk and returns their paths. |
|
|
139
|
+
| `inline` | Returns MCP `image` content without saving it. Each image is limited by `inline_max_bytes`. |
|
|
140
|
+
| `both` | Saves images and also returns inline MCP image content. |
|
|
136
141
|
|
|
137
|
-
|
|
142
|
+
Use `file` for large images. Use `inline` or `both` only when the MCP client can display image content and the result fits its payload limits.
|
|
138
143
|
|
|
139
|
-
|
|
140
|
-
- 使用本機圖片作為參考圖或編輯輸入,透過 `input_images` 傳入。
|
|
141
|
-
- 預設將生成圖片儲存到磁碟,避免透過 stdio 傳送大型 base64 payload。
|
|
142
|
-
- 可選擇回傳 inline MCP 圖片內容,讓支援的客戶端直接顯示圖片。
|
|
143
|
-
- 可用環境變數或每次呼叫的 `model` 參數覆寫預設模型。
|
|
144
|
+
## Tool parameters
|
|
144
145
|
|
|
145
|
-
|
|
146
|
+
### Common parameters
|
|
146
147
|
|
|
147
|
-
|
|
|
148
|
-
| --- | --- | --- |
|
|
149
|
-
| `
|
|
150
|
-
| `
|
|
151
|
-
| `
|
|
148
|
+
| Parameter | Required | Default | Description |
|
|
149
|
+
| --- | --- | --- | --- |
|
|
150
|
+
| `prompt` | Yes | — | Image generation or editing instructions. |
|
|
151
|
+
| `model` | No | Provider default | Compatible provider model ID. |
|
|
152
|
+
| `input_images` | No | `[]` | Local reference/edit image paths. |
|
|
153
|
+
| `output_path` | No | `DEFAULT_OUTPUT_DIR` or `./generated-images` | Saved output file or directory. |
|
|
154
|
+
| `return_mode` | No | `file` | `file`, `inline`, or `both`. |
|
|
155
|
+
| `inline_max_bytes` | No | `4194304` (4 MiB) | Maximum size of each inline image. |
|
|
156
|
+
| `n` | No | Provider default | Number of results, from 1 to 10, when supported. Google currently returns one image. |
|
|
157
|
+
| `user` | No | — | Optional end-user identifier for provider abuse monitoring. |
|
|
152
158
|
|
|
153
|
-
|
|
159
|
+
### OpenAI parameters
|
|
154
160
|
|
|
155
|
-
|
|
161
|
+
| Parameter | Values or examples | Default |
|
|
162
|
+
| --- | --- | --- |
|
|
163
|
+
| `size` | `auto`, `1024x1024`, `1536x1024`, or another supported `WIDTHxHEIGHT` | Provider default |
|
|
164
|
+
| `quality` | `auto`, `low`, `medium`, `high`, `xhigh`, `max`, `standard`, `hd` (model-dependent) | Provider default |
|
|
165
|
+
| `background` | `transparent`, `opaque`, `auto` | Provider default |
|
|
166
|
+
| `output_format` | `png`, `jpeg`, `webp` | `png` |
|
|
167
|
+
| `output_compression` | Integer `0`–`100`, GPT Image JPEG/WebP output only | Provider default |
|
|
168
|
+
| `moderation` | `low`, `auto` | Provider default |
|
|
169
|
+
| `style` | `vivid`, `natural`; used with DALL-E 3 | Provider default |
|
|
156
170
|
|
|
157
|
-
|
|
158
|
-
| --- | --- |
|
|
159
|
-
| xAI | `grok-imagine-image-quality`, `grok-imagine-image` |
|
|
160
|
-
| Google | `gemini-3.1-flash-image`, `gemini-3.1-flash-lite-image`, `gemini-3-pro-image` |
|
|
161
|
-
| OpenAI | `gpt-image-2` |
|
|
171
|
+
### Google parameters
|
|
162
172
|
|
|
163
|
-
|
|
173
|
+
| Parameter | Values or examples | Default |
|
|
174
|
+
| --- | --- | --- |
|
|
175
|
+
| `aspect_ratio` | For example `1:1`, `16:9`, `9:16`, `4:3`, or `3:2` | Provider default |
|
|
176
|
+
| `image_size` | `512`, `0.5K`, `1K`, `2K`, `4K`; availability depends on the model | Provider default |
|
|
177
|
+
| `output_format` | `png`, `jpeg` | Provider default |
|
|
178
|
+
| `thinking_level` | `minimal`, `high`, for Gemini 3.1 Flash Image / Flash Lite Image | Provider default |
|
|
179
|
+
| `previous_interaction_id` | `metadata.interactionId` from a previous Google result | None |
|
|
164
180
|
|
|
165
|
-
|
|
166
|
-
npm install
|
|
167
|
-
npm run build
|
|
168
|
-
```
|
|
181
|
+
`0.5K` is accepted as a legacy alias and normalized to `512`.
|
|
169
182
|
|
|
170
|
-
|
|
183
|
+
For a follow-up edit, pass the previous result's `metadata.interactionId` as `previous_interaction_id` with the new prompt. The previous interaction must still be available in the same Google account. This uses provider-stored conversation context and follows Google's retention policy. A stale or unavailable ID returns an error without silently starting a new generation. Lite is not optimized for sequential editing. Higher thinking levels can increase cost and latency.
|
|
171
184
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
"XAI_API_KEY": "your_xai_key",
|
|
180
|
-
"GOOGLE_API_KEY": "your_google_key",
|
|
181
|
-
"OPENAI_API_KEY": "your_openai_key"
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
```
|
|
185
|
+
### xAI parameters
|
|
186
|
+
|
|
187
|
+
| Parameter | Values or examples | Default |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| `aspect_ratio` | For example `1:1`, `16:9`, `9:16`, `3:2`, or `auto` | Provider default |
|
|
190
|
+
| `resolution` | `1k`, `2k` | Provider default |
|
|
191
|
+
| `quality` | `low`, `medium`, `auto`, for Imagine Image 2.0 or compatible newer models | Provider `auto` |
|
|
187
192
|
|
|
188
|
-
|
|
189
|
-
> 只需要設定會用到的供應商 API Key。Key 會在呼叫對應工具時才檢查。
|
|
193
|
+
Aspect ratios also accept `21:9` and `5:2`. When xAI returns its serving model ID, it is included as `metadata.servedModel`. The top-level `model` continues to identify the requested model.
|
|
190
194
|
|
|
191
|
-
##
|
|
195
|
+
## Configuration
|
|
192
196
|
|
|
193
|
-
|
|
|
197
|
+
| Variable | Required | Behavior |
|
|
194
198
|
| --- | --- | --- |
|
|
195
|
-
| `
|
|
196
|
-
| `GOOGLE_API_KEY`
|
|
197
|
-
| `
|
|
198
|
-
| `
|
|
199
|
-
| `DEFAULT_GOOGLE_IMAGE_MODEL` |
|
|
200
|
-
| `
|
|
201
|
-
| `DEFAULT_OUTPUT_DIR` |
|
|
199
|
+
| `OPENAI_API_KEY` | For the OpenAI tool | OpenAI API key. |
|
|
200
|
+
| `GOOGLE_API_KEY` or `GEMINI_API_KEY` | For the Google tool | Google Gemini API key. |
|
|
201
|
+
| `XAI_API_KEY` | For the xAI tool | xAI API key. |
|
|
202
|
+
| `DEFAULT_OPENAI_IMAGE_MODEL` | No | Default OpenAI model when `model` is omitted. An explicit `model` argument can override it. |
|
|
203
|
+
| `DEFAULT_GOOGLE_IMAGE_MODEL` | No | Pins Google requests to this model and takes precedence over the per-call `model` argument. |
|
|
204
|
+
| `DEFAULT_XAI_IMAGE_MODEL` | No | Pins xAI requests to this model and takes precedence over the per-call `model` argument. |
|
|
205
|
+
| `DEFAULT_OUTPUT_DIR` | No | Default saved-image directory. Defaults to `./generated-images` from the MCP server working directory. |
|
|
206
|
+
|
|
207
|
+
Keys are checked only when their corresponding tool is called, so one provider key is enough to start.
|
|
202
208
|
|
|
203
|
-
##
|
|
209
|
+
## Advanced direct tool calls
|
|
204
210
|
|
|
205
|
-
|
|
211
|
+
Most users can ask their MCP client in natural language. If your client supports direct tool invocation, the equivalent OpenAI request is:
|
|
206
212
|
|
|
207
213
|
```json
|
|
208
214
|
{
|
|
209
215
|
"tool": "openai_generate_image",
|
|
210
216
|
"arguments": {
|
|
211
217
|
"prompt": "A cinematic product photo of a transparent mechanical keyboard on a marble desk",
|
|
212
|
-
"model": "gpt-image-2",
|
|
218
|
+
"model": "gpt-image-2.5-sunburst",
|
|
213
219
|
"size": "1024x1024",
|
|
214
220
|
"return_mode": "file",
|
|
215
|
-
"output_path": "
|
|
221
|
+
"output_path": "/absolute/path/to/generated-images"
|
|
216
222
|
}
|
|
217
223
|
}
|
|
218
224
|
```
|
|
219
225
|
|
|
220
|
-
|
|
226
|
+
Google reference-image example:
|
|
221
227
|
|
|
222
228
|
```json
|
|
223
229
|
{
|
|
@@ -225,25 +231,42 @@ npm run build
|
|
|
225
231
|
"arguments": {
|
|
226
232
|
"prompt": "Turn this sketch into a polished mobile app hero illustration",
|
|
227
233
|
"model": "gemini-3.1-flash-image",
|
|
228
|
-
"input_images": ["
|
|
234
|
+
"input_images": ["/absolute/path/to/sketch.png"],
|
|
229
235
|
"aspect_ratio": "16:9",
|
|
230
236
|
"image_size": "512",
|
|
231
|
-
"return_mode": "
|
|
237
|
+
"return_mode": "file",
|
|
238
|
+
"output_path": "/absolute/path/to/generated-images"
|
|
232
239
|
}
|
|
233
240
|
}
|
|
234
241
|
```
|
|
235
242
|
|
|
236
|
-
|
|
243
|
+
## Troubleshooting
|
|
237
244
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
245
|
+
### `npx` is not found
|
|
246
|
+
|
|
247
|
+
Install Node.js 20 or later and confirm that both `node --version` and `npx --version` work in a new terminal.
|
|
248
|
+
|
|
249
|
+
### The MCP tools do not appear
|
|
243
250
|
|
|
244
|
-
|
|
251
|
+
- Check the JSON or TOML syntax and the config file location.
|
|
252
|
+
- Completely restart the MCP client after editing its configuration.
|
|
253
|
+
- In Codex, use `/mcp`; in Cursor, check **Settings > Tools & MCP**.
|
|
245
254
|
|
|
246
|
-
|
|
255
|
+
### Authentication or billing errors
|
|
256
|
+
|
|
257
|
+
Confirm that the key matches the selected tool and that the provider account has access, billing, or credits enabled. Only the provider whose tool is called checks its key.
|
|
258
|
+
|
|
259
|
+
### An input image cannot be found
|
|
260
|
+
|
|
261
|
+
Replace relative paths with absolute paths that are readable by the local MCP server process.
|
|
262
|
+
|
|
263
|
+
### An inline image exceeds 4 MiB
|
|
264
|
+
|
|
265
|
+
Use `return_mode: "file"`, or raise `inline_max_bytes` only if the client can accept the larger payload.
|
|
266
|
+
|
|
267
|
+
## Development
|
|
268
|
+
|
|
269
|
+
For contributors working from the source repository:
|
|
247
270
|
|
|
248
271
|
```bash
|
|
249
272
|
npm install
|
|
@@ -251,4 +274,8 @@ npm run dev
|
|
|
251
274
|
npm run ci
|
|
252
275
|
```
|
|
253
276
|
|
|
254
|
-
`npm run ci`
|
|
277
|
+
`npm run ci` runs type checking, tests, and a production build.
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
|
|
281
|
+
[MIT](./LICENSE)
|
package/README.zh-TW.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# image-router-mcp
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/image-router-mcp)
|
|
4
|
+
[](https://www.npmjs.com/package/image-router-mcp)
|
|
5
|
+
|
|
6
|
+
[English](./README.md) | 繁體中文
|
|
7
|
+
|
|
8
|
+
開源 MCP 伺服器,可將圖片生成與編輯請求路由到 xAI/Grok、Google Gemini/Nano Banana 與 OpenAI。
|
|
9
|
+
|
|
10
|
+
可直接透過 npm 的 `npx` 執行。只需準備實際要使用之供應商的 API Key。
|
|
11
|
+
|
|
12
|
+
## 功能
|
|
13
|
+
|
|
14
|
+
- 分別提供 xAI、Google 與 OpenAI 三個明確的 MCP 工具。
|
|
15
|
+
- 支援文字生成圖片、本機參考圖及圖片編輯流程。
|
|
16
|
+
- 接受彈性的模型 ID,不會因固定允許清單而阻擋相容的新模型。
|
|
17
|
+
- 預設將圖片儲存成檔案,避免透過 stdio 傳送大型 base64 payload。
|
|
18
|
+
- 可選擇回傳 inline 圖片內容,讓支援的 MCP 客戶端直接顯示。
|
|
19
|
+
|
|
20
|
+
## 工具與預設模型
|
|
21
|
+
|
|
22
|
+
| 工具 | 供應商 | 內建預設模型 | 支援內容 |
|
|
23
|
+
| --- | --- | --- | --- |
|
|
24
|
+
| `openai_generate_image` | OpenAI | `gpt-image-2.5-sunburst` | 文字生成圖片及本機參考圖/編輯圖,支援 `xhigh`/`max` 品質 |
|
|
25
|
+
| `google_generate_image` | Google Gemini/Nano Banana | `gemini-3.1-flash-image` | 透過 Interactions API 進行文字生成圖片及本機參考圖/編輯圖 |
|
|
26
|
+
| `xai_generate_image` | xAI/Grok | `grok-imagine-image-2.0` | 文字生成圖片,以及最多 5 張本機輸入圖的編輯 |
|
|
27
|
+
|
|
28
|
+
以上模型是範例與預設值,不是允許清單。可透過 `model` 參數傳入其他相容的模型 ID。
|
|
29
|
+
|
|
30
|
+
### 模型更新說明(2026-09-09)
|
|
31
|
+
|
|
32
|
+
- OpenAI:預設使用偏重精準編輯的 Sunburst,日常生成可選 `gpt-image-2.5-flare`。若要維持先前模型,可明確指定 `gpt-image-2`。`xhigh`、`max` 需使用 GPT Image 2.5 或相容的新模型。[官方指南](https://developers.openai.com/api/docs/guides/image-generation)
|
|
33
|
+
- Google:可選速度更快、成本較低的 `gemini-3.1-flash-lite-image`(Nano Banana 2 Lite),僅支援 `1K` 輸出。預設仍為 `gemini-3.1-flash-image`。[官方指南](https://ai.google.dev/gemini-api/docs/image-generation)
|
|
34
|
+
- xAI:內建預設改為 Imagine Image 2.0。舊 `grok-imagine-image-quality` 預計於 2026 年 11 月 2 日轉向 2.0 的 `low` 品質。2.0 未指定 `quality` 時採供應商的 `auto`,目前生成使用 `low`、編輯使用 `medium`,費用隨實際品質而異。可明確指定品質,環境變數固定模型的行為仍保留。[遷移指南](https://docs.x.ai/developers/migration/imagine-image-quality-nov-2)
|
|
35
|
+
|
|
36
|
+
模型存取權依供應商帳戶而定。無法使用時,不會自動改呼叫其他付費模型。
|
|
37
|
+
|
|
38
|
+
## 前置需求
|
|
39
|
+
|
|
40
|
+
- Node.js 20 以上版本,並包含 `npm` 與 `npx`。
|
|
41
|
+
- 支援本機 stdio server 的 MCP 客戶端,例如 Codex、Claude Desktop 或 Cursor。
|
|
42
|
+
- 至少一組供應商 API Key:
|
|
43
|
+
- OpenAI 使用 `OPENAI_API_KEY`
|
|
44
|
+
- Google 使用 `GOOGLE_API_KEY` 或 `GEMINI_API_KEY`
|
|
45
|
+
- xAI 使用 `XAI_API_KEY`
|
|
46
|
+
|
|
47
|
+
呼叫供應商 API 可能產生費用。只需設定實際要使用的供應商 Key。
|
|
48
|
+
|
|
49
|
+
> [!WARNING]
|
|
50
|
+
> 請勿將真實 API Key 提交到版本控制。含有密鑰的設定建議放在使用者層級;若 MCP 客戶端支援,也可從既有環境變數轉送 Key。
|
|
51
|
+
|
|
52
|
+
## 快速開始
|
|
53
|
+
|
|
54
|
+
以下範例只設定 OpenAI。若要使用 Google 或 xAI,請依照[環境設定](#環境設定)取代或加入對應的環境變數。
|
|
55
|
+
|
|
56
|
+
### Codex
|
|
57
|
+
|
|
58
|
+
透過命令列加入 server:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
codex mcp add image-router-mcp --env OPENAI_API_KEY=your_openai_key -- npx -y image-router-mcp
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
也可以加入所有專案共用的 `~/.codex/config.toml`,或單一受信任專案中的 `.codex/config.toml`:
|
|
65
|
+
|
|
66
|
+
```toml
|
|
67
|
+
[mcp_servers.image-router-mcp]
|
|
68
|
+
command = "npx"
|
|
69
|
+
args = ["-y", "image-router-mcp"]
|
|
70
|
+
|
|
71
|
+
[mcp_servers.image-router-mcp.env]
|
|
72
|
+
OPENAI_API_KEY = "your_openai_key"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
編輯檔案後重新啟動 Codex,再使用 `/mcp` 確認 `image-router-mcp` 與三個工具已出現。
|
|
76
|
+
|
|
77
|
+
### Claude Desktop
|
|
78
|
+
|
|
79
|
+
開啟 **Settings > Developer > Edit Config**,將以下 server 加入 `claude_desktop_config.json`:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"mcpServers": {
|
|
84
|
+
"image-router-mcp": {
|
|
85
|
+
"command": "npx",
|
|
86
|
+
"args": ["-y", "image-router-mcp"],
|
|
87
|
+
"env": {
|
|
88
|
+
"OPENAI_API_KEY": "your_openai_key"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
預設設定檔位置:
|
|
96
|
+
|
|
97
|
+
- macOS:`~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
98
|
+
- Windows:`%APPDATA%\Claude\claude_desktop_config.json`
|
|
99
|
+
|
|
100
|
+
儲存後請完整結束並重新開啟 Claude Desktop。
|
|
101
|
+
|
|
102
|
+
### Cursor
|
|
103
|
+
|
|
104
|
+
將上方相同的 JSON 設定加入下列其中一個位置:
|
|
105
|
+
|
|
106
|
+
- 全域:`~/.cursor/mcp.json`
|
|
107
|
+
- 僅限單一專案:`.cursor/mcp.json`
|
|
108
|
+
|
|
109
|
+
開啟 **Cursor Settings > Tools & MCP**,確認 server 與工具已啟用。
|
|
110
|
+
|
|
111
|
+
## 生成第一張圖片
|
|
112
|
+
|
|
113
|
+
連線完成後,直接向 MCP 客戶端提出:
|
|
114
|
+
|
|
115
|
+
> 使用 `openai_generate_image` 生成一張 1024x1024 的電影感產品照,內容是放在大理石桌面上的透明機械鍵盤。將圖片儲存到 `/absolute/path/to/generated-images`,並回傳儲存路徑。
|
|
116
|
+
|
|
117
|
+
若要編輯既有圖片:
|
|
118
|
+
|
|
119
|
+
> 使用 `google_generate_image`,以 `/absolute/path/to/sketch.png` 作為輸入圖片。將它轉換成精緻的 16:9 行動 App 主視覺插圖,並儲存到 `/absolute/path/to/generated-images`。
|
|
120
|
+
|
|
121
|
+
請在提示詞與工具呼叫中使用絕對路徑。相對路徑會受到 MCP 客戶端所選工作目錄影響。
|
|
122
|
+
|
|
123
|
+
## 路徑與圖片輸出
|
|
124
|
+
|
|
125
|
+
- `input_images` 路徑會從 MCP server process 的工作目錄開始解析。
|
|
126
|
+
- `output_path` 可以是檔案路徑或資料夾。
|
|
127
|
+
- 已存在的資料夾,或沒有副檔名的新路徑,會被視為資料夾。
|
|
128
|
+
- 帶有副檔名的路徑會被視為指定輸出檔名;若有多張結果,檔名會加入編號。
|
|
129
|
+
- 未傳入 `output_path` 時,圖片會儲存在 `DEFAULT_OUTPUT_DIR`;若未設定該變數,則使用 MCP server 工作目錄下的 `./generated-images`。
|
|
130
|
+
- 建議輸入與輸出都使用絕對路徑,確保不同客戶端有一致結果。
|
|
131
|
+
- 若要跨供應商使用本機圖片,建議採用 PNG、JPG/JPEG 或 WebP。Google 與 xAI 的載入器接受這些副檔名。
|
|
132
|
+
- xAI Imagine Image 2.0 最多接受 5 張 `input_images`,舊模型維持 3 張限制。
|
|
133
|
+
|
|
134
|
+
### 回傳模式
|
|
135
|
+
|
|
136
|
+
| 模式 | 行為 |
|
|
137
|
+
| --- | --- |
|
|
138
|
+
| `file` | 預設。將圖片儲存到磁碟並回傳路徑。 |
|
|
139
|
+
| `inline` | 不儲存檔案,直接回傳 MCP `image` content;每張圖片受 `inline_max_bytes` 限制。 |
|
|
140
|
+
| `both` | 同時儲存圖片並回傳 inline MCP 圖片內容。 |
|
|
141
|
+
|
|
142
|
+
大型圖片建議使用 `file`。只有在 MCP 客戶端可以顯示圖片、且結果符合 payload 限制時,才使用 `inline` 或 `both`。
|
|
143
|
+
|
|
144
|
+
## 工具參數
|
|
145
|
+
|
|
146
|
+
### 共用參數
|
|
147
|
+
|
|
148
|
+
| 參數 | 必填 | 預設值 | 說明 |
|
|
149
|
+
| --- | --- | --- | --- |
|
|
150
|
+
| `prompt` | 是 | — | 圖片生成或編輯指示。 |
|
|
151
|
+
| `model` | 否 | 供應商預設值 | 相容的供應商模型 ID。 |
|
|
152
|
+
| `input_images` | 否 | `[]` | 本機參考圖/編輯圖路徑。 |
|
|
153
|
+
| `output_path` | 否 | `DEFAULT_OUTPUT_DIR` 或 `./generated-images` | 儲存結果的檔案或資料夾。 |
|
|
154
|
+
| `return_mode` | 否 | `file` | `file`、`inline` 或 `both`。 |
|
|
155
|
+
| `inline_max_bytes` | 否 | `4194304`(4 MiB) | 每張 inline 圖片允許的最大大小。 |
|
|
156
|
+
| `n` | 否 | 供應商預設值 | 供應商支援時的結果數量,範圍為 1 到 10;Google 目前回傳一張。 |
|
|
157
|
+
| `user` | 否 | — | 選用的終端使用者識別碼,供供應商進行濫用監控。 |
|
|
158
|
+
|
|
159
|
+
### OpenAI 參數
|
|
160
|
+
|
|
161
|
+
| 參數 | 值或範例 | 預設值 |
|
|
162
|
+
| --- | --- | --- |
|
|
163
|
+
| `size` | `auto`、`1024x1024`、`1536x1024` 或其他支援的 `WIDTHxHEIGHT` | 供應商預設值 |
|
|
164
|
+
| `quality` | `auto`、`low`、`medium`、`high`、`xhigh`、`max`、`standard`、`hd`,依模型支援 | 供應商預設值 |
|
|
165
|
+
| `background` | `transparent`、`opaque`、`auto` | 供應商預設值 |
|
|
166
|
+
| `output_format` | `png`、`jpeg`、`webp` | `png` |
|
|
167
|
+
| `output_compression` | 整數 `0`–`100`,僅供 GPT Image 的 JPEG/WebP 輸出使用 | 供應商預設值 |
|
|
168
|
+
| `moderation` | `low`、`auto` | 供應商預設值 |
|
|
169
|
+
| `style` | `vivid`、`natural`;供 DALL-E 3 使用 | 供應商預設值 |
|
|
170
|
+
|
|
171
|
+
### Google 參數
|
|
172
|
+
|
|
173
|
+
| 參數 | 值或範例 | 預設值 |
|
|
174
|
+
| --- | --- | --- |
|
|
175
|
+
| `aspect_ratio` | 例如 `1:1`、`16:9`、`9:16`、`4:3` 或 `3:2` | 供應商預設值 |
|
|
176
|
+
| `image_size` | `512`、`0.5K`、`1K`、`2K`、`4K`;實際可用值依模型而定 | 供應商預設值 |
|
|
177
|
+
| `output_format` | `png`、`jpeg` | 供應商預設值 |
|
|
178
|
+
| `thinking_level` | `minimal`、`high`,供 Gemini 3.1 Flash Image/Flash Lite Image 使用 | 供應商預設值 |
|
|
179
|
+
| `previous_interaction_id` | 前次 Google 結果的 `metadata.interactionId` | 無 |
|
|
180
|
+
|
|
181
|
+
`0.5K` 是相容舊版的別名,送出前會正規化為 `512`。
|
|
182
|
+
|
|
183
|
+
連續編輯時,將前次結果的 `metadata.interactionId` 放入 `previous_interaction_id`,並提供新的提示詞。前次互動必須仍可由同一 Google 帳戶存取。此功能使用供應商儲存的對話脈絡,適用 Google 的資料保留政策。ID 過期或無法使用時會回報錯誤,不會悄悄重新生成。Lite 不以連續編輯為主要用途,提高思考等級可能增加費用與等待時間。
|
|
184
|
+
|
|
185
|
+
### xAI 參數
|
|
186
|
+
|
|
187
|
+
| 參數 | 值或範例 | 預設值 |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| `aspect_ratio` | 例如 `1:1`、`16:9`、`9:16`、`3:2` 或 `auto` | 供應商預設值 |
|
|
190
|
+
| `resolution` | `1k`、`2k` | 供應商預設值 |
|
|
191
|
+
| `quality` | `low`、`medium`、`auto`,供 Imagine Image 2.0 或相容新模型使用 | 供應商 `auto` |
|
|
192
|
+
|
|
193
|
+
長寬比亦可指定 `21:9`、`5:2`。xAI 回傳實際服務模型時,會記錄於 `metadata.servedModel`。最外層的 `model` 仍表示請求指定的模型。
|
|
194
|
+
|
|
195
|
+
## 環境設定
|
|
196
|
+
|
|
197
|
+
| 變數 | 必要性 | 行為 |
|
|
198
|
+
| --- | --- | --- |
|
|
199
|
+
| `OPENAI_API_KEY` | 使用 OpenAI 工具時需要 | OpenAI API Key。 |
|
|
200
|
+
| `GOOGLE_API_KEY` 或 `GEMINI_API_KEY` | 使用 Google 工具時需要 | Google Gemini API Key。 |
|
|
201
|
+
| `XAI_API_KEY` | 使用 xAI 工具時需要 | xAI API Key。 |
|
|
202
|
+
| `DEFAULT_OPENAI_IMAGE_MODEL` | 否 | 未傳入 `model` 時使用的 OpenAI 預設模型;明確傳入的 `model` 可以覆寫。 |
|
|
203
|
+
| `DEFAULT_GOOGLE_IMAGE_MODEL` | 否 | 將 Google 請求固定到此模型,優先於每次呼叫的 `model` 參數。 |
|
|
204
|
+
| `DEFAULT_XAI_IMAGE_MODEL` | 否 | 將 xAI 請求固定到此模型,優先於每次呼叫的 `model` 參數。 |
|
|
205
|
+
| `DEFAULT_OUTPUT_DIR` | 否 | 預設圖片儲存資料夾;預設為 MCP server 工作目錄下的 `./generated-images`。 |
|
|
206
|
+
|
|
207
|
+
Key 只會在呼叫對應工具時檢查,因此只設定一個供應商 Key 即可開始使用。
|
|
208
|
+
|
|
209
|
+
## 進階直接工具呼叫
|
|
210
|
+
|
|
211
|
+
多數使用者直接用自然語言要求 MCP 客戶端即可。若客戶端支援直接呼叫工具,對等的 OpenAI 請求如下:
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"tool": "openai_generate_image",
|
|
216
|
+
"arguments": {
|
|
217
|
+
"prompt": "A cinematic product photo of a transparent mechanical keyboard on a marble desk",
|
|
218
|
+
"model": "gpt-image-2.5-sunburst",
|
|
219
|
+
"size": "1024x1024",
|
|
220
|
+
"return_mode": "file",
|
|
221
|
+
"output_path": "/absolute/path/to/generated-images"
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Google 參考圖範例:
|
|
227
|
+
|
|
228
|
+
```json
|
|
229
|
+
{
|
|
230
|
+
"tool": "google_generate_image",
|
|
231
|
+
"arguments": {
|
|
232
|
+
"prompt": "Turn this sketch into a polished mobile app hero illustration",
|
|
233
|
+
"model": "gemini-3.1-flash-image",
|
|
234
|
+
"input_images": ["/absolute/path/to/sketch.png"],
|
|
235
|
+
"aspect_ratio": "16:9",
|
|
236
|
+
"image_size": "512",
|
|
237
|
+
"return_mode": "file",
|
|
238
|
+
"output_path": "/absolute/path/to/generated-images"
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## 疑難排解
|
|
244
|
+
|
|
245
|
+
### 找不到 `npx`
|
|
246
|
+
|
|
247
|
+
安裝 Node.js 20 以上版本,並在新的終端機中確認 `node --version` 與 `npx --version` 都能執行。
|
|
248
|
+
|
|
249
|
+
### MCP 工具沒有出現
|
|
250
|
+
|
|
251
|
+
- 檢查 JSON 或 TOML 語法,以及設定檔位置。
|
|
252
|
+
- 編輯設定後,完整重新啟動 MCP 客戶端。
|
|
253
|
+
- Codex 使用 `/mcp` 檢查;Cursor 前往 **Settings > Tools & MCP**。
|
|
254
|
+
|
|
255
|
+
### API 驗證或帳務錯誤
|
|
256
|
+
|
|
257
|
+
確認 Key 與選擇的工具相符,且供應商帳戶已取得存取權限並啟用帳務或額度。只有被呼叫工具所屬的供應商會檢查 Key。
|
|
258
|
+
|
|
259
|
+
### 找不到輸入圖片
|
|
260
|
+
|
|
261
|
+
將相對路徑改為本機 MCP server process 可以讀取的絕對路徑。
|
|
262
|
+
|
|
263
|
+
### Inline 圖片超過 4 MiB
|
|
264
|
+
|
|
265
|
+
改用 `return_mode: "file"`;只有在客戶端能接受較大 payload 時,才提高 `inline_max_bytes`。
|
|
266
|
+
|
|
267
|
+
## 開發
|
|
268
|
+
|
|
269
|
+
若要從原始碼進行開發:
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
npm install
|
|
273
|
+
npm run dev
|
|
274
|
+
npm run ci
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
`npm run ci` 會執行型別檢查、測試與正式建置。
|
|
278
|
+
|
|
279
|
+
## 授權
|
|
280
|
+
|
|
281
|
+
[MIT](./LICENSE)
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
3
4
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
5
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
6
|
import { formatError } from "./errors.js";
|
|
@@ -30,11 +31,11 @@ function errorResult(error) {
|
|
|
30
31
|
export function createServer() {
|
|
31
32
|
const server = new McpServer({
|
|
32
33
|
name: "image-router-mcp",
|
|
33
|
-
version: "
|
|
34
|
+
version: JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version,
|
|
34
35
|
});
|
|
35
36
|
server.registerTool("openai_generate_image", {
|
|
36
37
|
title: "OpenAI image generation and editing",
|
|
37
|
-
description: "Generate or edit images with OpenAI
|
|
38
|
+
description: "Generate or edit images with OpenAI GPT Image 2.5 Sunburst and Flare, including xhigh/max quality and JPEG/WebP compression. Compatible model IDs remain flexible.",
|
|
38
39
|
inputSchema: openAIInputShape,
|
|
39
40
|
}, async (rawParams) => {
|
|
40
41
|
try {
|
|
@@ -48,7 +49,7 @@ export function createServer() {
|
|
|
48
49
|
});
|
|
49
50
|
server.registerTool("google_generate_image", {
|
|
50
51
|
title: "Google Gemini/Nano Banana image generation and editing",
|
|
51
|
-
description: "Generate or edit images with Google Gemini image models, including Nano Banana, Nano Banana 2, and Nano Banana Pro
|
|
52
|
+
description: "Generate or edit images with Google Gemini image models, including Nano Banana 2, Nano Banana 2 Lite, and Nano Banana Pro. Continue edits with previous_interaction_id and choose PNG/JPEG output.",
|
|
52
53
|
inputSchema: googleInputShape,
|
|
53
54
|
}, async (rawParams) => {
|
|
54
55
|
try {
|
|
@@ -62,7 +63,7 @@ export function createServer() {
|
|
|
62
63
|
});
|
|
63
64
|
server.registerTool("xai_generate_image", {
|
|
64
65
|
title: "xAI/Grok image generation and editing",
|
|
65
|
-
description: "Generate or edit images with xAI
|
|
66
|
+
description: "Generate or edit images with xAI Imagine Image 2.0, with low/medium/auto quality and up to five local reference images sent as data URIs. Older model IDs remain available.",
|
|
66
67
|
inputSchema: xaiInputShape,
|
|
67
68
|
}, async (rawParams) => {
|
|
68
69
|
try {
|
package/dist/providers/google.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { GoogleGenAI } from "@google/genai";
|
|
2
2
|
import { base64FromUrl, bytesFromBase64, loadInputImages } from "../files.js";
|
|
3
|
-
import { requireEnv } from "../errors.js";
|
|
3
|
+
import { requireEnv, UserInputError } from "../errors.js";
|
|
4
4
|
let googleClient;
|
|
5
5
|
function getGoogleClient() {
|
|
6
6
|
if (!googleClient) {
|
|
@@ -13,6 +13,9 @@ export function setGoogleClientForTests(client) {
|
|
|
13
13
|
}
|
|
14
14
|
export async function generateGoogleImage(params, client = getGoogleClient()) {
|
|
15
15
|
const model = process.env.DEFAULT_GOOGLE_IMAGE_MODEL ?? params.model;
|
|
16
|
+
if (/^gemini-3\.1-flash-lite-image(?:-|$)/.test(model) && params.image_size && params.image_size !== "1K") {
|
|
17
|
+
throw new UserInputError("Gemini 3.1 Flash Lite Image supports only image_size 1K.");
|
|
18
|
+
}
|
|
16
19
|
const inputImages = await loadInputImages(params.input_images);
|
|
17
20
|
const input = [
|
|
18
21
|
...inputImages.map((image) => ({
|
|
@@ -25,9 +28,12 @@ export async function generateGoogleImage(params, client = getGoogleClient()) {
|
|
|
25
28
|
const response = await client.interactions.create({
|
|
26
29
|
model,
|
|
27
30
|
input,
|
|
31
|
+
...(params.previous_interaction_id ? { previous_interaction_id: params.previous_interaction_id } : {}),
|
|
32
|
+
...(params.thinking_level ? { generation_config: { thinking_level: params.thinking_level } } : {}),
|
|
28
33
|
response_format: {
|
|
29
34
|
type: "image",
|
|
30
35
|
delivery: "inline",
|
|
36
|
+
...(params.output_format ? { mime_type: `image/${params.output_format}` } : {}),
|
|
31
37
|
...(params.aspect_ratio ? { aspect_ratio: params.aspect_ratio } : {}),
|
|
32
38
|
...(params.image_size ? { image_size: params.image_size === "0.5K" ? "512" : params.image_size } : {}),
|
|
33
39
|
},
|
|
@@ -37,7 +43,7 @@ export async function generateGoogleImage(params, client = getGoogleClient()) {
|
|
|
37
43
|
if (outputImage?.data) {
|
|
38
44
|
image = {
|
|
39
45
|
data: outputImage.data,
|
|
40
|
-
mimeType: outputImage.mime_type ?? "
|
|
46
|
+
mimeType: outputImage.mime_type ?? `image/${params.output_format ?? "png"}`,
|
|
41
47
|
bytes: bytesFromBase64(outputImage.data),
|
|
42
48
|
};
|
|
43
49
|
}
|
package/dist/providers/openai.js
CHANGED
|
@@ -30,10 +30,20 @@ async function imageFromOpenAIItem(item, mimeType) {
|
|
|
30
30
|
throw new Error("OpenAI response did not include b64_json or url image data.");
|
|
31
31
|
}
|
|
32
32
|
export async function generateOpenAIImage(params, client = getOpenAIClient()) {
|
|
33
|
-
const model = params.model ?? process.env.DEFAULT_OPENAI_IMAGE_MODEL ?? "gpt-image-2";
|
|
33
|
+
const model = params.model ?? process.env.DEFAULT_OPENAI_IMAGE_MODEL ?? "gpt-image-2.5-sunburst";
|
|
34
34
|
const outputFormat = params.output_format ?? "png";
|
|
35
35
|
const inputImages = params.input_images ?? [];
|
|
36
36
|
const dallEModel = isDallEModel(model);
|
|
37
|
+
if (params.output_compression !== undefined && (dallEModel || outputFormat === "png")) {
|
|
38
|
+
throw new UserInputError("output_compression requires a GPT Image model with output_format jpeg or webp.");
|
|
39
|
+
}
|
|
40
|
+
if ((params.quality === "xhigh" || params.quality === "max") &&
|
|
41
|
+
(dallEModel || /^gpt-image-(?:1(?:[.-]|$)|2(?:-|$))/.test(model))) {
|
|
42
|
+
throw new UserInputError("Quality xhigh and max require GPT Image 2.5 or a compatible newer model.");
|
|
43
|
+
}
|
|
44
|
+
// SDK 6 keeps Node.js 20 support. It forwards the API's new quality values unchanged,
|
|
45
|
+
// but its type declarations predate GPT Image 2.5. Wire-format tests cover both endpoints.
|
|
46
|
+
const quality = params.quality;
|
|
37
47
|
const common = {
|
|
38
48
|
model,
|
|
39
49
|
prompt: params.prompt,
|
|
@@ -45,6 +55,7 @@ export async function generateOpenAIImage(params, client = getOpenAIClient()) {
|
|
|
45
55
|
: {
|
|
46
56
|
background: params.background,
|
|
47
57
|
output_format: outputFormat,
|
|
58
|
+
output_compression: params.output_compression,
|
|
48
59
|
}),
|
|
49
60
|
};
|
|
50
61
|
const response = inputImages.length > 0
|
|
@@ -54,7 +65,7 @@ export async function generateOpenAIImage(params, client = getOpenAIClient()) {
|
|
|
54
65
|
}
|
|
55
66
|
return client.images.edit({
|
|
56
67
|
...common,
|
|
57
|
-
quality:
|
|
68
|
+
quality: quality,
|
|
58
69
|
image: inputImages.length === 1
|
|
59
70
|
? createReadStream(inputImages[0])
|
|
60
71
|
: inputImages.map((imagePath) => createReadStream(imagePath)),
|
|
@@ -62,7 +73,7 @@ export async function generateOpenAIImage(params, client = getOpenAIClient()) {
|
|
|
62
73
|
})()
|
|
63
74
|
: await client.images.generate({
|
|
64
75
|
...common,
|
|
65
|
-
quality
|
|
76
|
+
quality,
|
|
66
77
|
...(dallEModel ? { style: params.style } : { moderation: params.moderation }),
|
|
67
78
|
});
|
|
68
79
|
const data = "data" in response ? response.data ?? [] : [];
|
package/dist/providers/xai.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { base64FromUrl, bytesFromBase64, loadInputImages, toDataUri } from "../files.js";
|
|
2
|
-
import { requireEnv } from "../errors.js";
|
|
2
|
+
import { requireEnv, UserInputError } from "../errors.js";
|
|
3
3
|
const XAI_BASE_URL = "https://api.x.ai/v1";
|
|
4
4
|
async function imageFromXAIItem(item) {
|
|
5
5
|
if (item.b64_json) {
|
|
@@ -17,6 +17,13 @@ async function imageFromXAIItem(item) {
|
|
|
17
17
|
}
|
|
18
18
|
export async function generateXAIImage(params, fetchImpl = fetch) {
|
|
19
19
|
const model = process.env.DEFAULT_XAI_IMAGE_MODEL ?? params.model;
|
|
20
|
+
const legacyModel = /^grok-imagine-image(?:-quality|-pro)?(?:-latest|-\d{8})?$/.test(model);
|
|
21
|
+
if (legacyModel && (params.input_images?.length ?? 0) > 3) {
|
|
22
|
+
throw new UserInputError("This older xAI model supports up to 3 input images. Use grok-imagine-image-2.0 for up to 5.");
|
|
23
|
+
}
|
|
24
|
+
if (legacyModel && params.quality !== undefined) {
|
|
25
|
+
throw new UserInputError("The quality parameter requires grok-imagine-image-2.0 or a compatible newer model.");
|
|
26
|
+
}
|
|
20
27
|
const inputImages = await loadInputImages(params.input_images);
|
|
21
28
|
const isEdit = inputImages.length > 0;
|
|
22
29
|
const body = {
|
|
@@ -25,6 +32,7 @@ export async function generateXAIImage(params, fetchImpl = fetch) {
|
|
|
25
32
|
n: params.n,
|
|
26
33
|
aspect_ratio: params.aspect_ratio,
|
|
27
34
|
resolution: params.resolution,
|
|
35
|
+
quality: params.quality,
|
|
28
36
|
response_format: "b64_json",
|
|
29
37
|
user: params.user,
|
|
30
38
|
};
|
|
@@ -58,6 +66,7 @@ export async function generateXAIImage(params, fetchImpl = fetch) {
|
|
|
58
66
|
images: await Promise.all(data.map(imageFromXAIItem)),
|
|
59
67
|
metadata: {
|
|
60
68
|
usage: "usage" in responseBody ? responseBody.usage : undefined,
|
|
69
|
+
servedModel: "model" in responseBody ? responseBody.model : undefined,
|
|
61
70
|
},
|
|
62
71
|
};
|
|
63
72
|
}
|
package/dist/schemas.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export declare const openAIInputShape: {
|
|
|
13
13
|
low: "low";
|
|
14
14
|
medium: "medium";
|
|
15
15
|
high: "high";
|
|
16
|
+
xhigh: "xhigh";
|
|
17
|
+
max: "max";
|
|
16
18
|
standard: "standard";
|
|
17
19
|
hd: "hd";
|
|
18
20
|
}>>;
|
|
@@ -26,6 +28,7 @@ export declare const openAIInputShape: {
|
|
|
26
28
|
jpeg: "jpeg";
|
|
27
29
|
webp: "webp";
|
|
28
30
|
}>>;
|
|
31
|
+
readonly output_compression: z.ZodOptional<z.ZodNumber>;
|
|
29
32
|
readonly moderation: z.ZodOptional<z.ZodEnum<{
|
|
30
33
|
auto: "auto";
|
|
31
34
|
low: "low";
|
|
@@ -48,6 +51,15 @@ export declare const openAIInputShape: {
|
|
|
48
51
|
};
|
|
49
52
|
export declare const googleInputShape: {
|
|
50
53
|
readonly model: z.ZodDefault<z.ZodString>;
|
|
54
|
+
readonly output_format: z.ZodOptional<z.ZodEnum<{
|
|
55
|
+
png: "png";
|
|
56
|
+
jpeg: "jpeg";
|
|
57
|
+
}>>;
|
|
58
|
+
readonly previous_interaction_id: z.ZodOptional<z.ZodString>;
|
|
59
|
+
readonly thinking_level: z.ZodOptional<z.ZodEnum<{
|
|
60
|
+
high: "high";
|
|
61
|
+
minimal: "minimal";
|
|
62
|
+
}>>;
|
|
51
63
|
readonly aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
52
64
|
readonly image_size: z.ZodOptional<z.ZodEnum<{
|
|
53
65
|
512: "512";
|
|
@@ -71,6 +83,11 @@ export declare const googleInputShape: {
|
|
|
71
83
|
export declare const xaiInputShape: {
|
|
72
84
|
readonly input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
73
85
|
readonly model: z.ZodDefault<z.ZodString>;
|
|
86
|
+
readonly quality: z.ZodOptional<z.ZodEnum<{
|
|
87
|
+
auto: "auto";
|
|
88
|
+
low: "low";
|
|
89
|
+
medium: "medium";
|
|
90
|
+
}>>;
|
|
74
91
|
readonly aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
75
92
|
readonly resolution: z.ZodOptional<z.ZodEnum<{
|
|
76
93
|
"1k": "1k";
|
|
@@ -95,6 +112,8 @@ export declare const openAIParamsSchema: z.ZodObject<{
|
|
|
95
112
|
low: "low";
|
|
96
113
|
medium: "medium";
|
|
97
114
|
high: "high";
|
|
115
|
+
xhigh: "xhigh";
|
|
116
|
+
max: "max";
|
|
98
117
|
standard: "standard";
|
|
99
118
|
hd: "hd";
|
|
100
119
|
}>>;
|
|
@@ -108,6 +127,7 @@ export declare const openAIParamsSchema: z.ZodObject<{
|
|
|
108
127
|
jpeg: "jpeg";
|
|
109
128
|
webp: "webp";
|
|
110
129
|
}>>;
|
|
130
|
+
output_compression: z.ZodOptional<z.ZodNumber>;
|
|
111
131
|
moderation: z.ZodOptional<z.ZodEnum<{
|
|
112
132
|
auto: "auto";
|
|
113
133
|
low: "low";
|
|
@@ -130,6 +150,15 @@ export declare const openAIParamsSchema: z.ZodObject<{
|
|
|
130
150
|
}, z.core.$strict>;
|
|
131
151
|
export declare const googleParamsSchema: z.ZodObject<{
|
|
132
152
|
model: z.ZodDefault<z.ZodString>;
|
|
153
|
+
output_format: z.ZodOptional<z.ZodEnum<{
|
|
154
|
+
png: "png";
|
|
155
|
+
jpeg: "jpeg";
|
|
156
|
+
}>>;
|
|
157
|
+
previous_interaction_id: z.ZodOptional<z.ZodString>;
|
|
158
|
+
thinking_level: z.ZodOptional<z.ZodEnum<{
|
|
159
|
+
high: "high";
|
|
160
|
+
minimal: "minimal";
|
|
161
|
+
}>>;
|
|
133
162
|
aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
134
163
|
image_size: z.ZodOptional<z.ZodEnum<{
|
|
135
164
|
512: "512";
|
|
@@ -153,6 +182,11 @@ export declare const googleParamsSchema: z.ZodObject<{
|
|
|
153
182
|
export declare const xaiParamsSchema: z.ZodObject<{
|
|
154
183
|
input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
155
184
|
model: z.ZodDefault<z.ZodString>;
|
|
185
|
+
quality: z.ZodOptional<z.ZodEnum<{
|
|
186
|
+
auto: "auto";
|
|
187
|
+
low: "low";
|
|
188
|
+
medium: "medium";
|
|
189
|
+
}>>;
|
|
156
190
|
aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
157
191
|
resolution: z.ZodOptional<z.ZodEnum<{
|
|
158
192
|
"1k": "1k";
|
package/dist/schemas.js
CHANGED
|
@@ -5,7 +5,7 @@ export const returnModeSchema = z
|
|
|
5
5
|
.default("file")
|
|
6
6
|
.describe("How generated images are returned. Use file for large images; inline embeds base64 image content in the MCP response.");
|
|
7
7
|
const nonEmptyString = z.string().trim().min(1);
|
|
8
|
-
const defaultOpenAIImageModel = process.env.DEFAULT_OPENAI_IMAGE_MODEL ?? "gpt-image-2";
|
|
8
|
+
const defaultOpenAIImageModel = process.env.DEFAULT_OPENAI_IMAGE_MODEL ?? "gpt-image-2.5-sunburst";
|
|
9
9
|
const commonImageShape = {
|
|
10
10
|
prompt: nonEmptyString.describe("Image generation or editing instructions."),
|
|
11
11
|
model: nonEmptyString.optional().describe("Provider model ID. Any string is allowed so newly released models are not blocked."),
|
|
@@ -23,17 +23,21 @@ const commonImageShape = {
|
|
|
23
23
|
};
|
|
24
24
|
export const openAIInputShape = {
|
|
25
25
|
...commonImageShape,
|
|
26
|
-
model: nonEmptyString.default(defaultOpenAIImageModel).describe("OpenAI image model ID. Defaults to DEFAULT_OPENAI_IMAGE_MODEL or gpt-image-2."),
|
|
26
|
+
model: nonEmptyString.default(defaultOpenAIImageModel).describe("OpenAI image model ID. Defaults to DEFAULT_OPENAI_IMAGE_MODEL or gpt-image-2.5-sunburst. Use gpt-image-2.5-flare for everyday generation."),
|
|
27
27
|
size: nonEmptyString.optional().describe("OpenAI size, such as auto, 1024x1024, 1536x1024, or any supported WIDTHxHEIGHT."),
|
|
28
|
-
quality: z.enum(["auto", "low", "medium", "high", "standard", "hd"]).optional().describe("OpenAI quality."),
|
|
28
|
+
quality: z.enum(["auto", "low", "medium", "high", "xhigh", "max", "standard", "hd"]).optional().describe("OpenAI quality. xhigh and max require GPT Image 2.5 or a compatible newer model. Higher quality can increase cost and latency."),
|
|
29
29
|
background: z.enum(["transparent", "opaque", "auto"]).optional().describe("OpenAI background option."),
|
|
30
30
|
output_format: z.enum(["png", "jpeg", "webp"]).default("png").describe("Output image format."),
|
|
31
|
+
output_compression: z.number().int().min(0).max(100).optional().describe("Output compression from 0 to 100 for JPEG or WebP GPT Image output."),
|
|
31
32
|
moderation: z.enum(["low", "auto"]).optional().describe("OpenAI moderation setting for supported models."),
|
|
32
33
|
style: z.enum(["vivid", "natural"]).optional().describe("DALL-E 3 style option."),
|
|
33
34
|
};
|
|
34
35
|
export const googleInputShape = {
|
|
35
36
|
...commonImageShape,
|
|
36
37
|
model: nonEmptyString.default("gemini-3.1-flash-image").describe("Google image model ID. Defaults to Nano Banana 2."),
|
|
38
|
+
output_format: z.enum(["png", "jpeg"]).optional().describe("Requested Google image output format. Omit to use the provider default."),
|
|
39
|
+
previous_interaction_id: nonEmptyString.optional().describe("Continue a Google image editing conversation using metadata.interactionId from a previous result. Requires that interaction to remain available in the same provider account."),
|
|
40
|
+
thinking_level: z.enum(["minimal", "high"]).optional().describe("Thinking level for Gemini 3.1 Flash Image and Flash Lite Image. Higher thinking can increase cost and latency."),
|
|
37
41
|
aspect_ratio: nonEmptyString.optional().describe("Aspect ratio, for example 1:1, 16:9, 9:16, 4:3, or 3:2."),
|
|
38
42
|
image_size: z
|
|
39
43
|
.enum(["512", "0.5K", "1K", "2K", "4K"])
|
|
@@ -42,8 +46,9 @@ export const googleInputShape = {
|
|
|
42
46
|
};
|
|
43
47
|
export const xaiInputShape = {
|
|
44
48
|
...commonImageShape,
|
|
45
|
-
input_images: z.array(nonEmptyString).max(
|
|
46
|
-
model: nonEmptyString.default("grok-imagine-image-
|
|
49
|
+
input_images: z.array(nonEmptyString).max(5).optional().describe("Local image paths used as edit/reference inputs. Imagine Image 2.0 supports up to 5 input images, older models up to 3."),
|
|
50
|
+
model: nonEmptyString.default("grok-imagine-image-2.0").describe("xAI/Grok image model ID. Defaults to Imagine Image 2.0."),
|
|
51
|
+
quality: z.enum(["low", "medium", "auto"]).optional().describe("Imagine Image 2.0 quality. Provider auto currently uses low for generation and medium for editing, with different costs. Set low or medium to pin quality."),
|
|
47
52
|
aspect_ratio: nonEmptyString.optional().describe("Aspect ratio, for example 1:1, 16:9, 9:16, 3:2, or auto."),
|
|
48
53
|
resolution: z.enum(["1k", "2k"]).optional().describe("xAI image resolution."),
|
|
49
54
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "image-router-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "MCP server for routing image generation and editing across xAI, Google Gemini/Nano Banana, and OpenAI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -11,8 +11,12 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
13
13
|
"README.md",
|
|
14
|
+
"README.zh-TW.md",
|
|
14
15
|
"LICENSE"
|
|
15
16
|
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20.0.0"
|
|
19
|
+
},
|
|
16
20
|
"scripts": {
|
|
17
21
|
"build": "tsc -p tsconfig.json",
|
|
18
22
|
"dev": "tsx src/index.ts",
|
|
@@ -33,8 +37,8 @@
|
|
|
33
37
|
"author": "",
|
|
34
38
|
"license": "MIT",
|
|
35
39
|
"dependencies": {
|
|
36
|
-
"@google/genai": "2.
|
|
37
|
-
"@modelcontextprotocol/sdk": "1.
|
|
40
|
+
"@google/genai": "2.21.0",
|
|
41
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
38
42
|
"openai": "6.49.0",
|
|
39
43
|
"zod": "4.4.3"
|
|
40
44
|
},
|
|
@@ -42,7 +46,7 @@
|
|
|
42
46
|
"@types/node": "25.9.1",
|
|
43
47
|
"tsx": "4.23.1",
|
|
44
48
|
"typescript": "6.0.3",
|
|
45
|
-
"vitest": "4.1.
|
|
49
|
+
"vitest": "4.1.11"
|
|
46
50
|
},
|
|
47
51
|
"repository": {
|
|
48
52
|
"type": "git",
|