Schema validation: a pattern declined for one schema's compile budget stays declined for every later schema in the session
Which version line?
v2 — current (@modelcontextprotocol/inspector@latest)
Which client?
All / shared core
Inspector version
2.14.0 (released package; also v2/main @ 3b387d3)
Node version
v22.23.4
Operating system (and browser, for the web client)
Linux (Debian)
Transport
Not applicable / never connected
MCP server under inspection
Not server-specific. Reproduced directly against LinearPatternJsonSchemaValidator (core/mcp/linearPatternValidator.ts), which InspectorClient creates once (this.outputValidator ??= …) and shares for every tool's outputSchema check and every app-rendered elicitation for the life of the client.
Steps to reproduce
- One schema (tool A's
outputSchema) has 11 distinct patterns near the per-pattern program limit, e.g.^x0a{0,4990}$…^x10a{0,4990}$, plus^[a-z]+$. It exceedsSCHEMA_COMPILE_BUDGET, so^[a-z]+$compiles to{ ok: false, reason: "the patterns together exceed the compilation budget" }. That is expected for A. - A second, tiny schema (tool B):
{ "type": "string", "pattern": "^[a-z]+$" }. - Validate
"abc"against B on the same validator.
const v = new LinearPatternJsonSchemaValidator();
v.getValidator(schemaA)({ late: "abc" }); // A: invalid, budget exceeded (expected)
v.getValidator(schemaB)("abc"); // B: ???
Expected behavior
Schema B is compiled against its own fresh budget (as tracked() intends), so "abc" validates: { valid: true }. That's what a fresh validator returns for B alone.
Actual behavior
B is reported invalid with A's reason:
pattern "^[a-z]+$" was not evaluated (the patterns together exceed the compilation budget), so the value could not be verified against the schema
Cause: Ajv's usePattern registers each regex in the engine-wide ValueScope keyed by rx.toString(), which LinearPattern.toString() makes /${source}/${flags}. ValueScope.value() returns the first entry for an existing key, so the failed LinearPattern built while compiling A is the one every later schema on that Ajv engine uses for the same pattern. B's own compile call does run (and charges B's fresh budget), but its result is discarded.
Effect: one server's heavy outputSchema makes structured output of other tools, and app elicitation results, read as "could not be verified" for the rest of the session, with a reason that's false for those schemas. The per-schema budget becomes per-session for any pattern a budget-exhausted schema touched.
Logs, errors, or screenshots
Repro as a vitest file (core/mcp/tests): control (B alone) passes; B-after-A fails with the message above. The linear matcher itself looks right: a differential fuzz against native RegExp (~1.8M pattern×input comparisons, u and non-u) found no divergence.
Already prototyped a fix?
Not a PR. Possible fixes: don't let a budget-declined compilation become the scope entry, e.g. give LinearPattern.toString() a distinct key when !compiled.ok && reason === COMPILE_BUDGET_EXHAUSTED (so a later schema gets its own attempt), or keep a per-engine cache of successful compilations only.
Before you submit
- [x] I searched existing issues and this is not a duplicate.
- [x] This is not a security vulnerability report (those go through the private advisory process).
Triage: Priority Medium (total 6)
- Severity 3 — schemas checked later in the session wrongly report "could not be verified". It only happens after some other schema has used up its compile budget (several patterns close to the per-pattern limit), and reconnecting clears it.
- Urgency 2 — wanted eventually, nothing blocked on it
- Bonuses: +1
buglabel
Board: #28, Status Incoming (awaiting maintainer review — no milestone yet).
I prototyped a fix for this locally. Per CONTRIBUTING, I'm sharing the prompt, the before/after behaviour and how I verified it, rather than opening a PR.
Prompt used
In
core/mcp/linearPatternValidator.ts, Ajv'susePatternregisters each regex in the engine-wideValueScopeunderrx.toString(), andLinearPattern.toString()returns/${source}/${flags}. BecauseValueScope.value()returns the first entry for an existing key, aLinearPatternwhose compilation was declined withCOMPILE_BUDGET_EXHAUSTEDwhile compiling one over-budget schema is reused by every later schema on the same engine (#2934). Fix it so that only a budget-declined pattern gets a cache key of its own:
- Compute the key once, in the
LinearPatternconstructor.- Use
/${source}/${flags}when compilation succeeded, or when it was declined for a reason that doesn't depend on the budget (unsupported construct, too long, too complex).- Use that key plus a
#<n>suffix from a module-level counter when!compiled.ok && compiled.reason === COMPILE_BUDGET_EXHAUSTED.- Have
toString()return the stored key, and drop the now-unusedflagsfield.- Move the explanation of why the key matters onto the new field's doc comment. This replaces the old "per-schema pattern cache" comment, which is inaccurate because the cache is engine-wide.
- Do not change
tracked(), the budgets, orgetValidator.Add a test to the "bounded pattern compilation" block of
clients/web/src/test/core/mcp/linearPatternValidator.test.ts, on one provider:
- Validate an over-budget schema: the block's existing 1,000-pattern
propertiesplus alate: { pattern: "^[a-z]+$" }property. Assert that^[a-z]+$is reported as not evaluated for the budget.- Validate
{ type: "string", pattern: "^[a-z]+$" }on the same provider. Assert that"abc"gives{ valid: true, data: "abc", errorMessage: undefined }, and that"ABC"givesdata must match pattern "^[a-z]+$".
Before (v2/main @ 3b387d3): the second schema reports { valid: false } for "abc", with the first schema's reason: pattern "^[a-z]+$" was not evaluated (the patterns together exceed the compilation budget), so the value could not be verified against the schema.
After: the second schema compiles ^[a-z]+$ against its own budget. "abc" is valid, and "ABC" fails with the ordinary must match pattern error. Nothing else changes:
- The over-budget schema still reports its declined pattern.
- The existing test's re-check of the cached schema still reads as unverified.
- Patterns that compiled, or that are unsupported whatever the budget (lookaround, backreferences, too long), keep the shared key, so they are still compiled once per engine.
Verification
- The new test fails on v2/main (25 passed / 1 failed):
expected { valid: false, … } to deeply equal { valid: true, data: 'abc', … }. With the change it passes (26/26). vitest run --project=unitpasses 184/184 across the validator,linearRegExp,toolOutputValidation,appElicitationandinspectorClient-app-elicitationtest files.- Prettier is clean on both files. ESLint is clean on
core/mcp/linearPatternValidator.ts. tscpasses underclients/web/tsconfig.test.json, scoped to the test file and its imports.- I could not run the full
local:gate, the coverage gate, or ESLint across the whole web client: my sandbox ran out of memory. Both sides of the new ternary are exercised, though: compiled patterns by the existing tests, and the budget-declined path by the new test. - The change is about 15 lines in
core/mcp/linearPatternValidator.tsplus a 26-line test. I can share it as a DCO-signed branch if that would help.
One trade-off to weigh: each time an over-budget schema is recompiled, it now adds one small scope entry per declined pattern, where before all of them shared one entry. A new schema object gets recompiled, for example after tools/list_changed. The entries are tiny (LinearPattern objects with no compiled program), and their number per compile is bounded by the schema's pattern count. If you'd rather avoid even that, the alternative from the issue (a per-engine cache of successful compilations only) would remove it, at the cost of more code.
AI disclosure: I used an AI coding assistant to reproduce the bug, draft the change and tests, and write this comment. I reviewed the diff and the test results myself.
Fixed by #2969 (merged into v2/main as fa78edf).