Skip to content

Per-call CancelToken is not flipped by inbound notifications/cancelled in Python and Rust (TypeScript is wired) #122

Done

Description

@tercel

Summary

All three SDKs register a per-call CancelToken and attach it to the execution Context, but only TypeScript flips that token when the MCP client sends notifications/cancelled. In Python and Rust the token is never cancelled by the inbound notification:

  • TypeScript: SDK extra.signal → cancelToken.cancel() for both sync and async dispatch. Works.
  • Python: the router has a call_id → CancelToken map and a public cancel(call_id), but nothing in the transport calls it; extra carries no abort signal. A module cannot observe the notification.
  • Rust: ExecutionRouter::cancel_call has no production call site; notifications/cancelled is wired only to the async-task bridge (session/task keys), never to the direct-call token map.

The shared spec is also contradictory: apcore-mcp/docs/features/execution-router.md:158 says "Rust handles this via TransportManager::set_cancel_handler / notify_cancel", while :153 says synchronous tool calls "are NOT cancelled". The Rust docstrings repeat the first claim; the code matches the second.

Cross-language result

SDK Version / HEAD Per-call token attached to Context Notification flips it Evidence
Python 0.23.4 / 2887706 Yes (router.py:287-343, 506) No router.py:111-112, 618-654 describe the forwarding; no transport caller, and extra has no signal
TypeScript 0.23.4 / 8506903 Yes (router.ts:412-437, 514-526) Yes extra.signal → cancelToken.cancel() (router.ts:425-427, factory.ts:658-674); tests router-signal.test.ts:15-34
Rust 0.23.4 / a991ed1 Yes (router.rs:926-950, 1417-1477) No cancel_call only in tests; apcore_mcp.rs:952-974 wires notifications to the async bridge only

Impact

  • A cancelled Python/Rust tool call keeps running (subprocess, render, file lock) after the client disconnects.
  • Client-visible behavior differs by SDK, breaking the cross-language parity promise.
  • The Rust docstrings and the spec's own feature doc disagree with the code, so integrators cannot tell what is supported.
  • There is no conformance fixture for cancellation, so the divergence is not caught in CI (apcore-mcp/conformance/fixtures/ has none).

Proposed fix

  1. Implement one mechanism in Python and Rust, matching the TypeScript behavior:
    • Python: forward the SDK/transport cancellation signal to router.cancel(call_id) (populate extra.call_id from the JSON-RPC request id and hook the notification handler), or carry an abort signal in extra.
    • Rust: call router.cancel_call(request_id) from the notifications/cancelled handler, in addition to the existing async-task bridge wiring.
  2. Remove the stale Rust docstrings and fix execution-router.md:153,158 to describe one behavior.
  3. Add a cross-language conformance fixture (e.g. cancellation.json) with a fixture driver contract, like async_task_cancellation.json.

Acceptance criteria

  • A Python and a Rust sync tool both observe context.cancel_token.is_cancelled after notifications/cancelled.
  • The tool can terminate its subprocess and return/raise cleanly.
  • stdio and streamable-http transports are both covered.
  • A shared conformance fixture pins the behavior for all three SDKs.
  • The feature doc no longer contradicts itself.

Related

  • Python issue: aiperceivable/apcore-mcp-python#11 (being corrected to this scope)
  • Spec: apcore-mcp/docs/features/execution-router.md §"What's still missing today"

Activity

  1. added
    bug
    on Nov 23, 2026
  2. tercel commented on Nov 29, 2026

    @tercel
    ContributorAuthor

    Direct-call cancellation landed in #124. Main has real-wire target isolation tests for Python and Rust and Rust tests for Stdio and Streamable HTTP. The execution-router feature page still describes the old behavior. This issue remains open for that documentation correction and the original shared cross-language fixture check, with #58 consolidated here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions