Skip to content

Commit dcbbab9

Browse files
committed
fix(core-internal): cache AJV validators for schemas without $id
AjvJsonSchemaValidator recompiled schemas without an $id on every getValidator() call, and AJV retained each compiled validator forever. Long-running clients that periodically refresh their tool catalogue (e.g. via Client.listTools()) therefore grew the heap without bound until the process aborted (modelcontextprotocol#2605). Identical schemas are now cached by their JSON serialization, so each distinct schema compiles at most once. Schemas with an $id keep using the existing $id-based lookup.
1 parent 3924de9 commit dcbbab9

3 files changed

Lines changed: 196 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/core-internal': patch
3+
---
4+
5+
Fix a memory leak in `AjvJsonSchemaValidator`: schemas without an `$id` were recompiled on every `getValidator()` call and retained by the AJV engine forever, so long-running clients that periodically refresh their tool catalogue (e.g. via `Client.listTools()`) grew the heap without bound. Identical schemas are now cached by their JSON serialization, so each distinct schema compiles at most once.

‎packages/core-internal/src/validators/ajvProvider.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
8181
private _ajvDraft7: AjvLike | undefined;
8282
/** Lazy 2019-09 engine, built on the first 2019-09-declared schema. */
8383
private _ajv2019: AjvLike | undefined;
84+
/**
85+
* Compiled validators for schemas without a usable `$id`, keyed by the
86+
* schema's JSON serialization. AJV only deduplicates compilations by
87+
* `$id`; without this cache every `getValidator` call recompiles the same
88+
* schema and the engine retains each compiled validator forever, so
89+
* long-running clients that refresh their tool catalogue grow the heap
90+
* without bound (#2605).
91+
*/
92+
private readonly _compiledBySource = new Map<string, AjvValidateFunction>();
8493
/** True iff the constructor received a caller-supplied engine; the `$schema` dispatch is skipped. */
8594
private readonly _userAjv: boolean;
8695

@@ -133,7 +142,7 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
133142
const ajvValidator =
134143
'$id' in schema && typeof schema.$id === 'string'
135144
? (engine.getSchema(schema.$id) ?? engine.compile(schema))
136-
: engine.compile(schema);
145+
: this._compiledValidator(engine, schema);
137146

138147
return (input: unknown): JsonSchemaValidatorResult<T> => {
139148
const valid = ajvValidator(input);
@@ -151,6 +160,59 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
151160
};
152161
};
153162
}
163+
164+
/** Compile a schema, reusing the previous compilation for identical schemas. */
165+
private _compiledValidator(engine: AjvLike, schema: JsonSchemaType): AjvValidateFunction {
166+
const key = this._cacheKey(schema);
167+
if (key === undefined) {
168+
// Non-serializable schema (e.g. cyclic): skip the content cache and
169+
// let the engine compile it as before.
170+
return engine.compile(schema);
171+
}
172+
173+
const cached = this._compiledBySource.get(key);
174+
if (cached !== undefined) {
175+
return cached;
176+
}
177+
178+
// Compile a snapshot derived from the key, never the caller's object.
179+
// AJV caches compiled schemas by object identity, so if a caller
180+
// mutates its schema in place, compiling the live object could return
181+
// a stale validator that we would then store under the fresh key
182+
// (#2605).
183+
const compiled = engine.compile(JSON.parse(key) as JsonSchemaType);
184+
this._compiledBySource.set(key, compiled);
185+
return compiled;
186+
}
187+
188+
/**
189+
* Build a canonical cache key for a schema: JSON-serialized with object
190+
* keys recursively sorted, so structurally identical schemas (regardless
191+
* of key order or object identity) share one compiled validator. Returns
192+
* `undefined` for schemas that cannot be serialized.
193+
*/
194+
private _cacheKey(schema: JsonSchemaType): string | undefined {
195+
try {
196+
return JSON.stringify(sortJsonKeys(schema));
197+
} catch {
198+
return undefined;
199+
}
200+
}
201+
}
202+
203+
/** Recursively sort object keys so structurally equal JSON has one representation. */
204+
function sortJsonKeys(value: unknown): unknown {
205+
if (Array.isArray(value)) {
206+
return value.map(item => sortJsonKeys(item));
207+
}
208+
if (value !== null && typeof value === 'object') {
209+
return Object.fromEntries(
210+
Object.keys(value as Record<string, unknown>)
211+
.toSorted()
212+
.map(key => [key, sortJsonKeys((value as Record<string, unknown>)[key])])
213+
);
214+
}
215+
return value;
154216
}
155217

