Skip to content

fix(validators): allow custom AJV options on AjvJsonSchemaValidator while preserving dialect dispatch - #3059

Open
pei711 wants to merge 1 commit into
modelcontextprotocol:mainfrom
pei711:fix/ajv-options-engine
Open

pei711 wants to merge 1 commit into
modelcontextprotocol:mainfrom
pei711:fix/ajv-options-engine

Conversation

@pei711

@pei711 pei711 commented Oct 20, 2026

Copy link
Copy Markdown

Summary

Closes #3017.

AjvJsonSchemaValidator picks an AJV engine according to each schema's declared $schema dialect (Ajv2020 for 2020-12, Ajv2019 for 2019-09, and classic Draft7 Ajv for draft-07/06). Previously, the only way to configure engine options was to pass a pre-configured instance (new AjvJsonSchemaValidator(ajv)), which bypassed the $schema dialect dispatch entirely.

This PR adds support for passing validator options:

const validator = new AjvJsonSchemaValidator({ options: { useDefaults: true } });

The options are merged into the default options (strict: false, validateFormats: true, validateSchema: false, allErrors: true, plus caller options) across each dialect engine while preserving lazy construction and dialect dispatch.

Changes

  • Defined AjvJsonSchemaValidatorOptions interface and exported it from @modelcontextprotocol/core-internal/validators/ajv, @modelcontextprotocol/server/validators/ajv, and @modelcontextprotocol/client/validators/ajv.
  • Updated AjvJsonSchemaValidator constructor to accept ajvOrOptions?: AjvLike | AjvJsonSchemaValidatorOptions.
  • Forwarded options to createDefaultAjvInstance when lazily constructing dialect engines.
  • Preserved existing behavior when passing an AjvLike instance (new AjvJsonSchemaValidator(ajv)).
  • Added unit tests in packages/core-internal/test/validators/ajvLazyInit.test.ts verifying that custom engine options (like useDefaults: true) work across all dialects (2020-12, 2019-09, draft-07) while maintaining lazy instantiation and dialect dispatch.
  • Added JSDoc example and type-checked example in ajvProvider.examples.ts.
  • Added patch changeset for @modelcontextprotocol/server and @modelcontextprotocol/client.

Testing

  • Unit tests in packages/core-internal/test/validators/ajvLazyInit.test.ts: all passed.
  • pnpm --filter @modelcontextprotocol/core-internal run check: typecheck, eslint, and prettier passed cleanly.
  • pnpm --filter @modelcontextprotocol/client run check: passed.
  • pnpm --filter @modelcontextprotocol/server run check: passed.
  • pnpm --filter @modelcontextprotocol/core-internal test: 1526/1526 passed.
  • pnpm --filter @modelcontextprotocol/client test: 998/998 passed.
  • pnpm --filter @modelcontextprotocol/server test: 577/577 passed.
  • pnpm run sync:snippets: confirmed all snippets up to date.

@pei711
pei711 requested a review from a team as a code owner October 20, 2026 18:23
@changeset-bot

changeset-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 86ee45e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/server Patch
@modelcontextprotocol/client Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/core-internal Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-20T18:25:24.326863Z 86ee45e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Oct 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3059

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3059

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3059

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3059

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3059

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3059

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3059

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3059

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3059

commit: 86ee45e

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Oct 21, 2026
@0xamlab

0xamlab commented Oct 21, 2026

Copy link
Copy Markdown

Checked the test against the merge-base (46d9c99) to confirm it guards the regression. On this branch, test/validators/ajvLazyInit.test.ts passes 7/7 and the full core-internal suite passes 1837/1837 (77 files, 6.7s). On base with only the test file overlaid, the new test fails its first assertion: the old constructor treats any argument as a caller engine, so { options: { useDefaults: true } } lands in the _ajv slot and the $schema dispatch in _engineFor would be skipped. The isAjvLike guard at packages/core-internal/src/validators/ajvProvider.ts:63 separates the two constructor forms, and the spread at line 39 applies the options to each per-dialect engine.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AjvJsonSchemaValidator: allow ajv options (e.g. useDefaults) without giving up the per-dialect engine choice

2 participants