Repository navigation
fix(model_armor): scan MCP tool calls for pre_mcp_call / during_mcp_call modes - #32296
Conversation
…all modes ModelArmorGuardrail.async_pre_call_hook and async_moderation_hook hardcoded their inner should_run_guardrail event type to pre_call / during_call. The central dispatcher already remaps call_mcp_tool -> pre_mcp_call/during_mcp_call and passes the outer gate, but Model Armor's redundant inner gate then rejected MCP calls for a guardrail configured with mode pre_mcp_call/during_mcp_call, so tool-call content was silently skipped. Remap call_mcp_tool -> pre_mcp_call/during_mcp_call in both hooks, matching the existing behavior of the noma and cisco guardrails. Adds regression tests covering both hooks (scan runs on MCP calls, still skipped for chat traffic). Generated with AI Co-Authored-By: Claude Code
Greptile SummaryThis PR fixes a bug where Model Armor guardrails configured for
Confidence Score: 5/5Safe to merge — the change is a minimal, targeted two-line fix per hook with no effect on existing chat or LLM call paths. The fix is surgical: only MCP tool calls get the remapped event type, and the gate logic for every other call type is completely unchanged. The new tests confirm both that MCP calls are now scanned and that regular chat traffic remains unaffected. CallTypes.call_mcp_tool, GuardrailEventHooks.pre_mcp_call, and GuardrailEventHooks.during_mcp_call all exist in the type definitions, so there is no risk of a silent no-op from a typo. No mutable state, no new dependencies, no backwards-incompatible behavior. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py | Adds a two-line remap in async_pre_call_hook and async_moderation_hook so that a call_type of call_mcp_tool switches the event gate to pre_mcp_call / during_mcp_call before calling should_run_guardrail, fixing the silent skip of MCP tool-call scanning. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py | Adds 4 targeted regression tests: 2 assert MCP tool calls are scanned when the guardrail is configured for pre_mcp_call/during_mcp_call, and 2 assert ordinary chat traffic is still skipped. All tests mock the HTTP layer cleanly with no real network calls. |
Reviews (1): Last reviewed commit: "fix(model_armor): scan MCP tool calls fo..." | Re-trigger Greptile
Greptile SummaryThis PR fixes a bug in
Confidence Score: 4/5Safe to merge; the change is small and isolated to Model Armor's inner event-type gate, with passing tests and no impact on other guardrail providers. The two-line fix per hook is correct and symmetric with the remapping already present in the outer dispatcher. Four new tests cover the regression and the scoping boundary. The only latent concern is that ModelArmorGuardrail's default event_hook list (pre_call, during_call, post_call) does not include the new MCP variants, so users who want MCP scanning must explicitly opt-in — but this is the stated design intent and is not introduced by this PR. No files require special attention; both changed files are straightforward.
|
| Filename | Overview |
|---|---|
| litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py | Adds a CallTypes import and two symmetrical if-blocks that remap call_mcp_tool → pre_mcp_call / during_mcp_call in the inner should_run_guardrail gate; logic is correct and mirrors the existing outer-dispatcher remapping in proxy/utils.py. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py | Adds four new async pytest tests (2 regression proofs + 2 scoping guards) using existing helpers _make_guardrail and _armor_response; all mocks are properly scoped and assertions correctly target mock_post.assert_called() / assert_not_called(). |
Reviews (2): Last reviewed commit: "fix(model_armor): scan MCP tool calls fo..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d037d13. Configure here.
|
Re: the 4/5 review's noted concern about |
Relevant issues
Linear ticket
Pre-Submission checklist
@greptileaiand received a Confidence Score of at least 4/5Screenshots / Proof of Fix
Original author's proof of fix (from #32278): validated in a live dev deployment with a Model Armor guardrail configured for
pre_mcp_call/during_mcp_call; MCP tool calls are now scanned instead of silently skipped.Type
🐛 Bug Fix
Changes
A Model Armor guardrail configured with mode
pre_mcp_call(orduring_mcp_call) does not scan MCP tool calls; the tool name and arguments pass through completely unscanned.ModelArmorGuardrail.async_pre_call_hookandasync_moderation_hookhardcode their innershould_run_guardrailevent type topre_call/during_call. The central dispatcher (ProxyLogging._process_guardrail_callback/during_call_hookinlitellm/proxy/utils.py) already remapscall_mcp_tooltopre_mcp_call/during_mcp_calland passes the outer gate, but Model Armor's redundant inner gate then re-checks against the un-remappedpre_call/during_call, so a guardrail whose configured mode ispre_mcp_callfails its own inner check and the scan is skipped.The fix remaps
call_mcp_tooltopre_mcp_call/during_mcp_callin both hooks, matching the behavior already present in the unified guardrail path.Scope: this scans the MCP tool name/arguments on the
pre_mcp_call/during_mcp_callhooks. It does not scan MCP tool results; there is nopost_mcp_callevent and MCP output scanning is a separate, pre-existing framework gap, unchanged by this PR.Adds 4 tests in
tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py: 2 regression tests proving apre_mcp_call/during_mcp_callguardrail now scans an MCP tool call, and 2 scoping guards proving ordinary chat traffic is still skipped.Credit
Adopted from #32278 by @eugene-yao-zocdoc. Mirrored onto a
litellm_branch so CircleCI and the internal lint workflow run.Note
Medium Risk
Changes guardrail gating on MCP tool calls (security-sensitive); scope is narrow and covered by new tests, with no change to MCP result scanning.
Overview
Fixes a security gap where Model Armor guardrails configured for
pre_mcp_callorduring_mcp_callnever ran on MCP tool invocations, so tool names and arguments could bypass scanning.In
async_pre_call_hookandasync_moderation_hook, whencall_typeiscall_mcp_tool, the innershould_run_guardrailgate now usespre_mcp_call/during_mcp_callinstead of alwayspre_call/during_call, aligning with the proxy dispatcher. Ordinary chatcompletiontraffic is unchanged for MCP-only modes.Adds four regression tests covering MCP scans and chat skip behavior for both hooks.
Reviewed by Cursor Bugbot for commit d037d13. Bugbot is set up for automated code reviews on this repo. Configure here.