Class: MCP::ServerContext
- Inherits:
-
Object
- Object
- MCP::ServerContext
- Defined in:
- lib/mcp/server_context.rb
Instance Method Summary collapse
-
#create_sampling_message(**kwargs) ⇒ Object
Delegates to the session so the request is scoped to the originating client.
-
#initialize(context, progress:, notification_target:, related_request_id: nil) ⇒ ServerContext
constructor
A new instance of ServerContext.
- #method_missing(name) ⇒ Object
-
#notify_log_message(data:, level:, logger: nil) ⇒ Object
Sends a log message notification scoped to the originating session.
-
#report_progress(progress, total: nil, message: nil) ⇒ Object
Reports progress for the current tool operation.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(context, progress:, notification_target:, related_request_id: nil) ⇒ ServerContext
Returns a new instance of ServerContext.
5 6 7 8 9 10 |
# File 'lib/mcp/server_context.rb', line 5 def initialize(context, progress:, notification_target:, related_request_id: nil) @context = context @progress = progress @notification_target = notification_target @related_request_id = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/mcp/server_context.rb', line 46 def method_missing(name, ...) if @context.respond_to?(name) @context.public_send(name, ...) else super end end |
Instance Method Details
#create_sampling_message(**kwargs) ⇒ Object
Delegates to the session so the request is scoped to the originating client. Falls back to ‘@context` (via method_missing) when `@notification_target` does not support sampling.
36 37 38 39 40 41 42 43 44 |
# File 'lib/mcp/server_context.rb', line 36 def (**kwargs) if @notification_target.respond_to?(:create_sampling_message) @notification_target.(**kwargs, related_request_id: @related_request_id) elsif @context.respond_to?(:create_sampling_message) @context.(**kwargs, related_request_id: @related_request_id) else raise NoMethodError, "undefined method 'create_sampling_message' for #{self}" end end |
#notify_log_message(data:, level:, logger: nil) ⇒ Object
Sends a log message notification scoped to the originating session.
27 28 29 30 31 |
# File 'lib/mcp/server_context.rb', line 27 def (data:, level:, logger: nil) return unless @notification_target @notification_target.(data: data, level: level, logger: logger, related_request_id: @related_request_id) end |
#report_progress(progress, total: nil, message: nil) ⇒ Object
Reports progress for the current tool operation. The notification is automatically scoped to the originating session.
18 19 20 |
# File 'lib/mcp/server_context.rb', line 18 def report_progress(progress, total: nil, message: nil) @progress.report(progress, total: total, message: ) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
54 55 56 |
# File 'lib/mcp/server_context.rb', line 54 def respond_to_missing?(name, include_private = false) @context.respond_to?(name) || super end |