Skip to content

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

Merged
yucheng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_model_armor_mcp_guardrail
Jul 7, 2026
Merged

yucheng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_model_armor_mcp_guardrail

Conversation

@yucheng-berri

@yucheng-berri yucheng-berri commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5

Screenshots / 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.

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

Type

🐛 Bug Fix

Changes

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

ModelArmorGuardrail.async_pre_call_hook and async_moderation_hook hardcode their inner should_run_guardrail event type to pre_call / during_call. The central dispatcher (ProxyLogging._process_guardrail_callback / during_call_hook in litellm/proxy/utils.py) already remaps call_mcp_tool to 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.

The fix remaps call_mcp_tool to pre_mcp_call / during_mcp_call in 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_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, unchanged by this PR.

Adds 4 tests in tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py: 2 regression tests proving a pre_mcp_call / during_mcp_call guardrail 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_call or during_mcp_call never ran on MCP tool invocations, so tool names and arguments could bypass scanning.

In async_pre_call_hook and async_moderation_hook, when call_type is call_mcp_tool, the inner should_run_guardrail gate now uses pre_mcp_call / during_mcp_call instead of always pre_call / during_call, aligning with the proxy dispatcher. Ordinary chat completion traffic 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.

…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
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@CLAassistant

CLAassistant commented Jul 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where Model Armor guardrails configured for pre_mcp_call or during_mcp_call modes silently skipped scanning MCP tool calls. The root cause was that async_pre_call_hook and async_moderation_hook hardcoded their inner should_run_guardrail event-type gate to pre_call/during_call, causing the gate to fail for MCP traffic even though the outer dispatcher had already correctly classified the call.

  • Adds a two-line remap in each hook: when call_type == \"call_mcp_tool\", the event type is switched to pre_mcp_call/during_mcp_call before the should_run_guardrail check, matching the existing behavior in the unified guardrail dispatcher.
  • Adds 4 mock-only regression tests covering both the positive path (MCP calls are now scanned) and the negative path (regular chat traffic is still correctly skipped by a pre_mcp_call/during_mcp_call-only guardrail).

Confidence Score: 5/5

Safe 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.

Important Files Changed

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

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug in ModelArmorGuardrail where guardrails configured with mode pre_mcp_call or during_mcp_call silently skipped MCP tool calls. The inner should_run_guardrail gate in async_pre_call_hook and async_moderation_hook hardcoded its event type to pre_call / during_call, so the inner check always failed for pre_mcp_call / during_mcp_call guardrails even after the outer dispatcher had already passed them through.

  • Adds a call_type == call_mcp_tool remap in both hooks so the event type passed to should_run_guardrail matches the configured guardrail mode.
  • Adds four focused regression/scoping tests: two verify that the correct event type triggers a scan, two verify that ordinary chat traffic continues to be skipped when a guardrail is configured for the MCP event types.

Confidence Score: 4/5

Safe 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.

Important Files Changed

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

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 Author

bugbot run

@yucheng-berri
yucheng-berri requested a review from tin-berri July 7, 2026 00:05

@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 d037d13. Configure here.

@yucheng-berri
yucheng-berri merged commit f4623a1 into litellm_internal_staging Jul 7, 2026
78 checks passed
@yucheng-berri
yucheng-berri deleted the litellm_model_armor_mcp_guardrail branch July 7, 2026 00:09
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

Re: the 4/5 review's noted concern about ModelArmorGuardrail's default event_hook list not including pre_mcp_call/during_mcp_call: confirmed this is intentional, existing scope, not a gap introduced by this PR. MCP scanning is opt-in via explicit guardrail config (mode: pre_mcp_call / during_mcp_call), matching the same opt-in pattern already used by noma, cisco, and the unified guardrail dispatcher. Expanding the default list is a separate design decision outside this bug fix's scope.

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.

4 participants