liveBook · Manning

MCPTool

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

MCPTool is a book-specific asynchronous wrapper that exposes an MCP server tool through the framework's tool interface.

MCPTool is an asynchronous client-side wrapper that exposes an MCP server tool through the framework’s tool interface. Calls follow the framework’s usual tool-calling process, but execution happens on the MCP server. section 5.2 section 5.5

MCPTool subclasses AsyncBaseTool and provides name, description, parameters_json_schema, and an asynchronous __call__() method. It also stores optional MCP tool annotations and a reference to the MCPToolProvider that manages the server connection. section 5.2.2 section 5.2

Discovery and execution follow this path:

  • Call await provider.get_tools(). The provider obtains a session, lists server tools, and constructs one MCPTool per result; direct construction is not the intended usage. section 5.2.1 section 5.2.2
  • Each wrapper receives the server tool’s description, input schema, and annotations. Its framework-facing name uses the form mcp__{provider.name}__{tool.name}. section 5.2.1
  • When called with a ToolCall, the wrapper obtains the provider’s session, removes its naming prefix, and passes the server tool name and arguments to session.call_tool(). section 5.2.2
  • The response becomes a ToolCallResult containing the original call ID, serialized content, and the server’s error flag. section 5.2.2
Listing 5.7 Implementing MCPTool: __call__()
# llm_agents_from_scratch/tools/mcp/tool.py
… #A
from llm_agents_from_scratch.data_structures import (
    ToolCall, ToolCallResult
)
 
class MCPTool(AsyncBaseTool):
    … #B
 
    async def __call__(
        self,
        tool_call: ToolCall,
        *args: Any,
        **kwargs: Any,
    ) -> ToolCallResult:
        """Asynchronously execute the MCP tool call.""" #C
        session = await self.provider.session()
        # call tool
        result = await session.call_tool( #D
            name=self.name.removeprefix(f"mcp__{self.provider.name}__"), #E
            arguments=tool_call.arguments,
        )
 
        return ToolCallResult( #F
            tool_call_id=tool_call.id_,
            content=[el.model_dump() for el in result.content],
            error=result.is_error,
        )

Inspect the asynchronous forwarding and result conversion implementation. section 5.2.2

Hailstone tool call

A provider connected to the Hailstone server discovers one tool. Calling its wrapper with arguments {"x": 5} returns a ToolCallResult whose text content is "16" and whose error flag is false. section 5.2.2 section 5.2.1

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

How deep it goes
Implements discovery metadata, asynchronous request forwarding, name translation, and result conversion.
Examples
Demonstrates discovery of a Hailstone tool and a call mapping input 5 to output 16.
What it assumes
Assumes familiarity with the existing AsyncBaseTool interface and asynchronous Python; introduces MCP-specific provider and session integration.
What you can do after
Build it from scratch by implementing an asynchronous wrapper that forwards tool calls through a provider session.
How it connects
Connects MCP, asynchronous programming, JSON Schema, and tool calling.

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.