Module: ActionMCP::Client::RequestTimeouts

Included in:
Collection
Defined in:
lib/action_mcp/client/request_timeouts.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

Default timeout in seconds

1.0

Instance Method Summary collapse

Instance Method Details

#load_with_timeout(method_name, force: false, timeout: DEFAULT_TIMEOUT) ⇒ Boolean

Load resources with timeout support - blocking until response or timeout

Parameters:

  • method_name (Symbol)

    The method to call for loading (e.g., :list_resources)

  • force (Boolean) (defaults to: false)

    Whether to force reload even if already loaded

  • timeout (Float) (defaults to: DEFAULT_TIMEOUT)

    Timeout in seconds

Returns:

  • (Boolean)

    Success status



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/action_mcp/client/request_timeouts.rb', line 14

def load_with_timeout(method_name, force: false, timeout: DEFAULT_TIMEOUT)
  return true if @loaded && !force

  # Make the request and store its ID
  request_id = client.send(method_name)

  start_time = Time.now

  # Wait until either:
  # 1. The collection is loaded (@loaded becomes true from JsonRpcHandler)
  # 2. The timeout is reached
  sleep(0.1) while !@loaded && (Time.now - start_time) < timeout

  # If we timed out
  unless @loaded
    request = client.session.messages.requests.find_by(jsonrpc_id: request_id)

    if request && !request.request_acknowledged?
      # Send cancel notification
      client.send_jsonrpc_notification("notifications/cancelled", {
                                         requestId: request_id,
                                         reason: "Request timed out after #{timeout} seconds"
                                       })

      # Mark as cancelled in the database
      request.update(request_cancelled: true)

      log_error("Request #{method_name} timed out after #{timeout} seconds")
    end

    # Mark as loaded even though we timed out
    @loaded = true
    return false
  end

  # Collection was successfully loaded
  true
end