image-router-mcp 0.1.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/LICENSE +21 -0
- package/README.md +249 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +24 -0
- package/dist/files.d.ts +23 -0
- package/dist/files.js +123 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +88 -0
- package/dist/providers/google.d.ts +10 -0
- package/dist/providers/google.js +67 -0
- package/dist/providers/openai.d.ts +11 -0
- package/dist/providers/openai.js +76 -0
- package/dist/providers/xai.d.ts +4 -0
- package/dist/providers/xai.js +63 -0
- package/dist/schemas.d.ts +171 -0
- package/dist/schemas.js +47 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 image-router-mcp contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# image-router-mcp
|
|
2
|
+
|
|
3
|
+
English | [繁體中文](#繁體中文)
|
|
4
|
+
|
|
5
|
+
Open-source MCP server for routing image generation and editing across xAI/Grok, Google Gemini/Nano Banana, and OpenAI.
|
|
6
|
+
|
|
7
|
+
It keeps model IDs flexible instead of locking tools to a small allowlist, so newly released image models can work as soon as the provider API supports the same image endpoint and parameters.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Route image generation and editing through three MCP tools: xAI, Google, and OpenAI.
|
|
12
|
+
- Use local images as references or edit inputs via `input_images`.
|
|
13
|
+
- Save generated images to disk by default to avoid large base64 payloads over stdio.
|
|
14
|
+
- Optionally return inline MCP image content for clients that can render images directly.
|
|
15
|
+
- Override provider defaults with environment variables or per-call `model` arguments.
|
|
16
|
+
|
|
17
|
+
## Tools
|
|
18
|
+
|
|
19
|
+
| Tool | Provider | Supports |
|
|
20
|
+
| --- | --- | --- |
|
|
21
|
+
| `xai_generate_image` | xAI/Grok | Text-to-image and JSON image edits with local `input_images` |
|
|
22
|
+
| `google_generate_image` | Google Gemini/Nano Banana | Text-to-image and local reference/edit images |
|
|
23
|
+
| `openai_generate_image` | OpenAI | Text-to-image and local reference/edit images |
|
|
24
|
+
|
|
25
|
+
## Model examples
|
|
26
|
+
|
|
27
|
+
These are examples, not allowlists.
|
|
28
|
+
|
|
29
|
+
| Provider | Example models |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| xAI | `grok-imagine-image-quality`, `grok-imagine-image` |
|
|
32
|
+
| Google | `gemini-3.1-flash-image-preview`, `gemini-3-pro-image-preview`, `gemini-2.5-flash-image` |
|
|
33
|
+
| OpenAI | `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, `chatgpt-image-latest`, `dall-e-3` |
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install
|
|
39
|
+
npm run build
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For MCP clients, run the published package with `npx`:
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"mcpServers": {
|
|
47
|
+
"image-router-mcp": {
|
|
48
|
+
"command": "npx",
|
|
49
|
+
"args": ["-y", "image-router-mcp"],
|
|
50
|
+
"env": {
|
|
51
|
+
"XAI_API_KEY": "your_xai_key",
|
|
52
|
+
"GOOGLE_API_KEY": "your_google_key",
|
|
53
|
+
"OPENAI_API_KEY": "your_openai_key"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
> [!TIP]
|
|
61
|
+
> Set only the API keys for the providers you use. Keys are checked lazily when each tool is called.
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
| Variable | Required | Description |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `XAI_API_KEY` | For xAI tool | xAI API key. |
|
|
68
|
+
| `GOOGLE_API_KEY` or `GEMINI_API_KEY` | For Google tool | Google Gemini API key. |
|
|
69
|
+
| `OPENAI_API_KEY` | For OpenAI tool | OpenAI API key. |
|
|
70
|
+
| `DEFAULT_XAI_IMAGE_MODEL` | No | Overrides the default xAI image model. |
|
|
71
|
+
| `DEFAULT_GOOGLE_IMAGE_MODEL` | No | Overrides the default Google image model. |
|
|
72
|
+
| `DEFAULT_OPENAI_IMAGE_MODEL` | No | Overrides the default OpenAI image model. |
|
|
73
|
+
| `DEFAULT_OUTPUT_DIR` | No | Directory for saved images. Defaults to `./generated-images`. |
|
|
74
|
+
|
|
75
|
+
## How to use this MCP
|
|
76
|
+
|
|
77
|
+
After adding the server to your MCP client, ask the client to use one of the image tools. For example:
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"tool": "openai_generate_image",
|
|
82
|
+
"arguments": {
|
|
83
|
+
"prompt": "A cinematic product photo of a transparent mechanical keyboard on a marble desk",
|
|
84
|
+
"model": "gpt-image-2",
|
|
85
|
+
"size": "1024x1024",
|
|
86
|
+
"return_mode": "file",
|
|
87
|
+
"output_path": "./generated-images"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
To edit or reference existing local images, pass file paths with `input_images`:
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"tool": "google_generate_image",
|
|
97
|
+
"arguments": {
|
|
98
|
+
"prompt": "Turn this sketch into a polished mobile app hero illustration",
|
|
99
|
+
"model": "gemini-3.1-flash-image-preview",
|
|
100
|
+
"input_images": ["./assets/sketch.png"],
|
|
101
|
+
"aspect_ratio": "16:9",
|
|
102
|
+
"return_mode": "both"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Return modes
|
|
108
|
+
|
|
109
|
+
| Mode | Behavior |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `file` | Default. Saves images to disk and returns file paths. |
|
|
112
|
+
| `inline` | Returns MCP `image` content blocks, capped by `inline_max_bytes`. |
|
|
113
|
+
| `both` | Saves images to disk and also returns inline image content. |
|
|
114
|
+
|
|
115
|
+
Use `file` for large generations. Use `inline` or `both` only when your MCP client can display image content and the images fit within your payload limits.
|
|
116
|
+
|
|
117
|
+
## Development
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npm install
|
|
121
|
+
npm run dev
|
|
122
|
+
npm run ci
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`npm run ci` runs type checking, tests, and a production build.
|
|
126
|
+
|
|
127
|
+
## 繁體中文
|
|
128
|
+
|
|
129
|
+
開源 MCP 伺服器,可將圖片生成與編輯請求路由到 xAI/Grok、Google Gemini/Nano Banana 與 OpenAI。
|
|
130
|
+
|
|
131
|
+
此專案刻意接受彈性的模型 ID,而不是把工具鎖在固定清單內;只要供應商 API 支援相同圖片端點與參數,新模型通常可以直接使用。
|
|
132
|
+
|
|
133
|
+
## 功能
|
|
134
|
+
|
|
135
|
+
- 透過三個 MCP 工具路由圖片生成與編輯:xAI、Google、OpenAI。
|
|
136
|
+
- 使用本機圖片作為參考圖或編輯輸入,透過 `input_images` 傳入。
|
|
137
|
+
- 預設將生成圖片儲存到磁碟,避免透過 stdio 傳送大型 base64 payload。
|
|
138
|
+
- 可選擇回傳 inline MCP 圖片內容,讓支援的客戶端直接顯示圖片。
|
|
139
|
+
- 可用環境變數或每次呼叫的 `model` 參數覆寫預設模型。
|
|
140
|
+
|
|
141
|
+
## 工具
|
|
142
|
+
|
|
143
|
+
| 工具 | 供應商 | 支援內容 |
|
|
144
|
+
| --- | --- | --- |
|
|
145
|
+
| `xai_generate_image` | xAI/Grok | 文字生成圖片,以及使用本機 `input_images` 的 JSON 圖片編輯 |
|
|
146
|
+
| `google_generate_image` | Google Gemini/Nano Banana | 文字生成圖片,以及本機參考圖/編輯圖 |
|
|
147
|
+
| `openai_generate_image` | OpenAI | 文字生成圖片,以及本機參考圖/編輯圖 |
|
|
148
|
+
|
|
149
|
+
## 模型範例
|
|
150
|
+
|
|
151
|
+
以下是範例,不是允許清單。
|
|
152
|
+
|
|
153
|
+
| 供應商 | 模型範例 |
|
|
154
|
+
| --- | --- |
|
|
155
|
+
| xAI | `grok-imagine-image-quality`, `grok-imagine-image` |
|
|
156
|
+
| Google | `gemini-3.1-flash-image-preview`, `gemini-3-pro-image-preview`, `gemini-2.5-flash-image` |
|
|
157
|
+
| OpenAI | `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, `chatgpt-image-latest`, `dall-e-3` |
|
|
158
|
+
|
|
159
|
+
## 快速開始
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npm install
|
|
163
|
+
npm run build
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
在 MCP 客戶端中,可用 `npx` 執行已發布的套件:
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{
|
|
170
|
+
"mcpServers": {
|
|
171
|
+
"image-router-mcp": {
|
|
172
|
+
"command": "npx",
|
|
173
|
+
"args": ["-y", "image-router-mcp"],
|
|
174
|
+
"env": {
|
|
175
|
+
"XAI_API_KEY": "your_xai_key",
|
|
176
|
+
"GOOGLE_API_KEY": "your_google_key",
|
|
177
|
+
"OPENAI_API_KEY": "your_openai_key"
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
> [!TIP]
|
|
185
|
+
> 只需要設定會用到的供應商 API Key。Key 會在呼叫對應工具時才檢查。
|
|
186
|
+
|
|
187
|
+
## 設定
|
|
188
|
+
|
|
189
|
+
| 變數 | 必要性 | 說明 |
|
|
190
|
+
| --- | --- | --- |
|
|
191
|
+
| `XAI_API_KEY` | 使用 xAI 工具時需要 | xAI API Key。 |
|
|
192
|
+
| `GOOGLE_API_KEY` 或 `GEMINI_API_KEY` | 使用 Google 工具時需要 | Google Gemini API Key。 |
|
|
193
|
+
| `OPENAI_API_KEY` | 使用 OpenAI 工具時需要 | OpenAI API Key。 |
|
|
194
|
+
| `DEFAULT_XAI_IMAGE_MODEL` | 否 | 覆寫預設 xAI 圖片模型。 |
|
|
195
|
+
| `DEFAULT_GOOGLE_IMAGE_MODEL` | 否 | 覆寫預設 Google 圖片模型。 |
|
|
196
|
+
| `DEFAULT_OPENAI_IMAGE_MODEL` | 否 | 覆寫預設 OpenAI 圖片模型。 |
|
|
197
|
+
| `DEFAULT_OUTPUT_DIR` | 否 | 圖片輸出資料夾,預設為 `./generated-images`。 |
|
|
198
|
+
|
|
199
|
+
## 這個 MCP 怎麼用
|
|
200
|
+
|
|
201
|
+
將 server 加到 MCP 客戶端後,請客戶端呼叫其中一個圖片工具。例如:
|
|
202
|
+
|
|
203
|
+
```json
|
|
204
|
+
{
|
|
205
|
+
"tool": "openai_generate_image",
|
|
206
|
+
"arguments": {
|
|
207
|
+
"prompt": "A cinematic product photo of a transparent mechanical keyboard on a marble desk",
|
|
208
|
+
"model": "gpt-image-2",
|
|
209
|
+
"size": "1024x1024",
|
|
210
|
+
"return_mode": "file",
|
|
211
|
+
"output_path": "./generated-images"
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
若要使用既有本機圖片作為編輯或參考輸入,傳入 `input_images`:
|
|
217
|
+
|
|
218
|
+
```json
|
|
219
|
+
{
|
|
220
|
+
"tool": "google_generate_image",
|
|
221
|
+
"arguments": {
|
|
222
|
+
"prompt": "Turn this sketch into a polished mobile app hero illustration",
|
|
223
|
+
"model": "gemini-3.1-flash-image-preview",
|
|
224
|
+
"input_images": ["./assets/sketch.png"],
|
|
225
|
+
"aspect_ratio": "16:9",
|
|
226
|
+
"return_mode": "both"
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### 回傳模式
|
|
232
|
+
|
|
233
|
+
| 模式 | 行為 |
|
|
234
|
+
| --- | --- |
|
|
235
|
+
| `file` | 預設。將圖片存到磁碟並回傳檔案路徑。 |
|
|
236
|
+
| `inline` | 回傳 MCP `image` content block,大小受 `inline_max_bytes` 限制。 |
|
|
237
|
+
| `both` | 同時儲存圖片到磁碟並回傳 inline 圖片內容。 |
|
|
238
|
+
|
|
239
|
+
大型生成結果建議使用 `file`。只有在 MCP 客戶端支援圖片顯示、且圖片大小符合 payload 限制時,才使用 `inline` 或 `both`。
|
|
240
|
+
|
|
241
|
+
## 開發
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
npm install
|
|
245
|
+
npm run dev
|
|
246
|
+
npm run ci
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
`npm run ci` 會執行型別檢查、測試與正式建置。
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class UserInputError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "UserInputError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function requireEnv(names) {
|
|
8
|
+
for (const name of names) {
|
|
9
|
+
const value = process.env[name];
|
|
10
|
+
if (value) {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
throw new UserInputError(`Missing required environment variable: ${names.join(" or ")}`);
|
|
15
|
+
}
|
|
16
|
+
export function formatError(error) {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return error.message;
|
|
19
|
+
}
|
|
20
|
+
if (typeof error === "string") {
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
return JSON.stringify(error);
|
|
24
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { LoadedImage, ProviderResult } from "./types.js";
|
|
2
|
+
import type { ReturnMode } from "./schemas.js";
|
|
3
|
+
export declare function mimeFromPath(filePath: string): string;
|
|
4
|
+
export declare function extensionFromMime(mimeType: string): string;
|
|
5
|
+
export declare function loadInputImages(paths?: string[]): Promise<LoadedImage[]>;
|
|
6
|
+
export declare function toDataUri(image: LoadedImage): string;
|
|
7
|
+
export declare function bytesFromBase64(data: string): number;
|
|
8
|
+
export declare function base64FromUrl(url: string): Promise<{
|
|
9
|
+
data: string;
|
|
10
|
+
mimeType: string;
|
|
11
|
+
bytes: number;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function defaultOutputDir(): string;
|
|
14
|
+
export declare function persistImages(result: ProviderResult, outputPath?: string): Promise<ProviderResult>;
|
|
15
|
+
export type ToolContent = {
|
|
16
|
+
type: "text";
|
|
17
|
+
text: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: "image";
|
|
20
|
+
data: string;
|
|
21
|
+
mimeType: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function buildToolContent(result: ProviderResult, returnMode: ReturnMode, inlineMaxBytes: number): ToolContent[];
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
|
+
import { UserInputError } from "./errors.js";
|
|
5
|
+
const MIME_BY_EXT = {
|
|
6
|
+
".png": "image/png",
|
|
7
|
+
".jpg": "image/jpeg",
|
|
8
|
+
".jpeg": "image/jpeg",
|
|
9
|
+
".webp": "image/webp",
|
|
10
|
+
};
|
|
11
|
+
const EXT_BY_MIME = {
|
|
12
|
+
"image/png": ".png",
|
|
13
|
+
"image/jpeg": ".jpg",
|
|
14
|
+
"image/webp": ".webp",
|
|
15
|
+
};
|
|
16
|
+
export function mimeFromPath(filePath) {
|
|
17
|
+
const mime = MIME_BY_EXT[path.extname(filePath).toLowerCase()];
|
|
18
|
+
if (!mime) {
|
|
19
|
+
throw new UserInputError(`Unsupported image extension for ${filePath}. Use png, jpg, jpeg, or webp.`);
|
|
20
|
+
}
|
|
21
|
+
return mime;
|
|
22
|
+
}
|
|
23
|
+
export function extensionFromMime(mimeType) {
|
|
24
|
+
return EXT_BY_MIME[mimeType] ?? ".png";
|
|
25
|
+
}
|
|
26
|
+
export async function loadInputImages(paths = []) {
|
|
27
|
+
return Promise.all(paths.map(async (imagePath) => {
|
|
28
|
+
const absolutePath = path.resolve(imagePath);
|
|
29
|
+
const buffer = await readFile(absolutePath);
|
|
30
|
+
return {
|
|
31
|
+
path: absolutePath,
|
|
32
|
+
data: buffer.toString("base64"),
|
|
33
|
+
mimeType: mimeFromPath(absolutePath),
|
|
34
|
+
bytes: buffer.byteLength,
|
|
35
|
+
};
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
export function toDataUri(image) {
|
|
39
|
+
return `data:${image.mimeType};base64,${image.data}`;
|
|
40
|
+
}
|
|
41
|
+
export function bytesFromBase64(data) {
|
|
42
|
+
return Buffer.byteLength(data, "base64");
|
|
43
|
+
}
|
|
44
|
+
export async function base64FromUrl(url) {
|
|
45
|
+
const response = await fetch(url);
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw new Error(`Failed to download generated image URL (${response.status} ${response.statusText})`);
|
|
48
|
+
}
|
|
49
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
50
|
+
const mimeType = response.headers.get("content-type")?.split(";")[0] || "image/png";
|
|
51
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
52
|
+
return { data: buffer.toString("base64"), mimeType, bytes: buffer.byteLength };
|
|
53
|
+
}
|
|
54
|
+
export function defaultOutputDir() {
|
|
55
|
+
return path.resolve(process.env.DEFAULT_OUTPUT_DIR ?? path.join(process.cwd(), "generated-images"));
|
|
56
|
+
}
|
|
57
|
+
function safeModelSlug(model) {
|
|
58
|
+
return model.replace(/[^a-zA-Z0-9._-]+/g, "-").slice(0, 48) || "model";
|
|
59
|
+
}
|
|
60
|
+
function generatedFileName(provider, model, mimeType, index) {
|
|
61
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
62
|
+
const suffix = randomBytes(3).toString("hex");
|
|
63
|
+
const modelHash = createHash("sha1").update(model).digest("hex").slice(0, 8);
|
|
64
|
+
return `${provider}-${safeModelSlug(model)}-${modelHash}-${timestamp}-${index}-${suffix}${extensionFromMime(mimeType)}`;
|
|
65
|
+
}
|
|
66
|
+
async function resolveOutputPath(outputPath, result, image, index) {
|
|
67
|
+
const target = path.resolve(outputPath ?? defaultOutputDir());
|
|
68
|
+
if (outputPath) {
|
|
69
|
+
try {
|
|
70
|
+
const info = await stat(target);
|
|
71
|
+
if (info.isDirectory()) {
|
|
72
|
+
return path.join(target, generatedFileName(result.provider, result.model, image.mimeType, index));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
if (!path.extname(target)) {
|
|
77
|
+
await mkdir(target, { recursive: true });
|
|
78
|
+
return path.join(target, generatedFileName(result.provider, result.model, image.mimeType, index));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
82
|
+
if (result.images.length === 1) {
|
|
83
|
+
return target;
|
|
84
|
+
}
|
|
85
|
+
const parsed = path.parse(target);
|
|
86
|
+
return path.join(parsed.dir, `${parsed.name}-${index}${parsed.ext || extensionFromMime(image.mimeType)}`);
|
|
87
|
+
}
|
|
88
|
+
await mkdir(target, { recursive: true });
|
|
89
|
+
return path.join(target, generatedFileName(result.provider, result.model, image.mimeType, index));
|
|
90
|
+
}
|
|
91
|
+
export async function persistImages(result, outputPath) {
|
|
92
|
+
const images = await Promise.all(result.images.map(async (image, index) => {
|
|
93
|
+
const filePath = await resolveOutputPath(outputPath, result, image, index);
|
|
94
|
+
await writeFile(filePath, Buffer.from(image.data, "base64"));
|
|
95
|
+
return { ...image, path: filePath };
|
|
96
|
+
}));
|
|
97
|
+
return { ...result, images };
|
|
98
|
+
}
|
|
99
|
+
export function buildToolContent(result, returnMode, inlineMaxBytes) {
|
|
100
|
+
const summary = {
|
|
101
|
+
provider: result.provider,
|
|
102
|
+
model: result.model,
|
|
103
|
+
images: result.images.map((image) => ({
|
|
104
|
+
path: image.path,
|
|
105
|
+
url: image.url,
|
|
106
|
+
mimeType: image.mimeType,
|
|
107
|
+
bytes: image.bytes,
|
|
108
|
+
revisedPrompt: image.revisedPrompt,
|
|
109
|
+
})),
|
|
110
|
+
text: result.text,
|
|
111
|
+
metadata: result.metadata,
|
|
112
|
+
};
|
|
113
|
+
const content = [{ type: "text", text: JSON.stringify(summary, null, 2) }];
|
|
114
|
+
if (returnMode === "inline" || returnMode === "both") {
|
|
115
|
+
for (const image of result.images) {
|
|
116
|
+
if (image.bytes > inlineMaxBytes) {
|
|
117
|
+
throw new UserInputError(`Image is ${image.bytes} bytes, above inline_max_bytes ${inlineMaxBytes}. Use return_mode "file" or raise inline_max_bytes.`);
|
|
118
|
+
}
|
|
119
|
+
content.push({ type: "image", data: image.data, mimeType: image.mimeType });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return content;
|
|
123
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import { formatError } from "./errors.js";
|
|
6
|
+
import { buildToolContent, persistImages } from "./files.js";
|
|
7
|
+
import { generateGoogleImage } from "./providers/google.js";
|
|
8
|
+
import { generateOpenAIImage } from "./providers/openai.js";
|
|
9
|
+
import { generateXAIImage } from "./providers/xai.js";
|
|
10
|
+
import { googleInputShape, googleParamsSchema, openAIInputShape, openAIParamsSchema, xaiInputShape, xaiParamsSchema, } from "./schemas.js";
|
|
11
|
+
async function finalizeResult(result, outputPath, returnMode, inlineMaxBytes) {
|
|
12
|
+
const shouldPersist = returnMode === "file" || returnMode === "both";
|
|
13
|
+
const finalResult = shouldPersist ? await persistImages(result, outputPath) : result;
|
|
14
|
+
return {
|
|
15
|
+
content: buildToolContent(finalResult, returnMode, inlineMaxBytes),
|
|
16
|
+
structuredContent: {
|
|
17
|
+
provider: finalResult.provider,
|
|
18
|
+
model: finalResult.model,
|
|
19
|
+
images: finalResult.images.map(({ data: _data, ...image }) => image),
|
|
20
|
+
metadata: finalResult.metadata,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function errorResult(error) {
|
|
25
|
+
return {
|
|
26
|
+
isError: true,
|
|
27
|
+
content: [{ type: "text", text: formatError(error) }],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function createServer() {
|
|
31
|
+
const server = new McpServer({
|
|
32
|
+
name: "image-router-mcp",
|
|
33
|
+
version: "0.1.0",
|
|
34
|
+
});
|
|
35
|
+
server.registerTool("openai_generate_image", {
|
|
36
|
+
title: "OpenAI image generation and editing",
|
|
37
|
+
description: "Generate or edit images with OpenAI image models. Model is a flexible string so newly released image models are not blocked.",
|
|
38
|
+
inputSchema: openAIInputShape,
|
|
39
|
+
}, async (rawParams) => {
|
|
40
|
+
try {
|
|
41
|
+
const params = openAIParamsSchema.parse(rawParams);
|
|
42
|
+
const result = await generateOpenAIImage(params);
|
|
43
|
+
return await finalizeResult(result, params.output_path, params.return_mode, params.inline_max_bytes);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
return errorResult(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
server.registerTool("google_generate_image", {
|
|
50
|
+
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 model IDs.",
|
|
52
|
+
inputSchema: googleInputShape,
|
|
53
|
+
}, async (rawParams) => {
|
|
54
|
+
try {
|
|
55
|
+
const params = googleParamsSchema.parse(rawParams);
|
|
56
|
+
const result = await generateGoogleImage(params);
|
|
57
|
+
return await finalizeResult(result, params.output_path, params.return_mode, params.inline_max_bytes);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
return errorResult(error);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
server.registerTool("xai_generate_image", {
|
|
64
|
+
title: "xAI/Grok image generation and editing",
|
|
65
|
+
description: "Generate or edit images with xAI Grok Imagine models. Local input_images are sent as data URIs for the xAI JSON image edit endpoint.",
|
|
66
|
+
inputSchema: xaiInputShape,
|
|
67
|
+
}, async (rawParams) => {
|
|
68
|
+
try {
|
|
69
|
+
const params = xaiParamsSchema.parse(rawParams);
|
|
70
|
+
const result = await generateXAIImage(params);
|
|
71
|
+
return await finalizeResult(result, params.output_path, params.return_mode, params.inline_max_bytes);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
return errorResult(error);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return server;
|
|
78
|
+
}
|
|
79
|
+
export async function main() {
|
|
80
|
+
const server = createServer();
|
|
81
|
+
await server.connect(new StdioServerTransport());
|
|
82
|
+
}
|
|
83
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
84
|
+
main().catch((error) => {
|
|
85
|
+
console.error(formatError(error));
|
|
86
|
+
process.exitCode = 1;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GoogleGenAI } from "@google/genai";
|
|
2
|
+
import type { GoogleParams } from "../schemas.js";
|
|
3
|
+
import type { ProviderResult } from "../types.js";
|
|
4
|
+
export type GoogleImageClient = {
|
|
5
|
+
models: {
|
|
6
|
+
generateContent: GoogleGenAI["models"]["generateContent"];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export declare function setGoogleClientForTests(client: GoogleImageClient | undefined): void;
|
|
10
|
+
export declare function generateGoogleImage(params: GoogleParams, client?: GoogleImageClient): Promise<ProviderResult>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { GoogleGenAI, Modality } from "@google/genai";
|
|
2
|
+
import { bytesFromBase64, loadInputImages } from "../files.js";
|
|
3
|
+
import { requireEnv } from "../errors.js";
|
|
4
|
+
let googleClient;
|
|
5
|
+
function getGoogleClient() {
|
|
6
|
+
if (!googleClient) {
|
|
7
|
+
googleClient = new GoogleGenAI({ apiKey: requireEnv(["GOOGLE_API_KEY", "GEMINI_API_KEY"]) });
|
|
8
|
+
}
|
|
9
|
+
return googleClient;
|
|
10
|
+
}
|
|
11
|
+
export function setGoogleClientForTests(client) {
|
|
12
|
+
googleClient = client;
|
|
13
|
+
}
|
|
14
|
+
export async function generateGoogleImage(params, client = getGoogleClient()) {
|
|
15
|
+
const model = process.env.DEFAULT_GOOGLE_IMAGE_MODEL ?? params.model;
|
|
16
|
+
const inputImages = await loadInputImages(params.input_images);
|
|
17
|
+
const parts = [
|
|
18
|
+
{ text: params.prompt },
|
|
19
|
+
...inputImages.map((image) => ({
|
|
20
|
+
inlineData: {
|
|
21
|
+
data: image.data,
|
|
22
|
+
mimeType: image.mimeType,
|
|
23
|
+
},
|
|
24
|
+
})),
|
|
25
|
+
];
|
|
26
|
+
const response = await client.models.generateContent({
|
|
27
|
+
model,
|
|
28
|
+
contents: [{ role: "user", parts }],
|
|
29
|
+
config: {
|
|
30
|
+
responseModalities: [Modality.IMAGE, Modality.TEXT],
|
|
31
|
+
...(params.aspect_ratio || params.image_size
|
|
32
|
+
? {
|
|
33
|
+
imageConfig: {
|
|
34
|
+
...(params.aspect_ratio ? { aspectRatio: params.aspect_ratio } : {}),
|
|
35
|
+
...(params.image_size ? { imageSize: params.image_size } : {}),
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
: {}),
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const responseParts = response.candidates?.[0]?.content?.parts ?? [];
|
|
42
|
+
const images = responseParts
|
|
43
|
+
.filter((part) => part.inlineData?.data)
|
|
44
|
+
.map((part) => {
|
|
45
|
+
const data = part.inlineData.data;
|
|
46
|
+
const mimeType = part.inlineData.mimeType ?? "image/png";
|
|
47
|
+
return { data, mimeType, bytes: bytesFromBase64(data) };
|
|
48
|
+
});
|
|
49
|
+
const text = responseParts
|
|
50
|
+
.map((part) => part.text)
|
|
51
|
+
.filter((value) => Boolean(value))
|
|
52
|
+
.join("\n");
|
|
53
|
+
if (images.length === 0) {
|
|
54
|
+
throw new Error(text || response.promptFeedback?.blockReasonMessage || "Google response did not include generated image data.");
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
provider: "google",
|
|
58
|
+
model,
|
|
59
|
+
images,
|
|
60
|
+
text: text || undefined,
|
|
61
|
+
metadata: {
|
|
62
|
+
modelVersion: response.modelVersion,
|
|
63
|
+
responseId: response.responseId,
|
|
64
|
+
usageMetadata: response.usageMetadata,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import type { OpenAIParams } from "../schemas.js";
|
|
3
|
+
import type { ProviderResult } from "../types.js";
|
|
4
|
+
export type OpenAIImageClient = {
|
|
5
|
+
images: {
|
|
6
|
+
generate: OpenAI["images"]["generate"];
|
|
7
|
+
edit: OpenAI["images"]["edit"];
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export declare function setOpenAIClientForTests(client: OpenAIImageClient | undefined): void;
|
|
11
|
+
export declare function generateOpenAIImage(params: OpenAIParams, client?: OpenAIImageClient): Promise<ProviderResult>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
import { bytesFromBase64, base64FromUrl } from "../files.js";
|
|
4
|
+
import { requireEnv, UserInputError } from "../errors.js";
|
|
5
|
+
let openAIClient;
|
|
6
|
+
function getOpenAIClient() {
|
|
7
|
+
if (!openAIClient) {
|
|
8
|
+
openAIClient = new OpenAI({ apiKey: requireEnv(["OPENAI_API_KEY"]) });
|
|
9
|
+
}
|
|
10
|
+
return openAIClient;
|
|
11
|
+
}
|
|
12
|
+
export function setOpenAIClientForTests(client) {
|
|
13
|
+
openAIClient = client;
|
|
14
|
+
}
|
|
15
|
+
async function imageFromOpenAIItem(item, mimeType) {
|
|
16
|
+
if (item.b64_json) {
|
|
17
|
+
return {
|
|
18
|
+
data: item.b64_json,
|
|
19
|
+
mimeType,
|
|
20
|
+
bytes: bytesFromBase64(item.b64_json),
|
|
21
|
+
revisedPrompt: item.revised_prompt ?? undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (item.url) {
|
|
25
|
+
return { ...(await base64FromUrl(item.url)), url: item.url, revisedPrompt: item.revised_prompt ?? undefined };
|
|
26
|
+
}
|
|
27
|
+
throw new Error("OpenAI response did not include b64_json or url image data.");
|
|
28
|
+
}
|
|
29
|
+
export async function generateOpenAIImage(params, client = getOpenAIClient()) {
|
|
30
|
+
const model = process.env.DEFAULT_OPENAI_IMAGE_MODEL ?? params.model;
|
|
31
|
+
const outputFormat = params.output_format;
|
|
32
|
+
const inputImages = params.input_images ?? [];
|
|
33
|
+
const common = {
|
|
34
|
+
model,
|
|
35
|
+
prompt: params.prompt,
|
|
36
|
+
n: params.n,
|
|
37
|
+
size: params.size,
|
|
38
|
+
background: params.background,
|
|
39
|
+
output_format: outputFormat,
|
|
40
|
+
response_format: "b64_json",
|
|
41
|
+
user: params.user,
|
|
42
|
+
};
|
|
43
|
+
const response = inputImages.length > 0
|
|
44
|
+
? await (async () => {
|
|
45
|
+
if (params.quality === "hd") {
|
|
46
|
+
throw new UserInputError('OpenAI image edits do not support quality "hd". Use standard, auto, low, medium, or high.');
|
|
47
|
+
}
|
|
48
|
+
return client.images.edit({
|
|
49
|
+
...common,
|
|
50
|
+
quality: params.quality,
|
|
51
|
+
image: inputImages.length === 1
|
|
52
|
+
? createReadStream(inputImages[0])
|
|
53
|
+
: inputImages.map((imagePath) => createReadStream(imagePath)),
|
|
54
|
+
});
|
|
55
|
+
})()
|
|
56
|
+
: await client.images.generate({
|
|
57
|
+
...common,
|
|
58
|
+
quality: params.quality,
|
|
59
|
+
moderation: params.moderation,
|
|
60
|
+
style: params.style,
|
|
61
|
+
});
|
|
62
|
+
const data = "data" in response ? response.data ?? [] : [];
|
|
63
|
+
if (data.length === 0) {
|
|
64
|
+
throw new Error("OpenAI response did not include generated images.");
|
|
65
|
+
}
|
|
66
|
+
const mimeType = `image/${outputFormat === "jpeg" ? "jpeg" : outputFormat}`;
|
|
67
|
+
return {
|
|
68
|
+
provider: "openai",
|
|
69
|
+
model,
|
|
70
|
+
images: await Promise.all(data.map((item) => imageFromOpenAIItem(item, mimeType))),
|
|
71
|
+
metadata: {
|
|
72
|
+
created: "created" in response ? response.created : undefined,
|
|
73
|
+
usage: "usage" in response ? response.usage : undefined,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { base64FromUrl, bytesFromBase64, loadInputImages, toDataUri } from "../files.js";
|
|
2
|
+
import { requireEnv } from "../errors.js";
|
|
3
|
+
const XAI_BASE_URL = "https://api.x.ai/v1";
|
|
4
|
+
async function imageFromXAIItem(item) {
|
|
5
|
+
if (item.b64_json) {
|
|
6
|
+
return {
|
|
7
|
+
data: item.b64_json,
|
|
8
|
+
mimeType: item.mime_type ?? "image/png",
|
|
9
|
+
bytes: bytesFromBase64(item.b64_json),
|
|
10
|
+
url: item.url ?? undefined,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (item.url) {
|
|
14
|
+
return { ...(await base64FromUrl(item.url)), url: item.url };
|
|
15
|
+
}
|
|
16
|
+
throw new Error("xAI response did not include b64_json or url image data.");
|
|
17
|
+
}
|
|
18
|
+
export async function generateXAIImage(params, fetchImpl = fetch) {
|
|
19
|
+
const model = process.env.DEFAULT_XAI_IMAGE_MODEL ?? params.model;
|
|
20
|
+
const inputImages = await loadInputImages(params.input_images);
|
|
21
|
+
const isEdit = inputImages.length > 0;
|
|
22
|
+
const body = {
|
|
23
|
+
model,
|
|
24
|
+
prompt: params.prompt,
|
|
25
|
+
n: params.n,
|
|
26
|
+
aspect_ratio: params.aspect_ratio,
|
|
27
|
+
resolution: params.resolution,
|
|
28
|
+
response_format: "b64_json",
|
|
29
|
+
user: params.user,
|
|
30
|
+
};
|
|
31
|
+
if (isEdit) {
|
|
32
|
+
if (inputImages.length === 1) {
|
|
33
|
+
body.image = { url: toDataUri(inputImages[0]), type: "image_url" };
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
body.images = inputImages.map((image) => ({ url: toDataUri(image), type: "image_url" }));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const response = await fetchImpl(`${XAI_BASE_URL}/images/${isEdit ? "edits" : "generations"}`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Bearer ${requireEnv(["XAI_API_KEY"])}`,
|
|
43
|
+
"Content-Type": "application/json",
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify(body),
|
|
46
|
+
});
|
|
47
|
+
const responseBody = (await response.json().catch(() => ({})));
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new Error(`xAI image request failed (${response.status}): ${JSON.stringify(responseBody)}`);
|
|
50
|
+
}
|
|
51
|
+
const data = "data" in responseBody ? responseBody.data ?? [] : [];
|
|
52
|
+
if (data.length === 0) {
|
|
53
|
+
throw new Error("xAI response did not include generated images.");
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
provider: "xai",
|
|
57
|
+
model,
|
|
58
|
+
images: await Promise.all(data.map(imageFromXAIItem)),
|
|
59
|
+
metadata: {
|
|
60
|
+
usage: "usage" in responseBody ? responseBody.usage : undefined,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const DEFAULT_INLINE_MAX_BYTES: number;
|
|
3
|
+
export declare const returnModeSchema: z.ZodDefault<z.ZodEnum<{
|
|
4
|
+
file: "file";
|
|
5
|
+
inline: "inline";
|
|
6
|
+
both: "both";
|
|
7
|
+
}>>;
|
|
8
|
+
export declare const openAIInputShape: {
|
|
9
|
+
readonly model: z.ZodDefault<z.ZodString>;
|
|
10
|
+
readonly size: z.ZodOptional<z.ZodString>;
|
|
11
|
+
readonly quality: z.ZodOptional<z.ZodEnum<{
|
|
12
|
+
auto: "auto";
|
|
13
|
+
low: "low";
|
|
14
|
+
medium: "medium";
|
|
15
|
+
high: "high";
|
|
16
|
+
standard: "standard";
|
|
17
|
+
hd: "hd";
|
|
18
|
+
}>>;
|
|
19
|
+
readonly background: z.ZodOptional<z.ZodEnum<{
|
|
20
|
+
auto: "auto";
|
|
21
|
+
transparent: "transparent";
|
|
22
|
+
opaque: "opaque";
|
|
23
|
+
}>>;
|
|
24
|
+
readonly output_format: z.ZodDefault<z.ZodEnum<{
|
|
25
|
+
png: "png";
|
|
26
|
+
jpeg: "jpeg";
|
|
27
|
+
webp: "webp";
|
|
28
|
+
}>>;
|
|
29
|
+
readonly moderation: z.ZodOptional<z.ZodEnum<{
|
|
30
|
+
auto: "auto";
|
|
31
|
+
low: "low";
|
|
32
|
+
}>>;
|
|
33
|
+
readonly style: z.ZodOptional<z.ZodEnum<{
|
|
34
|
+
vivid: "vivid";
|
|
35
|
+
natural: "natural";
|
|
36
|
+
}>>;
|
|
37
|
+
readonly prompt: z.ZodString;
|
|
38
|
+
readonly output_path: z.ZodOptional<z.ZodString>;
|
|
39
|
+
readonly input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
40
|
+
readonly return_mode: z.ZodDefault<z.ZodEnum<{
|
|
41
|
+
file: "file";
|
|
42
|
+
inline: "inline";
|
|
43
|
+
both: "both";
|
|
44
|
+
}>>;
|
|
45
|
+
readonly inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
46
|
+
readonly n: z.ZodOptional<z.ZodNumber>;
|
|
47
|
+
readonly user: z.ZodOptional<z.ZodString>;
|
|
48
|
+
};
|
|
49
|
+
export declare const googleInputShape: {
|
|
50
|
+
readonly model: z.ZodDefault<z.ZodString>;
|
|
51
|
+
readonly aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
52
|
+
readonly image_size: z.ZodOptional<z.ZodEnum<{
|
|
53
|
+
"1K": "1K";
|
|
54
|
+
"2K": "2K";
|
|
55
|
+
"4K": "4K";
|
|
56
|
+
}>>;
|
|
57
|
+
readonly prompt: z.ZodString;
|
|
58
|
+
readonly output_path: z.ZodOptional<z.ZodString>;
|
|
59
|
+
readonly input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
60
|
+
readonly return_mode: z.ZodDefault<z.ZodEnum<{
|
|
61
|
+
file: "file";
|
|
62
|
+
inline: "inline";
|
|
63
|
+
both: "both";
|
|
64
|
+
}>>;
|
|
65
|
+
readonly inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
66
|
+
readonly n: z.ZodOptional<z.ZodNumber>;
|
|
67
|
+
readonly user: z.ZodOptional<z.ZodString>;
|
|
68
|
+
};
|
|
69
|
+
export declare const xaiInputShape: {
|
|
70
|
+
readonly model: z.ZodDefault<z.ZodString>;
|
|
71
|
+
readonly aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
72
|
+
readonly resolution: z.ZodOptional<z.ZodEnum<{
|
|
73
|
+
"1k": "1k";
|
|
74
|
+
"2k": "2k";
|
|
75
|
+
}>>;
|
|
76
|
+
readonly prompt: z.ZodString;
|
|
77
|
+
readonly output_path: z.ZodOptional<z.ZodString>;
|
|
78
|
+
readonly input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
79
|
+
readonly return_mode: z.ZodDefault<z.ZodEnum<{
|
|
80
|
+
file: "file";
|
|
81
|
+
inline: "inline";
|
|
82
|
+
both: "both";
|
|
83
|
+
}>>;
|
|
84
|
+
readonly inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
85
|
+
readonly n: z.ZodOptional<z.ZodNumber>;
|
|
86
|
+
readonly user: z.ZodOptional<z.ZodString>;
|
|
87
|
+
};
|
|
88
|
+
export declare const openAIParamsSchema: z.ZodObject<{
|
|
89
|
+
model: z.ZodDefault<z.ZodString>;
|
|
90
|
+
size: z.ZodOptional<z.ZodString>;
|
|
91
|
+
quality: z.ZodOptional<z.ZodEnum<{
|
|
92
|
+
auto: "auto";
|
|
93
|
+
low: "low";
|
|
94
|
+
medium: "medium";
|
|
95
|
+
high: "high";
|
|
96
|
+
standard: "standard";
|
|
97
|
+
hd: "hd";
|
|
98
|
+
}>>;
|
|
99
|
+
background: z.ZodOptional<z.ZodEnum<{
|
|
100
|
+
auto: "auto";
|
|
101
|
+
transparent: "transparent";
|
|
102
|
+
opaque: "opaque";
|
|
103
|
+
}>>;
|
|
104
|
+
output_format: z.ZodDefault<z.ZodEnum<{
|
|
105
|
+
png: "png";
|
|
106
|
+
jpeg: "jpeg";
|
|
107
|
+
webp: "webp";
|
|
108
|
+
}>>;
|
|
109
|
+
moderation: z.ZodOptional<z.ZodEnum<{
|
|
110
|
+
auto: "auto";
|
|
111
|
+
low: "low";
|
|
112
|
+
}>>;
|
|
113
|
+
style: z.ZodOptional<z.ZodEnum<{
|
|
114
|
+
vivid: "vivid";
|
|
115
|
+
natural: "natural";
|
|
116
|
+
}>>;
|
|
117
|
+
prompt: z.ZodString;
|
|
118
|
+
output_path: z.ZodOptional<z.ZodString>;
|
|
119
|
+
input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
120
|
+
return_mode: z.ZodDefault<z.ZodEnum<{
|
|
121
|
+
file: "file";
|
|
122
|
+
inline: "inline";
|
|
123
|
+
both: "both";
|
|
124
|
+
}>>;
|
|
125
|
+
inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
126
|
+
n: z.ZodOptional<z.ZodNumber>;
|
|
127
|
+
user: z.ZodOptional<z.ZodString>;
|
|
128
|
+
}, z.core.$strict>;
|
|
129
|
+
export declare const googleParamsSchema: z.ZodObject<{
|
|
130
|
+
model: z.ZodDefault<z.ZodString>;
|
|
131
|
+
aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
132
|
+
image_size: z.ZodOptional<z.ZodEnum<{
|
|
133
|
+
"1K": "1K";
|
|
134
|
+
"2K": "2K";
|
|
135
|
+
"4K": "4K";
|
|
136
|
+
}>>;
|
|
137
|
+
prompt: z.ZodString;
|
|
138
|
+
output_path: z.ZodOptional<z.ZodString>;
|
|
139
|
+
input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
140
|
+
return_mode: z.ZodDefault<z.ZodEnum<{
|
|
141
|
+
file: "file";
|
|
142
|
+
inline: "inline";
|
|
143
|
+
both: "both";
|
|
144
|
+
}>>;
|
|
145
|
+
inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
146
|
+
n: z.ZodOptional<z.ZodNumber>;
|
|
147
|
+
user: z.ZodOptional<z.ZodString>;
|
|
148
|
+
}, z.core.$strict>;
|
|
149
|
+
export declare const xaiParamsSchema: z.ZodObject<{
|
|
150
|
+
model: z.ZodDefault<z.ZodString>;
|
|
151
|
+
aspect_ratio: z.ZodOptional<z.ZodString>;
|
|
152
|
+
resolution: z.ZodOptional<z.ZodEnum<{
|
|
153
|
+
"1k": "1k";
|
|
154
|
+
"2k": "2k";
|
|
155
|
+
}>>;
|
|
156
|
+
prompt: z.ZodString;
|
|
157
|
+
output_path: z.ZodOptional<z.ZodString>;
|
|
158
|
+
input_images: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
159
|
+
return_mode: z.ZodDefault<z.ZodEnum<{
|
|
160
|
+
file: "file";
|
|
161
|
+
inline: "inline";
|
|
162
|
+
both: "both";
|
|
163
|
+
}>>;
|
|
164
|
+
inline_max_bytes: z.ZodDefault<z.ZodNumber>;
|
|
165
|
+
n: z.ZodOptional<z.ZodNumber>;
|
|
166
|
+
user: z.ZodOptional<z.ZodString>;
|
|
167
|
+
}, z.core.$strict>;
|
|
168
|
+
export type ReturnMode = z.infer<typeof returnModeSchema>;
|
|
169
|
+
export type OpenAIParams = z.infer<typeof openAIParamsSchema>;
|
|
170
|
+
export type GoogleParams = z.infer<typeof googleParamsSchema>;
|
|
171
|
+
export type XAIParams = z.infer<typeof xaiParamsSchema>;
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const DEFAULT_INLINE_MAX_BYTES = 4 * 1024 * 1024;
|
|
3
|
+
export const returnModeSchema = z
|
|
4
|
+
.enum(["file", "inline", "both"])
|
|
5
|
+
.default("file")
|
|
6
|
+
.describe("How generated images are returned. Use file for large images; inline embeds base64 image content in the MCP response.");
|
|
7
|
+
const nonEmptyString = z.string().trim().min(1);
|
|
8
|
+
const commonImageShape = {
|
|
9
|
+
prompt: nonEmptyString.describe("Image generation or editing instructions."),
|
|
10
|
+
model: nonEmptyString.optional().describe("Provider model ID. Any string is allowed so newly released models are not blocked."),
|
|
11
|
+
output_path: nonEmptyString.optional().describe("File path or directory for saved output. Defaults to DEFAULT_OUTPUT_DIR or ./generated-images."),
|
|
12
|
+
input_images: z.array(nonEmptyString).optional().describe("Local image paths used as edit/reference inputs."),
|
|
13
|
+
return_mode: returnModeSchema,
|
|
14
|
+
inline_max_bytes: z
|
|
15
|
+
.number()
|
|
16
|
+
.int()
|
|
17
|
+
.positive()
|
|
18
|
+
.default(DEFAULT_INLINE_MAX_BYTES)
|
|
19
|
+
.describe("Maximum bytes allowed for each inline image response."),
|
|
20
|
+
n: z.number().int().min(1).max(10).optional().describe("Number of images to generate when the provider supports it."),
|
|
21
|
+
user: nonEmptyString.optional().describe("Optional end-user identifier for provider abuse monitoring."),
|
|
22
|
+
};
|
|
23
|
+
export const openAIInputShape = {
|
|
24
|
+
...commonImageShape,
|
|
25
|
+
model: nonEmptyString.default("gpt-image-2").describe("OpenAI image model ID. Defaults to gpt-image-2."),
|
|
26
|
+
size: nonEmptyString.optional().describe("OpenAI size, such as auto, 1024x1024, 1536x1024, or any supported WIDTHxHEIGHT."),
|
|
27
|
+
quality: z.enum(["auto", "low", "medium", "high", "standard", "hd"]).optional().describe("OpenAI quality."),
|
|
28
|
+
background: z.enum(["transparent", "opaque", "auto"]).optional().describe("OpenAI background option."),
|
|
29
|
+
output_format: z.enum(["png", "jpeg", "webp"]).default("png").describe("Output image format."),
|
|
30
|
+
moderation: z.enum(["low", "auto"]).optional().describe("OpenAI moderation setting for supported models."),
|
|
31
|
+
style: z.enum(["vivid", "natural"]).optional().describe("DALL-E 3 style option."),
|
|
32
|
+
};
|
|
33
|
+
export const googleInputShape = {
|
|
34
|
+
...commonImageShape,
|
|
35
|
+
model: nonEmptyString.default("gemini-3.1-flash-image-preview").describe("Google image model ID. Defaults to Nano Banana 2."),
|
|
36
|
+
aspect_ratio: nonEmptyString.optional().describe("Aspect ratio, for example 1:1, 16:9, 9:16, 4:3, or 3:2."),
|
|
37
|
+
image_size: z.enum(["1K", "2K", "4K"]).optional().describe("Google image size where supported."),
|
|
38
|
+
};
|
|
39
|
+
export const xaiInputShape = {
|
|
40
|
+
...commonImageShape,
|
|
41
|
+
model: nonEmptyString.default("grok-imagine-image-quality").describe("xAI/Grok image model ID."),
|
|
42
|
+
aspect_ratio: nonEmptyString.optional().describe("Aspect ratio, for example 1:1, 16:9, 9:16, 3:2, or auto."),
|
|
43
|
+
resolution: z.enum(["1k", "2k"]).optional().describe("xAI image resolution."),
|
|
44
|
+
};
|
|
45
|
+
export const openAIParamsSchema = z.object(openAIInputShape).strict();
|
|
46
|
+
export const googleParamsSchema = z.object(googleInputShape).strict();
|
|
47
|
+
export const xaiParamsSchema = z.object(xaiInputShape).strict();
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type LoadedImage = {
|
|
2
|
+
path: string;
|
|
3
|
+
data: string;
|
|
4
|
+
mimeType: string;
|
|
5
|
+
bytes: number;
|
|
6
|
+
};
|
|
7
|
+
export type GeneratedImage = {
|
|
8
|
+
data: string;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
path?: string;
|
|
11
|
+
url?: string;
|
|
12
|
+
bytes: number;
|
|
13
|
+
revisedPrompt?: string;
|
|
14
|
+
};
|
|
15
|
+
export type ProviderResult = {
|
|
16
|
+
provider: "openai" | "google" | "xai";
|
|
17
|
+
model: string;
|
|
18
|
+
images: GeneratedImage[];
|
|
19
|
+
text?: string;
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
21
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "image-router-mcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP server for routing image generation and editing across xAI, Google Gemini/Nano Banana, and OpenAI.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"image-router-mcp": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc -p tsconfig.json",
|
|
18
|
+
"dev": "tsx src/index.ts",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
21
|
+
"ci": "npm run typecheck && npm test && npm run build"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"mcp",
|
|
25
|
+
"model-context-protocol",
|
|
26
|
+
"image-generation",
|
|
27
|
+
"openai",
|
|
28
|
+
"gemini",
|
|
29
|
+
"nano-banana",
|
|
30
|
+
"grok",
|
|
31
|
+
"xai"
|
|
32
|
+
],
|
|
33
|
+
"author": "",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@google/genai": "2.6.0",
|
|
37
|
+
"@modelcontextprotocol/sdk": "1.29.0",
|
|
38
|
+
"openai": "6.39.0",
|
|
39
|
+
"zod": "4.4.3"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "25.9.1",
|
|
43
|
+
"tsx": "4.22.3",
|
|
44
|
+
"typescript": "6.0.3",
|
|
45
|
+
"vitest": "4.1.7"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/JiaDians/image-router-mcp.git"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/JiaDians/image-router-mcp/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/JiaDians/image-router-mcp#readme"
|
|
55
|
+
}
|