Skip to content
Merged
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
8 changes: 4 additions & 4 deletions lib/mcp/server_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ def send_peer_cancellation(nested_request_id:, related_request_id: nil, reason:
def notify_elicitation_complete(elicitation_id:)
send_to_transport(Methods::NOTIFICATIONS_ELICITATION_COMPLETE, { elicitationId: elicitation_id })
rescue => e
@server.report_exception(e, notification: "elicitation_complete")
@server.configuration.exception_reporter.call(e, { notification: "elicitation_complete" })
end

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

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

send_to_transport(Methods::NOTIFICATIONS_PROGRESS, params, related_request_id: related_request_id)
rescue => e
@server.report_exception(e, notification: "progress")
@server.configuration.exception_reporter.call(e, { notification: "progress" })
end

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

send_to_transport(Methods::NOTIFICATIONS_MESSAGE, params, related_request_id: related_request_id)
rescue => e
@server.report_exception(e, { notification: "log_message" })
@server.configuration.exception_reporter.call(e, { notification: "log_message" })
end

private
Expand Down
29 changes: 29 additions & 0 deletions test/mcp/server_notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ def send_notification(method, params = nil)
assert_equal 4, call_count
end

test "ServerSession notification methods report transport errors through the server's exception reporter" do
transport = Class.new(MockTransport) do
def send_notification(method, params = nil)
raise StandardError, "Transport error"
end
end.new(@server)
session = ServerSession.new(server: @server, transport: transport)
session.configure_logging(MCP::LoggingMessageNotification.new(level: "error"))
reported = []
@server.configuration.exception_reporter = ->(exception, context) { reported << [exception.message, context] }

assert_nothing_raised do
session.notify_elicitation_complete(elicitation_id: "elicitation-1")
session.notify_resources_updated(uri: "file:///a.txt")
session.notify_progress(progress_token: "token-1", progress: 1)
session.notify_log_message(data: "message", level: "error")
end

assert_equal(
[
["Transport error", { notification: "elicitation_complete" }],
["Transport error", { notification: "resources_updated" }],
["Transport error", { notification: "progress" }],
["Transport error", { notification: "log_message" }],
],
reported,
)
end

test "multiple notification methods can be called in sequence" do
@server.notify_tools_list_changed
@server.notify_prompts_list_changed
Expand Down
Loading