Module: MCPClient::HttpTransportBase::SessionRecovery
- Included in:
- MCPClient::HttpTransportBase
- Defined in:
- lib/mcp_client/http_transport_base/session_recovery.rb
Overview
MCP 2025-11-25 session management for the HTTP transports: an HTTP 404 answering a request that carried an Mcp-Session-Id means the session expired, and "the client MUST start a new session by sending a new InitializeRequest without a session ID attached". That new session is a new session in every sense — the session epoch moves with it, so everything scoped to the old one (the tasks extension's task ids and input keys, which the replacement session may reuse for entirely different requests) dies with it.
Instance Method Summary collapse
-
#resend_after_session_restart(request) ⇒ Faraday::Response
Resend a request against the freshly restarted session — unless doing so could execute a side effect twice.
Instance Method Details
#resend_after_session_restart(request) ⇒ Faraday::Response
Resend a request against the freshly restarted session — unless doing so could execute a side effect twice.
A 404 usually means the server rejected the request outright, but it does not prove that: a session can expire after the tool ran. Automatic session recovery is worth having for idempotent methods, and would otherwise be a hole straight through the no-replay guarantee that with_retry enforces for NON_IDEMPOTENT_METHODS.
Raises ConnectionError (which with_retry never retries) so no other path can turn this into a second attempt.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mcp_client/http_transport_base/session_recovery.rb', line 29 def resend_after_session_restart(request) method = request['method'] # A request pinned to the session the 404 ended is not resent into the # session that replaced it: its payload (task ids, input request keys) # names something else there. Nothing was written, so the caller may # drop it — see {MCPClient::SessionPin}. check_session_pin! return send_http_request(request) unless MCPClient::JsonRpcCommon::NON_IDEMPOTENT_METHODS.include?(method) raise MCPClient::Errors::ConnectionError, "Session expired during #{method}; a new session was started but the request was NOT resent " \ 'because it may already have executed. Retry it explicitly if that is safe.' end |