Skip to content

[v2] CJS AJV validator subpath crashes when reading exported Ajv #2430

Description

@morluto

What happened?

CommonJS consumers cannot read the Ajv export from the AJV validator subpath.

Both @modelcontextprotocol/server and @modelcontextprotocol/client expose ./validators/ajv through a require condition, but the generated CJS output references an ESM alias that does not exist in the CJS module scope.

Object.defineProperty(exports, 'Ajv', {
  enumerable: true,
  get: function () {
    return import_ajv.Ajv;
  }
});

Accessing that export throws:

ReferenceError: import_ajv is not defined

This affects both package subpaths:

  • @modelcontextprotocol/server/validators/ajv
  • @modelcontextprotocol/client/validators/ajv

The issue is specifically the Ajv getter. Other exports from the same subpath, such as AjvJsonSchemaValidator and addFormats, are reachable.

Generated-output shape:

CJS subpath file
  dist/validators/ajv.cjs

  const require_ajvProvider = require('../ajvProvider-*.cjs')
          |
          |-- exports.addFormats = require_ajvProvider.addFormats              ok
          |-- exports.AjvJsonSchemaValidator = require_ajvProvider...          ok
          |
          `-- exports.Ajv getter
                    |
                    `-- return import_ajv.Ajv                                 crash
                               ^^^^^^^^^^
                               no binding with this name exists in ajv.cjs

Provider chunk
  dist/ajvProvider-*.cjs

  exports.import_ajv = ...
          |
          `-- correct CJS lookup would be require_ajvProvider.import_ajv.Ajv

What did you expect?

A CommonJS consumer should be able to require the documented AJV validator subpath and read the exported Ajv symbol without a ReferenceError.

For example:

const { Ajv, addFormats, AjvJsonSchemaValidator } = require('@modelcontextprotocol/server/validators/ajv');

const ajv = new Ajv({ strict: true, allErrors: true });
addFormats(ajv);
const validator = new AjvJsonSchemaValidator(ajv);

Code to reproduce

// repro.cjs
const serverAjv = require('@modelcontextprotocol/server/validators/ajv');
const clientAjv = require('@modelcontextprotocol/client/validators/ajv');

console.log(serverAjv.Ajv);
console.log(clientAjv.Ajv);

Run:

node repro.cjs

Observed result:

ReferenceError: import_ajv is not defined

Minimal direct probes:

node -e "const m = require('@modelcontextprotocol/server/validators/ajv'); m.Ajv"
node -e "const m = require('@modelcontextprotocol/client/validators/ajv'); m.Ajv"

SDK version

@modelcontextprotocol/server@2.0.0-beta.2
@modelcontextprotocol/client@2.0.0-beta.2

Area

Server / Client packaging, CJS validator subpath exports.

Metadata

Metadata

Assignees

No one assigned

    Labels

    v2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions