Class CancellationSignal
The client cancels an in-flight request with a notifications/cancelled notification
naming the request id. The MCP Java SDK (2.1.0) registers no handler for that notification, so
this server reads it off the stdio input itself (StdioCancellationInterceptor) and cancels
the signal of the matching tools/call. A handler observes the signal through
ToolHandler.handle(com.fasterxml.jackson.databind.JsonNode, ServerContext, CancellationSignal)
and through ServerContext.currentCancellation().
Point of no return
A handler that sends an irreversible request (the Safe Mutation Broker's create) calls
commit() immediately before sending it. commit() and cancel(String)
are mutually ordered: when commit() returns true, a later cancel is recorded
(cancelRequestedAfterCommit()) but the handler finishes and reports what it executed;
when it returns false, the call was already cancelled and nothing may be sent.
Thread-safe. Listeners registered with onCancel(Runnable) run once, on the thread
that cancels (or immediately when the signal is already cancelled).
-
Method Summary
Modifier and TypeMethodDescriptionvoidCancels the call.booleanbooleancommit()Marks the point of no return.static CancellationSignalcreate()booleanstatic CancellationSignalnone()voidRegisters a listener that runs when the call is cancelled before commit; runs it at once when already cancelled.reason()voidthrowIfCancelled(String stage) ThrowsToolCallCancelledExceptionwhen the call was cancelled.
-
Method Details
-
create
- Returns:
- a fresh signal that a client
notifications/cancelled(orcancel(String)) can trip
-
none
- Returns:
- a signal that is never cancelled: for direct handler invocations outside a served call
-
cancel
-
isCancelled
public boolean isCancelled()- Returns:
trueonce the call was cancelled before any irreversible step
-
reason
- Returns:
- the reason the client gave, or
null
-
commit
public boolean commit()Marks the point of no return.- Returns:
truewhen the handler may send its irreversible request;falsewhen the call was cancelled first (send nothing)
-
cancelRequestedAfterCommit
public boolean cancelRequestedAfterCommit()- Returns:
truewhen a cancel arrived aftercommit(): the irreversible request was already sent and the result must say so
-
onCancel
Registers a listener that runs when the call is cancelled before commit; runs it at once when already cancelled.- Parameters:
listener- the action, for example cancelling a pending prompt
-
throwIfCancelled
ThrowsToolCallCancelledExceptionwhen the call was cancelled.- Parameters:
stage- what was about to happen, for the message (for example "before the dry run")
-