ruby-sdk/test/mcp/tool/output_schema_test.rb
Koichi ITO 1fc698bb8e Advertise JSON Schema 2020-12 dialect on emitted tool schemas
## Motivation and Context

MCP 2025-11-25 (SEP-1613) establishes JSON Schema 2020-12 as the default dialect
for MCP schema definitions.
 https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1613

The Ruby SDK was emitting tool input and output schemas with no `$schema` field,
leaving the dialect implicit and inconsistent with the TypeScript SDK
(`packages/core/src/util/standardSchema.ts`, which emits with `target: 'draft-2020-12'`)
and the Python SDK (which preserves `$schema` in its types and conformance fixtures).

`Tool::Schema#to_h` now includes `"$schema": "https://json-schema.org/draft/2020-12/schema"`
unless the user supplied a different `$schema` value, so clients see the canonical dialect URI
on the wire.

Runtime metaschema and argument validation remain on draft-04 because the `json-schema` gem
does not yet support 2020-12. Switching the runtime validator is left as a separate change
because it requires either replacing the gem or waiting for upstream 2020-12 support.

## How Has This Been Tested?

Updated `Tool::Schema#to_h` expectations across `input_schema_test`, `output_schema_test`,
`tool_test`, and the `tools/list` server tests to reflect the new `$schema` field.
Added focused regression tests:

