notifyCancelled method

void notifyCancelled(
  1. Object requestId, {
  2. String? reason,
})

Cancel an in-flight server-side operation.

Per spec, cancellation is a NOTIFICATION (notifications/cancelled), not a request — fire-and-forget with requestId and an optional reason.

Only a request this client sent and is still waiting on can be cancelled; its call then fails, and a late response is ignored. (A call made with RequestOptions.cancel is cancelled the same way.)

Implementation

void notifyCancelled(Object requestId, {String? reason}) {
  if (!_initialized) {
    throw McpError('Client is not initialized');
  }
  final completer = _requestCompleters.remove(requestId);
  if (completer == null) {
    throw ArgumentError.value(requestId, 'requestId', 'no request of this client is in flight with that id');
  }
  _cancelOnWire(requestId, reason);
  if (!completer.isCompleted) completer.completeError(McpError('Request cancelled'));
}