Fix litellm/tests/test_litellm/proxy/_experimental/mcp_server/test_semantic_tool_filter.py tests

This commit is contained in:
Sameer Kankute
2026-02-03 12:08:19 +05:30
parent eb8f4d3e05
commit 9a6bafe89e
2 changed files with 30 additions and 5 deletions
@@ -12,8 +12,19 @@ sys.path.insert(0, os.path.abspath("../.."))
from mcp.types import Tool as MCPTool
# Check if semantic-router is available
try:
import semantic_router
SEMANTIC_ROUTER_AVAILABLE = True
except ImportError:
SEMANTIC_ROUTER_AVAILABLE = False
@pytest.mark.asyncio
@pytest.mark.skipif(
not SEMANTIC_ROUTER_AVAILABLE,
reason="semantic-router not installed. Install with: pip install 'litellm[semantic-router]'"
)
async def test_e2e_semantic_filter():
"""E2E: Load router/filter and verify hook filters tools."""
from litellm import Router
@@ -37,8 +48,6 @@ async def test_e2e_semantic_filter():
enabled=True,
)
hook = SemanticToolFilterHook(filter_instance)
# Create 10 tools
tools = [
MCPTool(name="gmail_send", description="Send an email via Gmail", inputSchema={"type": "object"}),
@@ -53,10 +62,16 @@ async def test_e2e_semantic_filter():
MCPTool(name="note_add", description="Add note", inputSchema={"type": "object"}),
]
# Build router with test tools
filter_instance._build_router(tools)
hook = SemanticToolFilterHook(filter_instance)
data = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "Send an email and create a calendar event"}],
"tools": tools,
"metadata": {}, # Initialize metadata dict for hook to store filter stats
}
# Call hook
@@ -71,6 +71,9 @@ async def test_semantic_filter_basic_filtering():
enabled=True,
)
# Build router with the tools before filtering
filter_instance._build_router(tools)
# Filter tools with email-related query
filtered = await filter_instance.filter_tools(
query="send an email to john@example.com",
@@ -139,6 +142,9 @@ async def test_semantic_filter_top_k_limiting():
enabled=True,
)
# Build router with the tools before filtering
filter_instance._build_router(tools)
# Filter tools
filtered = await filter_instance.filter_tools(
query="test query",
@@ -297,21 +303,25 @@ async def test_semantic_filter_hook_triggers_on_completion():
enabled=True,
)
# Create hook
hook = SemanticToolFilterHook(filter_instance)
# Prepare data - completion request with tools
tools = [
MCPTool(name=f"tool_{i}", description=f"Tool {i}", inputSchema={"type": "object"})
for i in range(10)
]
# Build router with the tools before filtering
filter_instance._build_router(tools)
# Create hook
hook = SemanticToolFilterHook(filter_instance)
data = {
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Send an email"}
],
"tools": tools,
"metadata": {}, # Hook needs metadata field to store filter stats
}
# Mock user API key dict and cache