Add logging support
A server can send structured logging messages to the client. https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#logging Logging was specified in the 2024-11-05 specification, but since it was not supported in ruby-sdk, I implemented it. https://modelcontextprotocol.io/specification/2024-11-05/server/utilities/logging I also made it possible to output a simple notification message in the examples.
This commit is contained in:
parent
830913be5a
commit
afa2fb4077
2 changed files with 434 additions and 7 deletions
|
|
@ -79,8 +79,12 @@ module MCP
|
|||
# Test resources notification
|
||||
@server.notify_resources_list_changed
|
||||
|
||||
# Test log notification
|
||||
@server.logging_message_notification = MCP::LoggingMessageNotification.new(level: "error")
|
||||
@server.notify_log_message(data: { error: "Connection Failed" }, level: "error")
|
||||
|
||||
# Check the notifications were sent
|
||||
assert_equal 3, @mock_stdout.output.size
|
||||
assert_equal 4, @mock_stdout.output.size
|
||||
|
||||
# Parse and verify each notification
|
||||
notifications = @mock_stdout.output.map { |msg| JSON.parse(msg) }
|
||||
|
|
@ -96,6 +100,10 @@ module MCP
|
|||
assert_equal "2.0", notifications[2]["jsonrpc"]
|
||||
assert_equal Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED, notifications[2]["method"]
|
||||
assert_nil notifications[2]["params"]
|
||||
|
||||
assert_equal "2.0", notifications[3]["jsonrpc"]
|
||||
assert_equal Methods::NOTIFICATIONS_MESSAGE, notifications[3]["method"]
|
||||
assert_equal({ "level" => "error", "data" => { "error" => "Connection Failed" } }, notifications[3]["params"])
|
||||
end
|
||||
|
||||
test "notifications include params when provided" do
|
||||
|
|
@ -120,6 +128,7 @@ module MCP
|
|||
@server.notify_tools_list_changed
|
||||
@server.notify_prompts_list_changed
|
||||
@server.notify_resources_list_changed
|
||||
@server.notify_log_message(data: { error: "Connection Failed" }, level: "error")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -240,6 +249,16 @@ module MCP
|
|||
assert_equal 2, @mock_stdout.output.size
|
||||
second_notification = JSON.parse(@mock_stdout.output.last)
|
||||
assert_equal Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED, second_notification["method"]
|
||||
|
||||
# Set log level and notify
|
||||
@server.logging_message_notification = MCP::LoggingMessageNotification.new(level: "error")
|
||||
|
||||
# Manually trigger notification
|
||||
@server.notify_log_message(data: { error: "Connection Failed" }, level: "error")
|
||||
assert_equal 3, @mock_stdout.output.size
|
||||
third_notification = JSON.parse(@mock_stdout.output.last)
|
||||
assert_equal Methods::NOTIFICATIONS_MESSAGE, third_notification["method"]
|
||||
assert_equal({ "data" => { "error" => "Connection Failed" }, "level" => "error" }, third_notification["params"])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -51,6 +51,12 @@ module MCP
|
|||
# Test resources notification
|
||||
@server.notify_resources_list_changed
|
||||
|
||||
# Set log level to error for log notification
|
||||
@server.logging_message_notification = MCP::LoggingMessageNotification.new(level: "error")
|
||||
|
||||
# Test log notification
|
||||
@server.notify_log_message(data: { error: "Connection Failed" }, level: "error")
|
||||
|
||||
# Check the notifications were received
|
||||
io.rewind
|
||||
output = io.read
|
||||
|
|
@ -58,6 +64,7 @@ module MCP
|
|||
assert_includes output, "data: {\"jsonrpc\":\"2.0\",\"method\":\"#{Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED}\"}"
|
||||
assert_includes output, "data: {\"jsonrpc\":\"2.0\",\"method\":\"#{Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED}\"}"
|
||||
assert_includes output, "data: {\"jsonrpc\":\"2.0\",\"method\":\"#{Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED}\"}"
|
||||
assert_includes output, "data: {\"jsonrpc\":\"2.0\",\"method\":\"#{Methods::NOTIFICATIONS_MESSAGE}\",\"params\":{\"data\":{\"error\":\"Connection Failed\"},\"level\":\"error\"}}\n\n"
|
||||
end
|
||||
|
||||
test "notifications are broadcast to all connected sessions" do
|
||||
|
|
@ -147,6 +154,7 @@ module MCP
|
|||
@server.notify_tools_list_changed
|
||||
@server.notify_prompts_list_changed
|
||||
@server.notify_resources_list_changed
|
||||
@server.notify_log_message(data: { error: "Connection Failed" }, level: "error")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue