Skip to main content
Glama
Garoth
by Garoth

generate_image

Create images from text descriptions using DALL-E AI models, with options for size, quality, style, and quantity.

Instructions

Generate an image using DALL-E based on a text prompt

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesText description of the desired image
modelNoDALL-E model to use (dall-e-2 or dall-e-3)
sizeNoSize of the generated image
qualityNoQuality of the generated image (dall-e-3 only)
styleNoStyle of the generated image (dall-e-3 only)
nNoNumber of images to generate (1-10)
saveDirNoDirectory to save the generated images
fileNameNoBase filename for the generated images (without extension)

Implementation Reference

  • The handler function that executes the 'generate_image' tool logic, calling DalleService and formatting the response with saved image paths.
    handler: async (args: GenerateImageArgs): Promise<ToolResponse> => {
      const result = await dalleService.generateImage(args.prompt, {
        model: args.model,
        size: args.size,
        quality: args.quality,
        style: args.style,
        n: args.n,
        saveDir: args.saveDir,
        fileName: args.fileName
      });
    
      if (!result.success) {
        return {
          content: [{
            type: "text",
            text: `Error generating image: ${result.error}`
          }]
        };
      }
    
      const imagePaths = result.imagePaths || [];
      const imageCount = imagePaths.length;
      const model = result.model || 'dall-e-3';
    
      let responseText = `Successfully generated ${imageCount} image${imageCount !== 1 ? 's' : ''} using ${model}.\n\n`;
      responseText += `Prompt: "${result.prompt}"\n\n`;
      responseText += `Image${imageCount !== 1 ? 's' : ''} saved to:\n`;
      
      imagePaths.forEach(imagePath => {
        responseText += `- ${imagePath}\n`;
      });
    
      return {
        content: [{
          type: "text",
          text: responseText
        }]
      };
    }
  • Core helper method in DalleService that performs the OpenAI DALL-E API call for image generation, handles base64 decoding, saves images to disk, and returns result.
    async generateImage(
      prompt: string,
      options: {
        model?: string;
        size?: string;
        quality?: string;
        style?: string;
        n?: number;
        saveDir?: string;
        fileName?: string;
      } = {}
    ): Promise<ImageGenerationResult> {
      try {
        // Set default options
        const model = options.model || 'dall-e-3';
        const size = options.size || '1024x1024';
        const quality = options.quality || 'standard';
        const style = options.style || 'vivid';
        const n = options.n || 1;
        const saveDir = options.saveDir || this.config.defaultSaveDir || process.cwd();
        const fileName = options.fileName || `dalle-${Date.now()}`;
    
        // Ensure save directory exists
        await fs.ensureDir(saveDir);
    
        // Make request to OpenAI API
        const response = await axios.post(
          `${this.baseUrl}/images/generations`,
          {
            model,
            prompt,
            n,
            size,
            quality,
            style,
            response_format: 'b64_json'
          },
          {
            headers: {
              'Content-Type': 'application/json',
              'Authorization': `Bearer ${this.config.apiKey}`
            }
          }
        );
    
        // Process response
        const data = response.data;
        const imagePaths: string[] = [];
    
        // Save each image
        for (let i = 0; i < data.data.length; i++) {
          const item = data.data[i];
          const imageBuffer = Buffer.from(item.b64_json, 'base64');
          let imagePath = path.join(saveDir, `${fileName}${n > 1 ? `-${i + 1}` : ''}.png`);
          
          // Ensure the path is absolute
          if (!path.isAbsolute(imagePath)) {
            imagePath = path.resolve(process.cwd(), imagePath);
          }
          
          await fs.writeFile(imagePath, imageBuffer);
          imagePaths.push(imagePath);
        }
    
        return {
          success: true,
          imagePaths,
          model,
          prompt
        };
      } catch (error) {
        console.log("DALL-E API Error:", error);
        
        let errorMessage = 'Failed to generate image';
        
        if (axios.isAxiosError(error) && error.response?.data?.error) {
          errorMessage = `DALL-E API Error: ${error.response.data.error.message}`;
        } else if (error instanceof Error) {
          errorMessage = error.message;
        }
        
        return {
          success: false,
          error: errorMessage
        };
      }
    }
  • Type definition for input arguments of the generate_image tool.
    export interface GenerateImageArgs {
      prompt: string;
      model?: string;
      size?: string;
      quality?: string;
      style?: string;
      n?: number;
      saveDir?: string;
      fileName?: string;
    }
  • Tool registration object defining name, description, inputSchema, and handler for 'generate_image'.
    {
      name: "generate_image",
      description: "Generate an image using DALL-E based on a text prompt",
      inputSchema: {
        type: "object",
        properties: {
          prompt: {
            type: "string",
            description: "Text description of the desired image"
          },
          model: {
            type: "string",
            description: "DALL-E model to use (dall-e-2 or dall-e-3)",
            enum: ["dall-e-2", "dall-e-3"]
          },
          size: {
            type: "string",
            description: "Size of the generated image",
            enum: ["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"]
          },
          quality: {
            type: "string",
            description: "Quality of the generated image (dall-e-3 only)",
            enum: ["standard", "hd"]
          },
          style: {
            type: "string",
            description: "Style of the generated image (dall-e-3 only)",
            enum: ["vivid", "natural"]
          },
          n: {
            type: "number",
            description: "Number of images to generate (1-10)",
            minimum: 1,
            maximum: 10
          },
          saveDir: {
            type: "string",
            description: "Directory to save the generated images"
          },
          fileName: {
            type: "string",
            description: "Base filename for the generated images (without extension)"
          }
        },
        required: ["prompt"]
      },
      handler: async (args: GenerateImageArgs): Promise<ToolResponse> => {
        const result = await dalleService.generateImage(args.prompt, {
          model: args.model,
          size: args.size,
          quality: args.quality,
          style: args.style,
          n: args.n,
          saveDir: args.saveDir,
          fileName: args.fileName
        });
    
        if (!result.success) {
          return {
            content: [{
              type: "text",
              text: `Error generating image: ${result.error}`
            }]
          };
        }
    
        const imagePaths = result.imagePaths || [];
        const imageCount = imagePaths.length;
        const model = result.model || 'dall-e-3';
    
        let responseText = `Successfully generated ${imageCount} image${imageCount !== 1 ? 's' : ''} using ${model}.\n\n`;
        responseText += `Prompt: "${result.prompt}"\n\n`;
        responseText += `Image${imageCount !== 1 ? 's' : ''} saved to:\n`;
        
        imagePaths.forEach(imagePath => {
          responseText += `- ${imagePath}\n`;
        });
    
        return {
          content: [{
            type: "text",
            text: responseText
          }]
        };
      }
    },
  • src/index.ts:24-32 (registration)
    MCP server capabilities registration enabling the 'generate_image' tool.
      capabilities: {
        tools: {
          generate_image: true,
          edit_image: true,
          create_variation: true,
          validate_key: true
        },
      },
    }

Tool Definition Quality

Score is being calculated. Check back soon.

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/Garoth/dalle-mcp'

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