Support to filter available MCP Tools (#345)

* feat: support to complex filter on ToolCollection

* refactor: use proper tool collection methot to filter tool in CrewAiEnterpriseTools

* feat: allow to filter available MCP tools
This commit is contained in:
Lucas Gomide
2025-06-25 13:32:22 -03:00
committed by GitHub
parent e8825d071a
commit 03917411b4
5 changed files with 193 additions and 23 deletions

View File

@@ -46,10 +46,18 @@ class MCPServerAdapter:
with MCPServerAdapter({"url": "http://localhost:8000/sse"}) as tools:
# tools is now available
# context manager with filtered tools
with MCPServerAdapter(..., "tool1", "tool2") as filtered_tools:
# only tool1 and tool2 are available
# manually stop mcp server
try:
mcp_server = MCPServerAdapter(...)
tools = mcp_server.tools
tools = mcp_server.tools # all tools
# or with filtered tools
mcp_server = MCPServerAdapter(..., "tool1", "tool2")
filtered_tools = mcp_server.tools # only tool1 and tool2
...
finally:
mcp_server.stop()
@@ -61,18 +69,22 @@ class MCPServerAdapter:
def __init__(
self,
serverparams: StdioServerParameters | dict[str, Any],
*tool_names: str,
):
"""Initialize the MCP Server
Args:
serverparams: The parameters for the MCP server it supports either a
`StdioServerParameters` or a `dict` respectively for STDIO and SSE.
*tool_names: Optional names of tools to filter. If provided, only tools with
matching names will be available.
"""
super().__init__()
self._adapter = None
self._tools = None
self._tool_names = list(tool_names) if tool_names else None
if not MCP_AVAILABLE:
import click
@@ -127,7 +139,11 @@ class MCPServerAdapter:
raise ValueError(
"MCP server not started, run `mcp_server.start()` first before accessing `tools`"
)
return ToolCollection(self._tools)
tools_collection = ToolCollection(self._tools)
if self._tool_names:
return tools_collection.filter_by_names(self._tool_names)
return tools_collection
def __enter__(self):
"""