From fd65989e0378981de27716badc911e598292a24c Mon Sep 17 00:00:00 2001 From: Lovish Arora <46993225+lavish0000@users.noreply.github.com> Date: Fri, 6 Mar 2026 05:51:47 +0100 Subject: [PATCH 1/2] docs: explain request IDs for FastMCP cancellation --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 487d48bee..ed16e8af4 100644 --- a/README.md +++ b/README.md @@ -1138,6 +1138,27 @@ The request context accessible via `ctx.request_context` contains request-specif - `ctx.request_context.request` - The original MCP request object for advanced processing - `ctx.request_context.request_id` - Unique identifier for this request +Inside a FastMCP tool, the current request ID is available as `ctx.request_id` +or `ctx.request_context.request_id`. This is the same JSON-RPC request ID that +would be used in a `notifications/cancelled` message. + +Use `notifications/cancelled` for cancelling normal in-flight requests such as +`tools/call`. For task-based work, use `tasks/cancel` instead. + +```python +@mcp.tool() +async def slow_tool(ctx: Context) -> str: + current_request_id = ctx.request_id + return f"handling request {current_request_id}" +``` + +If you are using the SDK's high-level client helpers such as +`client.call_tool()` or `client.session.call_tool()`, the SDK manages request +IDs for you and does not currently expose them directly. If you need explicit +client-driven cancellation, prefer a task-based workflow (`tasks/cancel`) or a +lower-level integration where you control request/notification correlation +yourself. + ```python # Example with typed lifespan context @dataclass From a20520f2e9948a038c8e8879f3c13408c4dbf849 Mon Sep 17 00:00:00 2001 From: Lovish Arora <46993225+lavish0000@users.noreply.github.com> Date: Fri, 6 Mar 2026 06:36:16 +0100 Subject: [PATCH 2/2] docs: update FastMCP cancellation docs in README.v2 --- README.md | 21 --------------------- README.v2.md | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ed16e8af4..487d48bee 100644 --- a/README.md +++ b/README.md @@ -1138,27 +1138,6 @@ The request context accessible via `ctx.request_context` contains request-specif - `ctx.request_context.request` - The original MCP request object for advanced processing - `ctx.request_context.request_id` - Unique identifier for this request -Inside a FastMCP tool, the current request ID is available as `ctx.request_id` -or `ctx.request_context.request_id`. This is the same JSON-RPC request ID that -would be used in a `notifications/cancelled` message. - -Use `notifications/cancelled` for cancelling normal in-flight requests such as -`tools/call`. For task-based work, use `tasks/cancel` instead. - -```python -@mcp.tool() -async def slow_tool(ctx: Context) -> str: - current_request_id = ctx.request_id - return f"handling request {current_request_id}" -``` - -If you are using the SDK's high-level client helpers such as -`client.call_tool()` or `client.session.call_tool()`, the SDK manages request -IDs for you and does not currently expose them directly. If you need explicit -client-driven cancellation, prefer a task-based workflow (`tasks/cancel`) or a -lower-level integration where you control request/notification correlation -yourself. - ```python # Example with typed lifespan context @dataclass diff --git a/README.v2.md b/README.v2.md index bd6927bf9..209d352fd 100644 --- a/README.v2.md +++ b/README.v2.md @@ -1134,6 +1134,27 @@ The request context accessible via `ctx.request_context` contains request-specif - `ctx.request_context.request` - The original MCP request object for advanced processing - `ctx.request_context.request_id` - Unique identifier for this request +Inside a FastMCP tool, the current request ID is available as `ctx.request_id` +or `ctx.request_context.request_id`. This is the same JSON-RPC request ID that +would be used in a `notifications/cancelled` message. + +Use `notifications/cancelled` for cancelling normal in-flight requests such as +`tools/call`. For task-based work, use `tasks/cancel` instead. + +```python +@mcp.tool() +async def slow_tool(ctx: Context) -> str: + current_request_id = ctx.request_id + return f"handling request {current_request_id}" +``` + +If you are using the SDK's high-level client helpers such as +`client.call_tool()` or `client.session.call_tool()`, the SDK manages request +IDs for you and does not currently expose them directly. If you need explicit +client-driven cancellation, prefer a task-based workflow (`tasks/cancel`) or a +lower-level integration where you control request/notification correlation +yourself. + ```python # Example with typed lifespan context @dataclass