Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/mcp/server/transports/streamable_http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1903,17 +1903,23 @@ def deliver_to_listen_subscriptions(method, params)
push_to_stream(subscription[:stream], notification, subscription: subscription)
end
rescue *STREAM_WRITE_ERRORS => e
MCP.configuration.exception_reporter.call(
e,
{ subscription_id: subscription[:request_id], error: "Failed to send notification" },
)
report_stream_write_failure(e, { subscription_id: subscription[:request_id], error: "Failed to send notification" })

# Closed only when the removal is this path's own, as every other removal path does: the keepalive loop,
# a token expiry, or the teardown that removed the entry first closes the stream itself.
close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key)
end
end
end

# Reports a write that failed on one stream of a delivery. A reporter that raises is ignored here, unlike elsewhere in the transport:
# the exception would leave the failed stream registered and cost the streams the delivery has not reached yet the message.
def report_stream_write_failure(exception, context)
MCP.configuration.exception_reporter.call(exception, context)
rescue StandardError
nil
end

def remove_listen_subscription(subscription_key)
@mutex.synchronize do
subscription = @listen_subscriptions.delete(subscription_key)
Expand Down Expand Up @@ -3361,7 +3367,7 @@ def store_and_write(session_id, related_request_id, json, pushed: true)

written = true
rescue *STREAM_WRITE_ERRORS => e
MCP.configuration.exception_reporter.call(e, { session_id: session_id, error: "Failed to send message" })
report_stream_write_failure(e, { session_id: session_id, error: "Failed to send message" })

drop_broken_stream(session_id, stream, related_request_id)
end
Expand Down
36 changes: 36 additions & 0 deletions test/mcp/server/transports/streamable_http_transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8704,6 +8704,42 @@ def wait_until_blocked_or_done(thread)
assert_equal "listen-2", second_events[1].dig("params", "_meta", "io.modelcontextprotocol/subscriptionId")
end

test "an exception reporter that raises for a failed listen stream does not cost the other streams the notification" do
broken = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
healthy = open_listen_stream(id: "listen-2", notifications: { toolsListChanged: true })
broken.close

original_reporter = MCP.configuration.exception_reporter
MCP.configuration.exception_reporter = ->(_error, _context) { raise "reporter boom" }
begin
assert_nothing_raised { @transport.send_notification("notifications/tools/list_changed") }
ensure
MCP.configuration.exception_reporter = original_reporter
end

assert_equal "notifications/tools/list_changed", sse_events(healthy).last["method"]
assert_equal ["listen-2"], @transport.instance_variable_get(:@listen_subscriptions).values.map { |entry| entry[:request_id] }
end

test "an exception reporter that raises for a failed GET stream does not cost the other sessions the notification" do
broken = StringIO.new
healthy = StringIO.new
broken_session_id = open_sse_session(@transport, io: broken)
open_sse_session(@transport, io: healthy)
broken.close

original_reporter = MCP.configuration.exception_reporter
MCP.configuration.exception_reporter = ->(_error, _context) { raise "reporter boom" }
begin
assert_nothing_raised { @transport.send_notification("notifications/tools/list_changed") }
ensure
MCP.configuration.exception_reporter = original_reporter
end

assert_equal "notifications/tools/list_changed", sse_events(healthy).last["method"]
refute @transport.instance_variable_get(:@sessions).key?(broken_session_id)
end

test "subscriptions/listen closes gracefully with a SubscriptionsListenResult on transport close" do
io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })

Expand Down