-
Notifications
You must be signed in to change notification settings - Fork 114
Cache Tool::Schema Validation to Avoid Re-validating Identical Schemas
#363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
koic
merged 1 commit into
modelcontextprotocol:main
from
koic:cache-tool-schema-validation
May 27, 2026
+137
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| module MCP | ||
| class Tool | ||
| class SchemaTest < ActiveSupport::TestCase | ||
| setup do | ||
| Schema::VALIDATION_CACHE.clear | ||
| end | ||
|
|
||
| test "validates a schema once and reuses the result for identical schemas" do | ||
| JSON::Validator.expects(:fully_validate).once.returns([]) | ||
|
|
||
| schema = { properties: { validates_once: { type: "string" } } } | ||
| InputSchema.new(schema) | ||
| InputSchema.new(schema) | ||
| end | ||
|
|
||
| test "validates distinct schemas separately" do | ||
| JSON::Validator.expects(:fully_validate).twice.returns([]) | ||
|
|
||
| InputSchema.new(properties: { distinct_a: { type: "string" } }) | ||
| InputSchema.new(properties: { distinct_b: { type: "string" } }) | ||
| end | ||
|
|
||
| test "a cache hit still yields a usable, validated schema" do | ||
| schema = { properties: { cache_hit: { type: "string" } }, required: ["cache_hit"] } | ||
| InputSchema.new(schema) | ||
| cached = InputSchema.new(schema) | ||
|
|
||
| assert_equal( | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| type: "object", | ||
| properties: { cache_hit: { type: "string" } }, | ||
| required: ["cache_hit"], | ||
| }, | ||
| cached.to_h, | ||
| ) | ||
| assert_nil(cached.validate_arguments(cache_hit: "value")) | ||
| assert_raises(InputSchema::ValidationError) do | ||
| cached.validate_arguments(cache_hit: 123) | ||
| end | ||
| end | ||
|
|
||
| test "an invalid schema raises every time and is not cached" do | ||
| invalid = { properties: { not_cached: { type: "invalid_type" } } } | ||
|
|
||
| assert_raises(ArgumentError) { InputSchema.new(invalid) } | ||
| assert_raises(ArgumentError) { InputSchema.new(invalid) } | ||
| end | ||
|
|
||
| test "a schema at the normalization depth limit is cached without a nesting error" do | ||
| # The deepest schema the initializer can still normalize via JSON.dump/parse. | ||
| # The cache key must tolerate the same depth; the default JSON.generate | ||
| # nesting limit (100) is stricter than normalization and would raise here. | ||
| schema = { properties: { leaf: { type: "string" } } } | ||
| loop do | ||
| candidate = { properties: { child: schema } } | ||
| JSON.parse(JSON.dump(candidate)) | ||
| schema = candidate | ||
| rescue JSON::NestingError | ||
| break | ||
| end | ||
|
|
||
| JSON::Validator.stub(:fully_validate, []) do | ||
| assert_nothing_raised do | ||
| InputSchema.new(schema) | ||
| InputSchema.new(schema) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test "ValidationCache evicts the oldest entry beyond its max size" do | ||
| cache = Schema::ValidationCache.new(max_size: 2) | ||
| cache.store("a") | ||
| cache.store("b") | ||
| cache.store("c") | ||
|
|
||
| refute cache.validated?("a") | ||
| assert cache.validated?("b") | ||
| assert cache.validated?("c") | ||
| end | ||
|
|
||
| test "ValidationCache#clear empties the cache" do | ||
| cache = Schema::ValidationCache.new | ||
| cache.store("a") | ||
| cache.clear | ||
|
|
||
| refute cache.validated?("a") | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of this being a constant this could be accepted as an additional argument in the initializer to allow custom cache implementations, like increasing the maximum size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I kept the cache internal on purpose: a goal of this PR, compared to the earlier
validate: falseidea, was to keep this as a transparent optimization without adding public API that would need long-term support.Since
Schemainstances are typically constructed internally by the tool DSL, an initializer argument would not be reachable in normal usage anyway, and the cache is intentionally process-wide and shared across instances.If cache-size tuning turns out to be necessary, adding it as a global
MCP.configurationoption (similar to the existingvalidate_tool_call_*settings) would probably make more sense than exposing it through the initializer.