Skip to content

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

Closed
avara1986 wants to merge 16 commits into
mainfrom
alberto.vara/appsec-71909-ai-guard-python-anthropic-evaluate-mcp-connector
Closed

avara1986 wants to merge 16 commits into
mainfrom
alberto.vara/appsec-71909-ai-guard-python-anthropic-evaluate-mcp-connector

Conversation

@avara1986

@avara1986 avara1986 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

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

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

.cursor/rules/ai-guard.mdc                                              @DataDog/asm-python
ddtrace/aiguard/_constants.py                                           @DataDog/asm-python
ddtrace/aiguard/_listener.py                                            @DataDog/asm-python
ddtrace/aiguard/integrations/_anthropic.py                              @DataDog/asm-python
ddtrace/aiguard/integrations/_anthropic_mcp.py                          @DataDog/asm-python
ddtrace/aiguard/integrations/_anthropic_streaming.py                    @DataDog/asm-python
docs/configuration.rst                                                  @DataDog/apm-python-guild
releasenotes/notes/aiguard-anthropic-mcp-connector-9a59c70d7bb700b0.yaml  @DataDog/apm-python
scripts/check_constant_log_message.py                                   @DataDog/apm-python-guild
tests/aiguard/anthropic/conftest.py                                     @DataDog/asm-python
tests/aiguard/anthropic/test_anthropic.py                               @DataDog/asm-python
tests/aiguard/anthropic/test_mcp.py                                     @DataDog/asm-python
tests/aiguard/anthropic/test_mcp_connector.py                           @DataDog/asm-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 22 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 22 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=103)
ddtrace.llmobs._integrations.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.llmobs._integrations.base -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.internal.span_bus -×-> ddtrace._trace.span  (internal-core -> product:tracing, score=35)
ddtrace.llmobs._llmobs -×-> ddtrace._trace.span  (product:llmobs -> product:tracing, score=33)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@pr-commenter

pr-commenter Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-26 08:56:08

Comparing candidate commit 46886f7 in PR branch alberto.vara/appsec-71909-ai-guard-python-anthropic-evaluate-mcp-connector with baseline commit 5fe87e7 in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 378 metrics, 9 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-729.931ns; +745.965ns] or [-6.841%; +6.991%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-36.882ns; +41.436ns] or [-5.778%; +6.491%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1887.739ns; +1952.298ns] or [-9.466%; +9.789%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1961.682ns; +1736.849ns] or [-10.073%; +8.918%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-373.610ns; +382.083ns] or [-8.795%; +8.995%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-221.634ns; +235.132ns] or [-8.346%; +8.855%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-108.727ns; +68.783ns] or [-7.850%; +4.966%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4799.283ns; +4411.695ns] or [-9.878%; +9.081%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-937.450ns; +960.864ns] or [-9.151%; +9.379%]

@avara1986

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T08:35:26.602585Z 46886f7 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

The Anthropic converter dropped mcp_tool_use and mcp_tool_result blocks as
unknown types, so the calls and results of MCP servers run by the Anthropic
MCP connector (client.beta.messages.create(mcp_servers=...)) were never
evaluated, neither in the response nor in the history of later requests.

- Convert mcp_tool_use into an assistant tool call and mcp_tool_result into
  a tool message, split like server tools (tool call, tool result, synthesis).
- With DD_AI_GUARD_MCP_ENABLED, reference a managed_by provider mcp_servers
  entry built from the name and URL origin of the request's mcp_servers entry
  with that name, or from the name alone when the request does not declare
  it. authorization_token is never read.
- Restore server_name and the mcp_tool_result type in the buffered stream
  reconstruction, which the shared contrib rebuild drops.
- Update the line pinned for _anthropic.py in the constant log message check.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 64f6d5a35c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@avara1986
avara1986 force-pushed the alberto.vara/appsec-71909-ai-guard-python-anthropic-evaluate-mcp-connector branch from 64f6d5a to 7ac8b8a Compare November 25, 2026 12:16
avara1986 and others added 2 commits November 25, 2026 13:35
…s and fail open

- attach_mcp no longer reassigns a tool call that already references a server, so an
  MCP connector call named like a client-managed MCP tool keeps its provider entry.
- mark_evaluated only counts calls that reference the tool's own server, so such a
  connector call no longer spares a later call of the client-managed tool its check.
- A failure while referencing the connector server drops only the MCP metadata.
- The buffered stream reconstruction keeps an mcp_tool_use input sent whole in the
  content_block_start chunk.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Anthropic buffered stream wrappers keep their own kwargs for the deferred
