generate_image
Create custom images from text prompts using OpenAI's DALL-E 3 model. Specify size, quality, and style, and save directly to the local filesystem for seamless integration.
Instructions
Generate an image using OpenAI's DALL-E 3 model based on a text prompt
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | No | Optional custom filename (without extension) | |
| prompt | Yes | The text prompt describing the image to generate | |
| quality | No | Image quality (standard or hd) | standard |
| size | No | Image size (1024x1024, 1024x1792, or 1792x1024) | 1024x1024 |
| style | No | Image style (vivid or natural) | vivid |
Implementation Reference
- src/index.ts:106-182 (handler)Main handler function for 'generate_image' tool. Parses arguments, calls OpenAI DALL-E 3 API to generate image URL, downloads the image, saves it as PNG to OUTPUT_DIR with timestamp-based filename, returns JSON result via MCP content.private async handleImageGeneration(args: any) { try { const { prompt, size = DEFAULT_SIZE, quality = DEFAULT_QUALITY, style = "vivid", filename, } = args; if (!process.env.OPENAI_API_KEY) { throw new Error("OPENAI_API_KEY environment variable is required"); } console.log(`[DALL-E] Generating image: "${prompt.slice(0, 50)}..."`); const response = await openai.images.generate({ model: "dall-e-3", prompt: prompt, n: 1, size: size as "1024x1024" | "1024x1792" | "1792x1024", quality: quality as "standard" | "hd", style: style as "vivid" | "natural", response_format: "url", }); if (!response.data || !response.data[0]?.url) { throw new Error("No image URL received from DALL-E API"); } const imageUrl = response.data[0].url; const imageResponse = await fetch(imageUrl); if (!imageResponse.ok) { throw new Error(`Failed to download image: ${imageResponse.statusText}`); } const imageBuffer = Buffer.from(await imageResponse.arrayBuffer()); await fs.ensureDir(OUTPUT_DIR); const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const baseFilename = filename || `dalle_${timestamp}`; const imagePath = path.join(OUTPUT_DIR, `${baseFilename}.png`); await fs.writeFile(imagePath, imageBuffer); const result = { success: true, message: "Image generated successfully", details: { prompt: prompt, size: size, quality: quality, style: style, file_path: path.resolve(imagePath), file_size: imageBuffer.length, timestamp: new Date().toISOString(), }, }; console.log(`[DALL-E] Image saved: ${imagePath}`); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; console.error(`[DALL-E Error] ${errorMessage}`); const errorResult = { success: false, error: errorMessage, timestamp: new Date().toISOString(), }; return { content: [{ type: "text", text: JSON.stringify(errorResult, null, 2) }] }; } }
- src/index.ts:60-91 (schema)Input schema definition for the 'generate_image' tool, specifying properties like prompt (required), size, quality, style, filename with types, enums, descriptions, and defaults.inputSchema: { type: "object", properties: { prompt: { type: "string", description: "The text prompt describing the image to generate", }, size: { type: "string", description: "Image size (1024x1024, 1024x1792, or 1792x1024)", enum: ["1024x1024", "1024x1792", "1792x1024"], default: DEFAULT_SIZE, }, quality: { type: "string", description: "Image quality (standard or hd)", enum: ["standard", "hd"], default: DEFAULT_QUALITY, }, style: { type: "string", description: "Image style (vivid or natural)", enum: ["vivid", "natural"], default: "vivid", }, filename: { type: "string", description: "Optional custom filename (without extension)", }, }, required: ["prompt"], },
- src/index.ts:56-92 (registration)Registration of 'generate_image' tool in the ListToolsRequest handler, providing name, description, and input schema.{ name: "generate_image", description: "Generate an image using OpenAI's DALL-E 3 model based on a text prompt", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "The text prompt describing the image to generate", }, size: { type: "string", description: "Image size (1024x1024, 1024x1792, or 1792x1024)", enum: ["1024x1024", "1024x1792", "1792x1024"], default: DEFAULT_SIZE, }, quality: { type: "string", description: "Image quality (standard or hd)", enum: ["standard", "hd"], default: DEFAULT_QUALITY, }, style: { type: "string", description: "Image style (vivid or natural)", enum: ["vivid", "natural"], default: "vivid", }, filename: { type: "string", description: "Optional custom filename (without extension)", }, }, required: ["prompt"], }, },
- src/index.ts:97-103 (registration)Registration of CallToolRequest handler that dispatches to generate_image handler when tool name matches.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name === "generate_image") { return await this.handleImageGeneration(request.params.arguments); } throw new Error(`Unknown tool: ${request.params.name}`); });