← Back to Pull requests

fix(aiguard): evaluate Anthropic MCP connector tool calls and results #22068

Closed
unknown closed this pull request without merging on Nov 26, 2026 09:27 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 16:26 UTC
Author

Description

AI Guard's Anthropic integration dropped mcp_tool_use and mcp_tool_result content 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:

  • Converts mcp_tool_use into an assistant tool call (id, name, JSON input) and mcp_tool_result into a tool message with its text content, whatever DD_AI_GUARD_MCP_ENABLED says, like the conversion of OpenAI Responses mcp_call items.

  • Converts each message's content blocks in block order. Text and tool calls gather into one assistant message; a result of a tool that Anthropic runs (server tools and connector servers) closes it and follows in place. Client tool_use calls 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.

  • With DD_AI_GUARD_MCP_ENABLED, gives each connector call an mcp.server_id that resolves to a managed_by: provider entry of mcp_servers:

  • - built from the name and url of the request's mcp_servers entry with that server_name, with transport unknown, the URL reduced to its origin and the tools mapped to themselves; authorization_token is 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.
  • Keeps connector calls apart from client-managed MCP tools (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.

  • Reads the iterables of a request once (messages, system, tools, mcp_servers, and the content of tool_result / mcp_tool_result blocks), 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.

  • Restores the server_name of mcp_tool_use, the type of mcp_tool_result and an input sent whole in content_block_start in reconstruct_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. Generic tool_result blocks are ignored in responses, where the rebuild also gives that type to server tool results.

  • Documents the connector under DD_AI_GUARD_MCP_ENABLED in docs/configuration.rst and in the AI Guard development rule.

  • Moves the line pinned for _anthropic.py in scripts/check_constant_log_message.py from 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 fixes release note is included.

    Testing

    tests/aiguard/anthropic/test_mcp_connector.py (33 tests, 9 of them need an SDK with the MCP connector) covers:

  • conversion of both blocks, block order (resumed results, pending client calls, new calls after a resumed result), and string, text-block and error results;

  • the /evaluate payload and the ai_guard span meta-struct, where every mcp.server_id resolves to the reported mcp_servers;

  • no authorization_token and 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, generator mcp_servers / system, single-use tool result content, structured content, fail-open materialization, and caller iterator errors reaching the caller with their own cause;

  • through the SDK: beta.messages.create sync and async, buffered streams sync and async (inline tool input, single-use history content), and a blocking verdict.
  • tests/aiguard/anthropic/test_mcp.py covers 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 anthropic conftest.py.

    Local runs with scripts/run-tests:

    | Suite | Environment | Result |
    | --- | --- | --- |
    | ai_guard_anthropic | 5316c60f847a py3.13, anthropic==0.84.0, mcp<2 | 227 passed |
    | ai_guard_anthropic | 5051ae7c17f6 py3.14, anthropic<1, mcp<3 | 227 passed |
    | ai_guard_anthropic | 8bc03089e6c5 py3.9, anthropic==0.28.0 | 139 passed, 17 skipped (no MCP connector, no mcp) |
    | ai_guard_litellm_guardrail | 1227021fb1a6 py3.10, litellm[proxy]==1.78.5 | 255 passed |

  • Each fix's new tests fail against the commit before it.

  • anthropic 2.4.0, which the suite matrix does not include (it pins <1): an offline script ran beta.messages.create sync and async, streamed and not, through the patched SDK; each evaluation had the split turn, a resolving managed_by: provider entry and no token.

  • scripts/lint (format, typing, constant log check) passes.

  • Not run: a live end-to-end call through the connector, and benchmarks: no benchmark scenario covers the AI Guard listeners. Measured offline, the request preparation costs about 0.8 µs per message, about 20% of the message conversion AI Guard already runs. The CI benchmarks report no regression.
  • Risks

  • Connector calls and results, and resumed server tool results, now reach AI Guard, so applications can get blocking verdicts on responses that passed before.

  • A client-managed server and a connector server on the same URL origin share a server ID, so they are sent as one entry that keeps the client-managed server's name, transport and 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_anthropic would fix them; it is tracked separately.
    2. With ChatAnthropic under 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

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: avara1986 Review duration: 2460m
    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
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%