Overview

5 MCP Tools

In this chapter, the framework is extended so an LLM agent can use tools exposed through the Model Context Protocol, which has rapidly become a common standard for connecting agents to external systems. The chapter begins by explaining the MCP ecosystem, where tool suppliers publish MCP-compatible capabilities and tool consumers, like our agent, can discover and use them through a standard interface. It also reinforces the familiar tool-calling flow from earlier chapters, showing that MCP does not change the basic request-response pattern so much as it standardizes where the tool runs and how the agent reaches it.

To support this integration, the book introduces two new abstractions: one for representing an MCP tool on the client side and one for managing the connection to the MCP server. The provider object is responsible for creating and maintaining an asynchronous session, supporting both local stdio servers and remote streamable HTTP servers, while handling connection setup, readiness signaling, reuse of established sessions, and clean shutdown. Once connected, it queries the server for available tools and converts the discovered server-side definitions into client-side tool objects, taking care to avoid circular import issues and preserve useful metadata such as descriptions, schemas, and annotations.

After the MCP integration is in place, the chapter adds a builder that makes agent construction easier when MCP tools come from multiple servers. This builder gathers normal tools and MCP providers, discovers tools from all configured servers concurrently, and assembles a ready-to-use agent with less manual setup. The chapter concludes by showing that the resulting MCP tools can be invoked just like any other asynchronous tools in the framework, enabling the agent to call a remote Hailstone tool, receive the correct result, and thereby gain access to the wider MCP ecosystem for more practical workflows.

We begin part 2 of our build plan, enabling our LLM agent to use external tools made available via MCP.
Suppliers and consumers within the MCP ecosystem
The tool-calling process, seen in Chapters 1 and 2, now adapted for MCP tools. The tool-call request in the form of a ToolCall object is passed to the MCP tool for execution, yielding a ToolCallResult object at completion. The only difference is that tool execution occurs on the external MCP server.
The MCP architecture integrated into LLM agents and our framework
An LLM agent with tools coming from the two different types of MCP servers: stdio and streamable HTTP.
UML class diagrams for MCPTool and MCPToolProvider.
Discovering tools from an MCP server via the MCPToolProvider. Initially, the MCPToolProvider has no knowledge of any existing tools on the MCP server (left panel) and thus sends a query to learn about them. After getting the response from the server, it creates an MCPTool object for each discovered server-side tool.
Integrating with mcp during tool discovery (the "Before" step from figure 5.7)
Forwarding a tool-call request to the MCP server for remote execution. The result is then transformed into a ToolCallResult.
The UML class diagram for LLMAgentBuilder

Summary

  • The MCP ecosystem offers many tools that LLM agents can use.
  • The two types of participants in the MCP ecosystem are tool suppliers and tool consumers. Suppliers build tools that adhere to the MCP standard, which MCP clients can use.
  • The tool-calling process for an MCP tool is like the tools introduced in Chapter 2, except that tool calls execute on the external MCP server.
  • MCP servers and MCP clients are part of the MCP architecture. They interact via connections that are called sessions.
  • MCP hosts are applications that create and manage MCP clients.
  • There are two types of MCP servers, distinguished by their transport mechanisms: stdio and streamable HTTP.
  • MCPToolProvider is a new class in our framework that establishes and maintains connections to its MCP server. It also handles tool discovery with the get_tools() method.
  • The MCPTool is another new class in our framework, essentially a client-side representation of the tools available on an MCP server.
  • LLMAgentBuilder is a new class that conveniently takes care of tool-discovery from MCP servers when constructing LLM agents that use MCP tools.

FAQ

What is MCP, and why is it important for LLM agents?MCP, or the Model Context Protocol, is a standard for connecting LLM agents to external systems and tools. It matters because it creates a common ecosystem where tool suppliers can expose capabilities and tool consumers, like our LLM agents, can use them consistently.
Who are the two main participants in the MCP ecosystem?The two main participants are tool suppliers and tool consumers. Suppliers create MCP-compliant tools and expose them through MCP servers, while consumers use those tools to extend AI applications such as LLM agents.
What are the three MCP primitives mentioned in the chapter?The three MCP primitives are tools, resources, and prompts. Tools allow actions to be performed, resources provide read-only data, and prompts provide reusable instruction templates. This chapter focuses on tools.
How does tool calling work for an MCP tool?The tool-calling process is the same as with other tools: the LLM creates a tool-call request, the tool is executed, and the result is returned to the LLM. The MCP-specific part is that the request is forwarded to an external MCP server where the tool logic actually runs.
What is an MCP server, and what is an MCP client session?An MCP server is the external system that hosts MCP tools. An MCP client connects to that server through a session, which is the communication channel used to send tool-call requests and receive results.
What transport types does MCP support in this chapter?The chapter covers two transport types: stdio and streamable HTTP. Stdio is typically used for local MCP servers, while streamable HTTP is typically used for remote MCP servers.
Why are MCPTool and MCPToolProvider both needed?MCPTool represents an individual tool that can be used by the LLM agent, while MCPToolProvider is responsible for connecting to the MCP server and discovering available tools. The provider creates the tool objects and keeps the server session managed.
How does MCPToolProvider discover tools from an MCP server?After establishing a session, MCPToolProvider calls the session’s list_tools() method. It then converts the returned server-side tool definitions into MCPTool objects, each linked back to the provider.
Why does MCPToolProvider use asyncio.Event and asyncio.Task?They are used to manage the asynchronous session lifecycle safely. One event signals when the session is ready, another signals shutdown, and a background task keeps the MCP session alive until it is time to close it.
What does LLMAgentBuilder add to the framework when working with MCP tools?LLMAgentBuilder automates MCP tool discovery and agent construction. Instead of manually fetching tools from each MCP server, you provide one or more MCPToolProvider instances, and the builder discovers all tools and creates the LLMAgent for you.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) ebook for free