From f0b77d9e8d63281173584379477fc32d806a7a5c Mon Sep 17 00:00:00 2001 From: jihtsan <15151999611@163.com> Date: Sun, 1 Nov 2026 21:08:01 +0800 Subject: [PATCH] fix: preserve dynamic reference output schema roots (#647) --- docs/_server/tools.md | 2 +- lib/mcp/tool/output_schema.rb | 2 +- test/mcp/server_test.rb | 21 ++++++++++++++++++ test/mcp/tool/output_schema_test.rb | 34 +++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/docs/_server/tools.md b/docs/_server/tools.md index e5902bef..06c20628 100644 --- a/docs/_server/tools.md +++ b/docs/_server/tools.md @@ -265,7 +265,7 @@ end ``` Please note: in this case, you must provide `type: "array"`. The default type for output schemas is `object`, -applied only when the schema declares no root keyword (`type`, `$ref`, `oneOf`, `anyOf`, `allOf`, `not`, `if`, `const`, `enum`). +applied only when the schema declares no root keyword (`type`, `$ref`, `$dynamicRef`, `oneOf`, `anyOf`, `allOf`, `not`, `if`, `const`, `enum`). Per SEP-2106, an output schema may be any valid JSON Schema 2020-12 document, including a primitive root (`{ type: "string" }`) or a root-level composition: diff --git a/lib/mcp/tool/output_schema.rb b/lib/mcp/tool/output_schema.rb index 1d6e2a58..119488cc 100644 --- a/lib/mcp/tool/output_schema.rb +++ b/lib/mcp/tool/output_schema.rb @@ -9,7 +9,7 @@ class ValidationError < StandardError; end # Root-level keywords whose presence means the user already chose a root schema shape, # so no `type: "object"` default should be merged in. - ROOT_SCHEMA_KEYWORDS = [:type, :"$ref", :oneOf, :anyOf, :allOf, :not, :if, :const, :enum].freeze + ROOT_SCHEMA_KEYWORDS = [:type, :"$ref", :"$dynamicRef", :oneOf, :anyOf, :allOf, :not, :if, :const, :enum].freeze def validate_result(result) errors = fully_validate(result) diff --git a/test/mcp/server_test.rb b/test/mcp/server_test.rb index 4cfa6456..72ec3d55 100644 --- a/test/mcp/server_test.rb +++ b/test/mcp/server_test.rb @@ -4321,6 +4321,27 @@ def server_context end end + test "#handle tools/call validates output against a root dynamic reference" do + server = Server.new( + name: "dynamic_output", + configuration: Configuration.new(validate_tool_call_results: true), + ) + server.define_tool( + name: "vector", + output_schema: { + "$defs": { vector: { "$dynamicAnchor": "vector", type: "array", items: { type: "number" } } }, + "$dynamicRef": "#vector", + }, + ) { Tool::Response.new(structured_content: [1, 2]) } + response = server.handle({ + jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "vector", arguments: {} }, + }) + + refute response.key?(:error) + assert_equal [1, 2], response.dig(:result, :structuredContent) + assert_equal "[1,2]", response.dig(:result, :content, 0, :text) + end + test "#handle tools/call mirrors non-object structuredContent when content is omitted or nil" do # Per SEP-2106, `structuredContent` may be any JSON value. Older clients may only read `content`, # so the server adds a serialized fallback when the tool provided no content blocks. diff --git a/test/mcp/tool/output_schema_test.rb b/test/mcp/tool/output_schema_test.rb index a9522ccc..5744dc28 100644 --- a/test/mcp/tool/output_schema_test.rb +++ b/test/mcp/tool/output_schema_test.rb @@ -238,6 +238,40 @@ class OutputSchemaTest < ActiveSupport::TestCase assert_raises(OutputSchema::ValidationError) { schema.validate_result(42) } end + [ + ["array", [1, 2], ["bad"]], + ["string", "result", 42], + ["number", 3.5, "bad"], + ["boolean", true, "true"], + ["null", nil, false], + ].each do |type, valid, invalid| + [false, true].each do |string_keys| + test "preserves a root $dynamicRef to #{type} with string keys #{string_keys}" do + target = { "$dynamicAnchor": "result", type: type } + target[:items] = { type: "number" } if type == "array" + definition = { "$defs": { result: target }, "$dynamicRef": "#result" } + definition = JSON.parse(JSON.generate(definition)) if string_keys + schema = OutputSchema.new(definition) + + refute schema.to_h.key?(:type) + assert_equal "#result", schema.to_h[:"$dynamicRef"] + assert_nothing_raised { schema.validate_result(valid) } + assert_raises(OutputSchema::ValidationError) { schema.validate_result(invalid) } + end + end + end + + test "preserves an explicit type constraint beside a root $dynamicRef" do + schema = OutputSchema.new( + "$defs": { result: { "$dynamicAnchor": "result", type: "array" } }, + "$dynamicRef": "#result", + type: "object", + ) + assert_equal "object", schema.to_h[:type] + assert_raises(OutputSchema::ValidationError) { schema.validate_result([1, 2]) } + assert_raises(OutputSchema::ValidationError) { schema.validate_result({}) } + end + test "allows primitive root schemas" do schema = OutputSchema.new(type: "string")