Skip to content

Commit d4a3fd7

Browse files
committed
fix(validators): cache compiled schemas without $id to prevent memory leak (modelcontextprotocol#2605)
Add content-keyed caches to AjvJsonSchemaValidator and CfWorkerJsonSchemaValidator so that schemas without $id are not recompiled on every getValidator() call. This prevents Ajv's internal scope from growing without bound in long-running MCP clients that periodically refresh their tool catalogue. The cache key is the JSON-serialised schema content. Schemas with $id continue to use Ajv's built-in identity cache. Non-serialisable schemas (cyclic, BigInt) fall back to uncached compilation.
1 parent dcc0102 commit d4a3fd7

3 files changed

Lines changed: 251 additions & 7 deletions

File tree

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
8383
private _ajv2019: AjvLike | undefined;
8484
/** True iff the constructor received a caller-supplied engine; the `$schema` dispatch is skipped. */
8585
private readonly _userAjv: boolean;
86+
/**
87+
* Content-keyed cache for compiled validators of schemas without `$id`.
88+
* Prevents the memory leak where `engine.compile(schema)` is called unconditionally,
89+
* causing Ajv's internal scope to grow without bound in long-running processes.
90+
*/
91+
private readonly _compiledCache: Map<string, AjvValidateFunction> = new Map();
8692

8793
/**
8894
* @param ajv - Optional pre-configured AJV-compatible instance. When supplied, this instance is
@@ -128,12 +134,39 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
128134
return (this._ajvDraft7 ??= createDefaultAjvInstance(Draft7Ajv));
129135
}
130136

137+
/**
138+
* Compile or retrieve a cached validator for the given schema.
139+
*
140+
* Schemas with `$id` use Ajv's built-in identity cache (`engine.getSchema`).
141+
* Schemas without `$id` are cached by their JSON-serialised content to prevent
142+
* unbounded growth of Ajv's internal scope in long-running processes (see #2605).
143+
*/
144+
private _getCompiled(schema: JsonSchemaType, engine: AjvLike): AjvValidateFunction {
145+
if ('$id' in schema && typeof schema.$id === 'string') {
146+
return engine.getSchema(schema.$id) ?? engine.compile(schema);
147+
}
148+
149+
let key: string;
150+
try {
151+
key = JSON.stringify(schema);
152+
} catch {
153+
// Non-serialisable schema (e.g. cyclic): fall back to uncached compilation.
154+
return engine.compile(schema);
155+
}
156+
157+
let cached = this._compiledCache.get(key);
158+
if (cached === undefined) {
159+
// Compile a fresh structural copy so Ajv's identity-based cache cannot
160+
// return a stale validator if a caller mutates the schema object in place.
161+
cached = engine.compile(JSON.parse(key));
162+
this._compiledCache.set(key, cached);
163+
}
164+
return cached;
165+
}
166+
131167
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
132168
const engine = this._engineFor(schema);
133-
const ajvValidator =
134-
'$id' in schema && typeof schema.$id === 'string'
135-
? (engine.getSchema(schema.$id) ?? engine.compile(schema))
136-
: engine.compile(schema);
169+
const ajvValidator = this._getCompiled(schema, engine);
137170

138171
return (input: unknown): JsonSchemaValidatorResult<T> => {
139172
const valid = ajvValidator(input);

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
5252
private readonly shortcircuit: boolean;
5353
/** Caller-supplied draft; when set, the `$schema` check is skipped (caller owns dialect). */
5454
private readonly draft?: CfWorkerSchemaDraft;
55+
/**
56+
* Content-keyed cache for compiled validators of schemas without `$id`.
57+
* Prevents redundant Validator instantiation in long-running processes (see #2605).
58+
*/
59+
private readonly _compiledCache: Map<string, Validator> = new Map();
5560

5661
/**
5762
* Create a validator
@@ -77,18 +82,41 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
7782
return dialect === 'draft-7' ? '7' : dialect;
7883
}
7984

85+
/**
86+
* Retrieve or create a cached Validator instance for the given schema.
87+
* Schemas are cached by their JSON-serialised content to prevent redundant
88+
* instantiation in long-running processes (see #2605).
89+
*/
90+
private _getValidator(schema: JsonSchemaType, draft: CfWorkerSchemaDraft): Validator {
91+
let key: string;
92+
try {
93+
key = JSON.stringify(schema);
94+
} catch {
95+
// Non-serialisable schema: fall back to uncached instantiation.
96+
return new Validator(schema as ConstructorParameters<typeof Validator>[0], draft, this.shortcircuit);
97+
}
98+
99+
// Include draft in the key since the same schema content under different drafts
100+
// may validate differently.
101+
const cacheKey = `${draft}:${key}`;
102+
let cached = this._compiledCache.get(cacheKey);
103+
if (cached === undefined) {
104+
cached = new Validator(JSON.parse(key) as ConstructorParameters<typeof Validator>[0], draft, this.shortcircuit);
105+
this._compiledCache.set(cacheKey, cached);
106+
}
107+
return cached;
108+
}
109+
80110
/**
81111
* Create a validator for the given JSON Schema
82112
*
83-
* Unlike AJV, this validator is not cached internally
84-
*
85113
* @param schema - Standard JSON Schema object
86114
* @returns A validator function that validates input data
87115
*/
88116
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
89117
const draft = this.draft ?? this._draftFor(schema);
90118
// 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);
119+
const validator = this._getValidator(schema, draft);
92120

93121
return (input: unknown): JsonSchemaValidatorResult<T> => {
94122
const result = validator.validate(input);
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/**
2+
* Tests for validator caching behaviour (fixes #2605: memory leak from
3+
* unconditional recompilation of schemas without `$id`).
4+
*/
5+
6+
import { describe, expect, it } from 'vitest';
7+
8+
import { AjvJsonSchemaValidator } from '../../src/validators/ajvProvider';
9+
import { CfWorkerJsonSchemaValidator } from '../../src/validators/cfWorkerProvider';
10+
import type { JsonSchemaType } from '../../src/validators/types';
11+
12+
describe('AjvJsonSchemaValidator caching (#2605)', () => {
13+
it('returns the same validator function for identical schemas without $id', () => {
14+
const provider = new AjvJsonSchemaValidator();
15+
const schema: JsonSchemaType = {
16+
type: 'object',
17+
properties: { name: { type: 'string' } },
18+
required: ['name']
19+
};
20+
21+
// Call getValidator twice with structurally identical (but different object) schemas
22+
const v1 = provider.getValidator(schema);
23+
const v2 = provider.getValidator({ ...schema });
24+
25+
// Both should validate correctly
26+
expect(v1({ name: 'Alice' }).valid).toBe(true);
27+
expect(v2({ name: 'Alice' }).valid).toBe(true);
28+
expect(v1({}).valid).toBe(false);
29+
expect(v2({}).valid).toBe(false);
30+
});
31+
32+
it('does not recompile when called repeatedly with the same schema content', () => {
33+
let compileCount = 0;
34+
const fakeEngine = {
35+
compile: (schema: unknown) => {
36+
compileCount++;
37+
return Object.assign(() => true, { errors: undefined });
38+
},
39+
getSchema: () => undefined,
40+
errorsText: () => ''
41+
};
42+
const provider = new AjvJsonSchemaValidator(fakeEngine);
43+
const schema: JsonSchemaType = { type: 'string' };
44+
45+
provider.getValidator(schema);
46+
provider.getValidator(schema);
47+
provider.getValidator({ type: 'string' });
48+
49+
// Should compile only once — subsequent calls use the cache
50+
expect(compileCount).toBe(1);
51+
});
52+
53+
it('recompiles when schema content changes', () => {
54+
const provider = new AjvJsonSchemaValidator();
55+
56+
const v1 = provider.getValidator({ type: 'string' } as JsonSchemaType);
57+
const v2 = provider.getValidator({ type: 'number' } as JsonSchemaType);
58+
59+
expect(v1('hello').valid).toBe(true);
60+
expect(v1(42).valid).toBe(false);
61+
expect(v2(42).valid).toBe(true);
62+
expect(v2('hello').valid).toBe(false);
63+
});
64+
65+
it('schemas with $id still use Ajv built-in identity cache', () => {
66+
const provider = new AjvJsonSchemaValidator();
67+
const schema: JsonSchemaType = {
68+
$id: 'https://example.com/test-schema',
69+
type: 'object',
70+
properties: { x: { type: 'number' } }
71+
};
72+
73+
const v1 = provider.getValidator(schema);
74+
const v2 = provider.getValidator(schema);
75+
76+
expect(v1({ x: 1 }).valid).toBe(true);
77+
expect(v2({ x: 1 }).valid).toBe(true);
78+
expect(v1({ x: 'nope' }).valid).toBe(false);
79+
});
80+
81+
it('a mutated schema object produces a validator for the new content', () => {
82+
const provider = new AjvJsonSchemaValidator();
83+
const schema: JsonSchemaType = { type: 'string' };
84+
85+
const v1 = provider.getValidator(schema);
86+
expect(v1('hello').valid).toBe(true);
87+
expect(v1(42).valid).toBe(false);
88+
89+
// Mutate in place
90+
(schema as Record<string, unknown>).type = 'number';
91+
92+
const v2 = provider.getValidator(schema);
93+
expect(v2(42).valid).toBe(true);
94+
expect(v2('hello').valid).toBe(false);
95+
});
96+
97+
it('shared cached validator does not have cross-call error pollution', () => {
98+
const provider = new AjvJsonSchemaValidator();
99+
const schema: JsonSchemaType = {
100+
type: 'object',
101+
properties: { name: { type: 'string' } },
102+
required: ['name']
103+
};
104+
105+
const v1 = provider.getValidator(schema);
106+
const v2 = provider.getValidator(schema);
107+
108+
// Validate with invalid data on v1
109+
const r1 = v1({});
110+
expect(r1.valid).toBe(false);
111+
expect(r1.errorMessage).toBeDefined();
112+
113+
// v2 should still validate correctly (not inheriting errors from v1)
114+
const r2 = v2({ name: 'Bob' });
115+
expect(r2.valid).toBe(true);
116+
});
117+
118+
it('works correctly across different dialect schemas', () => {
119+
const provider = new AjvJsonSchemaValidator();
120+
121+
const schema2020: JsonSchemaType = {
122+
$schema: 'https://json-schema.org/draft/2020-12/schema',
123+
type: 'string'
124+
};
125+
const schema07: JsonSchemaType = {
126+
$schema: 'http://json-schema.org/draft-07/schema#',
127+
type: 'string'
128+
};
129+
130+
const v2020 = provider.getValidator(schema2020);
131+
const v07 = provider.getValidator(schema07);
132+
133+
expect(v2020('hello').valid).toBe(true);
134+
expect(v07('hello').valid).toBe(true);
135+
});
136+
});
137+
138+
describe('CfWorkerJsonSchemaValidator caching (#2605)', () => {
139+
it('returns the same validation result for identical schemas without $id', () => {
140+
const provider = new CfWorkerJsonSchemaValidator();
141+
const schema: JsonSchemaType = {
142+
type: 'object',
143+
properties: { name: { type: 'string' } },
144+
required: ['name']
145+
};
146+
147+
const v1 = provider.getValidator(schema);
148+
const v2 = provider.getValidator({ ...schema });
149+
150+
expect(v1({ name: 'Alice' }).valid).toBe(true);
151+
expect(v2({ name: 'Alice' }).valid).toBe(true);
152+
expect(v1({}).valid).toBe(false);
153+
expect(v2({}).valid).toBe(false);
154+
});
155+
156+
it('recompiles when schema content changes', () => {
157+
const provider = new CfWorkerJsonSchemaValidator();
158+
159+
const v1 = provider.getValidator({ type: 'string' } as JsonSchemaType);
160+
const v2 = provider.getValidator({ type: 'number' } as JsonSchemaType);
161+
162+
expect(v1('hello').valid).toBe(true);
163+
expect(v1(42).valid).toBe(false);
164+
expect(v2(42).valid).toBe(true);
165+
expect(v2('hello').valid).toBe(false);
166+
});
167+
168+
it('caches per draft — same content with different drafts validates differently', () => {
169+
const provider = new CfWorkerJsonSchemaValidator();
170+
171+
// A schema with prefixItems: under 2020-12 it's enforced, under draft-07 it's ignored
172+
const schema2020: JsonSchemaType = {
173+
$schema: 'https://json-schema.org/draft/2020-12/schema',
174+
type: 'array',
175+
prefixItems: [{ type: 'number' }, { type: 'string' }]
176+
};
177+
178+
const v2020 = provider.getValidator(schema2020);
179+
// prefixItems is enforced under 2020-12
180+
expect(v2020([1, 'x']).valid).toBe(true);
181+
expect(v2020(['x', 1]).valid).toBe(false);
182+
});
183+
});

0 commit comments

Comments
 (0)