|
1 | 1 | """Cancellation interactions against the low-level Server, driven through the public Client API. |
2 | 2 |
|
3 | | -There is no client-side cancellation API: cancelling means sending a CancelledNotification |
4 | | -carrying the request id, which only the server-side handler can observe (`ctx.request_id`), so |
5 | | -these tests capture the id from inside the blocked handler before cancelling. The handler blocks |
6 | | -on an Event rather than a sleep, and every wait is bounded by `anyio.fail_after`. |
| 3 | +Client-side, cancelling means abandoning: cancelling the task that awaits a call makes the SDK |
| 4 | +carry the signal in the transport's own spelling (a cancelled frame on stream wires, closing the |
| 5 | +request's own response stream at 2026-07-28 streamable HTTP). The receiving-side tests instead |
| 6 | +script a CancelledNotification by hand, capturing the request id from inside the blocked handler. |
| 7 | +Handlers block on an Event rather than a sleep, and every wait is bounded by `anyio.fail_after`. |
7 | 8 | """ |
8 | 9 |
|
9 | 10 | import anyio |
|
20 | 21 | JSONRPCNotification, |
21 | 22 | JSONRPCRequest, |
22 | 23 | JSONRPCResponse, |
| 24 | + ListToolsResult, |
23 | 25 | PingRequest, |
24 | 26 | ServerCapabilities, |
25 | 27 | TextContent, |
| 28 | + Tool, |
26 | 29 | ) |
27 | 30 |
|
28 | 31 | from mcp import MCPError |
@@ -344,3 +347,121 @@ async def scripted_server(streams: MessageStream) -> None: |
344 | 347 | assert pong == snapshot(EmptyResult()) |
345 | 348 | # The stream is ordered, so a courtesy cancel would have arrived ahead of the ping. |
346 | 349 | assert received_methods == snapshot(["initialize", "ping"]) |
| 350 | + |
| 351 | + |
| 352 | +@requirement("protocol:cancel:abort-signal") |
| 353 | +async def test_abandoning_a_call_stops_the_server_handler(connect: Connect) -> None: |
| 354 | + """Cancelling the task that awaits a call cancels the request itself, not just the local wait: |
| 355 | + the server-side handler is interrupted, and the session serves later requests normally. |
| 356 | +
|
| 357 | + Spec-mandated (cancellation flow): the sender cancels requests it abandons; the wire spelling |
| 358 | + is per-transport (frame on stream wires, response-stream close at 2026 streamable HTTP). |
| 359 | + """ |
| 360 | + handler_started = anyio.Event() |
| 361 | + handler_cancelled = anyio.Event() |
| 362 | + |
| 363 | + async def call_tool(ctx: ServerRequestContext, params: types.CallToolRequestParams) -> CallToolResult: |
| 364 | + if params.name == "block": |
| 365 | + handler_started.set() |
| 366 | + try: |
| 367 | + await anyio.Event().wait() # parked until the client's abandonment cancels it |
| 368 | + except anyio.get_cancelled_exc_class(): |
| 369 | + handler_cancelled.set() |
| 370 | + raise |
| 371 | + assert params.name == "echo" |
| 372 | + return CallToolResult(content=[TextContent(text="ok")]) |
| 373 | + |
| 374 | + async def list_tools(ctx: ServerRequestContext, params: types.PaginatedRequestParams | None) -> ListToolsResult: |
| 375 | + return ListToolsResult(tools=[Tool(name=name, input_schema={"type": "object"}) for name in ("block", "echo")]) |
| 376 | + |
| 377 | + server = Server("blocker", on_list_tools=list_tools, on_call_tool=call_tool) |
| 378 | + |
| 379 | + async with connect(server) as client: |
| 380 | + abandon = anyio.CancelScope() |
| 381 | + |
| 382 | + async def call_and_abandon() -> None: |
| 383 | + with abandon: |
| 384 | + await client.call_tool("block", {}) |
| 385 | + raise NotImplementedError # unreachable: the call never resolves |
| 386 | + assert abandon.cancelled_caught |
| 387 | + |
| 388 | + async with anyio.create_task_group() as tg: |
| 389 | + tg.start_soon(call_and_abandon) |
| 390 | + with anyio.fail_after(5): |
| 391 | + await handler_started.wait() |
| 392 | + abandon.cancel() |
| 393 | + with anyio.fail_after(5): |
| 394 | + await handler_cancelled.wait() |
| 395 | + |
| 396 | + # Let the abandoned call's late error response (sent on the legacy arms) arrive and be |
| 397 | + # dropped while the client is still open, so teardown never races its delivery. |
| 398 | + await anyio.wait_all_tasks_blocked() |
| 399 | + result = await client.call_tool("echo", {}) |
| 400 | + assert result.content == [TextContent(text="ok")] |
| 401 | + |
| 402 | + |
| 403 | +@requirement("protocol:cancel:abort-scoped") |
| 404 | +async def test_abandoning_one_call_leaves_a_concurrent_call_running(connect: Connect) -> None: |
| 405 | + """Cancellation is scoped to the request it names: with two calls genuinely in flight, |
| 406 | + abandoning the first interrupts only its handler and the second returns its result. |
| 407 | +
|
| 408 | + Steps: |
| 409 | + 1. `doomed` and `survivor` are both mid-flight (each handler has started). |
| 410 | + 2. The client abandons `doomed`; its handler observes cancellation. |
| 411 | + 3. `survivor` is released and completes normally. |
| 412 | + """ |
| 413 | + doomed_started = anyio.Event() |
| 414 | + doomed_cancelled = anyio.Event() |
| 415 | + survivor_started = anyio.Event() |
| 416 | + release_survivor = anyio.Event() |
| 417 | + |
| 418 | + async def call_tool(ctx: ServerRequestContext, params: types.CallToolRequestParams) -> CallToolResult: |
| 419 | + if params.name == "doomed": |
| 420 | + doomed_started.set() |
| 421 | + try: |
| 422 | + await anyio.Event().wait() # parked until the client's abandonment cancels it |
| 423 | + except anyio.get_cancelled_exc_class(): |
| 424 | + doomed_cancelled.set() |
| 425 | + raise |
| 426 | + assert params.name == "survivor" |
| 427 | + survivor_started.set() |
| 428 | + with anyio.fail_after(5): |
| 429 | + await release_survivor.wait() |
| 430 | + return CallToolResult(content=[TextContent(text="survived")]) |
| 431 | + |
| 432 | + async def list_tools(ctx: ServerRequestContext, params: types.PaginatedRequestParams | None) -> ListToolsResult: |
| 433 | + return ListToolsResult( |
| 434 | + tools=[Tool(name=name, input_schema={"type": "object"}) for name in ("doomed", "survivor")] |
| 435 | + ) |
| 436 | + |
| 437 | + server = Server("pair", on_list_tools=list_tools, on_call_tool=call_tool) |
| 438 | + |
| 439 | + async with connect(server) as client: |
| 440 | + abandon = anyio.CancelScope() |
| 441 | + results: list[CallToolResult] = [] |
| 442 | + |
| 443 | + async def doomed_call() -> None: |
| 444 | + with abandon: |
| 445 | + await client.call_tool("doomed", {}) |
| 446 | + raise NotImplementedError # unreachable: the call never resolves |
| 447 | + |
| 448 | + async def survivor_call() -> None: |
| 449 | + results.append(await client.call_tool("survivor", {})) |
| 450 | + |
| 451 | + async with anyio.create_task_group() as tg: |
| 452 | + tg.start_soon(doomed_call) |
| 453 | + with anyio.fail_after(5): |
| 454 | + await doomed_started.wait() |
| 455 | + tg.start_soon(survivor_call) |
| 456 | + with anyio.fail_after(5): |
| 457 | + await survivor_started.wait() |
| 458 | + abandon.cancel() |
| 459 | + with anyio.fail_after(5): |
| 460 | + await doomed_cancelled.wait() |
| 461 | + release_survivor.set() |
| 462 | + |
| 463 | + # Let the abandoned call's late error response (sent on the legacy arms) arrive and be |
| 464 | + # dropped while the client is still open, so teardown never races its delivery. |
| 465 | + await anyio.wait_all_tasks_blocked() |
| 466 | + |
| 467 | + assert results == snapshot([CallToolResult(content=[TextContent(text="survived")])]) |
0 commit comments