11"""MCP Tool Provider implementation."""
22
33import logging
4- from typing import Callable , Optional , Pattern , Sequence , Union
4+ from typing import Any , Callable , Optional , Pattern , Sequence , Union
55
66from typing_extensions import TypedDict
77
@@ -23,37 +23,39 @@ class ToolFilters(TypedDict, total=False):
2323 Tools are filtered in this order:
2424 1. If 'allowed' is specified, only tools matching these patterns are included
2525 2. Tools matching 'rejected' patterns are then excluded
26- 3. If the result exceeds 'max_tools', it's truncated
2726 """
2827
2928 allowed : list [_ToolFilterPattern ]
3029 rejected : list [_ToolFilterPattern ]
31- max_tools : int
3230
3331
3432class MCPToolProvider (ToolProvider ):
3533 """Tool provider for MCP clients with managed lifecycle."""
3634
3735 def __init__ (
38- self , * , client : MCPClient , tool_filters : Optional [ToolFilters ] = None , disambiguator : Optional [str ] = None
36+ self ,
37+ * ,
38+ client : MCPClient ,
39+ tool_filters : Optional [ToolFilters ] = None ,
40+ prefix : Optional [str ] = None ,
41+ ** kwargs : Any ,
3942 ) -> None :
4043 """Initialize with an MCP client.
4144
4245 Args:
4346 client: The MCP client to manage.
4447 tool_filters: Optional filters to apply to tools.
45- disambiguator: Optional prefix for tool names.
48+ prefix: Optional prefix for tool names.
49+ **kwargs: Additional arguments for future compatibility.
4650 """
47- logger .debug (
48- "tool_filters=<%s>, disambiguator=<%s> | initializing MCPToolProvider" , tool_filters , disambiguator
49- )
51+ logger .debug ("tool_filters=<%s>, prefix=<%s> | initializing MCPToolProvider" , tool_filters , prefix )
5052 self ._client = client
5153 self ._tool_filters = tool_filters
52- self ._disambiguator = disambiguator
54+ self ._prefix = prefix
5355 self ._tools : Optional [list [MCPAgentTool ]] = None # None = not loaded yet, [] = loaded but empty
5456 self ._started = False
5557
56- async def load_tools (self ) -> Sequence [AgentTool ]:
58+ async def load_tools (self , ** kwargs : Any ) -> Sequence [AgentTool ]:
5759 """Load and return tools from the MCP client.
5860
5961 Returns:
@@ -77,12 +79,6 @@ async def load_tools(self) -> Sequence[AgentTool]:
7779 pagination_token = None
7880 page_count = 0
7981
80- # Determine max_tools limit for early termination
81- max_tools_limit = None
82- if self ._tool_filters and "max_tools" in self ._tool_filters :
83- max_tools_limit = self ._tool_filters ["max_tools" ]
84- logger .debug ("max_tools_limit=<%d> | will stop when reached" , max_tools_limit )
85-
8682 while True :
8783 logger .debug ("page=<%d>, token=<%s> | fetching tools page" , page_count , pagination_token )
8884 paginated_tools = self ._client .list_tools_sync (pagination_token )
@@ -91,15 +87,10 @@ async def load_tools(self) -> Sequence[AgentTool]:
9187 for tool in paginated_tools :
9288 # Apply filters
9389 if self ._should_include_tool (tool ):
94- # Apply disambiguation if needed
95- processed_tool = self ._apply_disambiguation (tool )
90+ # Apply prefix if needed
91+ processed_tool = self ._apply_prefix (tool )
9692 self ._tools .append (processed_tool )
9793
98- # Check if we've reached max_tools limit
99- if max_tools_limit is not None and len (self ._tools ) >= max_tools_limit :
100- logger .debug ("max_tools_reached=<%d> | stopping pagination early" , len (self ._tools ))
101- return self ._tools
102-
10394 logger .debug (
10495 "page=<%d>, page_tools=<%d>, total_filtered=<%d> | processed page" ,
10596 page_count ,
@@ -134,14 +125,14 @@ def _should_include_tool(self, tool: MCPAgentTool) -> bool:
134125
135126 return True
136127
137- def _apply_disambiguation (self , tool : MCPAgentTool ) -> MCPAgentTool :
138- """Apply disambiguation to a single tool if needed."""
139- if not self ._disambiguator :
128+ def _apply_prefix (self , tool : MCPAgentTool ) -> MCPAgentTool :
129+ """Apply prefix to a single tool if needed."""
130+ if not self ._prefix :
140131 return tool
141132
142- # Create new tool with disambiguated agent name but preserve original MCP name
133+ # Create new tool with prefixed agent name but preserve original MCP name
143134 old_name = tool .tool_name
144- new_agent_name = f"{ self ._disambiguator } _{ tool .mcp_tool .name } "
135+ new_agent_name = f"{ self ._prefix } _{ tool .mcp_tool .name } "
145136 new_tool = MCPAgentTool (tool .mcp_tool , tool .mcp_client , agent_facing_tool_name = new_agent_name )
146137 logger .debug ("tool_rename=<%s->%s> | renamed tool" , old_name , new_agent_name )
147138 return new_tool
@@ -160,7 +151,7 @@ def _matches_patterns(self, tool: MCPAgentTool, patterns: list[_ToolFilterPatter
160151 return True
161152 return False
162153
163- async def cleanup (self ) -> None :
154+ async def cleanup (self , ** kwargs : Any ) -> None :
164155 """Clean up the MCP client connection."""
165156 if not self ._started :
166157 return
0 commit comments