Skip to content

Commit 934a5e8

Browse files
committed
Remove rescue block and add test for error bubbling in InputSchema validation
1 parent 600c96f commit 934a5e8

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/mcp/tool/input_schema.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ def validate_arguments(arguments)
3838

3939
def validate_schema!
4040
schema = to_h
41-
begin
42-
schema_reader = JSON::Schema::Reader.new(
43-
accept_uri: false,
44-
accept_file: ->(path) { path.to_s.start_with?(Gem.loaded_specs["json-schema"].full_gem_path) },
45-
)
46-
metaschema = JSON::Validator.validator_for_name("draft4").metaschema
47-
errors = JSON::Validator.fully_validate(metaschema, schema, schema_reader: schema_reader)
48-
if errors.any?
49-
raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}"
50-
end
51-
rescue StandardError => e
52-
raise ArgumentError, "Invalid JSON Schema: #{e.message}"
41+
schema_reader = JSON::Schema::Reader.new(
42+
accept_uri: false,
43+
accept_file: ->(path) { path.to_s.start_with?(Gem.loaded_specs["json-schema"].full_gem_path) },
44+
)
45+
metaschema = JSON::Validator.validator_for_name("draft4").metaschema
46+
errors = JSON::Validator.fully_validate(metaschema, schema, schema_reader: schema_reader)
47+
if errors.any?
48+
raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}"
5349
end
5450
end
5551
end

test/mcp/tool/input_schema_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ class InputSchemaTest < ActiveSupport::TestCase
5151
schema.validate_arguments({ foo: 123 })
5252
end
5353
end
54+
55+
test "unexpected errors bubble up from validate_arguments" do
56+
schema = InputSchema.new(properties: { foo: { type: "string" } }, required: [:foo])
57+
58+
JSON::Validator.stub(:fully_validate, ->(*) { raise "unexpected error" }) do
59+
assert_raises(RuntimeError) do
60+
schema.validate_arguments({ foo: "bar" })
61+
end
62+
end
63+
end
5464
end
5565
end
5666
end

0 commit comments

Comments
 (0)