← Back to ToolBench
Utilities

Cancellation

Cancel in-flight requests from either side

What it does

Cancellation allows either party to cancel a previously-issued request that is still in progress. The cancelling side sends notifications/cancelled with the request ID. The other side should abort the operation and return a response. This prevents wasted computation when the user navigates away or the client no longer needs a result, and enables responsive UIs.

How it works

Client Server tools/call {id: "req-42"} processing... notifications/cancelled {requestId: "req-42"} error: request cancelled

Example implementation

server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
  const abortSignal = extra.signal;
  abortSignal.addEventListener("abort", () => {
    cleanup(); // Free resources on cancellation
  });
  return await longRunningOperation(abortSignal);
});