156218
/**
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { AjvJsonSchemaValidator } from '../../src/validators/ajvProvider';
4+
5+
/**
6+
* Regression tests for #2605: `getValidator()` must not recompile a schema
7+
* that has no `$id` on every call. Repeated `Client.listTools()` refreshes
8+
* call `getValidator` with structurally identical schemas; without caching,
9+
* each call compiles a fresh validator that the AJV engine retains forever,
10+
* so the heap grows without bound in long-running clients.
11+
*/
12+
13+
function makeFakeEngine() {
14+
let compiles = 0;
15+
let getSchemaCalls = 0;
16+
const engine = {
17+
compile: () => {
18+
compiles += 1;
19+
return Object.assign(() => true, { errors: undefined });
20+
},
21+
getSchema: () => {
22+
getSchemaCalls += 1;
23+
// eslint-disable-next-line unicorn/no-useless-undefined -- AjvLike.getSchema must return undefined
24+
return undefined;
25+
},
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27+
errorsText: (_errors?: any) => ''
28+
};
29+
return { engine, compileCount: () => compiles, getSchemaCount: () => getSchemaCalls };
30+
}
31+
32+
describe('AjvJsonSchemaValidator validator caching', () => {
33+
it('compiles a schema without $id only once across repeated getValidator calls', () => {
34+
const { engine, compileCount } = makeFakeEngine();
35+
const provider = new AjvJsonSchemaValidator(engine);
36+
const schema = { type: 'object', properties: { a: { type: 'string' } } };
37+
38+
provider.getValidator(schema);
39+
provider.getValidator(schema);
40+
41+
expect(compileCount()).toBe(1);
42+
});
43+
44+
it('hits the cache for structurally identical schemas with different object identity', () => {
45+
const { engine, compileCount } = makeFakeEngine();
46+
const provider = new AjvJsonSchemaValidator(engine);
47+
48+
provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } });
49+
provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } });
50+
51+
expect(compileCount()).toBe(1);
52+
});
53+
54+
it('compiles distinct schemas independently', () => {
55+
const { engine, compileCount } = makeFakeEngine();
56+
const provider = new AjvJsonSchemaValidator(engine);
57+
58+
provider.getValidator({ type: 'string' });
59+
provider.getValidator({ type: 'number' });
60+
provider.getValidator({ type: 'object' });
61+
62+
expect(compileCount()).toBe(3);
63+
});
64+
65+
it('shares one compilation for schemas that differ only in key order', () => {
66+
const { engine, compileCount } = makeFakeEngine();
67+
const provider = new AjvJsonSchemaValidator(engine);
68+
69+
provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } });
70+
provider.getValidator({ properties: { a: { type: 'string' } }, type: 'object' });
71+
72+
expect(compileCount()).toBe(1);
73+
});
74+
75+
it('is not poisoned when a caller mutates its schema object in place', () => {
76+
const { engine, compileCount } = makeFakeEngine();
77+
const provider = new AjvJsonSchemaValidator(engine);
78+
79+
const schema: Record<string, unknown> = {
80+
type: 'object',
81+
properties: { a: { type: 'string' } }
82+
};
83+
provider.getValidator(schema);
84+
expect(compileCount()).toBe(1);
85+
86+
// Mutate the same object in place — a fresh content key must trigger a
87+
// fresh compilation instead of reusing the stale validator for the
88+
// original content.
89+
schema.properties = { b: { type: 'number' } };
90+
provider.getValidator(schema);
91+
92+
expect(compileCount()).toBe(2);
93+
});
94+
95+
it('falls back to compiling without caching when a schema is not serializable', () => {
96+
const { engine, compileCount } = makeFakeEngine();
97+
const provider = new AjvJsonSchemaValidator(engine);
98+
99+
const cyclic: Record<string, unknown> = { type: 'object' };
100+
cyclic.self = cyclic;
101+
102+
expect(() => provider.getValidator(cyclic)).not.toThrow();
103+
expect(compileCount()).toBe(1);
104+
});
105+
106+
it('keeps using the $id-based lookup for schemas with an $id', () => {
107+
const { engine, compileCount, getSchemaCount } = makeFakeEngine();
108+
const provider = new AjvJsonSchemaValidator(engine);
109+
const schema = { $id: 'https://example.com/schema', type: 'string' };
110+
111+
provider.getValidator(schema);
112+
provider.getValidator(schema);
113+
114+
expect(getSchemaCount()).toBe(2);
115+
// getSchema always misses in the fake engine, so each call compiles once —
116+
// the $id path is unchanged by this fix.
117+
expect(compileCount()).toBe(2);
118+
});
119+
120+
it('returns a working validator after caching', () => {
121+
const provider = new AjvJsonSchemaValidator();
122+
const schema = { type: 'object', properties: { a: { type: 'string' } } };
123+
124+
const validate = provider.getValidator<{ a?: string }>(schema);
125+
expect(validate({ a: 'x' }).valid).toBe(true);
126+
expect(validate({ a: 1 }).valid).toBe(false);
127+
});
128+
});

0 commit comments

Comments
 (0)