Connecting MCP Servers to ADK Agents
Attach any MCP server to an ADK agent with McpToolset — transport choices, connection lifecycle, auth, resources, and how the three MCP integration patterns differ.
Published on • November 28, 2026
AI Assistant

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
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:
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()andawait toolset.read_resource(name)read them programmatically; settinguse_mcp_resources=Truegives the agent aLoadMcpResourceToolso it can discover and read on its own. - Auth. Prefer
auth_scheme+auth_credentialover raw headers — ADK builds theAuthorizationheader and manages OAuth 2.0 refreshes. For per-user tokens, aheader_provider(context)callable receives the activeReadonlyContextand builds headers per turn. - Confirmation.
require_confirmation=...(boolean or predicate over the arguments) gates destructive calls —drop/deletein a SQL string, for example — behind a human approval.
Lifecycle: the part that breaks in production
Three rules that the docs state explicitly:
- Production agents must construct
McpToolsetsynchronously inagent.py. Async/dynamic initialization is only supported for local debugging or standalone runners. - Outside
adk web, close the toolset yourself. Atry/finallyaround yourRunnerloop withawait toolset.close()is the difference between a clean exit and orphanednpxchildren. - 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.
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
McpToolsetis ADK’s MCP client: construct with connection params, connect lazily on first use, forwardcall_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, andauth_schemeas your scoping, approval, and credential layers. - Define the toolset synchronously in
agent.pyfor deployment and callawait toolset.close()in every non-adk webrunner.