Merge pull request #369 from koic/switch_to_json_schemer

Speed Up `Tool::Schema` Validation by 5x to 100x
This commit is contained in:
Koichi ITO 2026-06-13 20:13:00 +09:00 • committed by GitHub
commit 39676bc93c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 81 additions and 42 deletions

View file

@ -7,7 +7,7 @@ module MCP
class InputSchemaTest < ActiveSupport::TestCase
test "required arguments are converted to strings" do
input_schema = InputSchema.new(properties: { message: { type: "string" } }, required: [:message])
assert_equal ["message"], input_schema.schema[:required]
assert_equal ["message"], input_schema.to_h[:required]
end
test "to_h returns a hash representation of the input schema" do
@ -139,10 +139,10 @@ module MCP
test "unexpected errors bubble up from validate_arguments" do
schema = InputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
JSON::Validator.stub(:fully_validate, ->(*) { raise "unexpected error" }) do
assert_raises(RuntimeError) do
schema.validate_arguments({ foo: "bar" })
end
JSONSchemer::Schema.any_instance.stubs(:validate).raises("unexpected error")
assert_raises(RuntimeError) do
schema.validate_arguments(foo: "bar")
end
end
@ -200,6 +200,26 @@ module MCP
schema6 = InputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"], additionalProperties: false)
refute_equal schema1, schema6
end
test "format keyword is not enforced (legacy behavior)" do
schema = InputSchema.new(
properties: { email: { type: "string", format: "email" } },
required: ["email"],
)
assert_nil(schema.validate_arguments(email: "not_an_email"))
end
test "invalid pattern raises ArgumentError, not RegexpError" do
error = assert_raises(ArgumentError) do
InputSchema.new(properties: { id: { type: "string", pattern: "[" } })
end
assert_includes error.message, "Invalid JSON Schema"
end
test "Symbol values in arguments are treated as strings" do
schema = InputSchema.new(properties: { foo: { type: "string" } }, required: ["foo"])
assert_nil(schema.validate_arguments(foo: :bar))
end
end
end
end

View file

@ -110,10 +110,10 @@ module MCP
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
JSONSchemer::Schema.any_instance.stubs(:validate).raises("unexpected error")
assert_raises(RuntimeError) do
schema.validate_result(foo: "bar")
end
end

View file

@ -10,7 +10,7 @@ module MCP
end
test "validates a schema once and reuses the result for identical schemas" do
JSON::Validator.expects(:fully_validate).once.returns([])
JSONSchemer::Schema.any_instance.expects(:validate_schema).once.returns([])
schema = { properties: { validates_once: { type: "string" } } }
InputSchema.new(schema)
@ -18,7 +18,7 @@ module MCP
end
test "validates distinct schemas separately" do
JSON::Validator.expects(:fully_validate).twice.returns([])
JSONSchemer::Schema.any_instance.expects(:validate_schema).twice.returns([])
InputSchema.new(properties: { distinct_a: { type: "string" } })
InputSchema.new(properties: { distinct_b: { type: "string" } })
@ -64,11 +64,11 @@ module MCP
break
end
JSON::Validator.stub(:fully_validate, []) do
assert_nothing_raised do
InputSchema.new(schema)
InputSchema.new(schema)
end
JSONSchemer::Schema.any_instance.stubs(:validate_schema).returns([])
assert_nothing_raised do
InputSchema.new(schema)
InputSchema.new(schema)
end
end

View file

@ -167,8 +167,8 @@ module MCP
end
assert_includes error.message, "Invalid JSON Schema"
assert_includes error.message, "#/properties/count/minimum"
assert_includes error.message, "string did not match the following type: number"
assert_includes error.message, "properties/count/minimum"
assert_includes error.message, "number"
end
test ".define allows definition of simple tools with a block" do
@ -431,8 +431,8 @@ module MCP
end
assert_includes error.message, "Invalid JSON Schema"
assert_includes error.message, "#/properties/count/minimum"
assert_includes error.message, "string did not match the following type: number"
assert_includes error.message, "properties/count/minimum"
assert_includes error.message, "number"
end
test "output_schema accepts $ref in schema" do