Add HTTP client connect for initialize handshake#327
Draft
atesgoral wants to merge 2 commits intomodelcontextprotocol:mainfrom
Draft
Add HTTP client connect for initialize handshake#327atesgoral wants to merge 2 commits intomodelcontextprotocol:mainfrom
atesgoral wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Per MCP spec, clients that no longer need a session SHOULD send an HTTP DELETE to the MCP endpoint with the Mcp-Session-Id header to explicitly terminate it. Servers MAY respond with 405 Method Not Allowed when they don't support client-initiated termination. `MCP::Client::HTTP#close` sends the DELETE with session headers, clears local session state regardless of outcome, and is a no-op when no session has been established. https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management
Per MCP spec, clients must send an `initialize` request followed by a `notifications/initialized` notification before issuing any other requests. The server's `InitializeResult` (protocol version, server capabilities, server info, instructions) negotiates the session for the lifetime of the connection. `MCP::Client::HTTP#connect` performs the full handshake, caches the server's `InitializeResult`, and exposes it via the `server_info` reader. `connected?` reports handshake completion. `connect` is idempotent — a second call returns the cached result without contacting the server. After `close`, state is cleared and `connect` will handshake again. https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#initialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
MCP::Client::HTTP#connectto perform the MCP initialization handshake: sends theinitializerequest, followed by the requirednotifications/initializednotification, and caches the server'sInitializeResult(protocol version, capabilities, server info, instructions).connect(client_info:, protocol_version:, capabilities:)— idempotent; a second call returns the cached result without contacting the server. Afterclose, state is cleared andconnectwill handshake again.connected?— predicate set only after the full handshake completes (not mid-handshake), so a failed notification send leaves the transport in a consistent state.server_info— reader exposing the cachedInitializeResult.Replaces the previous pattern where callers had to construct the
initializerequest manually:Stacked on #326 — the diff here includes #326's commits; please merge #326 first, then this will show only the handshake commit.
Test plan
client_info/protocol_version/capabilities, idempotence, JSON-RPC error response, missingresult,connected?lifecycle throughclose, reconnect aftercloserake test— 775 runs, 1927 assertions, 0 failuresrubocopclean on touched filesexamples/streamable_http_server.rb: connect → tools → close → reconnect