Class: MCP::ServerContext
- Inherits:
-
Object
- Object
- MCP::ServerContext
- Defined in:
- lib/mcp/server_context.rb
Instance Attribute Summary collapse
-
#cancellation ⇒ Object
readonly
Returns the value of attribute cancellation.
Instance Method Summary collapse
- #cancelled? ⇒ Boolean
-
#create_form_elicitation(**kwargs) ⇒ Object
Delegates to the session so the request is scoped to the originating client.
-
#create_sampling_message(**kwargs) ⇒ Object
deprecated
Deprecated.
MCP Sampling (
sampling/createMessage) is deprecated as of MCP protocol version 2026-07-28 (SEP-2577). Use direct LLM provider APIs instead. -
#create_url_elicitation(**kwargs) ⇒ Object
Delegates to the session so the request is scoped to the originating client.
-
#initialize(context, progress:, notification_target:, related_request_id: nil, cancellation: nil) ⇒ ServerContext
constructor
A new instance of ServerContext.
-
#list_roots ⇒ Object
deprecated
Deprecated.
MCP Roots (
roots/listandnotifications/roots/list_changed) is deprecated as of MCP protocol version 2026-07-28 (SEP-2577). Use tool parameters, resource URIs, server configuration, or environment variables instead. -
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Forward arguments explicitly with
*args, **kwargs, &blockrather than the...forwarding syntax. -
#notify_elicitation_complete(**kwargs) ⇒ Object
Delegates to the session so the notification is scoped to the originating client.
-
#notify_log_message(data:, level:, logger: nil) ⇒ Object
deprecated
Deprecated.
MCP Logging (
logging/setLevelandnotifications/message) is deprecated as of MCP protocol version 2026-07-28 (SEP-2577). Use stderr or OpenTelemetry instead. -
#notify_resources_updated(uri:) ⇒ Object
Sends a resource updated notification scoped to the originating session.
-
#ping ⇒ Hash
Sends a
pingrequest to the originating client to verify it is still responsive. - #raise_if_cancelled! ⇒ Object
-
#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, cancellation: nil) ⇒ ServerContext
Returns a new instance of ServerContext.
7 8 9 10 11 12 13 |
# File 'lib/mcp/server_context.rb', line 7 def initialize(context, progress:, notification_target:, related_request_id: nil, cancellation: nil) @context = context @progress = progress @notification_target = notification_target @related_request_id = @cancellation = cancellation end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Forward arguments explicitly with *args, **kwargs, &block rather than the ... forwarding syntax.
The gem supports Ruby 2.7.0 (see required_ruby_version), but RuboCop's Parser backend only runs on Ruby 2.7.8,
so leading-argument forwarding like def method_missing(name, ...) is allowed by the linter even though it
raises a SyntaxError on Ruby 2.7.0 through 2.7.2 (it was added in Ruby 2.7.3). Explicit forwarding keeps
this method loadable on Ruby 2.7.0.
160 161 162 163 164 165 166 |
# File 'lib/mcp/server_context.rb', line 160 def method_missing(name, *args, **kwargs, &block) if @context.respond_to?(name) @context.public_send(name, *args, **kwargs, &block) else super end end |
Instance Attribute Details
#cancellation ⇒ Object (readonly)
Returns the value of attribute cancellation.
5 6 7 |
# File 'lib/mcp/server_context.rb', line 5 def cancellation @cancellation end |
Instance Method Details
#cancelled? ⇒ Boolean
15 16 17 |
# File 'lib/mcp/server_context.rb', line 15 def cancelled? !!@cancellation&.cancelled? end |
#create_form_elicitation(**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 elicitation.
The originating request id is stamped as a non-overridable
related_request_id, as with create_sampling_message (SEP-2260).
120 121 122 123 124 125 126 127 128 |
# File 'lib/mcp/server_context.rb', line 120 def create_form_elicitation(**kwargs) if @notification_target.respond_to?(:create_form_elicitation) @notification_target.create_form_elicitation(**kwargs, related_request_id: @related_request_id) elsif @context.respond_to?(:create_form_elicitation) @context.create_form_elicitation(**kwargs, related_request_id: @related_request_id) else raise NoMethodError, "undefined method 'create_form_elicitation' for #{self}" end end |
#create_sampling_message(**kwargs) ⇒ Object
MCP Sampling (sampling/createMessage) is deprecated as of
MCP protocol version 2026-07-28 (SEP-2577). Use direct LLM provider
APIs instead.
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.
The originating request id is stamped as related_request_id and cannot be
overridden by callers (the literal keyword after **kwargs wins),
satisfying SEP-2260's requirement that server-to-client requests be
associated with the client request being processed.
105 106 107 108 109 110 111 112 113 |
# File 'lib/mcp/server_context.rb', line 105 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 |
#create_url_elicitation(**kwargs) ⇒ Object
Delegates to the session so the request is scoped to the originating client.
Falls back to @context when @notification_target does not support URL mode elicitation.
The originating request id is stamped as a non-overridable related_request_id,
as with create_sampling_message (SEP-2260).
134 135 136 137 138 139 140 141 142 |
# File 'lib/mcp/server_context.rb', line 134 def create_url_elicitation(**kwargs) if @notification_target.respond_to?(:create_url_elicitation) @notification_target.create_url_elicitation(**kwargs, related_request_id: @related_request_id) elsif @context.respond_to?(:create_url_elicitation) @context.create_url_elicitation(**kwargs, related_request_id: @related_request_id) else raise NoMethodError, "undefined method 'create_url_elicitation' for #{self}" end end |
#list_roots ⇒ Object
MCP Roots (roots/list and
notifications/roots/list_changed) is deprecated as of MCP protocol
version 2026-07-28 (SEP-2577). Use tool parameters, resource URIs,
server configuration, or environment variables instead.
Delegates to the session so the request is scoped to the originating client.
The originating request id is stamped as related_request_id, satisfying
SEP-2260's requirement that server-to-client requests be associated with
the client request being processed.
64 65 66 67 68 69 70 |
# File 'lib/mcp/server_context.rb', line 64 def list_roots if @notification_target.respond_to?(:list_roots) @notification_target.list_roots(related_request_id: @related_request_id) else raise NoMethodError, "undefined method 'list_roots' for #{self}" end end |
#notify_elicitation_complete(**kwargs) ⇒ Object
Delegates to the session so the notification is scoped to the originating client.
145 146 147 148 149 150 151 152 153 |
# File 'lib/mcp/server_context.rb', line 145 def notify_elicitation_complete(**kwargs) if @notification_target.respond_to?(:notify_elicitation_complete) @notification_target.notify_elicitation_complete(**kwargs) elsif @context.respond_to?(:notify_elicitation_complete) @context.notify_elicitation_complete(**kwargs) else raise NoMethodError, "undefined method 'notify_elicitation_complete' for #{self}" end end |
#notify_log_message(data:, level:, logger: nil) ⇒ Object
MCP Logging (logging/setLevel and notifications/message)
is deprecated as of MCP protocol version 2026-07-28 (SEP-2577).
Use stderr or OpenTelemetry instead.
Sends a log message notification scoped to the originating session.
41 42 43 44 45 |
# File 'lib/mcp/server_context.rb', line 41 def (data:, level:, logger: nil) return unless @notification_target @notification_target.(data: data, level: level, logger: logger, related_request_id: @related_request_id) end |
#notify_resources_updated(uri:) ⇒ Object
Sends a resource updated notification scoped to the originating session.
50 51 52 53 54 |
# File 'lib/mcp/server_context.rb', line 50 def notify_resources_updated(uri:) return unless @notification_target @notification_target.notify_resources_updated(uri: uri) end |
#ping ⇒ Hash
Sends a ping request to the originating client to verify it is still responsive.
Per the MCP spec, the client MUST respond promptly with an empty result.
86 87 88 89 90 91 92 |
# File 'lib/mcp/server_context.rb', line 86 def ping if @notification_target.respond_to?(:ping) @notification_target.ping(related_request_id: @related_request_id) else raise NoMethodError, "undefined method 'ping' for #{self}" end end |
#raise_if_cancelled! ⇒ Object
19 20 21 |
# File 'lib/mcp/server_context.rb', line 19 def raise_if_cancelled! @cancellation&.raise_if_cancelled! 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.
29 30 31 |
# File 'lib/mcp/server_context.rb', line 29 def report_progress(progress, total: nil, message: nil) @progress.report(progress, total: total, message: ) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
168 169 170 |
# File 'lib/mcp/server_context.rb', line 168 def respond_to_missing?(name, include_private = false) @context.respond_to?(name) || super end |