Skip to content

Commit 49d6d7f

Browse files
authored
Merge pull request #435 from koic/fix_result_null_for_id_bearing_notification
Fix an incorrect `result: null` response to an id-bearing notification message
2 parents 5518e8d + 6d42cf7 commit 49d6d7f

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

lib/mcp/methods.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def initialize(method, capability)
4747
end
4848

4949
class << self
50+
def notification?(method)
51+
method.is_a?(String) && method.start_with?("notifications/")
52+
end
53+
5054
def ensure_capability!(method, capabilities)
5155
case method
5256
when PROMPTS_GET, PROMPTS_LIST

lib/mcp/server.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ def schema_contains_ref?(schema)
430430
end
431431

432432
def handle_request(request, method, session: nil, related_request_id: nil)
433+
# A well-formed notification carries no JSON-RPC id and receives no response.
434+
# If a client erroneously sends a notification-only method with an id, the message
435+
# is framed as a request; since notification methods have no request handler,
436+
# returning `nil` here makes `JsonRpcHandler` report "Method not found", matching
437+
# the TypeScript and Python SDKs rather than emitting a spurious `result: null`.
438+
return if Methods.notification?(method) && !related_request_id.nil?
439+
433440
# `notifications/cancelled` is dispatched directly: it is a notification (no JSON-RPC id)
434441
# and intentionally bypasses the `@handlers` lookup, capability check, in-flight registry,
435442
# and rescue blocks below.

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,38 @@ def string
8080
assert_equal({}, body["result"])
8181
end
8282

83+
test "id-bearing notification message is rejected with Method not found" do
84+
init_request = create_rack_request(
85+
"POST",
86+
"/",
87+
{ "CONTENT_TYPE" => "application/json" },
88+
{ jsonrpc: "2.0", method: "initialize", id: "init" }.to_json,
89+
)
90+
init_response = @transport.handle_request(init_request)
91+
session_id = init_response[1]["Mcp-Session-Id"]
92+
93+
request = create_rack_request(
94+
"POST",
95+
"/",
96+
{
97+
"CONTENT_TYPE" => "application/json",
98+
"HTTP_MCP_SESSION_ID" => session_id,
99+
},
100+
{ jsonrpc: "2.0", method: "notifications/cancelled", id: "123" }.to_json,
101+
)
102+
103+
response = @transport.handle_request(request)
104+
assert_equal 200, response[0]
105+
106+
io = StringIO.new
107+
response[2].call(io)
108+
body = JSON.parse(io.string.match(/^data: (.+)$/)[1])
109+
110+
assert_equal "123", body["id"]
111+
refute body.key?("result")
112+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, body["error"]["code"]
113+
end
114+
83115
test "handles POST request with invalid JSON" do
84116
request = create_rack_request(
85117
"POST",

test/mcp/server_cancellation_test.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,96 @@ def handle_request(request); end
144144
assert_nil response
145145
end
146146

147+
test "id-bearing notifications/cancelled is rejected with Method not found, not result: null" do
148+
response = @session.handle(
149+
jsonrpc: "2.0",
150+
id: 1001,
151+
method: Methods::NOTIFICATIONS_CANCELLED,
152+
)
153+
154+
assert_equal 1001, response[:id]
155+
refute response.key?(:result)
156+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, response.dig(:error, :code)
157+
end
158+
159+
test "id-bearing notifications/initialized is rejected with Method not found" do
160+
response = @session.handle(
161+
jsonrpc: "2.0",
162+
id: 1002,
163+
method: Methods::NOTIFICATIONS_INITIALIZED,
164+
)
165+
166+
assert_equal 1002, response[:id]
167+
refute response.key?(:result)
168+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, response.dig(:error, :code)
169+
end
170+
171+
test "id-bearing notifications/progress is rejected with Method not found" do
172+
response = @session.handle(
173+
jsonrpc: "2.0",
174+
id: 1003,
175+
method: Methods::NOTIFICATIONS_PROGRESS,
176+
params: { progressToken: "tok", progress: 1 },
177+
)
178+
179+
assert_equal 1003, response[:id]
180+
refute response.key?(:result)
181+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, response.dig(:error, :code)
182+
end
183+
184+
test "well-formed notifications/initialized still receives no response" do
185+
response = @session.handle(
186+
jsonrpc: "2.0",
187+
method: Methods::NOTIFICATIONS_INITIALIZED,
188+
)
189+
190+
assert_nil response
191+
end
192+
193+
test "id-bearing notification mixed into a batch yields only its Method not found error" do
194+
responses = @session.handle([
195+
{ jsonrpc: "2.0", method: Methods::NOTIFICATIONS_CANCELLED, params: { requestId: "x" } },
196+
{ jsonrpc: "2.0", id: 2001, method: Methods::NOTIFICATIONS_CANCELLED },
197+
{ jsonrpc: "2.0", id: 2002, method: Methods::PING },
198+
])
199+
200+
cancelled_error = responses.find { |response| response[:id] == 2001 }
201+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, cancelled_error.dig(:error, :code)
202+
203+
ping_response = responses.find { |response| response[:id] == 2002 }
204+
assert_equal({}, ping_response[:result])
205+
206+
# The well-formed notification carries no id and contributes no response.
207+
assert_equal 2, responses.size
208+
end
209+
210+
test "id-bearing custom notifications/* method is rejected with Method not found" do
211+
called = false
212+
@server.define_custom_method(method_name: "notifications/custom") { called = true }
213+
214+
response = @session.handle(
215+
jsonrpc: "2.0",
216+
id: 3001,
217+
method: "notifications/custom",
218+
)
219+
220+
assert_equal JsonRpcHandler::ErrorCode::METHOD_NOT_FOUND, response.dig(:error, :code)
221+
refute called
222+
end
223+
224+
test "well-formed custom notifications/* method is dispatched without a response" do
225+
called = false
226+
@server.define_custom_method(method_name: "notifications/custom") { called = true }
227+
228+
response = @session.handle(
229+
jsonrpc: "2.0",
230+
method: "notifications/custom",
231+
)
232+
233+
assert_nil response
234+
assert called
235+
end
236+
147237
test "duplicate cancellation for the same in-flight request is idempotent" do
148238
@server.define_tool(name: "slow_dup") do |server_context:|
149239
20.times do

0 commit comments

Comments
 (0)