Chapter 5: MCP Tools
This introduction situates MCP as a standard for connecting agents to external systems and enabling access to third-party tools.
After this chapter you will be able to
- MCP and external tool ecosystem. Explain how MCP relates to external systems and why its widespread adoption matters for agent capabilities.
- MCP suppliers and consumers. Explain the roles of MCP tool suppliers and consumers, and how the standard connects them.
- MCP Tool Calling Process. Trace the tool-calling process for an MCP tool, from the LLM's request through external execution to the returned result.
- MCP Client Server Roles. Explain how an MCP host, client, and server participate in executing externally sourced tools.
- Local and Remote Transports. Explain how stdio and streamable HTTP relate to server location and whether the tool-call exchange changes between them.
- Asynchronous MCP tool integration. Explain why MCPTool extends AsyncBaseTool and how it uses a provider to forward tool calls to an MCP server.
- Provider discovery and tool construction. Trace how an MCPToolProvider discovers server tools and establishes each MCPTool’s provider reference.
- Session creation and shutdown. Trace how the provider creates, reuses, and closes an MCP session.
- Concurrent session readiness. Explain how session() shares session creation between callers and avoids hiding initialization errors.
- Server tool discovery. Explain how get_tools() discovers server-side tools and represents them in the framework.
- MCPTool construction and discovery. Explain how MCPTool fits the asynchronous tool interface and how its instances are created for use by an LLM agent.
- Remote tool call forwarding. Trace how MCPTool executes a call and returns its result to the framework.
- Asynchronous Agent Builder. Describe how LLMAgentBuilder is configured and why its build method is asynchronous.
- Builder construction and discovery. Explain how LLMAgentBuilder uses its configuration and MCP providers to build an LLMAgent.
- Fluent configuration and accumulation. Explain how the builder's with_ methods support chaining and how tools and MCP providers are added.
- Selective MCP Tool Assignment. Explain how a specific MCP tool can be distributed among agents in a multi-agent system.
- Shared Session Creation Guard. Explain why MCPToolProvider shares a session-creation task among concurrent callers, including how concurrent subagent dispatches can create the need for the guard.
What it builds on
- Implementing ToolCall and ToolCallResult (chapter 2) · The existing representation of tool calls and their results.
- BaseTool: a blueprint for tools (chapter 2) · The framework's existing blueprint for tools.
- The AsyncBaseTool (chapter 2) · The AsyncBaseTool abstraction for asynchronous tools.
- LLMAgent: a simple LLM agent class (chapter 4) · LLMAgent construction and configuration
- Extending to MAS: Tools (chapter 2) · How tools can be distributed among agents in a multi-agent system.
Section by section
Introduction
This introduction situates MCP as a standard for connecting agents to external systems and enabling access to third-party tools.
- MCP and external tool ecosystem: Explain how MCP relates to external systems and why its widespread adoption matters for agent capabilities.
Why it matters: MCP gives agents access to a broader ecosystem of tools for extending what they can do.
Key terms: Model Context Protocol, MCP, external systems, third-party tools
5.1 Introducing MCP
This section previews the foundational MCP concepts needed to integrate MCP into the framework.
5.1.1 The MCP ecosystem
Introduces MCP tool suppliers and consumers and distinguishes tools from the other MCP primitives.
- MCP suppliers and consumers: Explain the roles of MCP tool suppliers and consumers, and how the standard connects them.
Why it matters: This distinction places our agents on the consumer side of the MCP ecosystem. - Three MCP primitives: Distinguish tools from resources and prompts at the level of what each makes available to consumers.
Why it matters: The distinction clarifies what the book's focus on MCP tools leaves outside its scope.
Key terms: MCP ecosystem, tool suppliers, tool consumers, MCP tools, MCP primitives, tools, resources, prompts
5.1.2 The tool-calling process for an MCP tool
This section explains how MCP adapts the familiar tool-calling process by executing tool logic in an external system.
- MCP Tool Calling Process: Trace the tool-calling process for an MCP tool, from the LLM's request through external execution to the returned result.
Why it matters: This shows what changes when an agent uses an MCP tool and what stays familiar. - Existing Tool Infrastructure Reuse: Explain why the existing tool infrastructure can support integration of MCP tools.
Why it matters: The unchanged process gives the framework a foundation for adding MCP tools without discarding its existing tool infrastructure.
Key terms: tool-calling process, tool-call request, MCP tool, MCP tools
5.1.3 The MCP architecture
This section explains MCP’s host-client-server architecture, connection representation, and local and remote transports for tool calls.
- MCP Client Server Roles: Explain how an MCP host, client, and server participate in executing externally sourced tools.
Why it matters: These roles show how an application reaches tools supplied by an external system. - Protocol and Library Sessions: Distinguish the MCP 2.0 protocol’s treatment of sessions from the Python library’s representation of connections.
Why it matters: This distinction prevents the library’s client-session terminology from being mistaken for a protocol requirement. - Local and Remote Transports: Explain how stdio and streamable HTTP relate to server location and whether the tool-call exchange changes between them.
Why it matters: Knowing the transport choices helps you connect tools from both local and remote servers without assuming different tool-call processes.
Key terms: MCP host, MCP client, MCP server, session, server-side definition, MCP 2.0, client sessions, mcp Python library, standard IO, stdio
5.2 Integrating MCP into our framework
This section outlines how asynchronous MCP tools and a connection-managing provider integrate MCP servers into the framework.
- Asynchronous MCP tool integration: Explain why MCPTool extends AsyncBaseTool and how it uses a provider to forward tool calls to an MCP server.
Why it matters: This design connects MCP server tools to the framework’s existing asynchronous tool interface. - Provider discovery and tool construction: Trace how an MCPToolProvider discovers server tools and establishes each MCPTool’s provider reference.
Why it matters: Discovery turns the server’s available tools into framework objects connected to the right provider. - Provider connection configuration: Describe the provider’s connection-management role and how its configuration depends on the MCP server type.
Why it matters: The provider’s configuration supplies the information needed to connect to the chosen type of MCP server.
Key terms: MCPTool, AsyncBaseTool, MCPToolProvider, provider, tool-discovery phase, provider reference, stdio_params, streamable_http_url, streamable_http_headers
5.2.1 Implementing MCPToolProvider
This section implements MCPToolProvider to manage MCP server sessions and discover server-side tools as MCPTool objects.
- Provider connection configuration: Describe the connection information required to construct an MCPToolProvider and how the Hailstone example supplies it.
Why it matters: The provider needs enough information to connect to the intended server. - Session creation and shutdown: Trace how the provider creates, reuses, and closes an MCP session.
Why it matters: Managing the session lets the provider reuse a connection and release it when finished. - Concurrent session readiness: Explain how session() shares session creation between callers and avoids hiding initialization errors.
Why it matters: Concurrent callers can share one connection attempt without silently waiting through a failed initialization. - Server tool discovery: Explain how get_tools() discovers server-side tools and represents them in the framework.
Why it matters: Discovery makes server-provided tools available as framework tool objects. - Circular import avoidance: Explain the circular import between MCPToolProvider and MCPTool and how the provider avoids it.
Why it matters: Deferring the import lets the mutually dependent classes be imported without a circular import problem.
Key terms: stdio_params, streamable_http_url, mcp.StdioServerParameters, session(), _create_session(), close(), mcp.ClientSession, asyncio.create_task(), asyncio.wait(), _session_ready
5.2.2 Implementing MCPTool
This section implements MCPTool as an asynchronous framework tool that is discovered through a provider and forwards calls to an MCP server.
- MCPTool construction and discovery: Explain how MCPTool fits the asynchronous tool interface and how its instances are created for use by an LLM agent.
Why it matters: This explains how a remotely discovered tool becomes usable through the framework’s existing tool interface. - Remote tool call forwarding: Trace how MCPTool executes a call and returns its result to the framework.
Why it matters: This shows how a framework tool call reaches the remote implementation and comes back in the framework’s result format.
Key terms: MCPTool, AsyncBaseTool, MCPToolProvider.get_tools(), mcp.Tool, parameters_json_schema, __call__(), call_tool(), MCP server, ToolCallResult
5.3 LLMAgentBuilder: a dynamic builder of LLMAgent
This section contrasts manual MCP tool setup with using LLMAgentBuilder to discover tools and construct an agent.
- Manual MCP Tool Setup: Explain how discovered MCP tools are added to an LLMAgent and why manual discovery becomes inconvenient at scale.
Why it matters: This explains why a builder is useful even though adding already-discovered tools is straightforward. - Asynchronous Agent Builder: Describe how LLMAgentBuilder is configured and why its build method is asynchronous.
Why it matters: This connects builder configuration to automated MCP tool discovery during agent creation.
Key terms: MCP tools, LLMAgent constructor, tool discovery, LLMAgentBuilder, llm, tools, templates, mcp_providers, MCPToolProvider
5.3.1 Implementing LLMAgentBuilder
This section implements and demonstrates an LLMAgentBuilder that configures an agent and discovers MCP tools during construction.
- Builder construction and discovery: Explain how LLMAgentBuilder uses its configuration and MCP providers to build an LLMAgent.
Why it matters: The builder makes MCP tool discovery part of creating an agent rather than a separate manual step. - Fluent configuration and accumulation: Explain how the builder's with_ methods support chaining and how tools and MCP providers are added.
Why it matters: Chaining makes builder configuration concise, while list extension preserves previously added tools and providers. - MCP tool usage verification: Describe how the Hailstone example verifies that the agent used its MCP tool.
Why it matters: Logging provides a way to check that the agent actually used the MCP tool in the example.
Key terms: LLMAgentBuilder, LLMAgent, mcp_providers, MCPToolProvider, build(), with_, fluent methods, with_tools(), with_mcp_providers(), Self
5.4 Extending to MAS: MCP Tools
This section explains selective MCP tool assignment in multi-agent systems and why a shared provider guards concurrent session creation.
- Selective MCP Tool Assignment: Explain how a specific MCP tool can be distributed among agents in a multi-agent system.
Why it matters: Selective assignment lets different agents have different tool access. - Shared Session Creation Guard: Explain why MCPToolProvider shares a session-creation task among concurrent callers, including how concurrent subagent dispatches can create the need for the guard.
Why it matters: A shared provider needs this guard to avoid leaving duplicate MCP connections unused.
Key terms: MCP tool, MAS, MCPToolProvider, session(), _session_task, session-creation task
5.5 Summary
Where this is used later
- (chapter intro) (chapter 10) · The earlier MCP architecture provides context for the contrast with the book's client-side MCP use.
- Where and when to discover skills (chapter 6) · Dynamic construction of an LLMAgent with MCP tools.
- (chapter intro) (chapter 9) · LLMAgentBuilder constructs LLMAgent instances.
- SubAgentSpec: a core type for specifying a subagent (chapter 9) · LLMAgentBuilder constructs LLMAgent instances.
- Introducing the LLM Agent Inspector for supervised task executions (chapter 8) · Using LLMAgentBuilder to specify an agent.
- Understanding subagents vs A2A agents (chapter 10) · LLMAgentBuilder as the builder used for agents
- Supporting subagents in our framework (chapter 9) · Subagent support provides the setting in which concurrent subagents may use a common MCP tool provider.
Concepts explained in this chapter
- MCPToolProviderMCPToolProvider is a framework class that establishes and maintains a connection to an MCP server and discovers its tools.
- MCPToolMCPTool is a book-specific asynchronous wrapper that exposes an MCP server tool through the framework's tool interface.
- dynamic tool discoveryTool discovery is the process of identifying and obtaining the descriptions of tools exposed by an external provider before constructing an agent that can use them.
- session()session() is an MCPToolProvider method that returns an established MCP session, creating it if necessary.
- MCP toolAn MCP tool is an executable capability exposed through the Model Context Protocol.
- mcp clientAn MCP client is a software component that connects an AI application to capabilities exposed through the Model Context Protocol.
- MCPModel Context Protocol is an open standard for connecting AI applications with external tools and contextual resources.
- LLMAgentBuilderLLMAgentBuilder is a book-specific framework class that discovers tools from selected MCP servers and constructs an LLMAgent with them.
- MCP serverAn MCP server is a program that exposes tools or contextual resources to clients through the Model Context Protocol.
- client-server architectureClient-server architecture is a system pattern in which clients request services from servers that provide them.
- tool useToolCall is a data structure representing a requested tool invocation and its arguments.
- asyncio.Eventasyncio.Event is a Python asyncio synchronization primitive that lets asynchronous tasks signal that a condition has occurred.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
