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