MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / ToolManager

Class ToolManager

src/mcp/server/mcpserver/tools/tool_manager.py:19–87  ·  view source on GitHub ↗

Manages MCPServer tools.

Source from the content-addressed store, hash-verified

17
18
19class ToolManager:
20 """Manages MCPServer tools."""
21
22 def __init__(self, warn_on_duplicate_tools: bool = True, *, tools: list[Tool] | None = None):
23 self._tools: dict[str, Tool] = {}
24 for tool in tools or ():
25 if warn_on_duplicate_tools and tool.name in self._tools:
26 logger.warning(f"Tool already exists: {tool.name}")
27 self._tools[tool.name] = tool
28
29 self.warn_on_duplicate_tools = warn_on_duplicate_tools
30
31 def get_tool(self, name: str) -> Tool | None:
32 """Get tool by name."""
33 return self._tools.get(name)
34
35 def list_tools(self) -> list[Tool]:
36 """List all registered tools."""
37 return list(self._tools.values())
38
39 def add_tool(
40 self,
41 fn: Callable[..., Any],
42 name: str | None = None,
43 title: str | None = None,
44 description: str | None = None,
45 annotations: ToolAnnotations | None = None,
46 icons: list[Icon] | None = None,
47 meta: dict[str, Any] | None = None,
48 structured_output: bool | None = None,
49 ) -> Tool:
50 """Add a tool to the server."""
51 tool = Tool.from_function(
52 fn,
53 name=name,
54 title=title,
55 description=description,
56 annotations=annotations,
57 icons=icons,
58 meta=meta,
59 structured_output=structured_output,
60 )
61 existing = self._tools.get(tool.name)
62 if existing:
63 if self.warn_on_duplicate_tools:
64 logger.warning(f"Tool already exists: {tool.name}")
65 return existing
66 self._tools[tool.name] = tool
67 return tool
68
69 def remove_tool(self, name: str) -> None:
70 """Remove a tool by name."""
71 if name not in self._tools:
72 raise ToolError(f"Unknown tool: {name}")
73 del self._tools[name]
74
75 async def call_tool(
76 self,

Calls

no outgoing calls