Skip to content

Commit a449353

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 7b781ed commit a449353

6 files changed

Lines changed: 334 additions & 3 deletions

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 the validator providers: schemas without an `$id` were recompiled (or re-instantiated) on every `getValidator()` call and retained 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 canonical serialization (key order independent), each distinct schema compiles at most once, compiled snapshots defeat Ajv's identity-based cache on in-place mutation, and the cache is FIFO-bounded so schemas that genuinely come and go cannot accumulate forever. Applies to both `AjvJsonSchemaValidator` and `CfWorkerJsonSchemaValidator` (the latter keys by schema + draft).

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Ajv2020 } from 'ajv/dist/2020.js';
88
import _addFormats from 'ajv-formats';
99

1010
import { declaredDialect } from './dialects';
11+
import { canonicalJson, createBoundedCache, VALIDATOR_CACHE_LIMIT } from './schemaCache';
1112
import type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator, JsonSchemaValidatorResult } from './types';
1213

1314
/** Structural subset of the AJV interface used by {@link AjvJsonSchemaValidator}. */
@@ -81,6 +82,15 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
8182
private _ajvDraft7: AjvLike | undefined;
8283
/** Lazy 2019-09 engine, built on the first 2019-09-declared schema. */
8384
private _ajv2019: AjvLike | undefined;
85+
/**
86+
* Compiled validators for schemas without a usable `$id`, keyed by the
87+
* schema's canonical serialization and bounded to {@link VALIDATOR_CACHE_LIMIT}
88+
* distinct schemas. AJV only deduplicates compilations by `$id`; without
89+
* this cache every `getValidator` call recompiles the same schema and the
90+
* engine retains each compiled validator forever, so long-running clients
91+
* that refresh their tool catalogue grow the heap without bound (#2605).
92+
*/
93+
private readonly _compiledBySource = createBoundedCache<AjvValidateFunction>(VALIDATOR_CACHE_LIMIT);
8494
/** True iff the constructor received a caller-supplied engine; the `$schema` dispatch is skipped. */
8595
private readonly _userAjv: boolean;
8696

@@ -133,7 +143,7 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
133143
const ajvValidator =
134144
'$id' in schema && typeof schema.$id === 'string'
135145
? (engine.getSchema(schema.$id) ?? engine.compile(schema))
136-
: engine.compile(schema);
146+
: this._compiledValidator(engine, schema);
137147

138148
return (input: unknown): JsonSchemaValidatorResult<T> => {
139149
const valid = ajvValidator(input);
@@ -151,6 +161,30 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
151161
};
152162
};
153163
}
164+
165+
/** Compile a schema, reusing the previous compilation for identical schemas. */
166+
private _compiledValidator(engine: AjvLike, schema: JsonSchemaType): AjvValidateFunction {
167+
const key = canonicalJson(schema);
168+
if (key === undefined) {
169+
// Non-serializable schema (e.g. cyclic): skip the content cache and
170+
// let the engine compile it as before.
171+
return engine.compile(schema);
172+
}
173+
174+
const cached = this._compiledBySource.get(key);
175+
if (cached !== undefined) {
176+
return cached;
177+
}
178+
179+
// Compile a snapshot derived from the key, never the caller's object.
180+
// AJV caches compiled schemas by object identity, so if a caller
181+
// mutates its schema in place, compiling the live object could return
182+
// a stale validator that we would then store under the fresh key
183+
// (#2605).
184+
const compiled = engine.compile(JSON.parse(key) as JsonSchemaType);
185+
this._compiledBySource.set(key, compiled);
186+
return compiled;
187+
}
154188
}
155189

