Skip to content

Add HTTP client connect for initialize handshake#327

Draft
atesgoral wants to merge 2 commits intomodelcontextprotocol:mainfrom
atesgoral:ag/client-connect-handshake
Draft

Add HTTP client connect for initialize handshake#327
atesgoral wants to merge 2 commits intomodelcontextprotocol:mainfrom
atesgoral:ag/client-connect-handshake

Conversation

@atesgoral
Copy link
Copy Markdown
Contributor

Summary

Adds MCP::Client::HTTP#connect to perform the MCP initialization handshake: sends the initialize request, followed by the required notifications/initialized notification, and caches the server's InitializeResult (protocol version, capabilities, server info, instructions).

  • connect(client_info:, protocol_version:, capabilities:) — idempotent; a second call returns the cached result without contacting the server. After close, state is cleared and connect will 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 cached InitializeResult.

Replaces the previous pattern where callers had to construct the initialize request manually:

transport = MCP::Client::HTTP.new(url: "https://api.example.com/mcp")
transport.connect
client = MCP::Client.new(transport: transport)

Stacked on #326 — the diff here includes #326's commits; please merge #326 first, then this will show only the handshake commit.

Test plan

  • New unit tests cover: happy path, default + custom client_info/protocol_version/capabilities, idempotence, JSON-RPC error response, missing result, connected? lifecycle through close, reconnect after close
  • rake test — 775 runs, 1927 assertions, 0 failures
  • rubocop clean on touched files
  • E2E validated against examples/streamable_http_server.rb: connect → tools → close → reconnect

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
@atesgoral atesgoral marked this pull request as draft April 20, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant