litellm/tests/integration/mcp
jesus-berri 0e62b06902
feat(model_armor): scan MCP tool results on post_mcp_call (#51887)
* feat(model_armor): scan MCP tool results on post_mcp_call

Model Armor now lists post_mcp_call as a supported event hook, so a guardrail with mode [post_call, post_mcp_call] loads at startup. post_mcp_call_hook also runs native-hook guardrails that override apply_guardrail and list post_mcp_call, which sends MCP tool results to sanitizeModelResponse.

apply_guardrail masks de-identified response text when mask_response_content is set, and raises on a Model Armor API error when fail_on_error is true and the guardrail is not logging_only.

* fix(model_armor): honor fail_on_error when a per-text response rescan fails

* fix(mcp): pass request guardrails to post_mcp_call on the Responses MCP path

A default_on false guardrail named on the key or the request was skipped for MCP tool results
on /v1/responses, because the hook got request data without the resolved guardrails

* fix(model_armor): log a block found on a per-text rescan as guardrail_flagged

When masking rescans each text and one of them is blocked, the request was rejected
but the only logged guardrail entry stayed at success from the joined scan.

* fix(model_armor): keep logging_only flagging de-identified responses after one scan

* fix(model_armor): fail a scan that skipped a filter and block redactions that span text blocks

* fix(model_armor): fail a skipped filter only on post_mcp_call and scan a repeated tool result text once

* fix(model_armor): leave whitespace-only texts out of the Model Armor scan

Model Armor answers HTTP 400 for a whitespace-only text, so a tool result
whose only text is blank was withheld, and a blank block next to a match
failed the masking rescan.

* fix(model_armor): mask a match found while another filter was skipped when failing open

On post_mcp_call with fail_on_error false, a scan where Model Armor skipped a
filter but its de-identify filter matched returned the raw value. It now masks
that match. A text rescan that skipped a filter keeps its text, and the
combined redaction check still blocks if a match is lost. fail_on_error true
still withholds the result.

* fix(model_armor): log a result masked after a skipped filter as guardrail_failed_to_respond

* fix(model_armor): rescan only the texts the redaction changed, and never block on a rescan

Masking rescanned every distinct text on its own, one Model Armor call each,
and a short fragment could trip a classifier the full scan did not, blocking a
clean result. Only texts whose lines the full redaction changed are rescanned
now, and a rescan only supplies the rewrite. The check that the rewrites
reproduce the full redaction still blocks when they do not.

* fix(model_armor): find the texts a redaction changed in linear time

SequenceMatcher was quadratic on repeated lines and ran on the event loop.
A redaction that keeps the line count is compared line by line, and one that
changes it rescans the span between the common prefix and suffix.

* fix(model_armor): take each text's rewrite from the full redaction when it keeps the line count

A rescan of one text alone can miss a value the full scan found from a
neighbouring line, and the join check then blocked a result that only needed
masking. When the full redaction has the same number of lines as the scanned
texts, each text's rewrite is now its own lines of that redaction, with no
rescan. Rescans run only when a redaction changes the line count.

* test(model_armor): integration cells for blank blocks, skipped filters and the apply_guardrail endpoint

The de-identify cell now expects one Model Armor scan, since a redaction
that keeps the line count is split back into the texts without a rescan.

* fix(model_armor): treat a de-identify match without a rewrite as a Model Armor error

With mask_response_content on, a de-identify MATCH_FOUND that carried no
data.text left the redaction equal to the original, so the raw text was
returned. It now counts as an incomplete scan, so fail_on_error decides and
the default withholds the result, as the streaming path already did. The
incomplete-scan checks move into one helper.

* fix(model_armor): fail open on every Model Armor error when fail_on_error is false

Timeouts and Google token refresh errors skipped fails_open in apply_guardrail,
so post_mcp_call answered 500 even with fail_on_error false. Both the full scan
and the per-text rescan now route any non-block error through fails_open, the
same as the native post_call hook.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-18 12:17:47 -08:00
..
_config_mcp_e2e.yaml chore: move LiteLLM's configs and e2e tests off gpt-4o and gpt-4.1 before their 2027-01-14 shutdown (#51800) 2026-11-14 10:50:59 -08:00
_math_server.py test: migrate proxy security, schema migration and MCP legacy tests (#45937) 2026-10-11 16:17:24 -07:00
test_admin_mcp_authorization.py fix(proxy): resolve admin MCP authorization without listing every key and team (#52096) 2026-11-16 14:05:10 -08:00
test_interactions.py test(integration): cap the OAuth metadata lookup in the mixed capabilities subscription case (#50867) 2026-11-10 18:11:16 +00:00
test_litellm_proxy_mcp_handler_real_io.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_mcp_access_matrix.py test: update integration tests left stale by recent product changes (#49143) 2026-10-29 18:06:28 -07:00
test_mcp_accounting_guardrails.py fix(mcp): refuse calls to guardrail-hidden tools (#48669) 2026-10-27 17:13:40 -07:00
test_mcp_agent_365_guardrail.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_mcp_akto_logging_only.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_mcp_client_real_io.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_mcp_credentials.py fix(mcp)!: forward the LiteLLM key when the admin configures it (#50604) 2026-11-10 20:51:34 -08:00
test_mcp_lifecycle.py test(mcp): cancel disconnected HTTP tool handlers (#49436) 2026-10-31 16:41:49 -07:00
test_mcp_listed_tool_metadata.py fix(mcp): refuse calls to guardrail-hidden tools (#48669) 2026-10-27 17:13:40 -07:00
test_mcp_llm_endpoints.py fix(responses): preserve native stream spend on guardrail rejection (#50286) 2026-11-05 15:47:24 -08:00
test_mcp_management.py fix(mcp): use the saved description and server name in registry.json (#52319) 2026-11-17 13:36:07 -08:00
test_mcp_model_armor_post_mcp_call.py feat(model_armor): scan MCP tool results on post_mcp_call (#51887) 2026-11-18 12:17:47 -08:00
test_mcp_oauth_flows.py test: update integration tests left stale by recent product changes (#49143) 2026-10-29 18:06:28 -07:00
test_mcp_protocol_errors.py refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
test_mcp_rate_limits.py feat(mcp): rate limit all MCP operations and add server-level rpm (#44600) 2026-10-07 16:30:52 -07:00
test_mcp_resilience.py test: update integration tests left stale by recent product changes (#49143) 2026-10-29 18:06:28 -07:00
test_mcp_submission_rules.py fix(ui): read MCP submission rules from bare-array /config/list response (#44648) 2026-10-06 10:09:46 -07:00
test_mcp_tool_permission_merge.py refactor(types): replace Any with proven types in 7 files (#43704) 2026-09-29 06:12:58 -07:00
test_mcp_toolsets.py test: update integration tests left stale by recent product changes (#49143) 2026-10-29 18:06:28 -07:00
test_mcp_transports.py test(mcp): hold the progress test's upstream notification acks until they are dispatched (#49967) 2026-11-03 15:26:48 -08:00
test_mcp_user_env_vars.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_oauth_configuration.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_openapi_to_mcp_generator_real_io.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_pagination.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_proxy_mcp_e2e.py test(mcp): give the startup-failure child process the rig's calibrated readiness budget (#52365) 2026-11-18 02:36:51 +00:00
test_responses_mcp_mixed_tools.py test: fix three order-dependent and timing-flaky tests (#44271) 2026-10-02 16:29:57 -07:00
test_responses_mcp_streaming_round_history.py fix(responses): keep every MCP tool round in streaming follow-up input (#47698) 2026-10-28 00:51:10 -07:00