sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[PORT] Per-call MCP tool cancellation (TS→Py)

Open yonib05 opened this issue 4 months ago • 2 comments

Parent: #1659

Source SDK

strands-ts

Target SDK

strands-py

Related Files

strands-ts/src/mcp/client.ts
strands-ts/src/tools/mcp-tool.ts
strands-ts/src/mcp/__tests__/client.test.ts

What needs to be migrated

  • McpCallToolOptions.signal (AbortSignal) on tool calls (client.ts:65-68)
  • Wired automatically from toolContext.agent.cancelSignal (mcp-tool.ts:51-53)
  • Unit coverage: client.test.ts:357-389

Python today: no per-call cancel. Pending invocations abort only when the whole session closes (mcp_client.py:1141-1155, the #995 fix). Longstanding related ask: #537 (force_stop).

Gotchas / dependencies

  • Python's sync facade runs calls on a background thread with a private asyncio loop; a cancellation token has to bridge threads (likely a threading.Event or contextvar checked on the loop side, translated to anyio cancellation of the in-flight request).
  • Semantics should match TS: cancel the single in-flight call, leave the session usable.
  • Independent of the 2026-07-28 RC; safe to do now.

Confirmation

  • [x] The change is complete in the source SDK, and every related artifact is listed above (source, tests, and docs, if the change has any).

yonib05 avatar Jul 15 '26 14:07 yonib05

@strandly-the-agent implement

arielnabavian avatar Jul 21 '26 19:07 arielnabavian

PR #3402 / Issue #3276 — Per-Call MCP Cancellation: Manual Test Results

Before: origin/main (33f5f741) · After: PR #3402 (f0e09040) Env: Python 3.12 · real MCP servers (stdio/HTTP/SSE) · real Bedrock global.anthropic.claude-sonnet-4-6 (acct 159773341612, role Bedrock-Access)

# Test case (manually run) How Before (origin/main) After (PR #3402)
1 call_tool_sync exposes cancel_signal param inspect.signature ❌ absent → TypeError ✅ present
2 Direct call_tool_sync, signal set mid-flight real stdio server, 6s tool, cancel @1s ⛔ n/a (no API) ✅ returns 1.02s, cancelled=True
3 Session reusable after per-call cancel add(2,3) after cancel ⛔ n/a ✅ →5, status=success
4 Pre-set signal before call starts signal .set() before call ⛔ n/a ✅ short-circuits ~0.0s, cancelled=True
5 agent.cancel() reaches in-flight MCP call (via MCPAgentTool.stream) real tool 6s, cancel @1s ❌ ran full 6.09s, cancelled=None ✅ stopped 1.02s, cancelled=True
6 Control — no signal, tool completes 2s tool, no cancel ✅ 2.04s, success, no cancelled ✅ 2.04s, success, no cancelled
7 Full agent loop → stop_reason (stub model) real MCP tool, agent.cancel() @1.5s (not applicable pre-feature) ✅ stop_reason='cancelled', 1.53s
8 Remote execution semantics server stderr progress after cancel — ⚠️ server ran to 10.00s — local cancel only (as documented)
9 Model-driven happy path — Bedrock calls MCP calculator(17,25) real agent("add 17 and 25") ✅ end_turn, answer=42 ✅ end_turn, answer=42
10 Model-driven cancel — Bedrock calls MCP slow_tool(15s), stop @2s real agent(...) + agent.cancel() ⚠️ cancelled but 19.18s (stopped_early=false) ✅ cancelled, 2.01s (stopped_early=true)
11 Model-driven cancel (standalone repro) real agent(...), stop @2s (not applicable pre-feature) ✅ stop_reason='cancelled', 2.04s

Documented MCP config mechanisms (After branch, real servers — unchanged by PR, not run before/after)

# Mechanism Result
1 Managed integration Agent(tools=[mcp_client]) ✅ PASS
2 Manual context with client: list_tools_sync() ✅ PASS
3a Transport: stdio ✅ PASS (1+2=3)
3b Transport: Streamable HTTP ✅ PASS (4+5=9)
3c Transport: SSE ✅ PASS (7+8=15)
4 Multiple MCP servers ✅ PASS (12 tools / 2 servers)
5a tool_filters allowed — string ✅ PASS
5b tool_filters allowed — regex ✅ PASS
5c Combined allowed → rejected ✅ PASS
6 prefix tool naming ✅ PASS
7 Direct invocation call_tool_sync ✅ PASS (10+20=30)
8 Elicitation elicitation_callback ✅ PASS
9a Progress — instance callback ✅ PASS (3 updates)
9b Progress — per-call override ✅ PASS
10 Use outside with raises MCPClientInitializationError ✅ PASS

Automated

Suite Result
PR unit tests (tests/strands/tools/mcp + test_event_loop.py) ✅ 242 passed

Notes

  • Core fix (rows 5, 10): before, agent.cancel() during an in-flight MCP call did nothing — the tool ran to completion; after, it stops in ~1–2s. Closes #3276.
  • No regression: happy path (rows 6, 9) identical before/after.
  • Local vs. remote (row 8): cancelled=True confirms local cancellation, not that the server stopped — matches the PR's documented contract.
  • Not tested: AWS IAM SigV4 transport (mcp-proxy-for-aws) — needs a live AWS-hosted MCP endpoint; transport shim over the same MCPClient, unrelated to this PR.

arielnabavian avatar Jul 22 '26 20:07 arielnabavian