From 29c1f2106bd6cf3ab690c660c1ebd3723eab7bb8 Mon Sep 17 00:00:00 2001 From: panjianxing <3207880464@qq.com> Date: Sat, 24 Oct 2026 20:58:09 +0800 Subject: [PATCH] test(mcp): cover virtual call logging failures --- .../mcp_server/rest_endpoints.py | 24 +++------ .../mcp_server/test_mcp_tool_search.py | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py index f99970a4dad..5ccca7ddac8 100644 --- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py +++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py @@ -393,23 +393,13 @@ if MCP_AVAILABLE: # MCP_TOOL_CALL_TOOL_NAME: the real tool call below owns admission and guardrails. # Building the logger separately prevents a second RPM or parallel slot charge. _request_start_time: Final = datetime.now() # noqa: DTZ005 # naive to match the tool start time below - try: - virtual_logging_obj: Final = await _build_virtual_call_logging_obj( - name=tool_arguments.get("tool_name", ""), - arguments=tool_arguments.get("arguments") or {}, - user_api_key_auth=user_api_key_dict, - raw_headers=virtual_raw_headers, - client_ip=rest_client_ip, - ) - except Exception as e: - await _safe_fire_mcp_tool_call_failure_logging( - None, - e, - _request_start_time, - user_api_key_dict, - data, - ) - raise + virtual_logging_obj: Final = await _build_virtual_call_logging_obj( + name=tool_arguments.get("tool_name", ""), + arguments=tool_arguments.get("arguments") or {}, + user_api_key_auth=user_api_key_dict, + raw_headers=virtual_raw_headers, + client_ip=rest_client_ip, + ) _tool_start_time: Final = datetime.now() try: result: Final = await handle_mcp_tool_call( diff --git a/tests/unit/proxy/_experimental/mcp_server/test_mcp_tool_search.py b/tests/unit/proxy/_experimental/mcp_server/test_mcp_tool_search.py index 2587bd08473..d6c5f7a7cdc 100644 --- a/tests/unit/proxy/_experimental/mcp_server/test_mcp_tool_search.py +++ b/tests/unit/proxy/_experimental/mcp_server/test_mcp_tool_search.py @@ -1197,6 +1197,57 @@ class TestDispatchVirtualMcpTool: assert logging_obj.model == "MCP: math-add" pre_call_hook.assert_not_awaited() + @pytest.mark.asyncio + async def test_virtual_logger_setup_failure_does_not_block_tool(self) -> None: + uak = UserAPIKeyAuth(api_key="k") + + with patch.object( + mcp_operations, + "function_setup", + side_effect=RuntimeError("logging unavailable"), + ): + logging_obj = await mcp_operations._build_virtual_call_logging_obj( + name="math-add", + arguments={"a": 1}, + user_api_key_auth=uak, + ) + + assert logging_obj is None + + @pytest.mark.asyncio + async def test_call_failure_logs_and_propagates(self) -> None: + uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) + sentinel_logging_obj = object() + failure = RuntimeError("tool failed") + + with ( + patch.object( + mcp_operations, + "_build_virtual_call_logging_obj", + new_callable=AsyncMock, + return_value=sentinel_logging_obj, + ), + patch( + "litellm.proxy._experimental.mcp_server.tool_search.handle_mcp_tool_call", + new_callable=AsyncMock, + side_effect=failure, + ), + patch.object( + mcp_operations, + "fire_mcp_tool_call_failure_logging", + new_callable=AsyncMock, + ) as mock_failure_log, + ): + with pytest.raises(RuntimeError, match="tool failed"): + await mcp_operations._dispatch_virtual_mcp_tool( + name=MCP_TOOL_CALL_TOOL_NAME, + arguments={"tool_name": "math-add", "arguments": {"a": 1}}, + user_api_key_auth=uak, + client_ip=None, + ) + + mock_failure_log.assert_awaited_once() + @pytest.mark.asyncio async def test_search_coerces_non_int_top_k(self) -> None: """Regression: a non-integer top_k from an MCP client must not raise; it