From ad3b06fd683d1cbf45fe9e3ecb96c3e8d1209d57 Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Mon, 2 Feb 2026 14:01:41 -0800 Subject: [PATCH] fix: filter_tools --- .../mcp_server/semantic_tool_filter.py | 13 +++++++------ tests/mcp_tests/test_semantic_tool_filter_e2e.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py b/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py index 94b41943eaa..e8a96ee05e5 100644 --- a/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py +++ b/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py @@ -150,6 +150,8 @@ class SemanticMCPToolFilter: Returns: Filtered and ordered list of tools (up to top_k) """ + # Query semantic router with limit for top-k matches + from semantic_router.schema import RouteChoice if not self.enabled or not available_tools: return available_tools @@ -169,11 +171,9 @@ class SemanticMCPToolFilter: verbose_logger.warning("Router rebuild failed, returning all tools") return available_tools - # Query semantic router - from semantic_router.schema import RouteChoice - verbose_logger.debug(f"Querying semantic router with: '{query[:50]}...'") - matches = self.tool_router(text=query) + verbose_logger.debug(f"Querying semantic router with: '{query[:50]}...' (top_k={top_k})") + matches = self.tool_router(text=query, limit=top_k) if not matches: verbose_logger.warning( @@ -184,10 +184,11 @@ class SemanticMCPToolFilter: # Extract matched tool names matched_names: List[str] = [] if isinstance(matches, RouteChoice): - matched_names = [matches.name] + if matches.name: + matched_names = [matches.name] elif isinstance(matches, list): # semantic-router returns list of RouteChoice, take top_k - matched_names = [m.name for m in matches[:top_k] if hasattr(m, 'name')] + matched_names = [m.name for m in matches[:top_k] if hasattr(m, 'name') and m.name is not None] if not matched_names: verbose_logger.warning("No matched tool names extracted, returning all tools") diff --git a/tests/mcp_tests/test_semantic_tool_filter_e2e.py b/tests/mcp_tests/test_semantic_tool_filter_e2e.py index dbcdeabdb95..cf951c1884b 100644 --- a/tests/mcp_tests/test_semantic_tool_filter_e2e.py +++ b/tests/mcp_tests/test_semantic_tool_filter_e2e.py @@ -55,7 +55,7 @@ async def test_e2e_semantic_filter(): data = { "model": "gpt-4", - "messages": [{"role": "user", "content": "Send an email"}], + "messages": [{"role": "user", "content": "Send an email and create a calendar event"}], "tools": tools, }