Gemini

How to connect MCP Servers to Google Gemini

Quick Overview

Connect your MCPHero servers to Google's Gemini ecosystem, including Gemini in Google AI Studio, the Gemini API, and Gemini integrations in IDEs. Google has adopted MCP as part of their agentic AI strategy.

MCP support in Gemini is available through Google AI Studio and the Gemini API. Consumer Gemini (gemini.google.com) does not yet support custom MCP servers.

Prerequisites


Option 1: Google AI Studio

Google AI Studio provides a web-based interface for testing Gemini with MCP tools.

Step 1: Get Your MCP Server Credentials

From your MCPHero dashboard:

  • Copy your server URL: https://api.mcphero.app/mcp/{server_id}/mcp
  • Generate an API key from the Keys tab

Step 2: Open Google AI Studio

Navigate to aistudio.google.com and sign in with your Google account.

Step 3: Configure MCP Server

  1. Create a new prompt or open an existing one
  2. Click Tools in the right sidebar
  3. Select Add MCP Server
  4. Enter your server details:
    • URL: https://api.mcphero.app/mcp/{server_id}/mcp
    • Authentication: Bearer Token
    • Token: Your MCPHero API key
  5. Click Connect

Step 4: Verify Tools

Once connected, your MCP tools appear in the Tools panel. You can:

  • View available tools and their schemas
  • Test individual tools
  • Use tools in conversations with Gemini

Option 2: Gemini API (Programmatic)

The google-genai SDK has native MCP support — you can pass an MCP ClientSession directly as a tool, and Gemini handles discovery and function calling automatically.

Step 1: Install Dependencies

pip install google-genai mcp

Step 2: Create the MCP Session

Connect to your MCPHero server using the MCP Python SDK:

from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

MCPHERO_URL = "https://api.mcphero.app/mcp/{server_id}/mcp"
MCPHERO_KEY = "YOUR_API_KEY"

Step 3: Call Gemini with MCP Tools

Pass the MCP session directly as a tool — Gemini discovers available tools and calls them automatically:

import asyncio
from google import genai

GEMINI_KEY = "YOUR_GEMINI_API_KEY"

client = genai.Client(api_key=GEMINI_KEY)

async def main():
    async with streamablehttp_client(
        url=MCPHERO_URL,
        headers={"Authorization": f"Bearer {MCPHERO_KEY}"}
    ) as (read, write, _):
        async with ClientSession(read, write) as session:
            await session.initialize()

            response = await client.aio.models.generate_content(
                model="gemini-2.5-flash",
                contents="What tools do you have available?",
                config=genai.types.GenerateContentConfig(
                    tools=[session],
                ),
            )
            print(response.text)

asyncio.run(main())

No manual tool conversion needed — the SDK handles MCP tool discovery and execution natively.


Option 3: Gemini in IDX

Google's Project IDX (cloud IDE) supports MCP servers for AI-assisted development.

Step 1: Open IDX Settings

In your IDX workspace:

  1. Click the gear icon to open Settings
  2. Navigate to AIMCP Servers

Step 2: Add MCPHero Server

Add a new server configuration:

{
  "servers": {
    "mcphero": {
      "url": "https://api.mcphero.app/mcp/{server_id}/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_API_KEY"
      }
    }
  }
}

Step 3: Use in Gemini Chat

Open Gemini in IDX and your MCP tools will be available for code generation, file operations, and other tasks defined in your server.


Troubleshooting

Connection Refused

  1. Verify your API key is valid and not expired
  2. Check the server URL includes the full path ending in /mcp
  3. Ensure your MCPHero server is deployed and running

Tools Not Appearing in AI Studio

  1. Refresh the Tools panel
  2. Check the browser console for connection errors
  3. Verify the MCP server responds to the tools/list method

Function Call Errors

  1. Ensure tool input schemas match expected parameters
  2. Check Gemini's function calling documentation for schema requirements
  3. Validate JSON schema compatibility between MCP and Gemini formats

Rate Limiting

Both Gemini API and MCPHero have rate limits:

  • Use exponential backoff for retries
  • Cache tool definitions to reduce MCP calls
  • Consider batching operations

Further Reading

On this page