generate_image
Create images from text descriptions using Gemini AI models. Specify prompts, aspect ratios, and resolutions to generate custom visual content.
Instructions
Generate an image from a text prompt using Gemini image models (Nano Banana Pro by default).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | Text description of the image to generate | |
| model | No | Image generation model (Nano Banana Pro by default) | gemini-3-pro-image-preview |
| aspectRatio | No | Aspect ratio of the generated image | 1:1 |
| imageSize | No | Image resolution (1K or 2K) | 1K |
Implementation Reference
- src/tools/generate-image.ts:26-58 (handler)The main handler function that executes the generate_image tool logic. It calls the Google GenAI API to generate an image from a text prompt, validates the response, extracts the image data, and returns it as MCP content.async ({ prompt, model, aspectRatio, imageSize }) => { try { const response = await ai.models.generateContent({ model, contents: prompt, config: { responseModalities: ['TEXT', 'IMAGE'], imageConfig: { aspectRatio, imageSize }, }, }); const image = extractImageFromResponse(response); if (!image) { return { content: [{ type: 'text' as const, text: 'No image was generated. Try a different prompt.' }], isError: true, }; } if (!validateImageSize(image.data)) { return { content: [{ type: 'text' as const, text: 'Generated image exceeds size limit. Try 1K imageSize or a simpler prompt.' }], isError: true, }; } return { content: [{ type: 'image' as const, data: image.data, mimeType: image.mimeType }], }; } catch (error) { return formatToolError(error); } },
- src/tools/generate-image.ts:11-25 (schema)Input schema definition for the generate_image tool, defining the prompt (required), model (with default gemini-3-pro-image-preview), aspectRatio (with default 1:1), and imageSize (with default 1K) parameters.{ title: 'Generate Image', description: 'Generate an image from a text prompt using Gemini image models (Nano Banana Pro by default).', inputSchema: { prompt: z.string().min(1).describe('Text description of the image to generate'), model: ImageModel.default('gemini-3-pro-image-preview').describe('Image generation model (Nano Banana Pro by default)'), aspectRatio: AspectRatio.default('1:1').describe('Aspect ratio of the generated image'), imageSize: ImageSize.default('1K').describe('Image resolution (1K or 2K)'), }, annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: true, }, },
- src/types.ts:12-24 (schema)Type definitions for the generate_image tool schema - ImageModel enum (gemini-2.5-flash-image, gemini-3-pro-image-preview), AspectRatio enum (various ratios), and ImageSize enum (1K, 2K, 4K).export const ImageModel = z.enum([ 'gemini-2.5-flash-image', 'gemini-3-pro-image-preview', ]); export type ImageModel = z.infer<typeof ImageModel>; export const AspectRatio = z.enum([ '1:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9', ]); export type AspectRatio = z.infer<typeof AspectRatio>; export const ImageSize = z.enum(['1K', '2K', '4K']); export type ImageSize = z.infer<typeof ImageSize>;
- src/index.ts:29-29 (registration)Registration of the generate_image tool with the MCP server, passing the server instance and AI client to the register function.registerGenerateImage(server, ai);
- src/utils/image.ts:8-20 (helper)Helper function extractImageFromResponse that parses the Google GenAI response to extract the inline image data and MIME type from the response parts.export function extractImageFromResponse(response: any): { data: string; mimeType: string } | null { const parts = response?.candidates?.[0]?.content?.parts; if (!parts) return null; for (const part of parts) { if (part.inlineData) { return { data: part.inlineData.data, mimeType: part.inlineData.mimeType, }; } } return null; }