test(mcp): cover virtual call logging failures

This commit is contained in:
panjianxing 2026-10-24 20:58:09 +08:00
parent 98022447d0
commit 29c1f2106b
2 changed files with 58 additions and 17 deletions

View file

@ -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(

View file

@ -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