From dd4d2f44e33118f1d0b1ea3884a3c5808cc6b195 Mon Sep 17 00:00:00 2001 From: Hashim1999164 <64767361+Hashim1999164@users.noreply.github.com> Date: Thu, 13 Aug 2026 04:13:51 +0500 Subject: [PATCH] Avoid empty dict fallbacks in mcp tool search settings --- litellm/proxy/_experimental/mcp_server/rest_endpoints.py | 8 ++++++-- litellm/proxy/_experimental/mcp_server/server.py | 8 ++++++-- litellm/proxy/_experimental/mcp_server/tool_search.py | 5 ++++- litellm/types/object_permission.py | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py index 1f49214fcd7..d6da662fd09 100644 --- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py +++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py @@ -189,12 +189,14 @@ if MCP_AVAILABLE: ) = _extract_mcp_headers_from_request(request, MCPRequestHandler) virtual_oauth2_headers: Final = MCPRequestHandler._get_oauth2_headers_from_headers(request.headers) if tool_name == MCP_TOOL_SEARCH_TOOL_NAME: + raw_settings = proxy_config.get_config_state().get("litellm_settings") + settings = raw_settings if isinstance(raw_settings, Mapping) else None return await handle_mcp_tool_search( query=tool_arguments.get("query", ""), top_k=resolve_mcp_tool_search_top_k( tool_arguments.get("top_k"), user_api_key_dict, - proxy_config.get_config_state().get("litellm_settings") or {}, + settings, ), user_api_key_dict=user_api_key_dict, client_ip=rest_client_ip, @@ -767,11 +769,13 @@ if MCP_AVAILABLE: ) from litellm.proxy.proxy_server import proxy_config + raw_settings = proxy_config.get_config_state().get("litellm_settings") + settings = raw_settings if isinstance(raw_settings, Mapping) else None return { "tools": get_virtual_tool_definitions( default_top_k=get_mcp_tool_search_default_top_k( user_api_key_dict, - proxy_config.get_config_state().get("litellm_settings") or {}, + settings, ) ), "error": None, diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.py index 350335577bd..157351c429b 100644 --- a/litellm/proxy/_experimental/mcp_server/server.py +++ b/litellm/proxy/_experimental/mcp_server/server.py @@ -791,12 +791,14 @@ if MCP_AVAILABLE: ) from litellm.proxy.proxy_server import proxy_config + raw_settings = proxy_config.get_config_state().get("litellm_settings") + settings = raw_settings if isinstance(raw_settings, Mapping) else None return [ Tool.model_validate(d) for d in get_virtual_tool_definitions( default_top_k=get_mcp_tool_search_default_top_k( user_api_key_auth, - proxy_config.get_config_state().get("litellm_settings") or {}, + settings, ) ) ] @@ -950,12 +952,14 @@ if MCP_AVAILABLE: if name == MCP_TOOL_SEARCH_TOOL_NAME: from litellm.proxy.proxy_server import proxy_config + raw_settings = proxy_config.get_config_state().get("litellm_settings") + settings = raw_settings if isinstance(raw_settings, Mapping) else None return await handle_mcp_tool_search( query=args.get("query", ""), top_k=resolve_mcp_tool_search_top_k( args.get("top_k"), user_api_key_auth, - proxy_config.get_config_state().get("litellm_settings") or {}, + settings, ), user_api_key_dict=user_api_key_auth, client_ip=client_ip, diff --git a/litellm/proxy/_experimental/mcp_server/tool_search.py b/litellm/proxy/_experimental/mcp_server/tool_search.py index 6f2d94320e4..226a54bbf5c 100644 --- a/litellm/proxy/_experimental/mcp_server/tool_search.py +++ b/litellm/proxy/_experimental/mcp_server/tool_search.py @@ -28,7 +28,10 @@ def get_mcp_tool_search_default_top_k( if key_top_k is not None: return coerce_top_k(key_top_k, default=DEFAULT_MCP_TOOL_SEARCH_TOP_K) - global_top_k = (litellm_settings or {}).get("mcp_tool_search_default_top_k") + if litellm_settings is None: + global_top_k = None + else: + global_top_k = litellm_settings.get("mcp_tool_search_default_top_k") if global_top_k is not None: return coerce_top_k(global_top_k, default=DEFAULT_MCP_TOOL_SEARCH_TOP_K) diff --git a/litellm/types/object_permission.py b/litellm/types/object_permission.py index a84355ebb11..52cf9f06422 100644 --- a/litellm/types/object_permission.py +++ b/litellm/types/object_permission.py @@ -23,4 +23,4 @@ class ObjectPermissionDict(TypedDict, total=False): models: list[str] | None search_tools: list[str] | None mcp_tool_search_enabled: bool | None - mcp_tool_search_top_k: int | None + mcp_tool_search_top_k: int | None # writable-ok: mutated on in-memory dict payloads before persistence