generate_image
Create images from text prompts using DALL-E 3 AI, saving them to specified paths with customizable size, quality, and style options.
Instructions
Generate an image using DALL-E 3
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | Text prompt for image generation | |
| output_path | Yes | Full path where the image should be saved | |
| size | No | Image size | 1024x1024 |
| quality | No | Image quality | hd |
| style | No | Image style | vivid |
Implementation Reference
- src/index.ts:140-251 (handler)The primary handler function that implements the generate_image tool logic. It calls the OpenAI DALL-E 3 API, downloads the generated image, handles output path (including directory fallback), saves the file, and returns a formatted response.private async generateImage(args: GenerateImageArgs) { const { prompt, output_path, size = '1024x1024', quality = 'hd', style = 'vivid', } = args; if (!prompt) { throw new McpError(ErrorCode.InvalidParams, 'Missing required parameter: prompt'); } if (!output_path) { throw new McpError(ErrorCode.InvalidParams, 'Missing required parameter: output_path'); } const apiKey = process.env.OPENAI_API_KEY; if (!apiKey) { throw new McpError(ErrorCode.InternalError, 'OPENAI_API_KEY environment variable not set'); } try { console.error('[DALL-E 3] Starting image generation...'); console.error('[DALL-E 3] Prompt:', prompt); console.error('[DALL-E 3] Output path:', output_path); const response = await fetch('https://api.openai.com/v1/images/generations', { method: 'POST', headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ model: 'dall-e-3', prompt, n: 1, size, quality, style, }), }); if (!response.ok) { const errorText = await response.text(); console.error('[DALL-E 3] API Error:', errorText); throw new McpError(ErrorCode.InternalError, `OpenAI API error: ${response.status} ${response.statusText} - ${errorText}`); } const data = (await response.json()) as OpenAIImageResponse; const imageUrl = data.data[0]?.url; const revisedPrompt = data.data[0]?.revised_prompt; if (!imageUrl) { throw new McpError(ErrorCode.InternalError, 'No image URL returned from OpenAI API'); } console.error('[DALL-E 3] Generated image URL:', imageUrl); console.error('[DALL-E 3] Revised prompt:', revisedPrompt); const imageResponse = await fetch(imageUrl); if (!imageResponse.ok) { throw new McpError(ErrorCode.InternalError, `Failed to download image: ${imageResponse.status} ${imageResponse.statusText}`); } const imageBuffer = await imageResponse.arrayBuffer(); let finalOutputPath = output_path; const stats = await stat(output_path).catch(() => null); if (stats?.isDirectory() || output_path.endsWith('/') || output_path.endsWith('\\')) { const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const promptSlug = prompt.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').substring(0, 50); const filename = `dalle3-${promptSlug}-${timestamp}.png`; finalOutputPath = path.join(output_path, filename); console.error(`[DALL-E 3] Directory detected, using filename: ${filename}`); } const outputDir = path.dirname(finalOutputPath); await mkdir(outputDir, { recursive: true }); await writeFile(finalOutputPath, Buffer.from(imageBuffer)); const imageSizeKB = Math.round(imageBuffer.byteLength / 1024); console.error(`[DALL-E 3] β Image saved successfully to: ${finalOutputPath}`); console.error(`[DALL-E 3] π Image size: ${imageSizeKB} KB`); return { content: [ { type: 'text', text: `β Image generated successfully! **Original Prompt:** ${prompt} **Revised Prompt:** ${revisedPrompt || 'N/A'} **Image URL:** ${imageUrl} **Saved to:** ${finalOutputPath} **Size:** ${size} **Quality:** ${quality} **Style:** ${style} **File Size:** ${imageSizeKB} KB The image has been saved to your specified location and is ready to use.`, }, ], }; } catch (error) { console.error('[DALL-E 3] Error:', error); if (error instanceof McpError) throw error; throw new McpError(ErrorCode.InternalError, `Failed to generate image: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:19-25 (schema)TypeScript interface defining the input arguments for the generate_image tool handler.interface GenerateImageArgs { prompt: string; output_path: string; size?: '1024x1024' | '1024x1792' | '1792x1024'; quality?: 'standard' | 'hd'; style?: 'vivid' | 'natural'; }
- src/index.ts:70-101 (schema)JSON Schema for the generate_image tool input, provided in the ListTools response for client validation.inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'Text prompt for image generation', }, output_path: { type: 'string', description: 'Full path where the image should be saved', }, size: { type: 'string', enum: ['1024x1024', '1024x1792', '1792x1024'], default: '1024x1024', description: 'Image size', }, quality: { type: 'string', enum: ['standard', 'hd'], default: 'hd', description: 'Image quality', }, style: { type: 'string', enum: ['vivid', 'natural'], default: 'vivid', description: 'Image style', }, }, required: ['prompt', 'output_path'], },
- src/index.ts:67-102 (registration)Tool registration in the ListToolsRequestSchema handler, defining name, description, and schema.{ name: 'generate_image', description: 'Generate an image using DALL-E 3', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'Text prompt for image generation', }, output_path: { type: 'string', description: 'Full path where the image should be saved', }, size: { type: 'string', enum: ['1024x1024', '1024x1792', '1792x1024'], default: '1024x1024', description: 'Image size', }, quality: { type: 'string', enum: ['standard', 'hd'], default: 'hd', description: 'Image quality', }, style: { type: 'string', enum: ['vivid', 'natural'], default: 'vivid', description: 'Image style', }, }, required: ['prompt', 'output_path'], }, },
- src/index.ts:107-115 (registration)Dispatch/registration in the CallToolRequestSchema handler, routing 'generate_image' calls to the handler function.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; if (name === 'generate_image') { return await this.generateImage(args as unknown as GenerateImageArgs); } else { throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`); } });