Skip to content

fix: cache compiled validators in AjvJsonSchemaValidator to prevent memory leak - #2806

Closed
KooshaPari wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
KooshaPari:fix-2605-ajv-memory-leak
Closed

fix: cache compiled validators in AjvJsonSchemaValidator to prevent memory leak#2806
KooshaPari wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
KooshaPari:fix-2605-ajv-memory-leak

Conversation

@KooshaPari

Copy link
Copy Markdown

Fixes #2605

…emory leak

Fixes #2605. `getValidator()` called `this._ajv.compile(schema)` on every
invocation for schemas without ``, which is the common case for MCP
tool schemas. Each compilation is retained by Ajv's internal scope
forever, causing unbounded heap growth in long-running clients that
periodically refresh their tool catalogue.

Adds a content-keyed cache (`Map<string, compiled>`) for schemas without
``. Schemas with `` continue to use Ajv's built-in cache. The cache
compiles a fresh structural copy (`JSON.parse(key)`) to avoid stale results
from Ajv's identity cache if a caller mutates the schema object in place.

Adds 4 tests verifying caching behavior: duplicate calls produce
consistent results, same-content schemas share validators, different
schemas get different validators, and each unique schema compiles only
once.
@KooshaPari
KooshaPari requested a review from a team as a code owner September 13, 2026 09:40
Copilot AI lite review requested due to automatic review settings September 13, 2026 09:40
@changeset-bot

changeset-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 74b4303

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pkg-pr-new

pkg-pr-new Bot commented Sep 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@2806

commit: 74b4303

@KooshaPari KooshaPari closed this by deleting the head repository Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants