From 934a5e8db4d31b44bcf70cf8001200995a2bc30d Mon Sep 17 00:00:00 2001 From: Tyler Rowsell Date: Mon, 9 Jun 2025 10:02:29 -0400 Subject: [PATCH] Remove rescue block and add test for error bubbling in InputSchema validation --- lib/mcp/tool/input_schema.rb | 20 ++++++++------------ test/mcp/tool/input_schema_test.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/mcp/tool/input_schema.rb b/lib/mcp/tool/input_schema.rb index 56510ef..4d1c46f 100644 --- a/lib/mcp/tool/input_schema.rb +++ b/lib/mcp/tool/input_schema.rb @@ -38,18 +38,14 @@ module MCP def validate_schema! schema = to_h - begin - schema_reader = JSON::Schema::Reader.new( - accept_uri: false, - accept_file: ->(path) { path.to_s.start_with?(Gem.loaded_specs["json-schema"].full_gem_path) }, - ) - metaschema = JSON::Validator.validator_for_name("draft4").metaschema - errors = JSON::Validator.fully_validate(metaschema, schema, schema_reader: schema_reader) - if errors.any? - raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}" - end - rescue StandardError => e - raise ArgumentError, "Invalid JSON Schema: #{e.message}" + schema_reader = JSON::Schema::Reader.new( + accept_uri: false, + accept_file: ->(path) { path.to_s.start_with?(Gem.loaded_specs["json-schema"].full_gem_path) }, + ) + metaschema = JSON::Validator.validator_for_name("draft4").metaschema + errors = JSON::Validator.fully_validate(metaschema, schema, schema_reader: schema_reader) + if errors.any? + raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}" end end end diff --git a/test/mcp/tool/input_schema_test.rb b/test/mcp/tool/input_schema_test.rb index 0dd5c4a..75d1446 100644 --- a/test/mcp/tool/input_schema_test.rb +++ b/test/mcp/tool/input_schema_test.rb @@ -51,6 +51,16 @@ module MCP schema.validate_arguments({ foo: 123 }) end end + + 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 + end + end end end end