156190
/**

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import { Validator } from '@cfworker/json-schema';
1212

1313
import { declaredDialect } from './dialects';
14+
import { canonicalJson, createBoundedCache, VALIDATOR_CACHE_LIMIT } from './schemaCache';
1415
import type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator, JsonSchemaValidatorResult } from './types';
1516

1617
/**
@@ -52,6 +53,14 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
5253
private readonly shortcircuit: boolean;
5354
/** Caller-supplied draft; when set, the `$schema` check is skipped (caller owns dialect). */
5455
private readonly draft?: CfWorkerSchemaDraft;
56+
/**
57+
* Instantiated validators by canonical schema + draft, bounded to
58+
* {@link VALIDATOR_CACHE_LIMIT} distinct entries. `@cfworker/json-schema`
59+
* compiles a fresh `Validator` per construction, so without caching
60+
* repeated `getValidator` calls with identical schemas recompile each
61+
* time and retain every instance (#2605).
62+
*/
63+
private readonly _validators = createBoundedCache<Validator>(VALIDATOR_CACHE_LIMIT);
5564

5665
/**
5766
* Create a validator
@@ -87,8 +96,22 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
8796
*/
8897
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
8998
const draft = this.draft ?? this._draftFor(schema);
90-
// Cast to the cfworker Schema type - our JsonSchemaType is structurally compatible
91-
const validator = new Validator(schema as ConstructorParameters<typeof Validator>[0], draft, this.shortcircuit);
99+
const key = canonicalJson(schema);
100+
let validator: Validator;
101+
if (key === undefined) {
102+
// Non-serializable schema (e.g. cyclic): skip the cache entirely.
103+
validator = new Validator(schema as ConstructorParameters<typeof Validator>[0], draft, this.shortcircuit);
104+
} else {
105+
const cacheKey = `${draft}:${key}`;
106+
const cached = this._validators.get(cacheKey);
107+
if (cached === undefined) {
108+
// Cast to the cfworker Schema type - our JsonSchemaType is structurally compatible
109+
validator = new Validator(schema as ConstructorParameters<typeof Validator>[0], draft, this.shortcircuit);
110+
this._validators.set(cacheKey, validator);
111+
} else {
112+
validator = cached;
113+
}
114+
}
92115

