Skip to content

Commit fc41dae

Browse files
authored
Merge pull request #631 from koic/report_failed_session_notifications_through_the_server_configuration
Report failed session notifications through the server configuration
2 parents ec6501b + a7bd967 commit fc41dae

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎lib/mcp/server_session.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ def send_peer_cancellation(nested_request_id:, related_request_id: nil, reason:
275275
def notify_elicitation_complete(elicitation_id:)
276276
send_to_transport(Methods::NOTIFICATIONS_ELICITATION_COMPLETE, { elicitationId: elicitation_id })
277277
rescue => e
278-
@server.report_exception(e, notification: "elicitation_complete")
278+
@server.configuration.exception_reporter.call(e, { notification: "elicitation_complete" })
279279
end
280280

281281
# Sends a resource updated notification to this session only.
282282
def notify_resources_updated(uri:)
283283
send_to_transport(Methods::NOTIFICATIONS_RESOURCES_UPDATED, { "uri" => uri })
284284
rescue => e
285-
@server.report_exception(e, notification: "resources_updated")
285+
@server.configuration.exception_reporter.call(e, { notification: "resources_updated" })
286286
end
287287

288288
# Sends a progress notification to this session only.
@@ -296,7 +296,7 @@ def notify_progress(progress_token:, progress:, total: nil, message: nil, relate
296296

297297
send_to_transport(Methods::NOTIFICATIONS_PROGRESS, params, related_request_id: related_request_id)
298298
rescue => e
299-
@server.report_exception(e, notification: "progress")
299+
@server.configuration.exception_reporter.call(e, { notification: "progress" })
300300
end
301301

302302
# Sends a log message notification to this session only.
@@ -320,7 +320,7 @@ def notify_log_message(data:, level:, logger: nil, related_request_id: nil)
320320

321321
send_to_transport(Methods::NOTIFICATIONS_MESSAGE, params, related_request_id: related_request_id)
322322
rescue => e
323-
@server.report_exception(e, { notification: "log_message" })
323+
@server.configuration.exception_reporter.call(e, { notification: "log_message" })
324324
end
325325

326326
private

‎test/mcp/server_notification_test.rb‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,35 @@ def send_notification(method, params = nil)
257257
assert_equal 4, call_count
258258
end
259259

260+
test "ServerSession notification methods report transport errors through the server's exception reporter" do
261+
transport = Class.new(MockTransport) do
262+
def send_notification(method, params = nil)
263+
raise StandardError, "Transport error"
264+
end
265+
end.new(@server)
266+
session = ServerSession.new(server: @server, transport: transport)
267+
session.configure_logging(MCP::LoggingMessageNotification.new(level: "error"))
268+
reported = []
269+
@server.configuration.exception_reporter = ->(exception, context) { reported << [exception.message, context] }
270+
271+
assert_nothing_raised do
272+
session.notify_elicitation_complete(elicitation_id: "elicitation-1")
273+
session.notify_resources_updated(uri: "file:///a.txt")
274+
session.notify_progress(progress_token: "token-1", progress: 1)
275+
session.notify_log_message(data: "message", level: "error")
276+
end
277+
278+
assert_equal(
279+
[
280+
["Transport error", { notification: "elicitation_complete" }],
281+
["Transport error", { notification: "resources_updated" }],
282+
["Transport error", { notification: "progress" }],
283+
["Transport error", { notification: "log_message" }],
284+
],
285+
reported,
286+
)
287+
end
288+
260289
test "multiple notification methods can be called in sequence" do
261290
@server.notify_tools_list_changed
262291
@server.notify_prompts_list_changed

0 commit comments

Comments
 (0)