liveBook · Manning

Dynamic tool discovery

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

Tool discovery is the process of identifying and obtaining the descriptions of tools exposed by an external provider before constructing an agent that can use them.

Dynamic tool discovery queries an MCP server for its available tools before an agent is constructed. An MCPToolProvider uses an established session to request the server’s tool list and turn each result into a client-side MCPTool linked to its provider. section 5.2 section 5.2.1

The discovery flow connects server metadata to agent-ready tools:

  • Call the provider’s asynchronous get_tools(); it obtains a session and invokes list_tools(). section 5.2.1
  • Convert each server tool’s name, description, input schema, and annotations into an MCPTool, retaining a reference to the provider. section 5.2.1
  • Pass the discovered tools to an LLM agent; use LLMAgentBuilder to discover tools across configured providers during its asynchronous build(). section 5.3
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
        ]

See the get_tools() implementation, including the server query and MCPTool construction. section 5.2.1

Hailstone server

An MCPToolProvider discovers the Hailstone server’s tool, whose description and parameter schema can then be inspected or supplied to an agent. section 5.2.1 section 5.3

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

How deep it goes
Implements asynchronous session lookup, list_tools(), and conversion of server metadata into MCPTool instances.
Examples
Uses a Hailstone MCP server to discover and inspect a tool before agent use.
What you can do after
Build it from scratch by querying an MCP session and mapping returned tools into provider-linked MCPTool objects.
How it connects
Links MCPToolProvider, MCPTool, MCP sessions, and LLM agents.

Also explained in

Compare how 4 books explain dynamic tool discovery →

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.