Skip to main content
Glama

generate_image

Generate images from text prompts, edit existing images using natural language, and compose multiple images with flexible aspect ratios. All outputs include SynthID watermarking.

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
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
image_onlyNoIf true, requests image-only output without text response
input_imagesNoOptional array of file paths to input images for editing or composition
output_pathYesPath where the generated image will be saved (must end in .png)output.png
promptYesText prompt describing the image to generate or edits to make

Implementation Reference

  • index.js:93-208 (handler)
    Main handler function for the 'generate_image' tool. Validates input, constructs payload for Gemini API, sends request, extracts and saves the generated image, and returns success/error response.
    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, }; } });
  • JSON schema definition for the generate_image tool input parameters, including prompt, input images, aspect ratio, output path, and image_only flag.
    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"], }, },
  • index.js:48-91 (registration)
    Registers the generate_image tool in the ListToolsRequestSchema handler for MCP clients to discover the tool.
    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 function to save the base64-encoded image data from Gemini response to the output file path.
    async saveImage(inlineData, outputPath) { const absolutePath = resolve(outputPath); const imageBuffer = Buffer.from(inlineData.data, "base64"); await writeFile(absolutePath, imageBuffer); }
  • Helper function to read input image files, encode them to base64, determine MIME type, and format for Gemini API request.
    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, }, }; }

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