Overview

5 MCP Tools

This chapter introduces the Model Context Protocol as a practical way for LLM agents to access external capabilities through a growing ecosystem of standardized tools. It frames the reader as a tool consumer and explains that MCP standardizes both how tools are published by suppliers and how applications discover and use them. The chapter also positions MCP tools within the familiar tool-calling flow introduced earlier, emphasizing that the main new idea is that execution happens on an external server rather than inside the agent process.

The core implementation work centers on adding two abstractions to the framework: a tool provider and a tool wrapper. The provider manages communication with an MCP server, supporting both local stdio servers and remote streamable HTTP servers, while maintaining an asynchronous client session safely and efficiently. It can discover available server-side tools and convert them into client-side tool objects, and it also handles clean shutdown and resource cleanup. The tool wrapper stores the tool metadata and provider reference, then forwards tool calls to the MCP server and converts the response back into the framework’s result format.

To make MCP usage easier in real applications, the chapter then adds a builder that can gather tools from one or many MCP providers and assemble an LLM agent automatically. This removes the need for manual discovery before agent creation and scales better when multiple servers are involved. The chapter closes by showing that these tools fit naturally into multi-agent systems, while highlighting an important concurrency safeguard: shared providers must avoid spawning duplicate session-creation tasks when multiple agents or subagents request tools at the same time.

We begin part 2 of our build plan, enabling our LLM agent to use external tools made available via MCP.
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
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 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
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.
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.
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.
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)
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.
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
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 the Model Context Protocol (MCP) used for in this chapter?MCP is used as the standard way for LLM agents to connect to external systems and use external tools. In this chapter, the framework is extended so agents can discover and call MCP tools from MCP servers.
Who are the two main participants in the MCP ecosystem?The two main participants are tool suppliers and tool consumers. Suppliers build MCP-compliant tools and expose them through MCP servers, while consumers, such as our LLM agents, use those tools to extend their capabilities.
What are the three MCP primitives mentioned in the chapter?The three MCP primitives are tools, resources, and prompts. Tools perform actions, resources provide read-only data, and prompts provide templates for injecting instructions or behaviors into LLM interactions.
How does tool calling work for an MCP tool?The tool-calling flow is the same as with other tools: the LLM generates a tool-call request, the tool is executed, and the result is returned to the LLM. The difference is that with MCP, the execution happens on an external MCP server.
What is the difference between an MCP client and an MCP server?An MCP server is the external system that hosts and exposes MCP tools. An MCP client connects to that server, establishes a session, and forwards tool-call requests for execution.
What transport types does MCP support for connecting to servers?MCP supports two transport types: standard IO (stdio) and streamable HTTP. Stdio is typically used for local servers, while streamable HTTP is typically used for remote servers.
Why does the chapter introduce both MCPToolProvider and MCPTool?MCPToolProvider manages the connection to an MCP server and discovers available tools. MCPTool represents one discovered server-side tool on the client side and forwards calls to the server through the provider.
Why is MCPToolProvider implemented with asyncio events and tasks?Because MCP connections are asynchronous and persistent. The provider uses asyncio.Task to create the session in the background and asyncio.Event objects to signal when the session is ready or when it should be shut down.
How does LLMAgentBuilder make working with MCP tools easier?LLMAgentBuilder automatically discovers MCP tools from one or more MCPToolProvider instances and then builds an LLMAgent with those tools. This removes the need to manually discover tools before creating the agent.
Why does the chapter emphasize guarding against concurrent session creation?Without the guard, multiple concurrent callers could each start their own session-creation task, causing duplicate or orphaned connections to the same MCP server. The guard ensures concurrent callers share a single in-flight session creation task.

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


Close
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) Build a Multi-Agent System (from Scratch) ebook for free
Close
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) Build a Multi-Agent System (from Scratch) ebook for free