- `to_h` preserves a user-supplied `$schema` URI rather than overwriting it
  (covered for both symbol-keyed input and the string-keyed Hash form that
  goes through the initializer's `symbolize_names` normalization)
- `validate_arguments` / `validate_result` continue to work when the user supplies
  a 2020-12 `$schema` URI on the schema (the validator strips the unknown top-level
  dialect URI before invoking the draft-04 validator, instead of raising `JSON::Schema::SchemaError`)
- `tools/list` JSON-RPC response includes the 2020-12 `$schema` on both `inputSchema`
  and `outputSchema` (transport-boundary assertion)

`bundle exec rake test` and `bundle exec rake rubocop` both pass.

## Breaking Changes

Additive change. Tolerant consumers of the `tools/list` payload are unaffected.
Consumers that strictly assert hash equality on the emitted `inputSchema` / `outputSchema`
need to account for the new `$schema` key, but no schema semantics change.

Schemas using 2020-12-only keywords (e.g. `prefixItems`, nested `$schema` on subschemas)
are still rejected by the draft-04 metaschema validator, as before.
Lifting that restriction depends on adopting a 2020-12-capable validator and is out of scope
for this change.
2026-05-11 13:44:30 +09:00

240 lines
8.1 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class Tool
class OutputSchemaTest < ActiveSupport::TestCase
test "to_h returns a hash representation of the output schema" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_equal(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: { result: { type: "string" } },
required: ["result"],
},
output_schema.to_h,
)
end
test "to_h preserves user-supplied $schema dialect" do
output_schema = OutputSchema.new(
"$schema": "https://json-schema.org/draft/2019-09/schema",
properties: { result: { type: "string" } },
)
assert_equal "https://json-schema.org/draft/2019-09/schema", output_schema.to_h[:"$schema"]
end
test "validate_result works when user supplies a 2020-12 $schema" do
output_schema = OutputSchema.new(
"$schema": "https://json-schema.org/draft/2020-12/schema",
properties: { result: { type: "string" } },
required: ["result"],
)
assert_nothing_raised do
output_schema.validate_result({ result: "success" })
end
assert_raises(OutputSchema::ValidationError) do
output_schema.validate_result({ result: 123 })
end
end
test "validate_result validates result against the schema" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_nothing_raised do
output_schema.validate_result({ result: "success" })
end
end
test "validate_result validates result with additional properties against the schema when additionalProperties set to nil (default)" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_nothing_raised do
output_schema.validate_result({ result: "success", extra: 123 })
end
end
test "validate_result validates result with additional properties against the schema when additionalProperties set to true" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"], additionalProperties: true)
assert_nothing_raised do
output_schema.validate_result({ result: "success", extra: 123 })
end
end
test "validate_result raises error with additional properties when additionalProperties set to false)" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"], additionalProperties: false)
assert_raises(OutputSchema::ValidationError) do
output_schema.validate_result({ result: "success", extra: 123 })
end
end
test "validate_result raises error for invalid result" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_raises(OutputSchema::ValidationError) do
output_schema.validate_result({ result: 123 })
end
end
test "validate_result raises error for missing required field" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_raises(OutputSchema::ValidationError) do
output_schema.validate_result({})
end
end
test "valid schema initialization" do
schema = OutputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
assert_equal(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: { foo: { type: "string" } },
required: ["foo"],
},
schema.to_h,
)
end
test "invalid schema raises argument error" do
assert_raises(ArgumentError) do
OutputSchema.new(properties: { foo: { type: "invalid_type" } }, required: ["foo"])
end
end
test "schema without required arguments is valid" do
assert_nothing_raised do
OutputSchema.new(properties: { foo: { type: "string" } })
end
end
test "unexpected errors bubble up from validate_result" do
schema = OutputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
JSON::Validator.stub(:fully_validate, ->(*) { raise "unexpected error" }) do
assert_raises(RuntimeError) do
schema.validate_result({ foo: "bar" })
end
end
end
test "accepts schemas with $ref references" do
schema = OutputSchema.new(
properties: {
foo: { type: "string" },
},
definitions: {
bar: { type: "string" },
},
required: ["foo"],
)
assert_includes schema.to_h.keys, :definitions
end
test "accepts schemas with $ref string key and includes $ref in to_h" do
schema = OutputSchema.new({
"properties" => {
"foo" => { "$ref" => "#/definitions/bar" },
},
"definitions" => {
"bar" => { "type" => "string" },
},
})
assert_equal "#/definitions/bar", schema.to_h[:properties][:foo][:$ref]
end
test "accepts schemas with $ref symbol key and includes $ref in to_h" do
schema = OutputSchema.new({
properties: {
foo: { :$ref => "#/definitions/bar" },
},
definitions: {
bar: { type: "string" },
},
})
assert_equal "#/definitions/bar", schema.to_h[:properties][:foo][:$ref]
end
test "== compares two output schemas with the same properties and required fields" do
schema1 = OutputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
schema2 = OutputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
assert_equal schema1, schema2
schema3 = OutputSchema.new(properties: { bar: { type: "string" } }, required: ["bar"])
refute_equal schema1, schema3
schema4 = OutputSchema.new(properties: { foo: { type: "string" } }, required: ["bar"])
refute_equal schema1, schema4
schema5 = OutputSchema.new(properties: { bar: { type: "string" } }, required: ["foo"])
refute_equal schema1, schema5
end
test "empty schema is valid" do
schema = OutputSchema.new
assert_equal(
{ "$schema": "https://json-schema.org/draft/2020-12/schema", type: "object" },
schema.to_h,
)
end
test "validates complex nested schemas" do
schema = OutputSchema.new(
properties: {
data: {
type: "object",
properties: {
items: { type: "array", items: { type: "string" } },
count: { type: "integer", minimum: 0 },
},
required: ["items"],
},
},
required: ["data"],
)
valid_result = {
data: {
items: ["item1", "item2"],
count: 2,
},
}
assert_nothing_raised do
schema.validate_result(valid_result)
end
invalid_result = {
data: {
items: [123, 456], # Should be strings
count: 2,
},
}
assert_raises(OutputSchema::ValidationError) do
schema.validate_result(invalid_result)
end
end
test "allow to declare array schemas" do
schema = OutputSchema.new({
type: "array",
items: {
properties: { foo: { type: "string" } },
required: ["foo"],
},
})
assert_equal(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
type: "array",
items: {
properties: { foo: { type: "string" } },
required: ["foo"],
},
},
schema.to_h,
)
end
end
end
end