ruby-sdk/test/mcp
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Koichi ITO 9d4dcbf1a4 Speed Up Tool::Schema Validation by 5x to 100x
## Motivation and Context

`MCP::Tool::Schema` previously used the pure-Ruby `json-schema` gem for
both construction-time metaschema validation and runtime argument /
result validation. For deep schemas this cost ~32-100ms per
construction (see issue #364).

`json_schemer` (https://rubygems.org/gems/json_schemer, v2.5.0) is
an actively maintained alternative that is much faster on this workload.
Switching reduces cold-start warming pressure in horizontally scaled
multi-tenant deployments (the scenario raised in #364) and brings
runtime data validation down to sub-millisecond per call once
the per-instance schemer is memoized.

The implementation preserves the legacy behavior of the `json-schema`
based code. Metaschema is pinned to draft-04 (`meta_schema:` option);
the `$schema` dialect URI is still emitted in `to_h` as 2020-12
but runtime validation continues to use draft-04, exactly as before.
Moving the runtime validator itself to 2020-12 is out of scope for
this PR and warrants its own discussion. `format` keywords are not enforced
(`format: false` option), matching what `json-schema`'s draft-04 path did.
Malformed schemas (e.g. an invalid `pattern` regex) continue to
surface as `ArgumentError, "Invalid JSON Schema: ..."`;
the `RegexpError` `json_schemer` raises during construction is wrapped.
`Symbol` values in argument data are coerced to strings inside
the internal `stringify` helper so they validate against `type: "string"`
the same way they did before.

The public `Schema#schema` accessor is dropped. It was a refactor
artifact from #198 (the consolidation of `InputSchema`'s
`properties` / `required` readers into a single hash) with no callers
in `lib/`; tests that needed the merged hash now read it through
`to_h`. The accessor's only remaining role would have been to expose
a mutation path that could desynchronize the memoized `@schemer`,
which this change removes.

Runtime dependency delta: drops `json-schema` (and `addressable`),
adds `json_schemer`, `hana`, `regexp_parser`, and `simpleidn`. Each added
gem is a single-purpose library tied to JSON Schema spec compliance.

Closes #364.

## How Has This Been Tested?

Side-by-side benchmark on representative schemas (simple, with `$ref` /
`additionalProperties`, nested, depth 20, depth 40):
construction-time metaschema validation is 4.7x to ~100x faster;
runtime data validation with the memoized schemer is sub-millisecond
across all sizes.

Four existing tests had their stub targets or message assertions updated
to track the new validator while preserving the original test intent.
The "unexpected errors bubble up" tests in `input_schema_test.rb`
and `output_schema_test.rb` now stub `JSONSchemer::Schema#validate`
instead of `JSON::Validator.fully_validate`. The cache tests in
`schema_test.rb` now stub `JSONSchemer::Schema#validate_schema`.
The two "detailed error message" tests in `tool_test.rb` switched from
asserting `json-schema`'s exact wording to format-agnostic substrings
(`"properties/count/minimum"`, `"number"`). The "required arguments are
converted to strings" test in `input_schema_test.rb` now reads the result
through `to_h[:required]` instead of the removed `schema` accessor.

Regression tests added for the legacy-behavior preservations above:
`format` keyword is not enforced, invalid `pattern` raises `ArgumentError`
(not `RegexpError`), and `Symbol` values validate against `type: "string"`.

## Breaking Changes

`Schema#schema` is no longer a public method. The accessor had no callers
outside the test suite, but anyone reading the internal representation
directly (rather than through `to_h`) will need to switch.
`Schema` / `InputSchema` / `OutputSchema` otherwise keep
their constructor signatures, `to_h` output, `ValidationError` /
`ArgumentError` raise points, and the `$schema` dialect emission.
The validator error message wording inside `ArgumentError` and
`ValidationError` now comes from `json_schemer` and still includes
the JSON pointer path and a description of the mismatch.
2026-06-01 21:47:30 +09:00
..
client Merge pull request #365 from koic/conformance_client_auth_offline_access_scope 2026-06-01 01:09:01 +09:00
prompt Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
resource Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
server Fix case-sensitive Accept header comparison 2026-05-23 21:36:27 +09:00
tool Speed Up Tool::Schema Validation by 5x to 100x 2026-06-01 21:47:30 +09:00
annotations_test.rb Replace assert_empty with assert_equal using explicit literals 2026-02-24 16:09:34 +09:00
cancellation_test.rb Support notifications/cancelled per MCP specification 2026-05-03 04:02:22 +09:00
client_test.rb fix: preserve client tool output schemas 2026-05-11 21:44:26 -04:00
configuration_test.rb feat: add opt-in tool output validation 2026-05-12 16:20:43 -04:00
content_test.rb Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
icon_test.rb Replace assert_empty with assert_equal using explicit literals 2026-02-24 16:09:34 +09:00
instrumentation_test.rb Add around_request hook for request instrumentation 2026-04-11 13:54:47 +09:00
logging_message_notification_test.rb Add logging support 2026-01-26 22:23:46 +09:00
methods_test.rb Add roots/list and notifications/roots/list_changed support 2026-04-15 02:39:14 +09:00
progress_test.rb Auto-set server.transport in Transport#initialize 2026-04-09 12:08:55 +09:00
prompt_test.rb Fix Prompt#validate_arguments! crash when arguments are nil 2026-03-01 17:21:41 +09:00
resource_template_test.rb Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
resource_test.rb Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
server_cancellation_test.rb Support notifications/cancelled per MCP specification 2026-05-03 04:02:22 +09:00
server_context_test.rb Support server-to-client ping per MCP specification 2026-05-22 11:28:51 +09:00
server_elicitation_test.rb Support notifications/cancelled per MCP specification 2026-05-03 04:02:22 +09:00
server_notification_test.rb Auto-set server.transport in Transport#initialize 2026-04-09 12:08:55 +09:00
server_progress_test.rb Auto-set server.transport in Transport#initialize 2026-04-09 12:08:55 +09:00
server_roots_test.rb Add roots/list and notifications/roots/list_changed support 2026-04-15 02:39:14 +09:00
server_sampling_test.rb Support notifications/cancelled per MCP specification 2026-05-03 04:02:22 +09:00
server_session_ping_test.rb Support server-to-client ping per MCP specification 2026-05-22 11:28:51 +09:00
server_test.rb Reject duplicate initialize requests (#350) 2026-05-18 00:00:23 -04:00
string_utils_test.rb Fix ReDoS vulnerability in StringUtils#underscore 2026-01-09 01:14:52 +09:00
tool_test.rb Speed Up Tool::Schema Validation by 5x to 100x 2026-06-01 21:47:30 +09:00
transport_test.rb Remove redundant require statements 2025-08-02 00:57:31 +09:00