Skip to content

Add HTTP client close for explicit session termination#326

Open
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
atesgoral:ag/client-session-termination
Open

Add HTTP client close for explicit session termination#326
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
atesgoral:ag/client-session-termination

Conversation

@atesgoral
Copy link
Copy Markdown
Contributor

Summary

Adds MCP::Client::HTTP#close to explicitly terminate a server session.

Per the Streamable HTTP spec, clients that no longer need a session SHOULD send an HTTP DELETE to the MCP endpoint with the Mcp-Session-Id header. The server MAY respond with 405 Method Not Allowed when it doesn't support client-initiated termination.

Behavior

  • Sends DELETE to the MCP endpoint with Mcp-Session-Id and MCP-Protocol-Version headers
  • Clears local session state (session_id, protocol_version) regardless of the server's response
  • 405 Method Not Allowed is treated as success
  • Other Faraday errors are swallowed so local cleanup always succeeds
  • No-op when no session has been established

Part of a stacked series

This is PR #4 of a series splitting #321 into small, self-contained changes:

Test plan

  • New unit tests cover: DELETE sent with session headers, local state cleared, no-op without session, tolerance of 405 and 5xx, idempotency
  • Full test suite passes (bundle exec rake test — 762 runs)
  • E2E verified against examples/streamable_http_server.rb: DELETE 200 from server, second request with the terminated session ID raises SessionExpiredError

🤖 Generated with Claude Code

@atesgoral atesgoral force-pushed the ag/client-session-termination branch from 9f681a1 to b56d607 Compare April 20, 2026 17:13
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
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