Skip to content

fix(mcp): record canceled client tool calls as errors - #21137

Open
bvolpato-dd wants to merge 1 commit into
DataDog:mainfrom
bvolpato-dd:bv/mcp-cancelled-tool-llmobs
Open

bvolpato-dd wants to merge 1 commit into
DataDog:mainfrom
bvolpato-dd:bv/mcp-cancelled-tool-llmobs

Conversation

@bvolpato-dd

@bvolpato-dd bvolpato-dd commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

When a caller cancels an in-flight ClientSession.call_tool(), the span is reported as successful in APM and LLM Observability. traced_call_tool() only caught Exception, but asyncio.CancelledError and trio.Cancelled inherit from BaseException.

The wrapper now catches BaseException, records the error, and re-raises it. This matches initialize and list_tools in the same file, which already record cancellation as an error.

As a consequence, KeyboardInterrupt, SystemExit with a nonzero code, and GeneratorExit (a suspended call whose coroutine is closed) are also recorded as errors for tool calls, as they are for initialize and list_tools.

Customer impact

Canceled MCP client tool calls appear as errors in APM and LLM Observability, with the cancellation exception as the error type. A release note is included.

Testing

tests/contrib/mcp/test_client_cancellation.py cancels a tool call once its request is in flight. It checks that the cancellation propagates, that the APM span has the error flag and error type, and that the LLM Observability event has error status and the same error type.

  • The test fails without the fix on mcp~=1.10.0, mcp<2, and mcp 2.x (Python 3.12).
  • The full tests/contrib/mcp suite passes with the fix in all 17 configured environments (Python 3.10 through 3.15).
  • Trio was checked once locally (Python 3.12, mcp~=1.10.0, trio==0.34.0) and the cancellation cases passed. It is not part of the CI matrix.
  • scripts/lint checks passes.

GitLab CI and System Tests have not run on this commit; see below.

Risks

Applications that cancel MCP client tool calls may see higher error counts, which can affect monitors on MCP error rates.

Additional Notes

download-s3-wheels fails because GitLab does not build wheels for fork commits. A maintainer needs to run CI through the fork procedure after reviewing the commit.

This PR was AI-assisted. If this change is not worth pursuing, please close it with a brief rationale.

@bvolpato-dd bvolpato-dd changed the title fix(mcp): preserve canceled tool spans fix(mcp): preserve canceled client tool spans Oct 27, 2026
@bvolpato-dd bvolpato-dd added changelog/no-changelog A changelog entry is not required for this PR. and removed changelog/no-changelog A changelog entry is not required for this PR. labels Oct 28, 2026
@bvolpato-dd
bvolpato-dd force-pushed the bv/mcp-cancelled-tool-llmobs branch from a5c7c9e to f28a633 Compare November 9, 2026 04:25
@bvolpato-dd bvolpato-dd changed the title fix(mcp): preserve canceled client tool spans fix(mcp): record canceled client tool calls as errors Nov 9, 2026
@bvolpato-dd
bvolpato-dd force-pushed the bv/mcp-cancelled-tool-llmobs branch from 5cda374 to 65ec576 Compare November 12, 2026 10:12
@bvolpato-dd
bvolpato-dd marked this pull request as ready for review November 12, 2026 10:12
@bvolpato-dd
bvolpato-dd requested review from a team as code owners November 12, 2026 10:12
@bvolpato-dd
bvolpato-dd requested review from duncanista and removed request for a team November 12, 2026 10:12
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-26T19:01:18.372817Z 8876286 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.

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 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.

Bits Code Review: PASS

More details

The cancellation handler preserves exception propagation for asyncio and Trio while leaving coroutine closure outside the error path. Runtime behavior remains unverified because required dependencies were unavailable.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 65ec576

@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: 004bad94c8

ℹ️ 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".

Comment thread tests/llmobs/suitespec.yml Resolved
@bvolpato-dd
bvolpato-dd force-pushed the bv/mcp-cancelled-tool-llmobs branch from 3284bc6 to a330156 Compare November 19, 2026 22:25

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 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.

Bits Code Review: PASS

More details

Cancellation exceptions now use the existing error-finalization path while preserving exception propagation and coroutine closure behavior. No actionable defect was identified; runtime validation was limited by a missing test dependency.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit a330156

@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: a33015636c

ℹ️ 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".

@bvolpato-dd
bvolpato-dd force-pushed the bv/mcp-cancelled-tool-llmobs branch from a330156 to 8876286 Compare November 26, 2026 18:57

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 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.

Bits Code Review: PASS

More details

Canceled MCP client tool calls now reach the existing error-finalization path while the original cancellation continues to propagate.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 8876286

@bvolpato-dd

Copy link
Copy Markdown
Author

Current state (8876286824838cf8f58ad6b359d52f40446bd20f): one signed commit, rebased on main with no conflicts, 3 files. No open review threads, and the automated reviews of this commit have no findings. Ready for code review.

What the PR does

  • traced_call_tool() catches BaseException instead of Exception, so canceled tool calls are recorded as errors in APM and LLM Observability and the cancellation still propagates.
  • Adds one regression test and a release note.

What changed in this revision

  • An earlier version caught asyncio.CancelledError and trio.Cancelled through a sys.modules lookup, and added a Trio test variant with a lockfile rename. initialize and list_tools in the same file already use except BaseException, so call_tool now does the same and that extra code and configuration is gone. The two resolved threads refer to the removed version.
  • Behavior difference to review: a suspended tool call whose coroutine is closed (GeneratorExit) is now an error too, as it is for initialize and list_tools. The test that asserted the opposite was removed.
  • The test now has one case that checks both the APM span and the LLM Observability event.

Validation

  • Full tests/contrib/mcp suite in all 17 configured environments (Python 3.10 through 3.15; mcp~=1.10.0, mcp<2, mcp 2.x): all passed.
  • The new test fails without the fix on all three mcp ranges (Python 3.12).
  • Trio, checked once with the same handler and the earlier parametrized test (Python 3.12, trio==0.34.0): the asyncio and Trio cancellation cases passed. Trio is not in the CI matrix.
  • scripts/lint formatting, typing, spelling, suitespec, and checks: all passed.

Why download-s3-wheels fails

  • It waits 30 minutes for Linux wheels that GitLab builds per commit, and GitLab does not mirror fork branches. The wheels for this commit do not exist, so the check cannot pass on this PR as a fork and no change in this diff can fix it. system-tests finished is the required check in that workflow; download-s3-wheels is not. It passed on the previous revision even though System Tests did not run.

Still needed before merge

  • Code review and approval.
  • Further testing: after reviewing the commit, a maintainer needs to run GitLab CI and System Tests through the fork procedure. Neither has run on this commit.

[bruno-seek-approval]

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