Skip to main content
Glama

generate_image

Create custom images from text descriptions using DALL-E AI models. Specify model, size, quality, style, and quantity to generate and save visual content.

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 main handler function for the 'generate_image' tool. It calls DalleService.generateImage with the provided arguments, handles success/error cases, constructs a text response with image paths, and returns a ToolResponse.
    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 }] }; }
  • TypeScript interface defining the input parameters for the generate_image tool, used for type safety in the handler.
    export interface GenerateImageArgs { prompt: string; model?: string; size?: string; quality?: string; style?: string; n?: number; saveDir?: string; fileName?: string; }
  • src/index.ts:24-31 (registration)
    MCP server capabilities section registering the 'generate_image' tool as supported.
    capabilities: { tools: { generate_image: true, edit_image: true, create_variation: true, validate_key: true }, },
  • src/index.ts:82-84 (registration)
    Dispatch logic in the CallToolRequest handler that specifically routes 'generate_image' calls to the typed tool handler.
    case 'generate_image': response = await (tool as Tool<GenerateImageArgs>).handler(args as unknown as GenerateImageArgs); break;
  • Core helper method in DalleService that performs the actual image generation by calling OpenAI DALL-E API, processing b64_json response, saving images to filesystem, and returning success/error with paths.
    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 }; } }

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