Skip to main content
Glama

generate_image

Create high-quality images from text prompts using DALL-E 3. Specify size, quality, and style, then save the output to a designated path for versatile visual content generation.

Instructions

Generate an image using DALL-E 3

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
output_pathYesFull path where the image should be saved
promptYesText prompt for image generation
qualityNoImage qualityhd
sizeNoImage size1024x1024
styleNoImage stylevivid

Implementation Reference

  • The primary handler function that executes the generate_image tool: validates input, calls OpenAI DALL-E 3 API, downloads the generated image, saves it to the specified path (auto-generates filename if directory), and returns a detailed text 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:67-102 (registration)
    Tool registration in the ListToolsRequestSchema handler: defines the name, description, and inputSchema for 'generate_image'.
    { 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)
    Registers the CallToolRequestSchema handler which dispatches calls to 'generate_image' to the generateImage method.
    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}`); } });
  • TypeScript type definition for the GenerateImageArgs used in the handler function.
    interface GenerateImageArgs { prompt: string; output_path: string; size?: '1024x1024' | '1024x1792' | '1792x1024'; quality?: 'standard' | 'hd'; style?: 'vivid' | 'natural'; }
  • TypeScript interface for the OpenAI image generation API response.
    interface OpenAIImageResponse { data: Array<{ url: string; revised_prompt: string; }>; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/chrisurf/imagegen-mcp-d3'

If you have feedback or need assistance with the MCP directory API, please join our Discord server