The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(guardrails): run bedrock guardrail on MCP tool calls in during_mcp_call mode (#35149)
Yassin Kortam3a429f3098a0
diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.pyindex 9c7bbbd3b4c..fbe70c03d74 100644--- a/litellm/integrations/custom_guardrail.py+++ b/litellm/integrations/custom_guardrail.py@@ -1182,6 +1182,7 @@ class CustomGuardrail(CustomLogger):call_type == CallTypes.completion.valueor call_type == CallTypes.acompletion.valueor call_type == CallTypes.anthropic_messages.value+ or call_type == CallTypes.call_mcp_tool.value):return data.get("messages")diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.pyindex cec682d772a..bdbe3f193d4 100644--- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py+++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py@@ -1634,6 +1634,8 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):)event_type: GuardrailEventHooks = GuardrailEventHooks.during_call+ if call_type == CallTypes.call_mcp_tool.value:+ event_type = GuardrailEventHooks.during_mcp_callif self.should_run_guardrail(data=data, event_type=event_type) is not True:return@@ -1666,7 +1668,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):source="INPUT",messages=filtered_messages,request_data=data,- logging_event_type=GuardrailEventHooks.during_call,+ logging_event_type=event_type,)#########################################################diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.pyindex 53f32fd96fb..65d6e33588f 100644--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py@@ -21,7 +21,7 @@ from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import ()from litellm.proxy.utils import ProxyLoggingfrom litellm.types.guardrails import GuardrailEventHooks-from litellm.types.utils import ModelResponse+from litellm.types.utils import CallTypes, ModelResponse@pytest.mark.asyncio@@ -3614,3 +3614,59 @@ class TestBedrockIncrementalFlagInteractions:)scanned = [m["content"] for m in mock_api.call_args.kwargs["messages"]]assert scanned == ["q1"], "latest-role selection must exclude the system prompt"+++ "mode, call_type, should_scan",+ [+ ("during_mcp_call", "call_mcp_tool", True),+ ("during_call", "completion", True),+ ("during_mcp_call", "completion", False),+ ("during_call", "call_mcp_tool", False),+ ],+)+async def test_moderation_hook_honors_the_mcp_event_type(mode, call_type, should_scan):+ """A guardrail configured mode: during_mcp_call must actually scan MCP tool calls.++ ProxyLogging.during_call_hook remaps call_mcp_tool to during_mcp_call before+ dispatching, so re-checking during_call here would reject the very requests the+ guardrail was configured for and let the tool call through unscanned.+ """+ guardrail = BedrockGuardrail(+ guardrail_name="bedrock-mcp",+ guardrailIdentifier="gid",+ guardrailVersion="1",+ event_hook=mode,+ default_on=True,+ )+ data = {+ "messages": [{"role": "user", "content": "scan me"}],+ "mcp_tool_name": "search",+ "mcp_arguments": {"query": "scan me"},+ }++ with patch.object(guardrail, "make_bedrock_api_request", new_callable=AsyncMock) as mock_api:+ mock_api.return_value = MagicMock(+ action="NONE", output=[], outputs=[], assessments=[]+ )+ await guardrail.async_moderation_hook(+ data=data,+ user_api_key_dict=UserAPIKeyAuth(api_key="sk-test", user_id="u"),+ call_type=call_type,+ )++ assert (mock_api.call_count == 1) is should_scan, (+ f"mode={mode} call_type={call_type}: expected scan={should_scan}, "+ f"bedrock api called {mock_api.call_count} times"+ )+ if should_scan:+ expected_event = (+ GuardrailEventHooks.during_mcp_call+ if call_type == CallTypes.call_mcp_tool.value+ else GuardrailEventHooks.during_call+ )+ assert mock_api.call_args.kwargs["logging_event_type"] == expected_event, (+ "the scan must be logged under the event it actually ran for, so guardrail logs, "+ "OTel spans, and Langfuse metadata do not misclassify MCP enforcement as an LLM call"+ )