From 1a97ccc5af136d7dcb95df72f602c8c1f79be855 Mon Sep 17 00:00:00 2001 From: panjianxing <3207880464@qq.com> Date: Thu, 29 Oct 2026 21:28:22 +0800 Subject: [PATCH] fix(mcp): isolate keyword search admission Keep the existing mcp_tool_call logger semantics untouched so keyword-search admission remains independent of the merge order with the call-accounting change. Co-authored-by: Codex --- litellm/proxy/mcp_server/operations.py | 38 +++++++++++++++---- .../proxy/mcp_server/test_mcp_tool_search.py | 6 --- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/litellm/proxy/mcp_server/operations.py b/litellm/proxy/mcp_server/operations.py index b387dcc0558..01d7d0b71a3 100644 --- a/litellm/proxy/mcp_server/operations.py +++ b/litellm/proxy/mcp_server/operations.py @@ -359,21 +359,45 @@ async def _build_admitted_mcp_logging_obj( async def _build_virtual_call_logging_obj( name: str, - arguments: dict[str, object], # mutable-ok: existing virtual-call request payload + arguments: dict[str, object], user_api_key_auth: UserAPIKeyAuth, logging_auth: UserAPIKeyAuth | None = None, raw_headers: Mapping[str, str] | None = None, client_ip: str | None = None, ) -> LiteLLMLoggingObj | None: - """Run the pre-call pipeline for a virtual ``mcp_tool_call``.""" - return await _build_admitted_mcp_logging_obj( - name=name, - arguments=arguments, - user_api_key_auth=user_api_key_auth, - logging_auth=logging_auth, + """Run the pre-call pipeline (guardrails + logging setup) for a virtual + mcp_tool_call so the SSE path spend-logs like the REST path.""" + from litellm.proxy.common_request_processing import ( + ProxyBaseLLMRequestProcessing, + ) + from litellm.proxy.proxy_server import ( + general_settings, + proxy_config, + proxy_logging_obj, + ) + + request: Final = build_synthetic_mcp_request( + path="/mcp/tools/call", raw_headers=raw_headers, client_ip=client_ip, ) + pre_call_result: Final = await ProxyBaseLLMRequestProcessing( + data={"name": name, "arguments": arguments} + ).common_processing_pre_call_logic( + request=request, + user_api_key_dict=user_api_key_auth, + proxy_config=proxy_config, + route_type=CallTypes.call_mcp_tool.value, + proxy_logging_obj=proxy_logging_obj, + general_settings=general_settings, + logging_auth=logging_auth, + ) + _, logging_candidate = TypeAdapter(tuple[dict[str, object], object]).validate_python(pre_call_result) + if not isinstance(logging_candidate, LiteLLMLoggingObj): + verbose_logger.warning("MCP request processing did not return a logging object") + return None + virtual_logging_obj: Final = logging_candidate + return virtual_logging_obj async def _build_keyword_search_logging_obj( diff --git a/tests/unit/proxy/mcp_server/test_mcp_tool_search.py b/tests/unit/proxy/mcp_server/test_mcp_tool_search.py index c430846c81c..4b8d2da0113 100644 --- a/tests/unit/proxy/mcp_server/test_mcp_tool_search.py +++ b/tests/unit/proxy/mcp_server/test_mcp_tool_search.py @@ -1077,8 +1077,6 @@ class TestDispatchVirtualMcpTool: @pytest.mark.asyncio async def test_routes_agent_search_to_its_handler(self) -> None: - from litellm.proxy.mcp_server import server as srv - uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) with patch( # test-quality-ok: dispatch routing is the subject; the handler is faked like its siblings here "litellm.proxy.mcp_server.tool_search.handle_agent_search", @@ -1112,8 +1110,6 @@ class TestDispatchVirtualMcpTool: @pytest.mark.asyncio async def test_routes_call_with_client_ip(self) -> None: - from litellm.proxy.mcp_server import server as srv - uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) with patch( "litellm.proxy.mcp_server.tool_search.handle_mcp_tool_call", @@ -1145,8 +1141,6 @@ class TestDispatchVirtualMcpTool: """Regression: the SSE dispatch must run the pre-call pipeline and forward the resulting logging object to handle_mcp_tool_call, otherwise mcp_tool_call over /mcp/ skips spend logging and guardrails (unlike the REST path).""" - from litellm.proxy.mcp_server import server as srv - uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) sentinel_logging_obj = object() with (