|
| 1 | +# API Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The MCP Server GPT Image-1 provides multiple endpoints for image generation and management. The server supports both MCP protocol communication and direct HTTP/SSE streaming. |
| 6 | + |
| 7 | +## Base URL |
| 8 | + |
| 9 | +``` |
| 10 | +http://localhost:3000 |
| 11 | +``` |
| 12 | + |
| 13 | +## Authentication |
| 14 | + |
| 15 | +All requests require an OpenAI API key, which should be provided via environment variable: |
| 16 | + |
| 17 | +```bash |
| 18 | +OPENAI_API_KEY=your-api-key-here |
| 19 | +``` |
| 20 | + |
| 21 | +## Endpoints |
| 22 | + |
| 23 | +### Health Check |
| 24 | + |
| 25 | +Check if the server is running and healthy. |
| 26 | + |
| 27 | +```http |
| 28 | +GET /health |
| 29 | +``` |
| 30 | + |
| 31 | +**Response:** |
| 32 | +```json |
| 33 | +{ |
| 34 | + "status": "ok", |
| 35 | + "service": "gpt-image-1-mcp-server" |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### MCP Protocol Endpoint |
| 40 | + |
| 41 | +Main endpoint for MCP protocol communication. |
| 42 | + |
| 43 | +```http |
| 44 | +POST /mcp |
| 45 | +``` |
| 46 | + |
| 47 | +**Headers:** |
| 48 | +- `Content-Type: application/json` |
| 49 | +- `X-Session-ID: <session-id>` (optional, will be generated if not provided) |
| 50 | + |
| 51 | +**Request Body:** |
| 52 | +```json |
| 53 | +{ |
| 54 | + "jsonrpc": "2.0", |
| 55 | + "method": "tools/call", |
| 56 | + "params": { |
| 57 | + "name": "generate_image", |
| 58 | + "arguments": { |
| 59 | + "prompt": "A beautiful landscape", |
| 60 | + "size": "1024x1024", |
| 61 | + "quality": "high" |
| 62 | + } |
| 63 | + }, |
| 64 | + "id": 1 |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +**Response:** |
| 69 | +```json |
| 70 | +{ |
| 71 | + "jsonrpc": "2.0", |
| 72 | + "result": { |
| 73 | + "content": [ |
| 74 | + { |
| 75 | + "type": "text", |
| 76 | + "text": "Generated 1 image(s) successfully." |
| 77 | + }, |
| 78 | + { |
| 79 | + "type": "image", |
| 80 | + "data": "base64_encoded_image_data...", |
| 81 | + "mimeType": "image/png" |
| 82 | + } |
| 83 | + ] |
| 84 | + }, |
| 85 | + "id": 1 |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Streaming Image Generation |
| 90 | + |
| 91 | +Generate images with real-time progress updates using Server-Sent Events. |
| 92 | + |
| 93 | +```http |
| 94 | +POST /mcp/stream |
| 95 | +``` |
| 96 | + |
| 97 | +**Headers:** |
| 98 | +- `Content-Type: application/json` |
| 99 | +- `X-Session-ID: <session-id>` (optional) |
| 100 | + |
| 101 | +**Request Body:** |
| 102 | +```json |
| 103 | +{ |
| 104 | + "prompt": "A futuristic city at sunset", |
| 105 | + "size": "1024x1024", |
| 106 | + "quality": "high", |
| 107 | + "format": "jpeg", |
| 108 | + "partialImages": 3, |
| 109 | + "output_compression": 80 |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +**Response:** Server-Sent Events stream |
| 114 | + |
| 115 | +**Event Format:** |
| 116 | +``` |
| 117 | +data: {"type":"<event-type>","data":{...}} |
| 118 | +
|
| 119 | +``` |
| 120 | + |
| 121 | +**Event Types:** |
| 122 | + |
| 123 | +1. **Progress Event** |
| 124 | +```json |
| 125 | +{ |
| 126 | + "type": "progress", |
| 127 | + "data": { |
| 128 | + "progress": 50, |
| 129 | + "message": "Processing prompt..." |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +2. **Partial Image Event** |
| 135 | +```json |
| 136 | +{ |
| 137 | + "type": "partial", |
| 138 | + "data": { |
| 139 | + "partialImage": "base64_encoded_partial_image...", |
| 140 | + "partialImageIndex": 0, |
| 141 | + "progress": 40, |
| 142 | + "message": "Partial image 1 received" |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +3. **Complete Event** |
| 148 | +```json |
| 149 | +{ |
| 150 | + "type": "complete", |
| 151 | + "data": { |
| 152 | + "finalImage": "base64_encoded_final_image...", |
| 153 | + "revisedPrompt": "A futuristic cityscape with tall buildings...", |
| 154 | + "progress": 100, |
| 155 | + "message": "Image generation completed!" |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +4. **Error Event** |
| 161 | +```json |
| 162 | +{ |
| 163 | + "type": "error", |
| 164 | + "error": "Error message here" |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +**End of Stream:** |
| 169 | +``` |
| 170 | +data: [DONE] |
| 171 | +``` |
| 172 | + |
| 173 | +### Session Management |
| 174 | + |
| 175 | +Delete a session and clean up resources. |
| 176 | + |
| 177 | +```http |
| 178 | +DELETE /mcp/session/:sessionId |
| 179 | +``` |
| 180 | + |
| 181 | +**Response:** |
| 182 | +```json |
| 183 | +{ |
| 184 | + "message": "Session closed" |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## MCP Tools |
| 189 | + |
| 190 | +### generate_image |
| 191 | + |
| 192 | +Generate images from text prompts. |
| 193 | + |
| 194 | +**Parameters:** |
| 195 | + |
| 196 | +| Parameter | Type | Required | Default | Description | |
| 197 | +|-----------|------|----------|---------|-------------| |
| 198 | +| prompt | string | Yes | - | Text description of the image | |
| 199 | +| size | string | No | "1024x1024" | Image dimensions: "1024x1024", "1024x1536", "1536x1024", "auto" | |
| 200 | +| quality | string | No | "auto" | Quality level: "low", "medium", "high", "auto" | |
| 201 | +| format | string | No | "png" | Output format: "png", "jpeg", "webp" | |
| 202 | +| background | string | No | "auto" | Background: "transparent", "opaque", "auto" | |
| 203 | +| output_compression | number | No | - | Compression level (0-100) | |
| 204 | +| moderation | string | No | "auto" | Content moderation: "auto", "low" | |
| 205 | +| n | number | No | 1 | Number of images (1-4) | |
| 206 | +| partialImages | number | No | - | Number of partial images (1-3) | |
| 207 | +| stream | boolean | No | false | Enable streaming mode | |
| 208 | + |
| 209 | +**Example:** |
| 210 | +```json |
| 211 | +{ |
| 212 | + "name": "generate_image", |
| 213 | + "arguments": { |
| 214 | + "prompt": "A serene mountain lake at sunrise", |
| 215 | + "size": "1536x1024", |
| 216 | + "quality": "high", |
| 217 | + "format": "jpeg", |
| 218 | + "output_compression": 85, |
| 219 | + "partialImages": 2 |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +### edit_image |
| 225 | + |
| 226 | +Edit existing images with text prompts and optional masks. |
| 227 | + |
| 228 | +**Parameters:** |
| 229 | + |
| 230 | +| Parameter | Type | Required | Default | Description | |
| 231 | +|-----------|------|----------|---------|-------------| |
| 232 | +| prompt | string | Yes | - | Text description of the edit | |
| 233 | +| images | string[] | Yes | - | Array of base64-encoded images | |
| 234 | +| mask | string | No | - | Base64-encoded mask for inpainting | |
| 235 | +| size | string | No | "1024x1024" | Output dimensions | |
| 236 | +| quality | string | No | "auto" | Quality level | |
| 237 | +| format | string | No | "png" | Output format | |
| 238 | +| n | number | No | 1 | Number of variations | |
| 239 | + |
| 240 | +**Example:** |
| 241 | +```json |
| 242 | +{ |
| 243 | + "name": "edit_image", |
| 244 | + "arguments": { |
| 245 | + "prompt": "Add a rainbow in the sky", |
| 246 | + "images": ["base64_encoded_image..."], |
| 247 | + "mask": "base64_encoded_mask..." |
| 248 | + } |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +### clear_cache |
| 253 | + |
| 254 | +Clear all cached images from memory and disk. |
| 255 | + |
| 256 | +**Parameters:** None |
| 257 | + |
| 258 | +**Example:** |
| 259 | +```json |
| 260 | +{ |
| 261 | + "name": "clear_cache", |
| 262 | + "arguments": {} |
| 263 | +} |
| 264 | +``` |
| 265 | + |
| 266 | +### cache_stats |
| 267 | + |
| 268 | +Get cache statistics. |
| 269 | + |
| 270 | +**Parameters:** None |
| 271 | + |
| 272 | +**Response Example:** |
| 273 | +```json |
| 274 | +{ |
| 275 | + "content": [ |
| 276 | + { |
| 277 | + "type": "text", |
| 278 | + "text": "Cache Statistics:\n- Memory entries: 5\n- Estimated disk usage: 2.5 MB" |
| 279 | + } |
| 280 | + ] |
| 281 | +} |
| 282 | +``` |
| 283 | + |
| 284 | +## Error Handling |
| 285 | + |
| 286 | +### Error Response Format |
| 287 | + |
| 288 | +```json |
| 289 | +{ |
| 290 | + "error": { |
| 291 | + "code": "error_code", |
| 292 | + "message": "Human-readable error message" |
| 293 | + } |
| 294 | +} |
| 295 | +``` |
| 296 | + |
| 297 | +### Common Error Codes |
| 298 | + |
| 299 | +| Code | Description | |
| 300 | +|------|-------------| |
| 301 | +| `invalid_params` | Invalid or missing parameters | |
| 302 | +| `method_not_found` | Unknown tool or method | |
| 303 | +| `internal_error` | Server internal error | |
| 304 | +| `api_error` | OpenAI API error | |
| 305 | +| `streaming_error` | Streaming-specific error | |
| 306 | + |
| 307 | +### HTTP Status Codes |
| 308 | + |
| 309 | +| Status | Description | |
| 310 | +|--------|-------------| |
| 311 | +| 200 | Success | |
| 312 | +| 400 | Bad Request - Invalid parameters | |
| 313 | +| 404 | Not Found - Session or resource not found | |
| 314 | +| 500 | Internal Server Error | |
| 315 | + |
| 316 | +## Rate Limiting |
| 317 | + |
| 318 | +The server respects OpenAI's rate limits: |
| 319 | +- Requests are queued if rate limits are reached |
| 320 | +- Cached responses bypass API calls entirely |
| 321 | +- Monitor `X-RateLimit-*` headers in responses |
| 322 | + |
| 323 | +## Best Practices |
| 324 | + |
| 325 | +### 1. Session Management |
| 326 | +- Reuse session IDs for related requests |
| 327 | +- Clean up sessions when done |
| 328 | +- Sessions auto-expire after inactivity |
| 329 | + |
| 330 | +### 2. Caching |
| 331 | +- Identical requests return cached results |
| 332 | +- Use cache_stats to monitor usage |
| 333 | +- Clear cache periodically if needed |
| 334 | + |
| 335 | +### 3. Streaming |
| 336 | +- Use streaming for better UX with progress updates |
| 337 | +- Handle partial images for preview |
| 338 | +- Implement proper error handling for SSE |
| 339 | + |
| 340 | +### 4. Error Handling |
| 341 | +- Always check for error events in streams |
| 342 | +- Implement exponential backoff for retries |
| 343 | +- Log errors for debugging |
| 344 | + |
| 345 | +### 5. Performance |
| 346 | +- Use appropriate quality settings |
| 347 | +- Enable caching for repeated requests |
| 348 | +- Use streaming for large images |
| 349 | + |
| 350 | +## Examples |
| 351 | + |
| 352 | +### cURL Examples |
| 353 | + |
| 354 | +**Basic Generation:** |
| 355 | +```bash |
| 356 | +curl -X POST http://localhost:3000/mcp \ |
| 357 | + -H "Content-Type: application/json" \ |
| 358 | + -H "X-Session-ID: test-session" \ |
| 359 | + -d '{ |
| 360 | + "jsonrpc": "2.0", |
| 361 | + "method": "tools/call", |
| 362 | + "params": { |
| 363 | + "name": "generate_image", |
| 364 | + "arguments": { |
| 365 | + "prompt": "A cute cat" |
| 366 | + } |
| 367 | + }, |
| 368 | + "id": 1 |
| 369 | + }' |
| 370 | +``` |
| 371 | + |
| 372 | +**Streaming Generation:** |
| 373 | +```bash |
| 374 | +curl -X POST http://localhost:3000/mcp/stream \ |
| 375 | + -H "Content-Type: application/json" \ |
| 376 | + -H "X-Session-ID: test-stream" \ |
| 377 | + -d '{ |
| 378 | + "prompt": "A majestic dragon", |
| 379 | + "partialImages": 3, |
| 380 | + "quality": "high" |
| 381 | + }' \ |
| 382 | + -N |
| 383 | +``` |
| 384 | + |
| 385 | +### JavaScript/TypeScript Examples |
| 386 | + |
| 387 | +See the `examples/` directory for complete implementations: |
| 388 | +- `streaming-client.ts` - SSE streaming example |
| 389 | +- `test-client.ts` - Basic MCP client |
| 390 | +- `optimization-demo.ts` - Image optimization demo |
0 commit comments