response evaluation, while the before-hook materialized an iterable mcp_servers
only in the copy the contrib receives. The deferred evaluation then read an
exhausted generator and identified MCP connector calls by name alone.

Materialize an iterable mcp_servers in the wrapper kwargs before forwarding the
call, so the SDK and the deferred evaluation read the same servers.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@avara1986
avara1986 marked this pull request as ready for review November 25, 2026 14:13
@avara1986
avara1986 requested review from a team as code owners November 25, 2026 14:13
@avara1986
avara1986 requested review from florentinl and removed request for a team November 25, 2026 14:13

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

Two critical correctness issues stand out: iterator-backed mcp_tool_result content is consumed without being written back to the shared request payload, so the SDK and after-hook both lose the result text; and same-origin connector calls are incorrectly recorded as evaluated client-tool calls, allowing a matching client invocation to bypass its pre-execution AI Guard check entirely.

Open Bits AI session

🤖 Bits Code Review · Commit b26e381

…ient-tool marks

- Write a materialized iterable tool result content back into its block, so the
  SDK still sends the content that the conversion consumed.
- Skip the response's mcp_tool_use calls when marking evaluated client-managed
  MCP tool calls: on a shared URL origin both servers have one ID, so only the
  call's provenance tells a provider-run call apart.
- Emit a client tool_use that follows a server tool result after that result,
  so the evaluated turn ends with the pending call.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The PR adds Anthropic MCP connector calls and results to AI Guard, and the latest commit fixes same-origin evaluation marks, same-turn call ordering, and exhausted result generators. The new code can drop resumed server results before a pending client call and freeze reusable result content, sending stale history on later requests.

Open Bits AI session

🤖 Bits Code Review · Commit 456d3af

- Emit server tool results when the message has calls before them or pending
  client calls after them, so a resumed turn whose result's call is in an
  earlier message keeps that result, before the pending call.
- Write back only one-shot iterator tool result content, so a reusable view in
  the caller's history stays live.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1f92bfa6c8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The most critical issues involve the Anthropic integration's parser incorrectly ordering conversation turns for AI Guard: resumed turns containing MCP tool/connector results do not set the appropriate boundary flag, causing synthesis text to be emitted before the results it describes. A separate issue affects single-use iterable wrappers for mcp_tool_result content, which are silently drained by the before-hook leaving subsequent hooks with empty content.

Open Bits AI session

🤖 Bits Code Review · Commit 1f92bfa

avara1986 and others added 2 commits November 25, 2026 15:55
…sage

A resumed Anthropic turn can hold a server or MCP connector tool result whose
call is in an earlier message. The converter dropped such results, so a
result-only response skipped the response evaluation and a result followed by
text evaluated only the text.

Emit server tool results whenever a message has them, and treat text after a
result as synthesis so the evaluated order matches the conversation.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cover a resumed Anthropic turn shaped [mcp_tool_result, text, tool_use] in
response and history conversion: the text stays after the result and before the
pending client call.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: WARN

AI Guard now evaluates Anthropic MCP connector calls and results, including buffered streams, with provider-managed server metadata. The previously reported iterable-content loss and resumed-turn ordering defects are fixed; resumed results remain before subsequent synthesis and pending client calls, and no findings remain open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 0ff6dd8

…order

- materialize_request reads the iterables of an Anthropic request once, before
  the SDK and any AI Guard hook: messages and mcp_servers become lists, and a
  block whose content is an iterable (a generator or any single-use iterable)
  gets a request-local copy with that content as a list. The caller's objects
  are not changed, so reusable iterables stay live. The stream wrappers call it
  too, so the deferred evaluation reads the same values.
- Convert content blocks in a single in-order walk instead of pre/post buckets:
  text and tool calls gather into one message, and each result of a tool
  Anthropic runs closes it and follows in place, so a new call after a resumed
  result is evaluated after that result.
- Move the line pinned for _anthropic.py in the constant log message check.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

Two of the most critical issues are: request normalization running outside the fail-open boundary (causing exceptions to abort application calls), and buffered server-tool results being appended after the assistant's synthesis block (causing AIGuard to misclassify responses as INPUT and skip output-only evaluation). A third distinct issue involves shallow dict() conversion of code-execution result blocks producing malformed JSON arrays instead of objects.

Open Bits AI session

🤖 Bits Code Review · Commit 85e8211

…g requests

- Guard materialize_request: an unexpected request shape is logged at debug
  level and leaves the request as it is, in the before-hook and the stream
  wrappers.
