Module: ModelContextProtocol::Server::Progressable
Instance Method Summary collapse
-
#progressable(max_duration:, message: nil) { ... } ⇒ Object
Execute a block with automatic time-based progress reporting.
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.
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 = if "#{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: }) 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 |