Skip to main content
Glama

generate_image

Create custom images from text descriptions using AI generation. Enter a prompt to produce original visual content for projects and designs.

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

  • The primary handler function for the generate_image tool. It validates configuration, calls the Gemini API to generate an image from the prompt, processes the response (saving the image file and extracting inline data), builds a formatted text response with file paths and instructions, and returns the MCP tool result.
    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)}`
        );
      }
    }
  • src/index.ts:71-84 (registration)
    Registration of the generate_image tool in the ListToolsRequestSchema handler, 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"],
      },
    },
  • Input schema definition for the generate_image tool, specifying a required 'prompt' string parameter.
    inputSchema: {
      type: "object",
      properties: {
        prompt: {
          type: "string",
          description: "Text prompt describing the NEW image to create from scratch",
        },
      },
      required: ["prompt"],
    },
  • Dispatcher case in the CallToolRequestSchema 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