- Materialize only the content of tool_result and mcp_tool_result blocks, the
  ones the SDK types as an Iterable, and never SDK models, which iterate over
  their fields.
- Ignore generic tool_result blocks in responses: a response holds none, and the
  stream rebuild gives that type to server tool results, which would otherwise
  end the evaluated response with a tool message.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

In _anthropic.py, generator-backed or one-shot results (e.g. MCP tool results) can be silently emptied when an exception occurs after the iterator is consumed but before the copied snapshot is published — snapshots must be persisted before any subsequent inspection that could raise.

Open Bits AI session

🤖 Bits Code Review · Commit 256140d

materialize_request published its request-local copy of the messages only at
the end, so a failure on a later message, caught to fail open, lost tool result
content already read from a single-use iterable. Publish each copy as soon as
its content is read.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The PR adds AI Guard evaluation for Anthropic MCP connector calls and results, and earlier results now survive a later history lookup error. A buffered request can still send an empty result if copying the current block fails after its iterator has been consumed.

Open Bits AI session

🤖 Bits Code Review · Commit 3baeba9

@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 46886f7 | Docs | View more details | Give us feedback!

Co-authored-by: datadog-prod-us1-6[bot] <266788760+datadog-prod-us1-6[bot]@users.noreply.github.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

AI Guard now evaluates Anthropic MCP connector calls and results, including buffered streams, with provider-managed server metadata. Earlier content-loss, ordering, and exception-propagation defects remain resolved; request copies are now prepared before consuming single-use content, and no findings remain open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 04087c2

…thropic-evaluate-mcp-connector

Resolve the conflicts with #22038: keep the in-order content walk and apply its
rule there, so the tool results of a user turn come before that turn's text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d1f8370782

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

AI Guard evaluates Anthropic MCP connector calls and results and preserves result snapshots across later lookup failures. Request preparation can still swallow an iterator error and send an empty connector list, and a connector result after a pending client call can cause output-only middleware to skip evaluation.

Open Bits AI session

🤖 Bits Code Review · Commit d1f8370

…thropic test helpers

- materialize_request also reads iterable system and tools once, so the deferred
  stream evaluation keeps the system prompt and the client-managed MCP tools;
  the separate conversions in the before-hook and _attach_mcp_servers are gone.
- Move the SSE, transport, client and payload helpers of the Anthropic AI Guard
  tests into their conftest, shared by test_mcp and test_mcp_connector.
- Shorten the _format_content_blocks and materialize_request docstrings.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The PR adds AI Guard evaluation for Anthropic MCP connector calls and results. Request preparation can now send empty system instructions as well as an empty connector list after iterator errors, and a trailing connector result can still cause output-only middleware to skip evaluation of a pending client call.

Open Bits AI session

🤖 Bits Code Review · Commit 121dd08

…ending client calls

- An error raised by a caller iterable while materialize_request reads it now
  reaches the caller, as it would from the SDK before sending the request;
  only failures of AI Guard's own inspection stay fail-open.
- Client tool_use calls are pending until the next user turn, so they end the
  assistant turn after any server or connector result, and a response that
  stops for a client call stays an output evaluation.
- _connector_mcp_servers lists mcp_servers only, instead of walking the history
  again after the before-hook.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

AI Guard evaluates Anthropic MCP connector calls and results, classifies pending client calls as output, and stops requests when their iterators raise. The new re-raise clears the original exception chain, hiding the underlying failure from callers.

Open Bits AI session

🤖 Bits Code Review · Commit 26f8317

avara1986 and others added 2 commits November 26, 2026 09:29
Co-authored-by: datadog-prod-us1-6[bot] <266788760+datadog-prod-us1-6[bot]@users.noreply.github.com>
…t iterable

- Re-raise a caller iterable's error outside the exception handler, so it keeps
  its own __cause__ and __context__.
- Say in the materialize_request docstring that caller iterator errors reach the
  caller and only AI Guard's own failures fail open.
- Drop the second mcp_servers listing in _connector_mcp_servers: it is already a
  list by then.
- Test caller iterator errors for messages, system, tools, mcp_servers and tool
  result content, with the caller's own cause.

APPSEC-71909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

AI Guard evaluates Anthropic MCP connector calls and results, including buffered streams, with provider-managed server metadata. The latest change preserves caller exception chains while keeping inspection failures fail-open; earlier content-loss and ordering fixes remain intact, and no findings remain open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 7b94c94

@avara1986

Copy link
Copy Markdown
Member Author

Superseded by #22113: a minimal rewrite of the same fix from main. Closing this one.

@avara1986 avara1986 closed this Nov 26, 2026
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.

1 participant