93116
return (input: unknown): JsonSchemaValidatorResult<T> => {
94117
const result = validator.validate(input);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Shared bounded-cache helpers for validator providers.
3+
*
4+
* Both `AjvJsonSchemaValidator` and `CfWorkerJsonSchemaValidator` compile
5+
* (or instantiate) a validator per distinct schema. Without caching, repeated
6+
* calls with identical schemas recompile every time; without a bound, a
7+
* caller whose schemas genuinely evolve keeps every distinct schema ever
8+
* seen. See #2605.
9+
*/
10+
11+
/** Number of distinct schemas a provider keeps compiled before evicting the oldest. */
12+
export const VALIDATOR_CACHE_LIMIT = 1000;
13+
14+
/** Recursively sort object keys so structurally equal JSON has one representation. */
15+
export function sortJsonKeys(value: unknown): unknown {
16+
if (Array.isArray(value)) {
17+
return value.map(item => sortJsonKeys(item));
18+
}
19+
if (value !== null && typeof value === 'object') {
20+
return Object.fromEntries(
21+
Object.keys(value as Record<string, unknown>)
22+
.toSorted()
23+
.map(key => [key, sortJsonKeys((value as Record<string, unknown>)[key])])
24+
);
25+
}
26+
return value;
27+
}
28+
29+
/**
30+
* Canonical cache key for a schema: JSON-serialized with object keys
31+
* recursively sorted, so structurally identical schemas (regardless of key
32+
* order or object identity) share one entry. Returns `undefined` for schemas
33+
* that cannot be serialized (e.g. cyclic objects).
34+
*/
35+
export function canonicalJson(value: unknown): string | undefined {
36+
try {
37+
return JSON.stringify(sortJsonKeys(value));
38+
} catch {
39+
return undefined;
40+
}
41+
}
42+
43+
/**
44+
* FIFO-bounded string-keyed cache. Evicts the oldest entry once `limit` is
45+
* exceeded, so the cache cannot grow without bound. FIFO (rather than LRU) is
46+
* deliberate: schema catalogs are refreshed wholesale, so recency is not a
47+
* reliable signal of reuse.
48+
*/
49+
export function createBoundedCache<T>(limit: number): {
50+
get(key: string): T | undefined;
51+
set(key: string, value: T): void;
52+
} {
53+
const entries = new Map<string, T>();
54+
return {
55+
get(key: string): T | undefined {
56+
return entries.get(key);
57+
},
58+
set(key: string, value: T): void {
59+
entries.set(key, value);
60+
if (entries.size > limit) {
61+
const oldest = entries.keys().next();
62+
if (!oldest.done) {
63+
entries.delete(oldest.value);
64+
}
65+
}
66+
}
67+
};
68+
}
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+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { canonicalJson, createBoundedCache } from '../../src/validators/schemaCache';
4+
5+
describe('createBoundedCache', () => {
6+
it('returns undefined for missing keys', () => {
7+
const cache = createBoundedCache<string>(10);
8+
expect(cache.get('nope')).toBeUndefined();
9+
});
10+
11+
it('stores and returns values', () => {
12+
const cache = createBoundedCache<string>(10);
13+
cache.set('a', '1');
14+
expect(cache.get('a')).toBe('1');
15+
});
16+
17+
it('evicts the oldest entry beyond the limit (FIFO)', () => {
18+
const cache = createBoundedCache<string>(3);
19+
cache.set('a', '1');
20+
cache.set('b', '2');
21+
cache.set('c', '3');
22+
cache.set('d', '4'); // evicts 'a'
23+
24+
expect(cache.get('a')).toBeUndefined();
25+
expect(cache.get('b')).toBe('2');
26+
expect(cache.get('c')).toBe('3');
27+
expect(cache.get('d')).toBe('4');
28+
});
29+
30+
it('keeps only the most recent entries after repeated overflow', () => {
31+
const cache = createBoundedCache<string>(2);
32+
for (let i = 0; i < 5; i++) {
33+
cache.set(`k${i}`, String(i));
34+
}
35+
expect(cache.get('k0')).toBeUndefined();
36+
expect(cache.get('k3')).toBe('3');
37+
expect(cache.get('k4')).toBe('4');
38+
});
39+
40+
it('overwriting an existing key does not count as a new entry', () => {
41+
const cache = createBoundedCache<string>(2);
42+
cache.set('a', '1');
43+
cache.set('b', '2');
44+
cache.set('a', '1-updated'); // update, size still 2
45+
cache.set('c', '3'); // evicts 'a' (oldest insertion)
46+
47+
expect(cache.get('a')).toBeUndefined();
48+
expect(cache.get('b')).toBe('2');
49+
expect(cache.get('c')).toBe('3');
50+
});
51+
});
52+
53+
describe('canonicalJson', () => {
54+
it('serializes with recursively sorted keys', () => {
55+
expect(canonicalJson({ b: 1, a: { d: 4, c: 3 } })).toBe('{"a":{"c":3,"d":4},"b":1}');
56+
});
57+
58+
it('produces the same key for structurally identical schemas regardless of key order', () => {
59+
const first = canonicalJson({ type: 'object', properties: { a: { type: 'string' } } });
60+
const second = canonicalJson({ properties: { a: { type: 'string' } }, type: 'object' });
61+
expect(first).toBe(second);
62+
});
63+
64+
it('returns undefined for cyclic objects', () => {
65+
const cyclic: Record<string, unknown> = { type: 'object' };
66+
cyclic.self = cyclic;
67+
expect(canonicalJson(cyclic)).toBeUndefined();
68+
});
69+
70+
it('handles arrays', () => {
71+
expect(canonicalJson({ a: [1, { b: 2, a: 1 }] })).toBe('{"a":[1,{"a":1,"b":2}]}');
72+
});
73+
});

0 commit comments

Comments
 (0)