MCP Imagen Server
This tool provides image generation capabilities using Google’s Imagen models (via Vertex AI). It is one of the MCP tools for Google Cloud Genmedia services, functioning as an MCP server component to allow LLMs and other MCP clients to generate images from text prompts.
MCP Tool Definition
Section titled “MCP Tool Definition”The following tool is exposed by this server:
1. imagen_t2i
Section titled “1. imagen_t2i”- Description: Generates an image based on a text prompt using Google’s Imagen models. The image can be returned as base64 data, saved to a local directory, or stored in a Google Cloud Storage bucket.
- Handler:
imagenGenerationHandler(via wrapper) - Parameters:
prompt(string, required): Prompt for text to image generation.model(string, optional): The model for image generation. Can be a full model ID or a common alias. See themcp-common/models.gofile for a complete list of supported models and aliases.num_images(number, optional): Number of images to generate.- Default:
1 - Note: The maximum number of images depends on the selected model (see table above).
- Default:
aspect_ratio(string, optional): The aspect ratio for the generated image.- Default:
"1:1" - Common values:
"1:1"(square),"16:9"(widescreen),"9:16"(portrait)
- Default:
gcs_bucket_uri(string, optional): GCS URI prefix to store the generated images (e.g., “your-bucket/outputs/” or “gs://your-bucket/outputs/”). If provided, images are saved to GCS instead of returning bytes directly.output_directory(string, optional): If provided, specifies a local directory to save the generated image(s) to.
Resources
Section titled “Resources”The server exposes the following resources:
imagen://models: Returns a JSON object detailing the supported Imagen models and their aliases. You can read this resource usingmcptools resources get imagen://models ./mcp-imagen-go.imagen://segmentation_classes: Returns a JSON object of supported classes for semantic masking in image editing.
Environment Variable Configuration
Section titled “Environment Variable Configuration”The tool utilizes the following environment variables:
GOOGLE_CLOUD_PROJECT(string): Required. Your Google Cloud Project ID. The application will terminate if this is not set. Note:PROJECT_IDis also supported as a fallback.- Override: You can override this globally for this specific server by setting
IMAGEN_PROJECT_ID.
- Override: You can override this globally for this specific server by setting
GOOGLE_CLOUD_LOCATION(string): The preferred Google Cloud location/region for Vertex AI services.- Default:
"us-central1" - Fallback:
LOCATIONis also supported as a fallback forGOOGLE_CLOUD_LOCATION. - Override: You can override this globally for this specific server by setting
IMAGEN_LOCATION.
- Default:
GENMEDIA_BUCKET(string): An optional default Google Cloud Storage bucket to use for GCS outputs ifgcs_bucket_uriis not specified in the tool request. The pathimagen_outputs/will be appended to this bucket.- Default:
""(empty string, meaning no default GCS output path is formed from this variable unlessgcs_bucket_uriis also absent).
- Default:
ALLOW_UNSAFE_MODELS(boolean): Optional (true/false). Allows users to bypass strict local model constraint validation, enabling them to test experimental or pre-release model strings that are not yet hardcoded in the registry.- Default:
false
- Default:
ENABLE_OPTIONAL_HEADER_CAPTURE(boolean): Optional (true/false). Intended for internal debugging. When set totrue, the server intercepts API requests and injects the raw ADC Bearer token to capture and surface thex-goog-sherlog-linkheader in the tool output. This feature is supported for Imagen.- Default:
false
- Default:
PORT(string, for HTTP transport): The port for the HTTP server to listen on.- Default:
"8080"
- Default:
Transports Supported
Section titled “Transports Supported”stdio(default)sse(Server-Sent Events)http(Streamable HTTP)
CORS is enabled for the HTTP transport, allowing all origins by default.
Build the tool using go build or go install.
- STDIO (Default):
Terminal window ./mcp-imagen-go# or./mcp-imagen-go -transport stdio - HTTP:
The MCP server will be available at
Terminal window ./mcp-imagen-go -transport http# Optionally set PORT environment variable, e.g., PORT=8083 ./mcp-imagen-go -transport httphttp://localhost:<PORT>/mcp. - SSE (Server-Sent Events):
The MCP server will be available at
Terminal window ./mcp-imagen-go -transport sse# SSE server typically runs on port 8081 by default in this configuration.http://localhost:8081.
Example
Section titled “Example”Imagen Text-to-Image Generation
Section titled “Imagen Text-to-Image Generation”{ "method": "tools/call", "params": { "name": "imagen_t2i", "arguments": { "prompt": "A photorealistic image of a cat wearing a small wizard hat, sitting on a pile of ancient books.", "model": "imagen-3.0-generate-002", "num_images": 1, "aspect_ratio": "1:1", "output_directory": "./imagen_output" } }}