Skip to content

Commit 086e6f4

Browse files
committed
fix(validators): reuse compiled Ajv validators instead of recompiling per call
Ajv adds every compiled validator to the engine's scope and holds it for the engine's lifetime. `getValidator()` only consulted Ajv's cache when the schema carried an `$id`, so the common case — a tool schema without one — called `ajv.compile()` on every invocation. That matters because `Client.listTools()` calls `getValidator()` for each tool `outputSchema` on every `tools/list` response. Schemas arrive freshly parsed, so object identity never repeats and Ajv's `$id` cache cannot hit. A long-running client that periodically refreshed its tool catalogue therefore recompiled the same schemas indefinitely, each compilation retained by the engine, growing the heap without bound. `_cachedToolOutputValidators` is keyed by tool name, so the map stayed bounded and the leak was invisible: the previous validator became unreachable from the map while still being held. Validators are now cached per engine, keyed by the serialised schema. Distinct schemas still compile separately, and dialect engines stay isolated because the cache is keyed on the engine instance. Fixes modelcontextprotocol#2605.
1 parent c05225a commit 086e6f4

3 files changed

Lines changed: 107 additions & 4 deletions

File tree

‎.changeset/ajv-validator-reuse.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modelcontextprotocol/client': patch
3+
'@modelcontextprotocol/server': patch
4+
---
5+
6+
`AjvJsonSchemaValidator.getValidator()` no longer recompiles a schema it has already compiled. Ajv retains every compiled validator for the lifetime of its engine, so a long-running client that periodically refreshed its tool catalogue grew its heap without bound; tool schemas typically carry no `$id`, which meant Ajv's own cache never hit. Validators are now reused per engine, keyed by the serialised schema.

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ 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+
/** Compiled validators per engine, keyed by serialised schema — see {@linkcode getValidator}. */
87+
private readonly _validatorCache = new WeakMap<AjvLike, Map<string, AjvValidateFunction>>();
8688

8789
/**
8890
* @param ajv - Optional pre-configured AJV-compatible instance. When supplied, this instance is
@@ -128,12 +130,31 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
128130
return (this._ajvDraft7 ??= createDefaultAjvInstance(Draft7Ajv));
129131
}
130132

133+
/**
134+
* Compile `schema`, reusing the validator from a previous identical call.
135+
*
136+
* Ajv adds every compiled validator to the engine's scope and holds it for the engine's
137+
* lifetime, so recompiling the same schema grows the heap without bound. Schemas arrive
138+
* freshly parsed on every `tools/list`, so object identity never repeats and Ajv's own
139+
* `$id` cache misses for the common case of a schema without `$id` — hence keying on the
140+
* serialised schema, which is stable for a given server's output. (#2605)
141+
*/
131142
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
132143
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);
144+
let cache = this._validatorCache.get(engine);
145+
if (!cache) {
146+
cache = new Map();
147+
this._validatorCache.set(engine, cache);
148+
}
149+
const key = JSON.stringify(schema);
150+
let ajvValidator = cache.get(key);
151+
if (!ajvValidator) {
152+
ajvValidator =
153+
'$id' in schema && typeof schema.$id === 'string'
154+
? (engine.getSchema(schema.$id) ?? engine.compile(schema))
155+
: engine.compile(schema);
156+
cache.set(key, ajvValidator);
157+
}
137158

138159
return (input: unknown): JsonSchemaValidatorResult<T> => {
139160
const valid = ajvValidator(input);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { AjvJsonSchemaValidator } from '../../src/validators/ajvProvider';
4+
5+
/** Counting stand-in for an Ajv instance, so compile calls are observable. */
6+
function countingAjv() {
7+
const state = {
8+
compiles: 0,
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
compile: () => {
11+
state.compiles += 1;
12+
return Object.assign(() => true, { errors: undefined });
13+
},
14+
getSchema: () => undefined,
15+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16+
errorsText: (_errors?: any) => ''
17+
};
18+
return state;
19+
}
20+
21+
describe('AjvJsonSchemaValidator validator reuse', () => {
22+
it('compiles an identical schema only once', () => {
23+
const ajv = countingAjv();
24+
const provider = new AjvJsonSchemaValidator(ajv);
25+
const schema = { type: 'object' as const, properties: { a: { type: 'string' } } };
26+
27+
provider.getValidator(schema);
28+
provider.getValidator(schema);
29+
provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } });
30+
31+
expect(ajv.compiles).toBe(1);
32+
});
33+
34+
it('treats a freshly parsed but identical schema as the same schema', () => {
35+
const ajv = countingAjv();
36+
const provider = new AjvJsonSchemaValidator(ajv);
37+
38+
// What a client does on every tools/list: re-parse the tool catalogue.
39+
for (let i = 0; i < 5; i += 1) {
40+
provider.getValidator(JSON.parse('{"type":"object","properties":{"a":{"type":"string"}}}'));
41+
}
42+
43+
expect(ajv.compiles).toBe(1);
44+
});
45+
46+
it('still compiles distinct schemas separately', () => {
47+
const ajv = countingAjv();
48+
const provider = new AjvJsonSchemaValidator(ajv);
49+
50+
provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } });
51+
provider.getValidator({ type: 'object', properties: { a: { type: 'number' } } });
52+
53+
expect(ajv.compiles).toBe(2);
54+
});
55+
56+
it('caches per dialect engine rather than across them', () => {
57+
const provider = new AjvJsonSchemaValidator();
58+
const validate = provider.getValidator<{ a?: string }>({ type: 'object', properties: { a: { type: 'string' } } });
59+
60+
expect(validate({ a: 'x' }).valid).toBe(true);
61+
expect(validate({ a: 1 }).valid).toBe(false);
62+
});
63+
64+
it('returns a validator that keeps reporting the right verdict', () => {
65+
const provider = new AjvJsonSchemaValidator();
66+
const schema = { type: 'object' as const, properties: { a: { type: 'string' } } };
67+
const first = provider.getValidator<{ a?: string }>(schema);
68+
const second = provider.getValidator<{ a?: string }>(schema);
69+
70+
for (const validate of [first, second]) {
71+
expect(validate({ a: 'x' }).valid).toBe(true);
72+
expect(validate({ a: 1 }).valid).toBe(false);
73+
expect(validate({ a: 1 }).errorMessage).toBeTruthy();
74+
}
75+
});
76+
});

0 commit comments

Comments
 (0)