generateImage
Create custom images using text prompts with specific aspect ratios and output formats. Integrated with the Gemini API for cloud-powered image generation, enabling hybrid local-cloud workflows through the GeminiMcpServer.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| aspectRatio | No | ||
| outputFormat | No | ||
| prompt | Yes |
Implementation Reference
- server.js:25-61 (handler)The main handler function for the 'generateImage' tool. It uses the Google Gemini image generation model to create an image based on the provided prompt, handles optional aspectRatio and outputFormat, and returns the image data or an error message.async (params) => { const prompt = params.prompt || "rendered image of fly pig"; const outputFormat = params.outputFormat || "png"; try { // Initialize gemini-2.0-flash-exp-image-generation model const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash-exp-image-generation", generationConfig: { responseModalities: ['Text', 'Image'], }, }); // Generate the image const result = await model.generateContent(prompt); for (const part of result.response.candidates[0].content.parts) { if (part.inlineData) { const imageData = part.inlineData.data; return { content: [{ type: "image", data: imageData, mimeType: `image/${outputFormat}` }] }; } } } catch (error) { console.error("Image generation error:", error); return { content: [{ type: "text", text: `Error generating image: ${error.message}` }] }; } },
- server.js:20-24 (schema)Zod schema used for input validation of the generateImage tool parameters: prompt (required string), aspectRatio (optional string), outputFormat (optional string).{ prompt: z.string(), aspectRatio: z.string().optional(), outputFormat: z.string().optional() },
- server.js:64-68 (schema)JSON schema description for the generateImage tool parameters used in tool metadata.parameters: { prompt: { type: "string", description: "The text description of the image to generate" }, aspectRatio: { type: "string", description: "Aspect ratio of the image (e.g., '1:1', '16:9')", optional: true }, outputFormat: { type: "string", description: "Output image format ('png' or 'jpeg')", optional: true } }
- server.js:18-70 (registration)Registration of the 'generateImage' tool with the MCP server, specifying name, input schema, handler function, and tool description with parameter details.server.tool( "generateImage", { prompt: z.string(), aspectRatio: z.string().optional(), outputFormat: z.string().optional() }, async (params) => { const prompt = params.prompt || "rendered image of fly pig"; const outputFormat = params.outputFormat || "png"; try { // Initialize gemini-2.0-flash-exp-image-generation model const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash-exp-image-generation", generationConfig: { responseModalities: ['Text', 'Image'], }, }); // Generate the image const result = await model.generateContent(prompt); for (const part of result.response.candidates[0].content.parts) { if (part.inlineData) { const imageData = part.inlineData.data; return { content: [{ type: "image", data: imageData, mimeType: `image/${outputFormat}` }] }; } } } catch (error) { console.error("Image generation error:", error); return { content: [{ type: "text", text: `Error generating image: ${error.message}` }] }; } }, { description: "Generate an image using Gemini API", parameters: { prompt: { type: "string", description: "The text description of the image to generate" }, aspectRatio: { type: "string", description: "Aspect ratio of the image (e.g., '1:1', '16:9')", optional: true }, outputFormat: { type: "string", description: "Output image format ('png' or 'jpeg')", optional: true } } } );