Skip to content

Commit fd65989

Browse files
committed
docs: explain request IDs for FastMCP cancellation
1 parent b33c811 commit fd65989

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,27 @@ The request context accessible via `ctx.request_context` contains request-specif
11381138
- `ctx.request_context.request` - The original MCP request object for advanced processing
11391139
- `ctx.request_context.request_id` - Unique identifier for this request
11401140

1141+
Inside a FastMCP tool, the current request ID is available as `ctx.request_id`
1142+
or `ctx.request_context.request_id`. This is the same JSON-RPC request ID that
1143+
would be used in a `notifications/cancelled` message.
1144+
1145+
Use `notifications/cancelled` for cancelling normal in-flight requests such as
1146+
`tools/call`. For task-based work, use `tasks/cancel` instead.
1147+
1148+
```python
1149+
@mcp.tool()
1150+
async def slow_tool(ctx: Context) -> str:
1151+
current_request_id = ctx.request_id
1152+
return f"handling request {current_request_id}"
1153+
```
1154+
1155+
If you are using the SDK's high-level client helpers such as
1156+
`client.call_tool()` or `client.session.call_tool()`, the SDK manages request
1157+
IDs for you and does not currently expose them directly. If you need explicit
1158+
client-driven cancellation, prefer a task-based workflow (`tasks/cancel`) or a
1159+
lower-level integration where you control request/notification correlation
1160+
yourself.
1161+
11411162
```python
11421163
# Example with typed lifespan context
11431164
@dataclass

0 commit comments

Comments
 (0)