From 7c1aa6ea497d3c330fb106e9dbd70797cbf72664 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Mon, 21 Jul 2025 09:44:47 +0200 Subject: [PATCH 1/2] implements the notifications/initialized method - According to https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle, "After successful initialization, the client MUST send an initialized notification to indicate it is ready to begin normal operations". - This implementation just replies with an empty response (same as ping). --- lib/mcp/server.rb | 1 + test/mcp/server_test.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 2753589..8eed40a 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -66,6 +66,7 @@ def initialize( Methods::PROMPTS_GET => method(:get_prompt), Methods::INITIALIZE => method(:init), Methods::PING => ->(_) { {} }, + Methods::NOTIFICATIONS_INITIALIZED => ->(_) { {} }, # No op handlers for currently unsupported methods Methods::RESOURCES_SUBSCRIBE => ->(_) {}, diff --git a/test/mcp/server_test.rb b/test/mcp/server_test.rb index 3e5853f..d564d4b 100644 --- a/test/mcp/server_test.rb +++ b/test/mcp/server_test.rb @@ -141,6 +141,25 @@ class ServerTest < ActiveSupport::TestCase assert_instrumentation_data({ method: "unsupported_method" }) end + test "#handle notifications/initialized returns empty response" do + request = { + jsonrpc: "2.0", + method: "notifications/initialized", + id: "123", + } + + response = @server.handle(request) + assert_equal( + { + jsonrpc: "2.0", + id: "123", + result: {}, + }, + response, + ) + assert_instrumentation_data({ method: "notifications/initialized" }) + end + test "#handle tools/list returns available tools" do request = { jsonrpc: "2.0", From c6063209d87a3e270fecf026f62b61005dce750c Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 22 Jul 2025 09:11:31 +0200 Subject: [PATCH 2/2] do not respond to notifications/initialized - as per https://www.jsonrpc.org/specification#notification --- lib/mcp/server.rb | 2 +- test/mcp/server_test.rb | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 8eed40a..7cb0367 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -66,7 +66,7 @@ def initialize( Methods::PROMPTS_GET => method(:get_prompt), Methods::INITIALIZE => method(:init), Methods::PING => ->(_) { {} }, - Methods::NOTIFICATIONS_INITIALIZED => ->(_) { {} }, + Methods::NOTIFICATIONS_INITIALIZED => ->(_) {}, # No op handlers for currently unsupported methods Methods::RESOURCES_SUBSCRIBE => ->(_) {}, diff --git a/test/mcp/server_test.rb b/test/mcp/server_test.rb index d564d4b..ee0792b 100644 --- a/test/mcp/server_test.rb +++ b/test/mcp/server_test.rb @@ -141,22 +141,23 @@ class ServerTest < ActiveSupport::TestCase assert_instrumentation_data({ method: "unsupported_method" }) end - test "#handle notifications/initialized returns empty response" do + test "#handle notifications/initialized returns nil response" do request = { jsonrpc: "2.0", method: "notifications/initialized", - id: "123", } - response = @server.handle(request) - assert_equal( - { - jsonrpc: "2.0", - id: "123", - result: {}, - }, - response, - ) + assert_nil @server.handle(request) + assert_instrumentation_data({ method: "notifications/initialized" }) + end + + test "#handle_json notifications/initialized returns nil response" do + request = JSON.generate({ + jsonrpc: "2.0", + method: "notifications/initialized", + }) + + assert_nil @server.handle_json(request) assert_instrumentation_data({ method: "notifications/initialized" }) end