fix: semantic filter lazy-build router + handle missing metadata key

Two bugs from PR #20296 (MCP Semantic Filtering):

1. filter_tools() returned all tools when tool_router was None instead
   of lazily building the router from available_tools. This caused
   top_k to never limit results.

2. async_pre_call_hook() crashed with KeyError when data dict had no
   metadata key, causing the exception handler to return None (no
   filtering applied).
This commit is contained in:
Shin 2026-02-03 04:33:25 +00:00
parent 9202870e14
commit e7600ccd63
2 changed files with 15 additions and 3 deletions

View file

@ -172,10 +172,20 @@ class SemanticMCPToolFilter:
if not query or not query.strip():
return available_tools
# Router should be built on startup - if not, something went wrong
# Lazy-build router from available_tools if not already initialized
if self.tool_router is None:
verbose_logger.warning("Router not initialized - was build_router_from_mcp_registry() called on startup?")
return available_tools
verbose_logger.info(
"Router not pre-built, building from available_tools on first call"
)
try:
self._build_router(available_tools)
except Exception as e:
verbose_logger.error(f"Failed to lazy-build router: {e}")
return available_tools
if self.tool_router is None:
verbose_logger.warning("Router still None after build attempt")
return available_tools
# Run semantic filtering
try:

View file

@ -224,6 +224,8 @@ class SemanticToolFilterHook(CustomLogger):
tool_names_csv = self._get_tool_names_csv(filtered_tools)
_metadata_variable_name = self._get_metadata_variable_name(data)
if _metadata_variable_name not in data:
data[_metadata_variable_name] = {}
data[_metadata_variable_name]["litellm_semantic_filter_stats"] = filter_stats
data[_metadata_variable_name]["litellm_semantic_filter_tools"] = tool_names_csv