generate_image
Create custom images by providing a detailed prompt and filename. This tool leverages OpenAI's DALL·E-3 model within the Image Generator MCP Server to transform text descriptions into visual content.
Instructions
Generate an image from a prompt.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imageName | Yes | The filename for the image excluding any extensions. | |
| prompt | Yes | A prompt detailing what image to generate. |
Implementation Reference
- src/index.ts:74-104 (handler)The CallToolRequestSchema handler that executes the generate_image tool: validates arguments, generates base64 image via ImageGenerator, saves to desktop folder, returns file URI.this.server.setRequestHandler( CallToolRequestSchema, async (request) => { if (request.params.name !== "generate_image") { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } if (!isValidImageGenerationArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, "Invalid image generation arguments" ) }; const { prompt, imageName } = request.params.arguments; const base64 = await new ImageGenerator().generateImage(prompt); const fileName = `${imageName.replace(/\..*$/, '')}.png`; const filepath = await imageSaver.saveBase64(fileName, base64!); return { toolResult: { uri: `file://${filepath}`, type: 'image', mimeType: 'image/png' } } } )
- src/index.ts:50-72 (registration)Registers the generate_image tool in ListToolsRequestSchema response with name, description, and input schema.this.server.setRequestHandler( ListToolsRequestSchema, async () => ({ tools: [{ name: "generate_image", description: "Generate an image from a prompt.", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "A prompt detailing what image to generate." }, imageName: { type: "string", description: "The filename for the image excluding any extensions." } }, required: ["prompt", "imageName"] } }] }) );
- src/types.ts:1-13 (schema)Type definition and runtime validation function for generate_image input parameters.export interface ImageGenerationRequestParams { prompt: string; imageName: string; } export function isValidImageGenerationArgs(args: any): args is ImageGenerationRequestParams { return typeof args === "object" && args !== null && "prompt" in args && typeof args.prompt === 'string' && "imageName" in args && typeof args.imageName === 'string'; }
- src/image-generator.ts:12-20 (helper)Core image generation logic using OpenAI DALL-E 3 API to produce base64-encoded PNG image from prompt.async generateImage(prompt: string, size: ImageGenerateParams['size'] = "1024x1024") { const response = await this.openai.images.generate({ model: IMAGE_MODEL, prompt, size, response_format: 'b64_json' }); return response.data[0].b64_json; }