fix(aiguard): evaluate Anthropic MCP connector tool calls and results #22068
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #22068"
general
weight: 0.35
avara1986
Description
AI Guard's Anthropic integration dropped
mcp_tool_useandmcp_tool_resultcontent blocks as unknown block types. With the Anthropic MCP connector (client.beta.messages.create(mcp_servers=[...])), Anthropic runs the MCP client itself and reports each call and its result with these blocks. So connector tool calls and their results never reached AI Guard: neither in the evaluation of the response nor in the history of later requests. A tool result is model-visible content, so this left an indirect prompt injection path unevaluated.This PR:
mcp_tool_useinto an assistant tool call (id, name, JSON input) andmcp_tool_resultinto a tool message with its text content, whateverDD_AI_GUARD_MCP_ENABLEDsays, like the conversion of OpenAI Responsesmcp_callitems.tool_usecalls end the assistant turn, after any such result, because the application has not run them yet. Calls, results and later text therefore keep the conversation's order, including a resumed turn whose result's call is in an earlier message. The tool results of a user turn come before its text.DD_AI_GUARD_MCP_ENABLED, gives each connector call anmcp.server_idthat resolves to amanaged_by: providerentry ofmcp_servers:- built from the
nameandurlof the request'smcp_serversentry with thatserver_name, with transportunknown, the URL reduced to its origin and the tools mapped to themselves;authorization_tokenis never read;- a server that the request does not declare is identified by its name alone;
- only servers referenced by a tool call are sent, so a request that only declares the connector carries no
mcp_servers.anthropic.lib.tools.mcp) with the same name, even on the same URL origin: a connector call keeps its provider reference and never spares a client-managed call its own check.messages,system,tools,mcp_servers, and the content oftool_result/mcp_tool_resultblocks), before the SDK and any AI Guard hook, including the buffered stream wrappers. Tool result content goes into a request-local block copy, so the caller's objects are never changed. An error raised by a caller iterable reaches the caller, as it would from the SDK; failures of AI Guard's own inspection fail open.server_nameofmcp_tool_use, the type ofmcp_tool_resultand an input sent whole incontent_block_startinreconstruct_anthropic, the buffered stream reconstruction. The shared contrib rebuild drops them; the fix stays on the AI Guard side, so the shape that LLM Observability reads is unchanged. Generictool_resultblocks are ignored in responses, where the rebuild also gives that type to server tool results.DD_AI_GUARD_MCP_ENABLEDindocs/configuration.rstand in the AI Guard development rule._anthropic.pyinscripts/check_constant_log_message.pyfrom 67 to 70.Jira: APPSEC-71909
Customer impact
Applications that use the Anthropic MCP connector now get AI Guard evaluations of the connector's tool calls and results, and those evaluations can block. Anthropic has already run a connector call when the response arrives, so a blocking verdict withholds the response from the application but cannot stop the call itself. Server tool results whose call is in an earlier message (resumed turns) are now evaluated too. A
fixesrelease note is included.Testing
tests/aiguard/anthropic/test_mcp_connector.py(33 tests, 9 of them need an SDK with the MCP connector) covers:/evaluatepayload and theai_guardspan meta-struct, where everymcp.server_idresolves to the reportedmcp_servers;authorization_tokenand no URL user information, path, query or fragment in the payload or the meta-struct;DD_AI_GUARD_MCP_ENABLED=false, an undeclared server, two server names on one origin, generatormcp_servers/system, single-use tool result content, structured content, fail-open materialization, and caller iterator errors reaching the caller with their own cause;beta.messages.createsync and async, buffered streams sync and async (inline tool input, single-use history content), and a blocking verdict.tests/aiguard/anthropic/test_mcp.pycovers a connector call named like a client-managed tool, on another origin and on the same origin. The shared HTTP and SSE helpers of both modules live in the anthropicconftest.py.Local runs with
scripts/run-tests:| Suite | Environment | Result |
| --- | --- | --- |
|
ai_guard_anthropic|5316c60f847apy3.13,anthropic==0.84.0,mcp<2| 227 passed ||
ai_guard_anthropic|5051ae7c17f6py3.14,anthropic<1,mcp<3| 227 passed ||
ai_guard_anthropic|8bc03089e6c5py3.9,anthropic==0.28.0| 139 passed, 17 skipped (no MCP connector, nomcp) ||
ai_guard_litellm_guardrail|1227021fb1a6py3.10,litellm[proxy]==1.78.5| 255 passed |<1): an offline script ranbeta.messages.createsync and async, streamed and not, through the patched SDK; each evaluation had the split turn, a resolvingmanaged_by: providerentry and no token.scripts/lint(format, typing, constant log check) passes.Risks
managed_by.Additional Notes
Follow-ups, not in this PR:
1. Streamed results of Anthropic's own server tools (web search, code execution) are still dropped by the buffered stream reconstruction. The same restore in
reconstruct_anthropicwould fix them; it is tracked separately.2. With
ChatAnthropicunder LangChain, the LangChain integration evaluates instead of the Anthropic hooks, and its converter still drops the connector blocks.🤖 Generated with Claude Code
[APPSEC-71909]: https://datadoghq.atlassian.net/browse/APPSEC-71909?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