summaryrefslogtreecommitdiff
path: root/doc/mcp.md
blob: 1fea44a72d1a1be481675af374dde24b8820689e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# MCPツール仕様

## 1. 概要

`generate-image-sd15-mcp` は`stdio`または`streamable-http`トランスポートで動作し、
Stable Diffusion 1.xによる画像生成を行う次の3ツールを公開する。

| ツール | 用途 |
|---|---|
| `generate_image` | テキストから画像を生成する |
| `generate_guided_image` | DepthまたはOpenPoseで誘導して画像を生成する |
| `inpaint_image` | 元画像のmask指定領域を再生成する |

全ツールの出力は1枚のPNGである。生成パラメータ、モデル、LoRA、ControlNet強度、seedは
ツール入力には公開しない。これらは設定ファイルまたはサーバ内部で管理する。

## 2. 共通仕様

### 2.1 Prompt

| 項目 | 仕様 |
|---|---|
| `positive_prompt` | 必須の文字列。前後の空白を除去した結果が空の場合はエラー |
| `negative_prompt` | 任意の文字列。省略または`null`の場合は空文字列 |
| 最大長 | それぞれ10,000文字 |

`[prompt]`設定でbase promptを指定している場合、LLMからのプロンプトと結合してから
pipelineへ渡す。positiveは`base, user`の順でカンマ区切り、negativeは`base\nuser`の順で
改行区切りとなる。base promptはMCPツール入力には公開しない。

### 2.2 Image size

`size`には次の列挙値だけを指定できる。省略または`null`の場合は
`generation.default_size`が使用される。

| 値 | 幅 | 高さ |
|---|---:|---:|
| `square_512` | 512 | 512 |
| `landscape_512` | 512 | 384 |
| `portrait_512` | 384 | 512 |

任意の`width`や`height`は指定できない。

### 2.3 入力画像

`input_image`と`mask_image`は、次のいずれかの文字列で指定する。

- PNGまたはJPEGバイナリをBase64エンコードした文字列
- `data:image/png;base64,...`形式のdata URI
- `data:image/jpeg;base64,...`または`data:image/jpg;base64,...`形式のdata URI

画像入力には次の制限がある。

| 項目 | 制限 |
|---|---|
| 画像形式 | PNG、JPEGのみ |
| Base64文字列長 | 36,000,000文字以下 |
| pixel数 | 40,000,000 pixel以下 |
| Base64 | 改行やBase64以外の文字を含まない正規のBase64 |

入力画像は生成サイズにリサイズされる。通常画像にはLanczos、maskにはnearest-neighborが
使用される。

### 2.4 出力

成功時はMCPの`ImageContent`を1件返す。

```json
{
  "type": "image",
  "data": "iVBORw0KGgoAAA...",
  "mimeType": "image/png"
}
```

`data`はMCP SDKによってBase64エンコードされる。PNGには次のtext metadataが埋め込まれる。

| Key | 内容 |
|---|---|
| `seed` | サーバが生成したseed |
| `size` | 使用したサイズID |
| `mode` | `text_to_image`、`guided_image`、`inpaint_image`のいずれか |
| `timestamp` | UTCのISO 8601生成日時 |
| `elapsed_seconds` | キュー待ちを含む処理時間(秒) |

出力ファイルの保存が有効な場合も、MCPレスポンスには同じPNGが返される。

## 3. `generate_image`

設定済みのbase checkpoint、VAE、LoRAを使ってText-to-Imageを実行する。

### 入力

| 名前 | 型 | 必須 | 既定値 |
|---|---|---:|---|
| `positive_prompt` | string | Yes | なし |
| `negative_prompt` | string \| null | No | `null`(内部では空文字列) |
| `size` | size enum \| null | No | `generation.default_size` |

### 呼び出し例

```json
{
  "positive_prompt": "a small lighthouse on a rocky coast, sunset",
  "negative_prompt": "low quality, blurry",
  "size": "landscape_512"
}
```

### JSON Schema

```json
{
  "type": "object",
  "title": "generate_imageArguments",
  "properties": {
    "positive_prompt": {
      "type": "string",
      "title": "Positive Prompt"
    },
    "negative_prompt": {
      "anyOf": [{"type": "string"}, {"type": "null"}],
      "default": null,
      "title": "Negative Prompt"
    },
    "size": {
      "anyOf": [
        {
          "type": "string",
          "enum": ["square_512", "landscape_512", "portrait_512"]
        },
        {"type": "null"}
      ],
      "default": null,
      "title": "Size"
    }
  },
  "required": ["positive_prompt"]
}
```

## 4. `generate_guided_image`

入力画像を固定preprocessorに通し、対応するControlNetで画像生成を誘導する。

