generate_image
Create custom images from text descriptions using OpenAI's DALL-E 3 model with configurable size, quality, and style options for visual content generation.
Instructions
Generate an image using OpenAI's DALL-E 3 model based on a text prompt
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | The text prompt describing the image to generate | |
| size | No | Image size (1024x1024, 1024x1792, or 1792x1024) | 1792x1024 |
| quality | No | Image quality (standard or hd) | hd |
| style | No | Image style (vivid or natural) | vivid |
| filename | No | Optional custom filename (without extension) |
Implementation Reference
- src/index.ts:106-182 (handler)The primary handler function that executes the generate_image tool. It calls the OpenAI DALL-E API, downloads the generated image, saves it locally, and returns a JSON result.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)Defines the input schema and parameters for the generate_image tool, including required prompt and optional size, quality, style, filename.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:53-95 (registration)Registers the generate_image tool in the ListTools response, providing its name, description, and schema.this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { 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)Sets up the CallTool request handler that dispatches to generate_image handler based on tool name.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}`); });