Skip to main content
Glama
qhdrl12

Gemini Image Generator MCP Server

transform_image_from_encoded

Modify existing images using text prompts with Google's Gemini AI. Provide a base64-encoded image and describe desired changes to generate transformed versions.

Instructions

Transform an existing image based on the given text prompt using Google's Gemini model.

Args:
    encoded_image: Base64 encoded image data with header. Must be in format:
                "data:image/[format];base64,[data]"
                Where [format] can be: png, jpeg, jpg, gif, webp, etc.
    prompt: Text prompt describing the desired transformation or modifications
    
Returns:
    Path to the transformed image file saved on the server

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
encoded_imageYes
promptYes

Implementation Reference

  • The primary handler function for the 'transform_image_from_encoded' MCP tool. Decorated with @mcp.tool(), it handles base64-encoded image input, prompt translation, image loading, and delegates to transformation processing using Gemini AI.
    @mcp.tool()
    async def transform_image_from_encoded(encoded_image: str, prompt: str) -> Tuple[bytes, str]:
        """Transform an existing image based on the given text prompt using Google's Gemini model.
    
        Args:
            encoded_image: Base64 encoded image data with header. Must be in format:
                        "data:image/[format];base64,[data]"
                        Where [format] can be: png, jpeg, jpg, gif, webp, etc.
            prompt: Text prompt describing the desired transformation or modifications
            
        Returns:
            Path to the transformed image file saved on the server
        """
        try:
            logger.info(f"Processing transform_image_from_encoded request with prompt: {prompt}")
    
            # Load and validate the image
            source_image, _ = await load_image_from_base64(encoded_image)
            
            # Translate the prompt to English
            translated_prompt = await translate_prompt(prompt)
            
            # Process the transformation
            return await process_image_transform(source_image, translated_prompt, prompt)
            
        except Exception as e:
            error_msg = f"Error transforming image: {str(e)}"
            logger.error(error_msg)
            return error_msg
  • Helper function specifically used by the tool handler to parse and validate the base64-encoded image input into a PIL Image object.
    async def load_image_from_base64(encoded_image: str) -> Tuple[PIL.Image.Image, str]:
        """Load an image from a base64-encoded string.
        
        Args:
            encoded_image: Base64 encoded image data with header
            
        Returns:
            Tuple containing the PIL Image object and the image format
        """
        if not encoded_image.startswith('data:image/'):
            raise ValueError("Invalid image format. Expected data:image/[format];base64,[data]")
        
        try:
            # Extract the base64 data from the data URL
            image_format, image_data = encoded_image.split(';base64,')
            image_format = image_format.replace('data:', '')  # Get the MIME type e.g., "image/png"
            image_bytes = base64.b64decode(image_data)
            source_image = PIL.Image.open(BytesIO(image_bytes))
            logger.info(f"Successfully loaded image with format: {image_format}")
            return source_image, image_format
        except ValueError as e:
            logger.error(f"Error: Invalid image data format: {str(e)}")
            raise ValueError("Invalid image data format. Image must be in format 'data:image/[format];base64,[data]'")
        except base64.binascii.Error as e:
            logger.error(f"Error: Invalid base64 encoding: {str(e)}")
            raise ValueError("Invalid base64 encoding. Please provide a valid base64 encoded image.")
        except PIL.UnidentifiedImageError:
            logger.error("Error: Could not identify image format")
            raise ValueError("Could not identify image format. Supported formats include PNG, JPEG, GIF, WebP.")
        except Exception as e:
            logger.error(f"Error: Could not load image: {str(e)}")
            raise
  • Key helper function called by the tool handler to perform the actual Gemini-powered image transformation, integrating prompts and source image.
    async def process_image_transform(
        source_image: PIL.Image.Image, 
        optimized_edit_prompt: str, 
        original_edit_prompt: str
    ) -> Tuple[bytes, str]:
        """Process image transformation with Gemini.
        
        Args:
            source_image: PIL Image object to transform
            optimized_edit_prompt: Optimized text prompt for transformation
            original_edit_prompt: Original user prompt for naming
            
        Returns:
            Path to the transformed image file
        """
        # Create prompt for image transformation
        edit_instructions = get_image_transformation_prompt(optimized_edit_prompt)
        
        # Process with Gemini and return the result
        return await process_image_with_gemini(
            [edit_instructions, source_image],
            original_edit_prompt
        )
  • Helper function that generates the detailed prompt template used for image transformation requests to the Gemini model.
    def get_image_transformation_prompt(prompt: str) -> str:
        """Create a detailed prompt for image transformation.
        
        Args:
            prompt: text prompt
            
        Returns:
            A comprehensive prompt for Gemini image transformation
        """
        return f"""You are an expert image editing AI. Please edit the provided image according to these instructions:
    
    EDIT REQUEST: {prompt}
    
    IMPORTANT REQUIREMENTS:
    1. Make substantial and noticeable changes as requested
    2. Maintain high image quality and coherence 
    3. Ensure the edited elements blend naturally with the rest of the image
    4. Do not add any text to the image
    5. Focus on the specific edits requested while preserving other elements
    
    The changes should be clear and obvious in the result."""
Behavior3/5

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

With no annotations provided, the description carries full burden. It discloses the tool uses Google's Gemini model and that it saves the transformed image on the server, which are useful behavioral traits. However, it doesn't mention rate limits, authentication requirements, file size limits, or potential side effects of the transformation process.

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?

The description is efficiently structured with a clear opening sentence stating the purpose, followed by well-organized sections for Args and Returns. Every sentence earns its place by providing essential information without redundancy.

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?

For a 2-parameter tool with no annotations and no output schema, the description provides good coverage of purpose, parameters, and basic behavior. It explains what the tool does, how to format inputs, and what to expect as output. The main gap is lack of information about error conditions, performance characteristics, or more detailed behavioral constraints.

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

Parameters5/5

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

With 0% schema description coverage, the description fully compensates by providing detailed semantics for both parameters. It specifies the exact format required for encoded_image (including header format and supported image types) and explains what the prompt parameter should contain. This adds significant value beyond the bare schema.

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?

The description clearly states the tool's purpose with specific verb ('Transform') and resource ('an existing image'), and distinguishes it from siblings by specifying it uses encoded image data rather than text or file inputs. The mention of Google's Gemini model adds technical specificity.

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?

The description provides clear context about when to use this tool (transforming existing images with encoded data) and implicitly distinguishes it from siblings (generate_image_from_text for text-to-image, transform_image_from_file for file-based transformation). However, it doesn't explicitly state when NOT to use this tool or mention specific prerequisites.

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/qhdrl12/mcp-server-gemini-image-generator'

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