Skip to content

Add HTTP client connect for initialize handshake#1

Closed
atesgoral wants to merge 1 commit intoag/client-session-terminationfrom
ag/client-connect-handshake
Closed

Add HTTP client connect for initialize handshake#1
atesgoral wants to merge 1 commit intoag/client-session-terminationfrom
ag/client-connect-handshake

Conversation

@atesgoral
Copy link
Copy Markdown
Owner

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. E.g.:

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

Stacked on modelcontextprotocol#326; will re-target to main once that merges.

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 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
Copy link
Copy Markdown
Owner Author

Reopening on upstream — this is the fork-local copy used only for visualizing the stack.

@atesgoral atesgoral closed this Apr 20, 2026
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