Skip to main content
Glama

generate_image

Generate images using Google Gemini and save to a specified file path. Automatically resolves filename conflicts by appending a numeric suffix.

Instructions

Generate an image with Gemini and save it to output_path.

Known model values (override via model):

  • gemini-2.5-flash-image (default — Nano Banana, fastest/cheapest)

  • gemini-3.1-flash-image-preview (Nano Banana 2, higher quality)

  • gemini-3-pro-image-preview (Nano Banana Pro, top quality)

If output_path already exists, a numeric suffix is appended (foo.png -> foo-1.png) and the final path is returned.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYes
output_pathYes
modelNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The @mcp.tool() decorator registers 'generate_image' as an MCP tool on the FastMCP server instance.
    @mcp.tool()
  • The main handler function that takes a prompt, output_path, and optional model, generates an image using Google's Gemini API (genai), extracts the image bytes from the response, and writes them to disk. Handles file collision by appending a numeric suffix and returns the final path.
    def generate_image(prompt: str, output_path: str, model: str | None = None) -> str:
        """Generate an image with Gemini and save it to output_path.
    
        Known model values (override via `model`):
          - gemini-2.5-flash-image           (default — Nano Banana, fastest/cheapest)
          - gemini-3.1-flash-image-preview   (Nano Banana 2, higher quality)
          - gemini-3-pro-image-preview       (Nano Banana Pro, top quality)
    
        If output_path already exists, a numeric suffix is appended
        (foo.png -> foo-1.png) and the final path is returned.
        """
        api_key = os.environ.get("GEMINI_API_KEY")
        if not api_key:
            raise RuntimeError("GEMINI_API_KEY is not set in the MCP server environment.")
    
        target = Path(output_path).expanduser().resolve()
        target.parent.mkdir(parents=True, exist_ok=True)
        final = _resolve_collision(target)
    
        client = genai.Client(api_key=api_key)
        response = client.models.generate_content(
            model=model or DEFAULT_MODEL,
            contents=prompt,
        )
    
        image_bytes = None
        for part in response.candidates[0].content.parts:
            inline = getattr(part, "inline_data", None)
            if inline and inline.data:
                image_bytes = inline.data
                break
    
        if image_bytes is None:
            text_parts = [
                p.text for p in response.candidates[0].content.parts
                if getattr(p, "text", None)
            ]
            raise RuntimeError(
                f"No image in response. Model said: {' '.join(text_parts) or '(empty)'}"
            )
    
        final.write_bytes(image_bytes)
        note = "" if final == target else f" (renamed from {target.name} to avoid overwrite)"
        return f"wrote: {final}{note}"
  • Default model constant used when no model override is provided.
    DEFAULT_MODEL = "gemini-2.5-flash-image"
  • Helper function _resolve_collision that appends a numeric suffix (e.g., -1, -2) to avoid overwriting existing files.
    def _resolve_collision(path: Path) -> Path:
        if not path.exists():
            return path
        stem, suffix, parent = path.stem, path.suffix, path.parent
        i = 1
        while True:
            candidate = parent / f"{stem}-{i}{suffix}"
            if not candidate.exists():
                return candidate
            i += 1
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Discloses the non-overwriting behavior (appending numeric suffix) and indicates that the final path is returned. With no annotations, this is above average transparency, though lacks detail on failure handling or authorization.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Economical two-sentence structure: first sentence gives the main action, then bullet-style model list and a clear conflict rule. No filler.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool has an output schema (which documents return structure) and only three parameters, the description covers model selection and file conflict behavior. Missing details on error handling or input format, but sufficient for basic usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Adds meaning to the 'model' parameter by listing known values and their trade-offs, but 'prompt' and 'output_path' receive no extra description beyond their names. Schema coverage is 0%, so description partially compensates.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states the action ('generate an image') and the destination ('save to output_path'), with specific reference to Gemini. No siblings exist, so differentiation is not needed.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides explicit model variants with nicknames and quality/cost tiers, plus conflict resolution behavior when output path exists. No exclusions or alternatives needed due to no siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/DenialGelon/nano-banana-mcp'

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