Module 28: Building a Custom MCP Tool
Theoryโ
Figure: Developing custom MCP tool providers to expose stateful backend capabilities to the AI ecosystem.
Becoming a Tool Providerโ
In the previous module, you acted as a consumer of an MCP tool. Your ADK agent was the client connecting to a pre-built server. Now, we flip the roles. You will become a provider by building your own standalone MCP server from scratch.
By exposing your application's capabilities via the Model Context Protocol, you are making them available not just to your own ADK agents, but to any MCP-compliant client. It turns your service into a reusable, AI-addressable component in a larger ecosystem.
The Anatomy of an MCP Serverโ
An MCP server is an application that listens for connections from clients and responds to requests according to the protocol. A server must implement handlers for two fundamental methods:
Figure: The 2-phase Model Context Protocol lifecycle: Tool Discovery schema negotiation followed by stateful Tool Execution.
1. list_tools()โ
When a client first connects, it calls list_tools(). The server's job is to respond with a "menu" of all the tools it provides. For each tool, it must provide a detailed schema, including:
- The tool's
name. - A
descriptionof what it does. - An
inputSchema(JSON Schema) that defines the arguments the tool expects (names, types, required fields).
The client's LLM uses this menu to decide which tool to call.
2. call_tool()โ
Once the client's LLM decides to use a tool, it sends a call_tool() request containing the name of the tool and a dictionary of arguments.
The server's call_tool() handler is responsible for:
- Receiving the request.
- Executing the corresponding logic for the requested tool.
- Managing any state the tool needs (e.g., an in-memory shopping cart or a database row).
- Returning a list of MCP content blocks (such as
mcp_types.TextContent) to the client.
Building an MCP Server in Pythonโ
The mcp Python SDK provides a low-level Server class and decorators to wire up protocol handlers directly:
- Install the library (via ADK's compatible extra):
uv add "google-adk[mcp]" - Import the low-level
Serverand protocol types: Note thatServeris imported frommcp.server.lowlevel(not top-levelmcp):import sys
from mcp import types as mcp_types
from mcp.server.lowlevel import Server, NotificationOptions
from mcp.server.models import InitializationOptions
import mcp.server.stdio
app = Server("my_mcp_server") - Implement Handlers with Decorators: Write
asyncfunctions and register them onapp:@app.list_tools(): Returns alist[mcp_types.Tool]describing every exposed tool and its JSON Schema.@app.call_tool(): Receives(name: str, arguments: dict)and returnslist[mcp_types.Content](for example,[mcp_types.TextContent(type="text", text=...)]).
- Run Over
stdioSafely (Never Print tostdout!): When you start a server withmcp.server.stdio.stdio_server(), standard output (stdout) IS the JSON-RPC wire transport between your server and the ADK client. Calling a plainprint("...")writes raw text intostdout, which corrupts the JSON-RPC stream and causes the MCP client to fail with JSON parse errors! Always send server logs tosys.stderr:Similarly, when launching a Python MCP server fromprint("[Server]: Tool called", file=sys.stderr, flush=True)StdioServerParameterson the client side, always passcommand=sys.executable(rather than a hardcoded'python3') so the subprocess runs inside the exact same.venvwheremcpis installed.
In the lab, you will build a stateful "shopping cart" MCP server and connect an ADK Agent to it.
Why Build an MCP Server?โ
Building an MCP server decouples your tool's logic from the agent, providing major architectural benefits:
- Independent Scalability: Scale the agent and the tool server independently.
- Modular Maintenance: Update tool logic without redeploying the agent.
- Reusability: Your MCP server can be consumed by any MCP-compliant client.
- Workflow Integration: In ADK 2.0, you can equip any
Agentnode with your custom MCP server across complex graph workflows.
Going Further: Exposing a Whole Agent as an MCP Tool (Experimental)โ
This module builds a hand-written MCP server exposing granular tools (add_item_to_cart, view_cart) โ no agent involved on the server side. ADK also offers the opposite direction: to_mcp_server(agent) (from google.adk.tools.mcp_tool, requires the [mcp] extra) wraps an entire ADK agent โ its model loop and all of its own tools โ as a single MCP tool that any MCP host can call, hiding the whole conversation behind one request/response. It is the MCP counterpart of to_a2a from Module 21, and is currently marked @experimental in ADK's source.
Why must a Python MCP server running over mcp.server.stdio.stdio_server() write diagnostic logs to sys.stderr instead of calling standard print()?
Key Takeawaysโ
- A custom Python MCP server uses
from mcp.server.lowlevel import Serverand implements@app.list_tools()(discovery) and@app.call_tool()(execution). - Over
stdio_server(),stdoutis reserved strictly for JSON-RPC frames โ always log withprint(..., file=sys.stderr, flush=True). - When spawning a local Python MCP server from ADK via
StdioServerParameters, usecommand=sys.executableso the child process inherits the active virtual environment's Python interpreter and installedmcppackage. - Decoupling stateful tools into standalone MCP servers makes capabilities reusable across any MCP-compliant host or agent framework.