diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 8a20d125..333543ec 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -35,8 +35,6 @@ class Server UNSUPPORTED_PROPERTIES_UNTIL_2025_06_18 = [:description, :icons].freeze UNSUPPORTED_PROPERTIES_UNTIL_2025_03_26 = [:title, :websiteUrl].freeze - DEFAULT_COMPLETION_RESULT = { completion: { values: [], hasMore: false } }.freeze - # Servers return an array of completion values ranked by relevance, with maximum 100 items per response. # https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/completion#completion-results MAX_COMPLETION_VALUES = 100 @@ -295,7 +293,7 @@ def initialize( Methods::NOTIFICATIONS_INITIALIZED => ->(_) {}, Methods::NOTIFICATIONS_PROGRESS => ->(_) {}, Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED => ->(_) {}, - Methods::COMPLETION_COMPLETE => ->(_) { DEFAULT_COMPLETION_RESULT }, + Methods::COMPLETION_COMPLETE => ->(_) {}, Methods::LOGGING_SET_LEVEL => method(:configure_logging_level), } # The protocol's own request methods, as opposed to the notifications above and to custom methods @@ -2308,10 +2306,8 @@ def validate_completion_params!(params) end def normalize_completion_result(result) - return DEFAULT_COMPLETION_RESULT unless result.is_a?(Hash) - - completion = result[:completion] || result["completion"] - return DEFAULT_COMPLETION_RESULT unless completion.is_a?(Hash) + completion = result[:completion] || result["completion"] if result.is_a?(Hash) + completion = {} unless completion.is_a?(Hash) values = completion[:values] || completion["values"] || [] total = completion[:total] || completion["total"] diff --git a/test/mcp/server_test.rb b/test/mcp/server_test.rb index c0835d55..0bbe9386 100644 --- a/test/mcp/server_test.rb +++ b/test/mcp/server_test.rb @@ -3645,7 +3645,7 @@ def read_resource_request(uri) server.handle({ jsonrpc: "2.0", method: "initialize", id: 1, params: initialize_params }) server.handle({ jsonrpc: "2.0", method: "notifications/initialized" }) - response = server.handle({ + request = { jsonrpc: "2.0", id: 2, method: "completion/complete", @@ -3653,7 +3653,9 @@ def read_resource_request(uri) ref: { type: "ref/prompt", name: "test" }, argument: { name: "arg", value: "val" }, }, - }) + } + response = server.handle(request) + next_response = server.handle(request.merge(id: 3)) assert_equal( { @@ -3663,6 +3665,10 @@ def read_resource_request(uri) }, response, ) + completion = response.fetch(:result).fetch(:completion) + next_completion = next_response.fetch(:result).fetch(:completion) + refute_same(completion, next_completion) + refute_same(completion.fetch(:values), next_completion.fetch(:values)) end test "#handle completion/complete with custom handler for ref/prompt" do @@ -4023,7 +4029,7 @@ def read_resource_request(uri) server.handle({ jsonrpc: "2.0", method: "initialize", id: 1, params: initialize_params }) server.handle({ jsonrpc: "2.0", method: "notifications/initialized" }) - response = server.handle({ + request = { jsonrpc: "2.0", id: 2, method: "completion/complete", @@ -4031,7 +4037,9 @@ def read_resource_request(uri) ref: { type: "ref/prompt", name: "test" }, argument: { name: "arg", value: "" }, }, - }) + } + response = server.handle(request) + next_response = server.handle(request.merge(id: 3)) assert_equal( { @@ -4041,6 +4049,10 @@ def read_resource_request(uri) }, response, ) + completion = response.fetch(:result).fetch(:completion) + next_completion = next_response.fetch(:result).fetch(:completion) + refute_same(completion, next_completion) + refute_same(completion.fetch(:values), next_completion.fetch(:values)) end test "#handle completion/complete with string-keyed handler result" do