Class: MCP::ServerSession
- Inherits:
-
Object
- Object
- MCP::ServerSession
- Defined in:
- lib/mcp/server_session.rb
Overview
Holds per-connection state for a single client session. Created by the transport layer; delegates request handling to the shared ‘Server`.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#logging_message_notification ⇒ Object
readonly
Returns the value of attribute logging_message_notification.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Instance Method Summary collapse
-
#client_capabilities ⇒ Object
Returns per-session client capabilities, falling back to global.
-
#configure_logging(logging_message_notification) ⇒ Object
Called by ‘Server#configure_logging_level`.
-
#create_form_elicitation(message:, requested_schema:, related_request_id: nil) ⇒ Object
Sends an ‘elicitation/create` request (form mode) scoped to this session.
-
#create_sampling_message(related_request_id: nil, **kwargs) ⇒ Object
Sends a ‘sampling/createMessage` request scoped to this session.
-
#create_url_elicitation(message:, url:, elicitation_id:, related_request_id: nil) ⇒ Object
Sends an ‘elicitation/create` request (URL mode) scoped to this session.
- #handle(request) ⇒ Object
- #handle_json(request_json) ⇒ Object
-
#initialize(server:, transport:, session_id: nil) ⇒ ServerSession
constructor
A new instance of ServerSession.
-
#notify_elicitation_complete(elicitation_id:) ⇒ Object
Sends an elicitation complete notification scoped to this session.
-
#notify_log_message(data:, level:, logger: nil, related_request_id: nil) ⇒ Object
Sends a log message notification to this session only.
-
#notify_progress(progress_token:, progress:, total: nil, message: nil, related_request_id: nil) ⇒ Object
Sends a progress notification to this session only.
-
#store_client_info(client:, capabilities: nil) ⇒ Object
Called by ‘Server#init` during the initialization handshake.
Constructor Details
#initialize(server:, transport:, session_id: nil) ⇒ ServerSession
Returns a new instance of ServerSession.
11 12 13 14 15 16 17 18 |
# File 'lib/mcp/server_session.rb', line 11 def initialize(server:, transport:, session_id: nil) @server = server @transport = transport @session_id = session_id @client = nil @client_capabilities = nil @logging_message_notification = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/mcp/server_session.rb', line 9 def client @client end |
#logging_message_notification ⇒ Object (readonly)
Returns the value of attribute logging_message_notification.
9 10 11 |
# File 'lib/mcp/server_session.rb', line 9 def @logging_message_notification end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
9 10 11 |
# File 'lib/mcp/server_session.rb', line 9 def session_id @session_id end |
Instance Method Details
#client_capabilities ⇒ Object
Returns per-session client capabilities, falling back to global.
40 41 42 |
# File 'lib/mcp/server_session.rb', line 40 def client_capabilities @client_capabilities || @server.client_capabilities end |
#configure_logging(logging_message_notification) ⇒ Object
Called by ‘Server#configure_logging_level`.
35 36 37 |
# File 'lib/mcp/server_session.rb', line 35 def configure_logging() @logging_message_notification = end |
#create_form_elicitation(message:, requested_schema:, related_request_id: nil) ⇒ Object
Sends an ‘elicitation/create` request (form mode) scoped to this session.
51 52 53 54 55 56 57 58 59 |
# File 'lib/mcp/server_session.rb', line 51 def create_form_elicitation(message:, requested_schema:, related_request_id: nil) unless client_capabilities&.dig(:elicitation) raise "Client does not support elicitation. " \ "The client must declare the `elicitation` capability during initialization." end params = { mode: "form", message: , requestedSchema: requested_schema } send_to_transport_request(Methods::ELICITATION_CREATE, params, related_request_id: ) end |
#create_sampling_message(related_request_id: nil, **kwargs) ⇒ Object
Sends a ‘sampling/createMessage` request scoped to this session.
45 46 47 48 |
# File 'lib/mcp/server_session.rb', line 45 def (related_request_id: nil, **kwargs) params = @server.build_sampling_params(client_capabilities, **kwargs) send_to_transport_request(Methods::SAMPLING_CREATE_MESSAGE, params, related_request_id: ) end |
#create_url_elicitation(message:, url:, elicitation_id:, related_request_id: nil) ⇒ Object
Sends an ‘elicitation/create` request (URL mode) scoped to this session.
62 63 64 65 66 67 68 69 70 |
# File 'lib/mcp/server_session.rb', line 62 def create_url_elicitation(message:, url:, elicitation_id:, related_request_id: nil) unless client_capabilities&.dig(:elicitation, :url) raise "Client does not support URL mode elicitation. " \ "The client must declare the `elicitation.url` capability during initialization." end params = { mode: "url", message: , url: url, elicitationId: elicitation_id } send_to_transport_request(Methods::ELICITATION_CREATE, params, related_request_id: ) end |
#handle(request) ⇒ Object
20 21 22 |
# File 'lib/mcp/server_session.rb', line 20 def handle(request) @server.handle(request, session: self) end |
#handle_json(request_json) ⇒ Object
24 25 26 |
# File 'lib/mcp/server_session.rb', line 24 def handle_json(request_json) @server.handle_json(request_json, session: self) end |
#notify_elicitation_complete(elicitation_id:) ⇒ Object
Sends an elicitation complete notification scoped to this session.
73 74 75 76 77 |
# File 'lib/mcp/server_session.rb', line 73 def notify_elicitation_complete(elicitation_id:) send_to_transport(Methods::NOTIFICATIONS_ELICITATION_COMPLETE, { elicitationId: elicitation_id }) rescue => e @server.report_exception(e, notification: "elicitation_complete") end |
#notify_log_message(data:, level:, logger: nil, related_request_id: nil) ⇒ Object
Sends a log message notification to this session only.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mcp/server_session.rb', line 94 def (data:, level:, logger: nil, related_request_id: nil) effective_logging = @logging_message_notification || @server. return unless effective_logging&.should_notify?(level) params = { "data" => data, "level" => level } params["logger"] = logger if logger send_to_transport(Methods::NOTIFICATIONS_MESSAGE, params, related_request_id: ) rescue => e @server.report_exception(e, { notification: "log_message" }) end |
#notify_progress(progress_token:, progress:, total: nil, message: nil, related_request_id: nil) ⇒ Object
Sends a progress notification to this session only.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mcp/server_session.rb', line 80 def notify_progress(progress_token:, progress:, total: nil, message: nil, related_request_id: nil) params = { "progressToken" => progress_token, "progress" => progress, "total" => total, "message" => , }.compact send_to_transport(Methods::NOTIFICATIONS_PROGRESS, params, related_request_id: ) rescue => e @server.report_exception(e, notification: "progress") end |
#store_client_info(client:, capabilities: nil) ⇒ Object
Called by ‘Server#init` during the initialization handshake.
29 30 31 32 |
# File 'lib/mcp/server_session.rb', line 29 def store_client_info(client:, capabilities: nil) @client = client @client_capabilities = capabilities end |