Skip to main content
Glama

generate_image

Create custom images from text descriptions using AI generation for visual content creation and design projects.

Instructions

Generate a NEW image from text prompt. Use this ONLY when creating a completely new image, not when modifying an existing one.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesText prompt describing the NEW image to create from scratch

Implementation Reference

  • Core handler function for 'generate_image' tool. Validates config, generates image via Gemini API from prompt, saves PNG to images directory, constructs response with status text and embedded image.
    private async generateImage(request: CallToolRequest): Promise<CallToolResult> { if (!this.ensureConfigured()) { throw new McpError(ErrorCode.InvalidRequest, "Gemini API token not configured. Use configure_gemini_token first."); } const { prompt } = request.params.arguments as { prompt: string }; try { const response = await this.genAI!.models.generateContent({ model: "gemini-2.5-flash-image-preview", contents: prompt, }); // Process response to extract image data const content: any[] = []; const savedFiles: string[] = []; let textContent = ""; // Get appropriate save directory based on OS const imagesDir = this.getImagesDirectory(); // Create directory await fs.mkdir(imagesDir, { recursive: true, mode: 0o755 }); if (response.candidates && response.candidates[0]?.content?.parts) { for (const part of response.candidates[0].content.parts) { // Process text content if (part.text) { textContent += part.text; } // Process image data if (part.inlineData?.data) { const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const randomId = Math.random().toString(36).substring(2, 8); const fileName = `generated-${timestamp}-${randomId}.png`; const filePath = path.join(imagesDir, fileName); const imageBuffer = Buffer.from(part.inlineData.data, 'base64'); await fs.writeFile(filePath, imageBuffer); savedFiles.push(filePath); this.lastImagePath = filePath; // Add image to MCP response content.push({ type: "image", data: part.inlineData.data, mimeType: part.inlineData.mimeType || "image/png", }); } } } // Build response content let statusText = `🎨 Image generated with nano-banana (Gemini 2.5 Flash Image)!\n\nPrompt: "${prompt}"`; if (textContent) { statusText += `\n\nDescription: ${textContent}`; } if (savedFiles.length > 0) { statusText += `\n\n📁 Image saved to:\n${savedFiles.map(f => `- ${f}`).join('\n')}`; statusText += `\n\n💡 View the image by:`; statusText += `\n1. Opening the file at the path above`; statusText += `\n2. Clicking on "Called generate_image" in Cursor to expand the MCP call details`; statusText += `\n\n🔄 To modify this image, use: continue_editing`; statusText += `\n📋 To check current image info, use: get_last_image_info`; } else { statusText += `\n\nNote: No image was generated. The model may have returned only text.`; statusText += `\n\n💡 Tip: Try running the command again - sometimes the first call needs to warm up the model.`; } // Add text content first content.unshift({ type: "text", text: statusText, }); return { content }; } catch (error) { console.error("Error generating image:", error); throw new McpError( ErrorCode.InternalError, `Failed to generate image: ${error instanceof Error ? error.message : String(error)}` ); } }
  • JSON schema defining the input parameters for the generate_image tool: requires a 'prompt' string.
    inputSchema: { type: "object", properties: { prompt: { type: "string", description: "Text prompt describing the NEW image to create from scratch", }, }, required: ["prompt"], },
  • src/index.ts:71-84 (registration)
    Registration of 'generate_image' tool in the ListTools response, including name, description, and input schema.
    { name: "generate_image", description: "Generate a NEW image from text prompt. Use this ONLY when creating a completely new image, not when modifying an existing one.", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "Text prompt describing the NEW image to create from scratch", }, }, required: ["prompt"], }, },
  • src/index.ts:159-160 (registration)
    Switch case in CallTool handler that routes 'generate_image' calls to the generateImage method.
    case "generate_image": return await this.generateImage(request);

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/ConechoAI/Nano-Banana-MCP'

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