Skip to content

Commit c269c39

Browse files
committed
fix(packaging): expose Ajv from CJS validator subpaths
1 parent ddba550 commit c269c39

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modelcontextprotocol/server': patch
3+
'@modelcontextprotocol/client': patch
4+
---
5+
6+
Fix the CommonJS `validators/ajv` subpath so reading the exported `Ajv` class no longer throws `ReferenceError: import_ajv is not defined`. The subpath now re-exports the bundled provider's concrete `Ajv` value in CJS output, matching the existing ESM behavior.

packages/client/test/client/barrelClean.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { execFileSync } from 'node:child_process';
22
import { existsSync, readFileSync } from 'node:fs';
3+
import { createRequire } from 'node:module';
34
import { dirname, join } from 'node:path';
45
import { fileURLToPath } from 'node:url';
56

67
import { beforeAll, describe, expect, test } from 'vitest';
78

89
const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..');
910
const distDir = join(pkgDir, 'dist');
11+
const requireDist = createRequire(join(pkgDir, 'package.json'));
1012
const NODE_ONLY = /\b(child_process|cross-spawn|node:stream|node:child_process)\b/;
1113
// Anchored at start-of-line so JSDoc-example `from 'ajv'` strings in vendored chunks don't match.
1214
const VALIDATOR_BACKEND_IMPORT = /^import[^\n]*?from\s+["'](?:ajv|ajv-formats|@cfworker\/json-schema)["']/m;
@@ -67,4 +69,15 @@ describe('@modelcontextprotocol/client root entry is browser-safe', () => {
6769
}
6870
}
6971
});
72+
73+
test('CJS AJV validator subpath exposes Ajv at runtime', () => {
74+
const { Ajv, AjvJsonSchemaValidator, addFormats } = requireDist(
75+
'./dist/validators/ajv.cjs'
76+
) as typeof import('../../src/validators/ajv');
77+
78+
const ajv = new Ajv({ strict: false, allErrors: true });
79+
addFormats(ajv);
80+
81+
expect(new AjvJsonSchemaValidator(ajv)).toBeInstanceOf(AjvJsonSchemaValidator);
82+
});
7083
});

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* AJV-based JSON Schema validator provider
33
*/
44

5+
import { Ajv as Draft7Ajv } from 'ajv';
56
import { Ajv2020 } from 'ajv/dist/2020.js';
67
import _addFormats from 'ajv-formats';
78

@@ -31,6 +32,7 @@ interface AjvValidateFunction {
3132
errors?: any;
3233
}
3334

35+
/** `ajv-formats` default export, normalised through the CJS/ESM interop wrapper. */
3436
const addFormats = _addFormats as unknown as typeof _addFormats.default;
3537

3638
function createDefaultAjvInstance(): AjvLike {
@@ -152,6 +154,6 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
152154
* declaration bundling — see #2339). To construct a custom 2020-12 instance, add `ajv` to your own
153155
* dependencies (matching the SDK's pinned version) and `import { Ajv2020 } from 'ajv/dist/2020.js'`.
154156
*/
155-
export { Ajv } from 'ajv';
156-
/** `ajv-formats` default export, normalised through the CJS/ESM interop wrapper. */
157-
export { addFormats };
157+
const Ajv = Draft7Ajv;
158+
159+
export { addFormats, Ajv };

packages/server/test/server/barrelClean.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { execFileSync } from 'node:child_process';
22
import { existsSync, readFileSync } from 'node:fs';
3+
import { createRequire } from 'node:module';
34
import { dirname, join } from 'node:path';
45
import { fileURLToPath } from 'node:url';
56

67
import { beforeAll, describe, expect, test } from 'vitest';
78

89
const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..');
910
const distDir = join(pkgDir, 'dist');
11+
const requireDist = createRequire(join(pkgDir, 'package.json'));
1012
const NODE_ONLY = /\b(child_process|cross-spawn|node:stream|node:child_process)\b/;
1113
// Anchored at start-of-line so JSDoc-example `from 'ajv'` strings in vendored chunks don't match.
1214
const VALIDATOR_BACKEND_IMPORT = /^import[^\n]*?from\s+["'](?:ajv|ajv-formats|@cfworker\/json-schema)["']/m;
@@ -68,4 +70,15 @@ describe('@modelcontextprotocol/server root entry is browser-safe', () => {
6870
}
6971
}
7072
});
73+
74+
test('CJS AJV validator subpath exposes Ajv at runtime', () => {
75+
const { Ajv, AjvJsonSchemaValidator, addFormats } = requireDist(
76+
'./dist/validators/ajv.cjs'
77+
) as typeof import('../../src/validators/ajv');
78+
79+
const ajv = new Ajv({ strict: false, allErrors: true });
80+
addFormats(ajv);
81+
82+
expect(new AjvJsonSchemaValidator(ajv)).toBeInstanceOf(AjvJsonSchemaValidator);
83+
});
7184
});

0 commit comments

Comments
 (0)