Skip to content
Blog

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

Transportconnection_paramsWhere it runsAuthNotes
stdioStdioConnectionParams(server_params=StdioServerParameters(...))Local subprocessThe server’s own env varsZero network overhead; dev and single-host deployments
SSESseConnectionParams(url=..., headers=..., timeout=...)RemoteBearer header or auth_schemeLegacy streaming protocol; still common
Streamable HTTPStreamableHTTPConnectionParams(url=..., headers=..., timeout=5, sse_read_timeout=300)RemoteBearer header or auth_schemePreferred 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:

PatternArchitectureContext impactIdeal for
Direct tool integration (McpToolset)Server tools adapted into the primary agent’s tool listEvery schema and raw output enters the main historyDeterministic APIs: Postgres, GitHub, Maps
Agent-exposed server (to_mcp_server)An ADK agent compiled into an MCP server for external clientsExternal caller sees only the final answerExposing ADK agents to IDEs and other hosts
Sub-agent delegation (AgentTool)A child LlmAgent invoked as a tool by the parentIntermediate reasoning stays out of the parentMulti-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.
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