Intro to Model Context Protocol (MCP) integration with Vertex AI

Source notebook

Repo path: gemini/mcp/intro_to_mcp.ipynb · Open on GitHub · intermediate

Shows how to connect Gemini on Vertex AI to custom and prebuilt MCP servers.

Summary

This notebook introduces Model Context Protocol integration with Vertex AI and Gemini. It installs google-genai, mcp, geopy, and uv, initializes a Vertex AI Gemini client, builds a custom weather MCP server, and runs an agent loop that lists tools, executes MCP tool calls, and returns tool responses to Gemini. It also demonstrates using a prebuilt BigQuery MCP server through uvx.

Key code patterns

Initialize Vertex AI Gemini client

from google import genai
 
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)

Configures google-genai to call Gemini through Vertex AI with project and region settings.

Define MCP server tools

from mcp.server.fastmcp import FastMCP
 
mcp = FastMCP("weather")
 
@mcp.tool()
async def get_alerts(state: str) -> str:
    endpoint = f"/alerts/active/area/{state.upper()}"
    data = await get_weather_response(endpoint)
    return format_alerts(data)

Exposes external weather functions as MCP tools callable by the model.

Execute Gemini tool calls via MCP

for func_call in function_calls:
    tool_name = func_call.name
    args = func_call.args if isinstance(func_call.args, dict) else {}
    tool_result = await session.call_tool(tool_name, args)
    tool_response_parts.append(
        types.Part.from_function_response(name=tool_name, response={"result": result_text})
    )

Bridges Gemini function calls to MCP session tool execution and packages results back as function responses.

Connect to custom stdio MCP server

weather_server_params = StdioServerParameters(
    command="python",
    args=["./server/weather_server.py"],
)
 
async with stdio_client(weather_server_params) as (read, write):
    async with ClientSession(read, write) as session:
        res = await run_agent_loop(prompt, client, session)

Starts a local MCP server over stdio and runs the Gemini agent loop against it.

Use prebuilt BigQuery MCP server

bq_server_params = StdioServerParameters(
    command="uvx",
    args=["mcp-server-bigquery", "--project", PROJECT_ID, "--location", LOCATION],
    env=None,
)

Shows how to launch a packaged MCP server that exposes BigQuery query, table listing, and schema tools.

Models & APIs used

  • Models: gemini-2.0-flash-001
  • APIs / services: Vertex AI, BigQuery
  • SDKs / libraries: google-genai, mcp, geopy, uv

When to use this

Use this pattern when Gemini on Vertex AI needs standardized tool access through custom or prebuilt MCP servers.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Vertex AI API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be set directly or through GOOGLE_CLOUD_PROJECT.
  • LOCATION defaults to us-central1 from GOOGLE_CLOUD_REGION when not provided.
  • The custom weather server path may need to be updated to the full absolute path.
  • The agent loop limits consecutive tool execution with DEFAULT_MAX_TOOL_TURNS = 5.
  • The BigQuery MCP server is launched with uvx and needs project and location arguments.

Best practices

  • Use environment variables for project and region when explicit values are not provided.
  • Validate and normalize tool inputs such as two-letter US state codes.
  • Handle HTTP status errors, timeouts, request errors, JSON decode errors, and unexpected exceptions in external API calls.
  • Return structured tool responses with either result or error payloads.
  • Use a maximum tool-turn limit to avoid endless tool-calling loops.
  • Close the shared async HTTP client during server shutdown.