Skip to content

Commit bd55244

Browse files
committed
Simplify Client#close since HTTP#delete handles errors internally
1 parent 4765528 commit bd55244

File tree

2 files changed

+2
-47
lines changed

2 files changed

+2
-47
lines changed

lib/mcp/client.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,11 @@ def get_prompt(name:)
147147
# Closes the connection with the MCP server.
148148
# For HTTP transport, this sends a DELETE request to terminate the session.
149149
# Session state is cleared regardless of whether the DELETE succeeds.
150+
# See: https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-termination
150151
def close
151152
return unless @session_id
152153

153-
begin
154-
transport.delete(headers: session_headers) if transport.respond_to?(:delete)
155-
rescue StandardError
156-
# Server may return 405 if it doesn't support session termination
157-
end
158-
154+
transport.delete(headers: session_headers)
159155
@session_id = nil
160156
@protocol_version = nil
161157
end

test/mcp/client_test.rb

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -401,47 +401,6 @@ def test_close_does_nothing_without_session
401401
assert_nil(client.session_id)
402402
end
403403

404-
def test_close_skips_delete_when_transport_lacks_method
405-
transport = mock
406-
407-
init_response = mock_response(
408-
body: { "result" => { "protocolVersion" => "2024-11-05" } },
409-
headers: { "mcp-session-id" => "session-123" },
410-
)
411-
transport.expects(:post).returns(init_response).once
412-
transport.stubs(:respond_to?).with(:delete).returns(false)
413-
414-
client = Client.new(transport: transport)
415-
client.connect(client_info: { name: "test", version: "1.0" })
416-
417-
# Should not raise, just clear state
418-
client.close
419-
420-
assert_nil(client.session_id)
421-
end
422-
423-
def test_close_rescues_errors_from_non_conforming_transports
424-
transport = mock
425-
426-
init_response = mock_response(
427-
body: { "result" => { "protocolVersion" => "2024-11-05" } },
428-
headers: { "mcp-session-id" => "session-123" },
429-
)
430-
transport.expects(:post).returns(init_response).once
431-
432-
client = Client.new(transport: transport)
433-
client.connect(client_info: { name: "test", version: "1.0" })
434-
435-
# Server returns 405 Method Not Allowed (doesn't support session termination)
436-
transport.expects(:delete).raises(Faraday::ClientError.new(nil, { status: 405 }))
437-
438-
# Should not raise, just clear state
439-
client.close
440-
441-
assert_nil(client.session_id)
442-
assert_nil(client.protocol_version)
443-
end
444-
445404
def test_session_id_not_overwritten_by_subsequent_responses
446405
transport = mock
447406

0 commit comments

Comments
 (0)