liveBook · Manning

MCPToolProvider

As explained in Build a Multi-Agent System (from Scratch)

MCPToolProvider is a framework class that establishes and maintains a connection to an MCP server and discovers its tools.

MCPToolProvider connects an agent framework to one MCP server and discovers its tools. Each discovered tool becomes an MCPTool that holds a reference to the provider, allowing it to forward tool calls to the server for execution. section 5.2 section 5.5

Construct a provider with a name and at least one connection parameter: stdio_params for a stdio server or streamable_http_url for a streamable HTTP server. Optional streamable_http_headers supply HTTP headers. Missing both connection parameters raises MissingMCPServerParamsError. section 5.2 section 5.2.1

The provider manages discovery and the session lifecycle asynchronously:

  • session() starts session creation when needed and returns the persistent ClientSession. The creation task initializes the session and signals when it is ready. section 5.2.1
  • Concurrent first-time callers share an in-flight creation task. This prevents duplicate connections and orphaned tasks when multiple agents use the same provider. section 5.2.1 section 5.4
  • get_tools() calls session.list_tools() and creates an MCPTool for each result, carrying its description, input schema, annotations, and provider reference. Each name follows mcp__{provider_name}__{tool_name}. section 5.2.1
  • close() signals shutdown, awaits the session task, and clears the session, task, and event state. section 5.2.1
Listing 5.4 Implementing MCPToolProvider: get_tools()
# llm_agents_from_scratch/tools/mcp/provider.py
… #A
 
from typing import TYPE_CHECKING
if TYPE_CHECKING: #B
    from .tool import MCPTool
 
 
class MCPToolProvider:
    """MCP Tool Provider class."""
    … #C
 
    async def get_tools(self) -> list["MCPTool"]:
        """Fetch tools from the MCP server and create MCPTool instances."""
        from llm_agents_from_scratch.tools.mcp.tool import MCPTool #D
 
        session = await self.session() #E
        response = await session.list_tools() #F
 
        return [
            MCPTool( #G
                provider=self,
                name=f"mcp__{self.name}__{tool.name}",
                desc=tool.description or "",
                params_json_schema=tool.input_schema,
                additional_annotations=tool.annotations,
            )
            for tool in response.tools
        ]

The discovery implementation shows how server tool metadata becomes client-side MCPTool instances. section 5.2.1

Discovering a stdio tool

For a Hailstone MCP server, create mcp.StdioServerParameters with the server command, arguments, and working directory, then pass it to MCPToolProvider as stdio_params. Calling await provider.get_tools() discovers one tool; call await provider.close() to release the session. section 5.2.1

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Implements constructor validation, asynchronous session management, tool discovery, concurrency protection, and shutdown.
Examples
Demonstrates stdio server construction, discovery of a Hailstone tool, session reuse, and cleanup.
What it assumes
Assumes familiarity with asynchronous tasks and agent infrastructure while introducing asyncio.Event for coroutine signaling.
What you can do after
Builds a provider from scratch to discover server tools and manage a reusable MCP session.
How it connects
Connects MCP clients and servers with asynchronous tool calls, tool discovery, and multi-agent systems.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.