Skip to main content
Glama

generate_image

Create images from text prompts, edit existing images using natural language, or combine multiple images with customizable aspect ratios.

Instructions

Generate or edit images using Gemini 2.5 Flash Image (Nano Banana). Supports text-to-image generation, image editing with natural language prompts, and multi-image composition. All generated images include a SynthID watermark.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesText prompt describing the image to generate or edits to make
input_imagesNoOptional array of file paths to input images for editing or composition
aspect_ratioNoOutput aspect ratio. Options: 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:91:1
output_pathYesPath where the generated image will be saved (must end in .png)output.png
image_onlyNoIf true, requests image-only output without text response

Implementation Reference

  • index.js:93-208 (handler)
    The core handler function for the 'generate_image' tool. It destructures arguments, validates inputs, constructs the Gemini API payload with optional input images, makes the HTTP request, extracts and saves the generated image as PNG, and returns a formatted text response or error.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== "generate_image") { throw new Error(`Unknown tool: ${request.params.name}`); } const { prompt, input_images = [], aspect_ratio = "1:1", output_path = "output.png", image_only = false, } = request.params.arguments; if (!prompt) { throw new Error("prompt is required"); } if (!output_path.endsWith(".png")) { throw new Error("output_path must end with .png"); } if (!ASPECT_RATIOS.includes(aspect_ratio)) { throw new Error(`Invalid aspect_ratio. Must be one of: ${ASPECT_RATIOS.join(", ")}`); } try { // Build request parts const parts = [{ text: prompt }]; // Add input images if provided for (const imagePath of input_images) { const imageData = await this.encodeImage(imagePath); parts.push(imageData); } // Build API request payload const payload = { contents: [{ parts }], generationConfig: { responseModalities: image_only ? ["Image"] : ["Text", "Image"], imageConfig: { aspectRatio: aspect_ratio, }, }, }; // Make API request const url = `${BASE_URL}?key=${GEMINI_API_KEY}`; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(payload), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${response.status} ${response.statusText}\n${errorText}`); } const result = await response.json(); // Extract and save image let textResponse = null; let imageSaved = false; if (result.candidates) { for (const candidate of result.candidates) { if (candidate.content) { for (const part of candidate.content.parts || []) { if (part.inlineData) { await this.saveImage(part.inlineData, output_path); imageSaved = true; } else if (part.text) { textResponse = part.text; } } } } } if (!imageSaved) { throw new Error("No image was generated in the response"); } // Return success response const responseText = [ `✓ Image generated successfully!`, ` Saved to: ${output_path}`, ` Aspect ratio: ${aspect_ratio}`, textResponse ? ` AI response: ${textResponse}` : null, ] .filter(Boolean) .join("\n"); return { content: [ { type: "text", text: responseText, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error generating image: ${error.message}`, }, ], isError: true, }; } });
  • Input schema defining parameters for generate_image: prompt (required), input_images (optional), aspect_ratio, output_path (required), image_only.
    inputSchema: { type: "object", properties: { prompt: { type: "string", description: "Text prompt describing the image to generate or edits to make", }, input_images: { type: "array", items: { type: "string", }, description: "Optional array of file paths to input images for editing or composition", }, aspect_ratio: { type: "string", enum: ASPECT_RATIOS, description: "Output aspect ratio. Options: " + ASPECT_RATIOS.join(", "), default: "1:1", }, output_path: { type: "string", description: "Path where the generated image will be saved (must end in .png)", default: "output.png", }, image_only: { type: "boolean", description: "If true, requests image-only output without text response", default: false, }, }, required: ["prompt", "output_path"],
  • index.js:48-91 (registration)
    Tool registration via ListToolsRequestSchema handler, providing name, description, and schema for 'generate_image'.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: "generate_image", description: "Generate or edit images using Gemini 2.5 Flash Image (Nano Banana). " + "Supports text-to-image generation, image editing with natural language prompts, " + "and multi-image composition. All generated images include a SynthID watermark.", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "Text prompt describing the image to generate or edits to make", }, input_images: { type: "array", items: { type: "string", }, description: "Optional array of file paths to input images for editing or composition", }, aspect_ratio: { type: "string", enum: ASPECT_RATIOS, description: "Output aspect ratio. Options: " + ASPECT_RATIOS.join(", "), default: "1:1", }, output_path: { type: "string", description: "Path where the generated image will be saved (must end in .png)", default: "output.png", }, image_only: { type: "boolean", description: "If true, requests image-only output without text response", default: false, }, }, required: ["prompt", "output_path"], }, }, ], }));
  • Helper methods encodeImage and saveImage used by the handler for processing input/output images.
    async encodeImage(imagePath) { const absolutePath = resolve(imagePath); const imageBuffer = await readFile(absolutePath); const base64Data = imageBuffer.toString("base64"); // Determine MIME type from extension const mimeTypes = { ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp", ".gif": "image/gif", }; const ext = imagePath.toLowerCase().match(/\.\w+$/)?.[0]; const mimeType = mimeTypes[ext] || "image/jpeg"; return { inlineData: { mimeType, data: base64Data, }, }; } async saveImage(inlineData, outputPath) { const absolutePath = resolve(outputPath); const imageBuffer = Buffer.from(inlineData.data, "base64"); await writeFile(absolutePath, imageBuffer); }
Install Server

Other Tools

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/brunoqgalvao/gemini-image-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server