Skip to content

fix(validators): reuse compiled validators for schemas without $id - #3260

Open
Devpaul-01 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Devpaul-01:fix/ajv-validator-cache-no-id
Open

Devpaul-01 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Devpaul-01:fix/ajv-validator-cache-no-id

Conversation

@Devpaul-01

Copy link
Copy Markdown

Motivation and Context

AjvJsonSchemaValidator.getValidator() only looked up an existing compiled validator when the schema declared a string $id. Every other schema went straight to engine.compile(), and AJV keeps each compiled schema in its internal cache for the lifetime of the engine. The provider holds one long-lived engine, so compiling an equal schema again retains another copy each time. Schemas received over the wire are freshly parsed objects, so AJV's own identity-keyed cache never hits for them.

This addresses the memory growth described in #2605 on main. One correction to the issue's framing: on current main, Client.listTools() no longer calls getValidator() for every tool on every call (the response cache sits in front of it). The same unbounded growth still happens on other paths that call it with a schema that has no $id:

  • Server.elicitInput form validation compiles requestedSchema on every accepted response.
  • Client.callTool(..., { toolDefinition }) compiles the supplied definition in isolation on every call.
  • A client that re-fetches its tool list after list_changed compiles every outputSchema again.

Fixing it in the provider covers all of them with one small change.

What changed

getValidator() now reuses the compiled validator for a schema without $id, keyed by the schema's JSON text (_compileWithoutId). The $id path is untouched.

How Has This Been Tested?

Measured on main with 50 distinct tool-like schemas passed as new objects over 40 simulated refreshes, forcing GC before each heap reading:

Ajv _cache entries after 40 refreshes Heap growth
before 2008 +10.5 MB
after 58 (flat from round 1) +0.9 MB (flat)
  • New ajvCompileReuse.test.ts: the first test fails on main (expected "compile" to be called 1 times, but got 5 times) and passes with the fix (verified by reverting only ajvProvider.ts).
  • core-internal: 1862 tests passed. client: 1116 passed. server: 725 passed.
  • ESLint, Prettier and tsc --noEmit pass.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Additional context

  • The cache is keyed by content and is not bounded. A schema that changes on every call still adds one entry per distinct schema, as before. If you prefer a size bound, I'm happy to add one.
  • Targets main. The issue also reports the same pattern in v1.x; I can send that backport as a separate small PR.
  • A changeset is included.

AjvJsonSchemaValidator.getValidator() only looked up an existing compiled
validator when the schema declared a string $id. Every other schema went
straight to engine.compile(), and AJV keeps each compiled schema in its
internal cache for the life of the engine. Tool schemas rarely carry an
$id, and Client.listTools() builds validators for every outputSchema on
each call, so a client that refreshes its tool list kept growing the
engine's cache by one entry per tool per refresh.

Cache the compiled validator by the schema's JSON text, so a schema that
is re-fetched as a new but equal object reuses the validator instead of
compiling (and retaining) another one.
Asserts that an equal schema arriving as a new object compiles once, that
schemas with different content stay separate, and that schemas with an $id
keep going through the engine's own lookup.
@Devpaul-01
Devpaul-01 requested a review from a team as a code owner December 1, 2026 07:31
@changeset-bot

changeset-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6043a4b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/client Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/core-internal Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3260

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3260

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3260

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3260

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3260

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3260

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3260

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3260

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3260

commit: 6043a4b

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Dec 1, 2026
@prkomb

prkomb commented Dec 2, 2026

Copy link
Copy Markdown

Linking the earlier PRs that cache compiled validators, so they can be looked at together. #2626 (9 Aug) and #2950 (4 Oct) are still open with Fixes #2605 and the same idea: compiled Ajv validators cached by JSON.stringify(schema). #2626 also compiles a copy of the schema and caches the cfworker Validator objects, and #2950 keeps one map per engine and puts $id schemas through it too. #2674 (16 Aug, draft) was opened for schemas that share an $id, but it also caches schemas without $id by body.

This PR changes only the no-$id path in AjvJsonSchemaValidator.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants