feat: _init_semantic_filter_settings_in_db

This commit is contained in:
Ishaan Jaffer 2026-02-04 17:29:10 -08:00
parent 5e0ee7aeb8
commit 6a1a0cb6b9
3 changed files with 76 additions and 2 deletions

View file

@ -11,7 +11,7 @@ import litellm
from litellm._logging import verbose_proxy_logger
async def route_a2a_agent_request(data: dict, route_type: str) -> Optional[Any]:
def route_a2a_agent_request(data: dict, route_type: str) -> Optional[Any]:
"""
Route A2A agent requests directly to litellm with injected API base.

View file

@ -3920,6 +3920,77 @@ class ProxyConfig:
prisma_client=prisma_client, proxy_config=self
)
if self._should_load_db_object(object_type="semantic_filter_settings"):
await self._init_semantic_filter_settings_in_db(
prisma_client=prisma_client
)
async def _init_semantic_filter_settings_in_db(self, prisma_client: PrismaClient):
"""
Initialize MCP semantic filter settings from database.
Called periodically (approximately every 10 seconds) by background task to hot-reload settings across all pods.
"""
import json
import litellm
from litellm.proxy.hooks.mcp_semantic_filter import SemanticToolFilterHook
try:
# Load litellm_settings from DB
config_record = await prisma_client.db.litellm_config.find_unique(
where={"param_name": "litellm_settings"}
)
if config_record is None or config_record.param_value is None:
return
litellm_settings = config_record.param_value
if isinstance(litellm_settings, str):
litellm_settings = json.loads(litellm_settings)
mcp_semantic_filter_config = litellm_settings.get(
"mcp_semantic_tool_filter", None
)
if mcp_semantic_filter_config is None:
return
# Check if settings have changed (compare with in-memory state)
if hasattr(self, "_last_semantic_filter_config"):
if self._last_semantic_filter_config == mcp_semantic_filter_config:
verbose_proxy_logger.debug(
"Semantic filter settings unchanged, skipping reinitialization"
)
return
# Remove old hooks using logging callback manager
litellm.logging_callback_manager.remove_callbacks_by_type(
litellm.callbacks, SemanticToolFilterHook
)
# Initialize new hook if enabled
if mcp_semantic_filter_config.get("enabled", False):
global llm_router
hook = await SemanticToolFilterHook.initialize_from_config(
config=mcp_semantic_filter_config,
llm_router=llm_router,
)
if hook:
litellm.logging_callback_manager.add_litellm_callback(hook)
verbose_proxy_logger.info(
"MCP Semantic Filter reinitialized from DB"
)
else:
verbose_proxy_logger.info("MCP Semantic Filter disabled")
# Store current config for comparison next time
self._last_semantic_filter_config = mcp_semantic_filter_config.copy()
except Exception as e:
verbose_proxy_logger.exception(
f"Error initializing semantic filter settings from DB: {e}"
)
async def _init_sso_settings_in_db(self, prisma_client: PrismaClient):
"""
Initialize SSO settings from database into the router on startup.

View file

@ -332,7 +332,10 @@ async def route_request(
route_a2a_agent_request,
)
return await route_a2a_agent_request(data, route_type)
result = route_a2a_agent_request(data, route_type)
if result is not None:
return result
# Fall through to raise exception below if result is None
elif user_model is not None:
return getattr(litellm, f"{route_type}")(**data)