Skip to main content
Glama
chrisurf

DALL-E 3 MCP Server

by chrisurf

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
NameRequiredDescriptionDefault
promptYesText prompt for image generation
output_pathYesFull path where the image should be saved
sizeNoImage size1024x1024
qualityNoImage qualityhd
styleNoImage stylevivid

Implementation Reference

  • 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'}`);
        }
      }
  • 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';
    }
  • 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}`);
      }
    });
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions the AI model (DALL-E 3) but fails to address critical aspects like rate limits, authentication requirements, cost implications, or what happens when the image is saved (e.g., overwrite behavior). This leaves significant gaps in understanding the tool's operational characteristics.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise (5 words) and front-loaded with the essential information. Every word earns its place, and there's no wasted text or unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 5 parameters, no annotations, and no output schema, the description is inadequate. It doesn't explain what the tool returns (e.g., success confirmation, error handling, or image metadata), nor does it address the behavioral aspects needed for safe and effective use. The high parameter count and lack of structured metadata require more comprehensive description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, with all parameters well-documented in the input schema. The description adds no additional parameter information beyond what's already in the structured fields, which meets the baseline expectation but doesn't provide extra value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Generate') and the resource ('image using DALL-E 3'), providing a specific verb+resource combination. However, since there are no sibling tools mentioned, it cannot demonstrate differentiation from alternatives, which prevents a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives, prerequisites, or contextual constraints. It simply states what the tool does without any usage context, which is insufficient for effective agent decision-making.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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