Class CancellationSignal

java.lang.Object
com.eqixiac.equinix.mcp.server.CancellationSignal

public final class CancellationSignal extends Object
The cancellation state of one MCP tool call.

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 Details

    • create

      public static CancellationSignal create()
      Returns:
      a fresh signal that a client notifications/cancelled (or cancel(String)) can trip
    • none

      public static CancellationSignal none()
      Returns:
      a signal that is never cancelled: for direct handler invocations outside a served call
    • cancel

      public void cancel(String why)
      Cancels the call. Has no effect on a none() signal or a signal that is already cancelled. After commit() the cancel is only recorded.
      Parameters:
      why - the client's reason, or null
    • isCancelled

      public boolean isCancelled()
      Returns:
      true once the call was cancelled before any irreversible step
    • reason

      public String reason()
      Returns:
      the reason the client gave, or null
    • commit

      public boolean commit()
      Marks the point of no return.
      Returns:
      true when the handler may send its irreversible request; false when the call was cancelled first (send nothing)
    • cancelRequestedAfterCommit

      public boolean cancelRequestedAfterCommit()
      Returns:
      true when a cancel arrived after commit(): the irreversible request was already sent and the result must say so
    • onCancel

      public void onCancel(Runnable listener)
      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

      public void throwIfCancelled(String stage)
      Throws ToolCallCancelledException when the call was cancelled.
      Parameters:
      stage - what was about to happen, for the message (for example "before the dry run")