generate_image
Create custom images from text prompts using AI, specifying content and filename for generated visuals.
Instructions
Generate an image from a prompt.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | A prompt detailing what image to generate. | |
| imageName | Yes | The filename for the image excluding any extensions. |
Implementation Reference
- src/index.ts:74-104 (handler)Handler for CallToolRequestSchema specifically for the 'generate_image' tool. Validates the tool name and arguments, generates the image using ImageGenerator, saves the file using FileSaver, and returns the file URI as tool result.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/types.ts:1-13 (schema)Type definition for image generation parameters and validation function used in the handler to check input arguments.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/index.ts:53-71 (registration)Registers the 'generate_image' tool in the ListToolsRequestHandler with description and input schema.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/image-generator.ts:12-20 (helper)Core image generation logic using OpenAI's DALL-E 3 model to generate base64 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; }