Skip to content

fix(model_armor): scan MCP tool calls for pre_mcp_call / during_mcp_call modes - #32278

Closed
eugene-yao-zocdoc wants to merge 7 commits into
BerriAI:litellm_internal_stagingfrom
eugene-yao-zocdoc:fix/model-armor-mcp-guardrail-event-hook
Closed

eugene-yao-zocdoc wants to merge 7 commits into
BerriAI:litellm_internal_stagingfrom
eugene-yao-zocdoc:fix/model-armor-mcp-guardrail-event-hook

Conversation

@eugene-yao-zocdoc

@eugene-yao-zocdoc eugene-yao-zocdoc commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Type

🐛 Bug Fix

Summary

A Model Armor guardrail configured with mode pre_mcp_call (or during_mcp_call) does not scan MCP tool calls — the tool name/arguments pass through completely unscanned.

Root cause

ModelArmorGuardrail.async_pre_call_hook and async_moderation_hook hardcode their inner should_run_guardrail event type to pre_call / during_call:

event_type = GuardrailEventHooks.pre_call
if self.should_run_guardrail(data=data, event_type=event_type) is not True:
    return data

The central dispatcher (ProxyLogging._process_guardrail_callback / during_call_hook in litellm/proxy/utils.py) already remaps call_mcp_tool → pre_mcp_call / during_mcp_call and passes the outer gate. But Model Armor's redundant inner gate then re-checks against the un-remapped pre_call / during_call, so a guardrail whose configured mode is pre_mcp_call fails its own inner check and the scan is skipped.

Fix

Remap call_mcp_tool → pre_mcp_call / during_mcp_call in both hooks, matching the behavior already present in the noma and cisco guardrails:

event_type = GuardrailEventHooks.pre_call
if call_type == CallTypes.call_mcp_tool.value:
    event_type = GuardrailEventHooks.pre_mcp_call

(and the analogous during_call → during_mcp_call in async_moderation_hook).

Scope: this scans the MCP tool name/arguments on the pre_mcp_call / during_mcp_call hooks. It does not scan MCP tool results — there is no post_mcp_call event and MCP output scanning is a separate, pre-existing framework gap (MCPGuardrailTranslationHandler.process_output_response is a documented no-op), unchanged by this PR.

Tests

Adds 4 tests in tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py:

  • 2 regression tests — a pre_mcp_call guardrail scans an MCP tool call (call_type=call_mcp_tool), and the same for the during_mcp_call / async_moderation_hook path. Both fail without this fix.
  • 2 scoping guards — the same guardrails skip ordinary chat traffic (call_type=completion), proving the remap is scoped to MCP call types and not applied blanket.

Test plan

The tests pass, and the two regression tests genuinely gate this bug — verified by reverting only the model_armor.py change and re-running: the two "scans" tests fail (AssertionError: Expected 'mock' to have been called); with the fix restored they pass.

pytest tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py \
  -k "mcp_tool_call or chat_traffic"
# 4 passed

pytest tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py
# 72 passed, 3 skipped

ruff format --check litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py
# already formatted

This fix has also been 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 (previously silently skipped).


Note

Medium Risk
Changes guardrail enforcement on MCP tool calls (security-sensitive); scope is small and aligned with existing proxy/guardrail MCP handling, with regression tests.

Overview
Fixes a security gap where Model Armor guardrails configured for pre_mcp_call or during_mcp_call did not run on MCP tool invocations because async_pre_call_hook and async_moderation_hook always gated on pre_call / during_call, so the inner should_run_guardrail check failed and scans were skipped.

model_armor.py now maps call_type == call_mcp_tool to pre_mcp_call in the pre-call hook and during_mcp_call in the moderation hook (same pattern as other guardrails such as Cisco). MCP traffic is still represented as synthetic user messages for the existing text-scan path; ordinary completion calls are unchanged.

Adds four tests (MCP scans vs chat skipped) for both hook paths.

Reviewed by Cursor Bugbot for commit 9374b40. Bugbot is set up for automated code reviews on this repo. Configure here.

yuneng-berri and others added 7 commits June 26, 2026 09:59
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
…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
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
2 out of 3 committers have signed the CLA.

✅ yuneng-berri
✅ shivamrawat1
❌ eugene-yao-zocdoc
You have signed the CLA already but the status is still pending? Let us recheck it.

@eugene-yao-zocdoc eugene-yao-zocdoc changed the title Fix/model armor mcp guardrail event hook fix(model_armor): scan MCP tool calls for pre_mcp_call / during_mcp_call modes Jul 6, 2026
@eugene-yao-zocdoc
eugene-yao-zocdoc marked this pull request as draft July 6, 2026 21:18
@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes ModelArmorGuardrail so that guardrails configured with mode: pre_mcp_call or during_mcp_call actually fire for MCP tool calls. Both async_pre_call_hook and async_moderation_hook hardcoded their inner event-type gate to pre_call/during_call, causing the should_run_guardrail check to fail silently for MCP traffic.

  • Production fix (model_armor.py): Four lines across two hooks that remap call_mcp_tool → pre_mcp_call / during_mcp_call before the inner gate check, matching the pattern already used by noma, cisco_ai_defense, and unified guardrails.
  • Tests (test_model_armor.py): Four new async regression tests covering the scan/skip behavior for both hooks — all mock-only and structurally correct, but not yet confirmed to pass in CI.

Confidence Score: 4/5

The production change is a minimal, correct two-location fix — the pattern is established by several sibling guardrails and carries low regression risk. The only gate to merge is that the added regression tests have not been run.

The fix itself is straightforward and matches patterns already present in noma, cisco, and unified guardrails. The new regression tests are structurally sound and mock-only, but the PR explicitly states they were never executed in the authoring environment, leaving the verification step incomplete before merge.

The test file needs a CI/local run before merging — the author flagged it as unverified. The production file is clean.

Important Files Changed

Filename Overview
litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py Adds call_mcp_tool → pre_mcp_call/during_mcp_call remapping in both async_pre_call_hook and async_moderation_hook, matching the pattern already used by noma, cisco, and unified guardrails.
tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py Adds 4 regression tests for the MCP call-type remapping (scan/skip for both pre- and during- hooks). Tests are mock-only and correct structurally, but the PR author explicitly states they were not executed before submission.

Reviews (1): Last reviewed commit: "fix(model_armor): scan MCP tool calls fo..." | Re-trigger Greptile

"metadata": {"guardrails": ["model-armor-test"]},
"mcp_tool_name": tool_name,
"mcp_arguments": arguments,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Tests not confirmed to pass before merge

The PR description explicitly notes: "⚠️ These commands were not run in the authoring environment and must be run before merge." Per the team's rule, a PR claiming to fix an issue should include evidence the fix is verified (e.g., passing test output). The 4 new regression tests are the primary verification mechanism here — they should be executed and their output confirmed before this lands.

Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass and are now confirmed — I've updated the PR description to remove the "not run before merge" note.

Verified by reverting only the model_armor.py change and re-running: the two MCP-scan tests fail (AssertionError: Expected 'mock' to have been called); with the fix restored, the targeted tests pass and the full file is 72 passed, 3 skipped. ruff format --check reports the file already formatted.

The two "scans" tests are true regression tests for this bug; the two "skips" tests are scoping guards confirming the remap is MCP-only. The fix has also been validated in a live dev deployment, where MCP tool calls configured for pre_mcp_call / during_mcp_call are now scanned (previously silently skipped).

- eugene-yao-zocdoc via Claude

@codecov

codecov Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@yucheng-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 9374b40. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants