Track Mcp-Session-Id and protocol version in HTTP client
Per the Streamable HTTP spec, if the server returns an Mcp-Session-Id header during `initialize`, the client MUST include it on subsequent requests. Capture the session ID and protocol version from the `initialize` response and attach them automatically on subsequent POSTs. Per the Protocol Version Header section of the spec, the client MUST also include `MCP-Protocol-Version` on all subsequent requests so the server can respond based on the negotiated protocol version. Map 404 responses to a new `SessionExpiredError` (a subclass of `RequestHandlerError` for backward compatibility) and clear local session state so callers can start a fresh session with a new `initialize` request, as required by the spec. - https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management - https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#protocol-version-header Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
baa093dfd5
commit
07226b7cf1
1 changed files with 275 additions and 10 deletions
|
|
@ -33,6 +33,16 @@ module MCP
|
|||
# server-returned JSON-RPC error, which is raised as `ServerError`.
|
||||
class ValidationError < StandardError; end
|
||||
|
||||
# Raised when the server responds 404 to a request containing a session ID,
|
||||
# indicating the session has expired. Inherits from `RequestHandlerError` for
|
||||
# backward compatibility with callers that rescue the generic error. Per spec,
|
||||
# clients MUST start a new session with a fresh `initialize` request in response.
|
||||
class SessionExpiredError < RequestHandlerError
|
||||
def initialize(message, request, original_error: nil)
|
||||
super(message, request, error_type: :not_found, original_error: original_error)
|
||||
end
|
||||
end
|
||||
|
||||
# Initializes a new MCP::Client instance.
|
||||
#
|
||||
# @param transport [Object] The transport object to use for communication with the server.
|
||||
|
|
|
|||
Loading…
Reference in a new issue