Class: MCP::Cancellation

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/cancellation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_id: nil) ⇒ Cancellation

Returns a new instance of Cancellation.



9
10
11
12
13
14
15
# File 'lib/mcp/cancellation.rb', line 9

def initialize(request_id: nil)
  @request_id = request_id
  @reason = nil
  @cancelled = false
  @callbacks = []
  @mutex = Mutex.new
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



7
8
9
# File 'lib/mcp/cancellation.rb', line 7

def reason
  @reason
end

#request_idObject (readonly)

Returns the value of attribute request_id.



7
8
9
# File 'lib/mcp/cancellation.rb', line 7

def request_id
  @request_id
end

Instance Method Details

#cancel(reason: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mcp/cancellation.rb', line 21

def cancel(reason: nil)
  callbacks = @mutex.synchronize do
    return false if @cancelled

    @cancelled = true
    @reason = reason
    @callbacks.tap { @callbacks = [] }
  end

  callbacks.each do |callback|
    callback.call(reason)
  rescue StandardError => e
    MCP.configuration.exception_reporter.call(e, { error: "Cancellation callback failed" })
  end

  true
end

#cancelled?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/mcp/cancellation.rb', line 17

def cancelled?
  @mutex.synchronize { @cancelled }
end

#off_cancel(handle) ⇒ Object

Removes a previously-registered ‘on_cancel` callback. Returns `true` if the callback was still pending (i.e. had not yet fired), `false` otherwise. Safe to call with `nil`.



62
63
64
65
66
# File 'lib/mcp/cancellation.rb', line 62

def off_cancel(handle)
  return false unless handle

  @mutex.synchronize { !@callbacks.delete(handle).nil? }
end

#on_cancel(&block) ⇒ Object

Registers a callback invoked synchronously on the first ‘cancel` call. If already cancelled, fires immediately.

Returns the block itself as a handle that can be passed to ‘off_cancel` to deregister it (e.g. when a nested request completes normally and the hook should not fire on a later parent cancellation).



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mcp/cancellation.rb', line 45

def on_cancel(&block)
  fire_now = false
  @mutex.synchronize do
    if @cancelled
      fire_now = true
    else
      @callbacks << block
    end
  end

  block.call(@reason) if fire_now
  block
end

#raise_if_cancelled!Object

Raises:



68
69
70
# File 'lib/mcp/cancellation.rb', line 68

def raise_if_cancelled!
  raise CancelledError.new(request_id: @request_id, reason: @reason) if cancelled?
end