diff --git a/litellm/proxy/guardrails/guardrail_hooks/airia/airia.py b/litellm/proxy/guardrails/guardrail_hooks/airia/airia.py index 8270d2a5167..d0f6c3a2ccd 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/airia/airia.py +++ b/litellm/proxy/guardrails/guardrail_hooks/airia/airia.py @@ -28,7 +28,7 @@ from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import UnifiedLLMGuardrails from litellm.types.guardrails import GuardrailEventHooks from litellm.types.proxy.guardrails.guardrail_hooks.airia import AiriaGuardrailConfigModel -from litellm.types.utils import CallTypesLiteral, GenericGuardrailAPIInputs +from litellm.types.utils import MCP_GUARDRAIL_CALL_TYPES, CallTypesLiteral, GenericGuardrailAPIInputs GUARDRAIL_PATH: Final = "/v1/guardrails/litellm" @@ -93,6 +93,21 @@ def _request_route(request_data: Mapping[str, object]) -> str | None: return None +def _mcp_tool_name(request_data: Mapping[str, object], logging_obj: "LiteLLMLoggingObj | None") -> str | None: + """The MCP tool this evaluation is for, or None for model traffic. + + The logger's call type wins when there is one, because a chat client can put MCP keys in its request body. + """ + if logging_obj is not None and logging_obj.call_type not in MCP_GUARDRAIL_CALL_TYPES: + return None + pre_call_name: Final = request_data.get("mcp_tool_name") + if isinstance(pre_call_name, str) and pre_call_name: + return pre_call_name + call_metadata: Final = _as_object(request_data.get("mcp_tool_call_metadata")) + post_call_name: Final = call_metadata.get("name") if call_metadata is not None else None + return post_call_name if isinstance(post_call_name, str) and post_call_name else None + + def _as_object(value: object) -> Mapping[str, object] | None: """`value` as a JSON object, or None. States the key type once so the checker does not see `dict[Unknown, Unknown]` at every read.""" @@ -171,6 +186,7 @@ class AiriaGuardrail(CustomGuardrail): "tool_calls": inputs.get("tool_calls") or (), "model": inputs.get("model"), "litellm_call_id": call_id, + "mcp_tool_name": _mcp_tool_name(request_data, logging_obj), }, headers={"Authorization": f"Bearer {self.api_key}"}, ) @@ -203,8 +219,7 @@ class AiriaGuardrail(CustomGuardrail): ) if action == ACTION_INTERVENED: - # during_call runs alongside the model call, so the prompt has already been sent and a - # rewrite of it would reach nothing but the logs + # during_call races the model call, so a rewrite of the prompt cannot be relied on to reach it if _EVALUATING_DURING_CALL.get(): raise self._blocked() if input_type == "response" and _stream_cannot_carry_a_rewrite(request_data): diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_airia.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_airia.py index dce8b861ff7..57ba1ed0b33 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_airia.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_airia.py @@ -1,5 +1,6 @@ import json import uuid +from datetime import datetime, timezone from collections.abc import AsyncGenerator from typing import Final, Literal from unittest.mock import AsyncMock, MagicMock @@ -11,6 +12,7 @@ from mcp.types import CallToolResult, TextContent import litellm from litellm.caching.caching import DualCache from litellm.exceptions import GuardrailRaisedException +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache from litellm.proxy.guardrails.guardrail_hooks.airia.airia import AiriaGuardrail @@ -144,6 +146,7 @@ async def test_pre_mcp_call_redaction_rewrites_the_tool_arguments() -> None: assert data["mcp_arguments"] == {"to": "[EmailAddress1]"} assert _post_mock(guardrail).call_args.kwargs["json"]["input_type"] == "request" assert _post_mock(guardrail).call_args.kwargs["json"]["texts"] == ["ada@example.com"] + assert _post_mock(guardrail).call_args.kwargs["json"]["mcp_tool_name"] == "send_email" @pytest.mark.asyncio @@ -175,12 +178,36 @@ async def test_post_mcp_call_redaction_masks_the_tool_result(monkeypatch: pytest checked: Final = await ProxyLogging(user_api_key_cache=UserApiKeyCache()).post_mcp_call_hook( response=CallToolResult(content=[TextContent(type="text", text="reach ada@example.com")]), - request_data={}, + request_data={"mcp_tool_call_metadata": {"name": "lookup_contact", "arguments": {}}}, user_api_key_dict=UserAPIKeyAuth(api_key="test"), ) assert checked.content == [TextContent(type="text", text="reach [EmailAddress1]")] assert _post_mock(guardrail).call_args.kwargs["json"]["input_type"] == "response" + assert _post_mock(guardrail).call_args.kwargs["json"]["mcp_tool_name"] == "lookup_contact" + + +@pytest.mark.asyncio +async def test_mcp_keys_in_a_chat_request_body_do_not_mark_it_as_a_tool_call() -> None: + guardrail: Final = _make_guardrail_with_response({"action": "NONE"}) + logging_obj: Final = LiteLLMLoggingObj( + model="gpt-4o", + messages=[], + stream=False, + call_type="acompletion", + start_time=datetime(2026, 1, 1, tzinfo=timezone.utc), + litellm_call_id="call-1", + function_id="1", + ) + + await guardrail.apply_guardrail( + inputs=_inputs(), + request_data={"mcp_tool_name": "send_email", "mcp_tool_call_metadata": {"name": "send_email"}}, + input_type="request", + logging_obj=logging_obj, + ) + + assert _post_mock(guardrail).call_args.kwargs["json"]["mcp_tool_name"] is None def _during_call_request() -> dict[str, object]: @@ -198,7 +225,6 @@ def _during_call_request() -> dict[str, object]: async def test_during_call_enforces_every_intervention_as_a_block( verdict: dict[str, object], monkeypatch: pytest.MonkeyPatch ) -> None: - """The prompt is already on its way to the model, so a redaction can no longer be delivered""" guardrail: Final = _make_guardrail_with_response(verdict, event_hook="during_call", default_on=True) monkeypatch.setattr(litellm, "callbacks", [guardrail])