Module: ModelContextProtocol::Server::Progressable

Included in:
Prompt, Resource, Tool
Defined in:
lib/model_context_protocol/server/progressable.rb

Instance Method Summary collapse

Instance Method Details

#progressable(max_duration:, message: nil) { ... } ⇒ Object

Execute a block with automatic time-based progress reporting. Uses Concurrent::TimerTask to send progress notifications at regular intervals.

Examples:

progressable(max_duration: 30) do  # 30 seconds
  perform_long_operation
end

Parameters:

  • max_duration (Numeric)

    Expected duration in seconds

  • message (String, nil) (defaults to: nil)

    Optional custom progress message

Yields:

  • block to execute with progress tracking

Returns:

  • (Object)

    the result of the block



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/model_context_protocol/server/progressable.rb', line 17

def progressable(max_duration:, message: nil, &block)
  context = Thread.current[:mcp_context]

  return yield unless context && context[:progress_token] && context[:transport]

  progress_token = context[:progress_token]
  transport = context[:transport]
  start_time = Time.now
  update_interval = [1.0, max_duration * 0.05].max

  timer_task = Concurrent::TimerTask.new(execution_interval: update_interval) do
    elapsed_seconds = Time.now - start_time
    progress_pct = [(elapsed_seconds / max_duration) * 100, 99].min

    progress_message = if message
      "#{message} (#{elapsed_seconds.round(1)}s / ~#{max_duration}s)"
    else
      "Processing... (#{elapsed_seconds.round(1)}s / ~#{max_duration}s)"
    end

    begin
      transport.send_notification("notifications/progress", {
        progressToken: progress_token,
        progress: progress_pct.round(1),
        total: 100,
        message: progress_message
      })
    rescue
      nil
    end

    timer_task.shutdown if elapsed_seconds >= max_duration
  end

  begin
    timer_task.execute
    result = yield

    begin
      transport.send_notification("notifications/progress", {
        progressToken: progress_token,
        progress: 100,
        total: 100,
        message: "Completed"
      })
    rescue
      nil
    end

    result
  ensure
    timer_task&.shutdown if timer_task&.running?
  end
end