MCP Server with Gemini Enterprise

Source notebook

Repo path: search/gemini-enterprise/mcp_with_gemini_enterprise.ipynb · Open on GitHub · advanced

Builds an MCP HR leave tool on Cloud Run, connects it to an ADK Gemini agent, and registers it with Gemini Enterprise.

Summary

This notebook demonstrates an end-to-end Gemini Enterprise integration for an employee leave application. It creates a BigQuery table, deploys a FastMCP server to Cloud Run, connects an ADK agent to the MCP server over SSE, tests the agent locally, deploys it to Agent Engine, and registers it with Gemini Enterprise through Discovery Engine REST APIs.

Key code patterns

BigQuery MCP tool

@mcp.tool()
def apply_leave(employee_id: int, start_date: str, end_date: str):
    data_to_insert = [{
        "employee_id": employee_id,
        "leave_start_date": start_date,
        "leave_end_date": end_date,
    }]
    errors = client.insert_rows_json(table_id, data_to_insert)

Exposes a business action as an MCP tool and persists tool calls to BigQuery.

SSE MCP server

sse = SseServerTransport("/messages/")
 
async def handle_sse(request: Request) -> None:
    _server = mcp._mcp_server
    async with sse.connect_sse(request.scope, request.receive, request._send) as (reader, writer):
        await _server.run(reader, writer, _server.create_initialization_options())

Provides the MCP transport endpoint that ADK uses to communicate with the deployed tool server.

Authenticated Cloud Run MCP connection

auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, MCP_SERVER_URL)
headers = {"Authorization": f"Bearer {id_token}"}
 
tools = MCPToolset(
    connection_params=SseServerParams(url=MCP_SERVER_URL, headers=headers)
)

Shows how the ADK toolset reaches a private Cloud Run MCP server using an ID token.

ADK agent with MCP tools

root_agent = Agent(
    model="gemini-2.5-pro",
    name="hr_agent",
    instruction=agent_prompt,
    tools=[tools],
)

Registers the MCP toolset with a Gemini-powered ADK agent for leave-request handling.

Agent Engine deployment

remote_app = agent_engines.create(
    display_name="HR Agent V3",
    agent_engine=app,
    requirements=[
        "google-adk (==1.5.0)",
        "google-genai (==1.24.0)",
        "pydantic (==2.11.7)",
    ],
)

Packages and deploys the ADK app as a managed Agent Engine resource.

Models & APIs used

  • Models: gemini-2.5-pro
  • APIs / services: BigQuery, Cloud Run, Artifact Registry, Cloud Build, IAM API, Vertex AI, Agent Engine, Cloud Storage, Discovery Engine, Gemini Enterprise
  • SDKs / libraries: google-adk, mcp, google-cloud-bigquery, google-genai, vertexai, google-cloud-aiplatform

When to use this

Use this pattern when a Gemini Enterprise agent needs to call a secured business application tool and store structured results in BigQuery.

Gotchas & caveats

  • Project ID, project number, Cloud Run URL, GCS staging bucket, OAuth client ID, OAuth client secret, Gemini Enterprise engine ID, and reasoning engine ID must be replaced.
  • The Cloud Run service is deployed with —no-allow-unauthenticated, so requests need an Authorization bearer token.
  • The notebook states Cloud Run authorization headers expire after one hour.
  • Agent Engine deployment uses us-central1 and requires a staging bucket.
  • The notebook pins different google-adk versions in different steps: 0.3.0, 1.4.2, and 1.5.0.

Best practices

  • Keep the MCP server private on Cloud Run with —no-allow-unauthenticated.
  • Use environment variables for project, dataset, table, host, port, and region configuration.
  • Test the ADK agent locally with reasoning_engines.AdkApp before deploying to Agent Engine.
  • Declare Agent Engine runtime requirements explicitly when creating the remote app.
  • Use a structured agent instruction that asks for employee ID, start date, and end date before applying leave.