---
title: "Connecting MCP Servers to ADK Agents"
date: "2026-11-28T09:25:00+07:00"
description: "Attach any MCP server to an ADK agent with McpToolset — transport choices, connection lifecycle, auth, resources, and how the three MCP integration patterns differ."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/connect-mcp-servers-to-adk-agents.jpg"
alt: "Interlocking circuit modules plugging into a central agent core"
tags: ['google-adk', 'adk-tools', 'mcp', 'model-context-protocol', 'integration']
---

Every MCP server you connect to is a tool surface that already exists somewhere else — a Postgres server maintained by the database team, a filesystem server shipping with npm, an internal service behind Streamable HTTP. `McpToolset` is ADK's client for that surface: it negotiates the protocol, discovers the tools, adapts their schemas into ADK tools, forwards calls, and shuts the connection down cleanly. No wrappers, no per-service glue.

This tutorial covers installing the MCP extra, wiring `McpToolset` over stdio and HTTP transports, choosing between the three MCP integration patterns ADK offers, and the lifecycle rules that separate a demo from a deployment.

## One toolset, one handshake

```python
from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from mcp import StdioServerParameters

root_agent = LlmAgent(
    model="gemini-flash-latest",
    name="filesystem_assistant",
    instruction="Help users manage local files.",
    tools=[
        McpToolset(
            connection_params=StdioConnectionParams(
                server_params=StdioServerParameters(
                    command="npx",
                    args=["-y", "@modelcontextprotocol/server-filesystem", TARGET_FOLDER],
                ),
            ),
            tool_filter=["list_directory", "read_file"],
        )
    ],
)
```

Under the hood the sequence is fixed: **initialize → protocol handshake → `list_tools` → schemas adapted into ADK tools → `call_tool` per invocation**. Constructing the toolset does *not* connect — the session opens the first time the agent actually needs the tools, and `McpToolset` handles shutdown when the agent or process ends.

Setup prerequisites: `pip install "google-adk[mcp]"`, Python 3.10+, and Node.js/`npx` on PATH for any npm-packaged server.

## Choosing a transport

| Transport | `connection_params` | Where it runs | Auth | Notes |
|---|---|---|---|---|
| **stdio** | `StdioConnectionParams(server_params=StdioServerParameters(...))` | Local subprocess | The server's own env vars | Zero network overhead; dev and single-host deployments |
| **SSE** | `SseConnectionParams(url=..., headers=..., timeout=...)` | Remote | Bearer header or `auth_scheme` | Legacy streaming protocol; still common |
| **Streamable HTTP** | `StreamableHTTPConnectionParams(url=..., headers=..., timeout=5, sse_read_timeout=300)` | Remote | Bearer header or `auth_scheme` | Preferred for stateless, scalable deployments |

Remote example — note that only the credential lives in a header, not the tool logic:

```python
root_agent = LlmAgent(
    model="gemini-flash-latest",
    name="travel_planner",
    instruction="Plan routes and search locations using Google Maps.",
    tools=[
        McpToolset(
            connection_params=StreamableHTTPConnectionParams(
                url="https://mapstools.googleapis.com/mcp",
                headers={"X-Goog-Api-Key": os.getenv("GOOGLE_MAPS_API_KEY")},
                timeout=5,
                sse_read_timeout=300,
            )
        )
    ],
)
```

A practical pattern is environment detection: pick stdio when running locally (`K_SERVICE` unset) and Streamable HTTP when on Cloud Run, so the same `agent.py` works in both.

## The three integration patterns

`McpToolset` is only one of three ways ADK can use MCP, and they differ mostly in what lands in your context window:

| Pattern | Architecture | Context impact | Ideal for |
|---|---|---|---|
| **Direct tool integration** (`McpToolset`) | Server tools adapted into the primary agent's tool list | Every schema and raw output enters the main history | Deterministic APIs: Postgres, GitHub, Maps |
| **Agent-exposed server** (`to_mcp_server`) | An ADK agent compiled into an MCP server for external clients | External caller sees only the final answer | Exposing ADK agents to IDEs and other hosts |
| **Sub-agent delegation** (`AgentTool`) | A child `LlmAgent` invoked as a tool by the parent | Intermediate reasoning stays out of the parent | Multi-step research with >20 tools |

Rule of thumb from the docs: direct integration when one model turn plus one deterministic call answers the question; delegation when exploration, retries, or large raw outputs would bloat the primary agent's history.

## Beyond tools: resources, auth, confirmation

- **Resources.** Servers can expose data files and records, not just functions. `await toolset.list_resources()` and `await toolset.read_resource(name)` read them programmatically; setting `use_mcp_resources=True` gives the *agent* a `LoadMcpResourceTool` so it can discover and read on its own.
- **Auth.** Prefer `auth_scheme` + `auth_credential` over raw headers — ADK builds the `Authorization` header and manages OAuth 2.0 refreshes. For per-user tokens, a `header_provider(context)` callable receives the active `ReadonlyContext` and builds headers per turn.
- **Confirmation.** `require_confirmation=...` (boolean or predicate over the arguments) gates destructive calls — `drop`/`delete` in a SQL string, for example — behind a human approval.

## Lifecycle: the part that breaks in production

Three rules that the docs state explicitly:

1. **Production agents must construct `McpToolset` synchronously in `agent.py`.** Async/dynamic initialization is only supported for local debugging or standalone runners.
2. **Outside `adk web`, close the toolset yourself.** A `try/finally` around your `Runner` loop with `await toolset.close()` is the difference between a clean exit and orphaned `npx` children.
3. **Restoration does not re-establish connections.** Session state survives lifecycle events, but the MCP session is re-initialized on demand — don't cache live handles across restarts.

```python
try:
    async for event in runner.run_async(...):
        ...
finally:
    await toolset.close()   # terminate subprocess / network session
```

Also worth knowing: state is restored but connections are not, tool schemas can change between server releases (pin server versions), and `tool_filter` is a security control — expose only the actions your agent legitimately needs.

## Takeaways

- `McpToolset` is ADK's MCP client: construct with connection params, connect lazily on first use, forward `call_tool`, close on teardown.
- Choose stdio for local/subprocess servers and Streamable HTTP for remote ones; SSE remains for older servers.
- Pick the integration pattern by context cost: direct for narrow deterministic APIs, sub-agent delegation for wide or chatty tool surfaces.
- Use `tool_filter`, `require_confirmation`, and `auth_scheme` as your scoping, approval, and credential layers.
- Define the toolset synchronously in `agent.py` for deployment and call `await toolset.close()` in every non-`adk web` runner.

## Further reading

- [Building an MCP client toolset for local and stdio servers](/blog/mcp-client-toolset-stdio-local-servers)
- [MCP vs direct function calling](/blog/mcp-vs-direct-function-calling)
- [MCP concepts: tools, resources, and prompts](/blog/mcp-concepts-tools-resources-prompts)
- [Defining function tools: type hints, docstrings, and validation](/blog/adk-function-tools-type-hints-docstrings)
- [Agentic workflows with MCP](/blog/agentic-workflows-with-mcp)
- [ADK: MCP tools](https://adk.dev/tools-custom/mcp-tools/)