### 入力

| 名前 | 型 | 必須 | 既定値 |
|---|---|---:|---|
| `positive_prompt` | string | Yes | なし |
| `control_mode` | control mode enum | Yes | なし |
| `input_image` | string | Yes | なし |
| `negative_prompt` | string \| null | No | `null`(内部では空文字列) |
| `size` | size enum \| null | No | `generation.default_size` |

`control_mode`の意味は次のとおり。

| 値 | ControlNet設定 | 前処理 |
|---|---|---|
| `depth` | `controlnet.depth` | MiDaS depth |
| `openpose` | `controlnet.openpose` | body pose |
| `openpose_full` | `controlnet.openpose` | body、hand、face pose |

要求したControlNetが設定で無効の場合はツールエラーになる。`openpose`と
`openpose_full`は同じControlNetモデルを共有する。

### 呼び出し例

```json
{
  "positive_prompt": "a dancer wearing a red dress, studio lighting",
  "control_mode": "openpose_full",
  "input_image": "data:image/png;base64,iVBORw0KGgoAAA...",
  "negative_prompt": "deformed hands",
  "size": "portrait_512"
}
```

### JSON Schema

```json
{
  "type": "object",
  "title": "generate_guided_imageArguments",
  "properties": {
    "positive_prompt": {
      "type": "string",
      "title": "Positive Prompt"
    },
    "control_mode": {
      "type": "string",
      "enum": ["depth", "openpose", "openpose_full"],
      "title": "Control Mode"
    },
    "input_image": {
      "type": "string",
      "title": "Input Image"
    },
    "negative_prompt": {
      "anyOf": [{"type": "string"}, {"type": "null"}],
      "default": null,
      "title": "Negative Prompt"
    },
    "size": {
      "anyOf": [
        {
          "type": "string",
          "enum": ["square_512", "landscape_512", "portrait_512"]
        },
        {"type": "null"}
      ],
      "default": null,
      "title": "Size"
    }
  },
  "required": ["positive_prompt", "control_mode", "input_image"]
}
```

## 5. `inpaint_image`

元画像とmask画像を使い、白い領域をControlNet Inpaintで再生成する。

### 入力

| 名前 | 型 | 必須 | 既定値 |
|---|---|---:|---|
| `positive_prompt` | string | Yes | なし |
| `input_image` | string | Yes | なし |
| `mask_image` | string | Yes | なし |
| `negative_prompt` | string \| null | No | `null`(内部では空文字列) |
| `size` | size enum \| null | No | `generation.default_size` |

`mask_image`はgrayscaleに変換される。輝度が50%を超えるpixelが再生成対象で、黒い領域は
保持対象となる。

### 呼び出し例

```json
{
  "positive_prompt": "a wooden table with a ceramic vase",
  "input_image": "data:image/png;base64,iVBORw0KGgoAAA...",
  "mask_image": "data:image/png;base64,iVBORw0KGgoAAA...",
  "negative_prompt": "artifacts",
  "size": "square_512"
}
```

### JSON Schema

```json
{
  "type": "object",
  "title": "inpaint_imageArguments",
  "properties": {
    "positive_prompt": {
      "type": "string",
      "title": "Positive Prompt"
    },
    "input_image": {
      "type": "string",
      "title": "Input Image"
    },
    "mask_image": {
      "type": "string",
      "title": "Mask Image"
    },
    "negative_prompt": {
      "anyOf": [{"type": "string"}, {"type": "null"}],
      "default": null,
      "title": "Negative Prompt"
    },
    "size": {
      "anyOf": [
        {
          "type": "string",
          "enum": ["square_512", "landscape_512", "portrait_512"]
        },
        {"type": "null"}
      ],
      "default": null,
      "title": "Size"
    }
  },
  "required": ["positive_prompt", "input_image", "mask_image"]
}
```

## 6. 実行制御とエラー

生成処理はサーバ内の単一lockで直列化される。同時に複数の要求を受信した場合、GPU推論は
1件ずつ実行される。

主なツールエラーは次のとおり。

| 分類 | 例 |
|---|---|
| 入力エラー | 空のprompt、不正なsize、不正なcontrol mode |
| 画像エラー | Base64不正、未対応形式、画像破損、上限超過 |
| 設定エラー | 要求したControlNetが無効、モデルをロードできない |
| 推論エラー | Diffusers処理失敗、出力画像数不正 |
| CUDA OOM | VRAM不足。CUDA cacheの解放を試みた後にエラーを返す |
| 保存エラー | output directoryの作成またはPNG書込み失敗 |

エラー時に画像は返されない。設定ファイル自体やbase modelが不正な場合は、MCPツールを
公開する前のサーバ起動処理が失敗する。