diff --git a/lib/mcp/server/transports/streamable_http_transport.rb b/lib/mcp/server/transports/streamable_http_transport.rb index 19c33187..fdfb7bcb 100644 --- a/lib/mcp/server/transports/streamable_http_transport.rb +++ b/lib/mcp/server/transports/streamable_http_transport.rb @@ -1903,10 +1903,8 @@ 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) @@ -1914,6 +1912,14 @@ def deliver_to_listen_subscriptions(method, params) 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) @@ -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 diff --git a/test/mcp/server/transports/streamable_http_transport_test.rb b/test/mcp/server/transports/streamable_http_transport_test.rb index 2ea899d4..d05d441b 100644 --- a/test/mcp/server/transports/streamable_http_transport_test.rb +++ b/test/mcp/server/transports/streamable_http_transport_test.rb @@ -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 })