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
