Skip to content

build: declare zod as a peer dependency alongside the regular dependency; floor ^4.0.0 - #2460

Closed
felixweinberger wants to merge 1 commit into
mainfrom
fweinberger/zod-peer-dependency
Closed

felixweinberger wants to merge 1 commit into
mainfrom
fweinberger/zod-peer-dependency

Conversation

@felixweinberger

Copy link
Copy Markdown
Contributor

Declares zod in peerDependencies alongside the existing regular dependency (the v1.x pattern), and lowers the floor from ^4.2.0 to ^4.0.0.

Motivation and Context

With zod as a plain dependency pinned ^4.2.0, a host application that itself depends on zod ends up with an identical nested zod copy under each of client/core/server — bundle and cold-start cost, plus cross-instance issues like .describe() metadata living in a different globalRegistry than the one the SDK reads. With the dual declaration, hosts on zod >= 4.0 share one hoisted copy (verified with bun, npm, and pnpm install fixtures: one root zod, zero nested, same module object across host and SDK, .describe() survives into tools/list); hosts without zod, or below the floor, keep getting a nested copy from the regular dependency — no install that works today changes behavior.

The floor drop is evidence-backed: every zod API the SDK uses exists in 4.0; toJSONSchema output at 4.0.0 is byte-identical to 4.4.3 for ordinary shapes under the explicit io option the SDK always passes, and ~standard.validate behavior is invariant across the line (87/87 corpus agreement, identical output values). The single known emission divergence below 4.3 (exhaustive-enum records missing required) is fixed independently in #2457, version-independently.

How Has This Been Tested?

Full workspace suite green after the change; install-layout fixtures for the three package managers (shared, nested, and no-zod host scenarios); runtime smoke registering a tool with the host's zod instance and asserting metadata and round-trips over InMemoryTransport.

Breaking Changes

None at install time. One consideration to call out: on shared installs the SDK runs on the host's zod, so host-level zod configuration (z.config, locales) also affects error text produced by SDK-internal schemas. A follow-up CI matrix leg pinned to the 4.0 floor would enforce the API-surface discipline mechanically; happy to add it here or separately.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Embedding applications whose root pins zod 3.x currently get a byte-identical
zod@4 copy nested under each published package (client, core, server,
server-legacy) — three-plus duplicate evaluations of the same library.

Restore the v1.29 dual declaration pattern: keep zod as a regular dependency
(a compatible copy is always present, installs never break for zod-3 hosts)
and additionally declare it as a peer dependency, so hosts already on zod 4
deduplicate to a single shared copy.

Also lower the floor from ^4.2.0 to ^4.0.0: the SDK's used zod API surface is
4.0-compatible, and the only 4.2-era feature (`~standard.jsonSchema`) is
already capability-detected with a z.toJSONSchema fallback.
@felixweinberger
felixweinberger requested a review from a team as a code owner July 7, 2026 16:35
@changeset-bot

changeset-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 9c47673

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

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

@modelcontextprotocol/core

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

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 9c47673

Comment thread pnpm-workspace.yaml
Comment on lines +56 to +62
# zod is declared as BOTH a regular dependency and a peer dependency in the
# published packages (the v1.x pattern): the dependency guarantees a compatible
# copy is always installed, while the peer range lets hosts already on zod 4
# share a single copy instead of nesting one under each package. The used zod
# API surface is 4.0-compatible; the only 4.2 feature (`~standard.jsonSchema`)
# is capability-detected with a `z.toJSONSchema` fallback, so the floor is ^4.0.0.
zod: ^4.0.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 The migration and troubleshooting docs still describe the old zod ^4.2.0 plain-dependency behavior and never mention the new peer-dependency dedup path: docs/migration/upgrade-to-v2.md (~lines 262–284, 693) says the v2 packages depend on zod ^4.2.0 and that bumping the workspace to ^4.2.0 is what re-dedupes nested copies, and docs/troubleshooting.md:44 recommends an override of "zod": "^4.2.0". Since this PR lowers the floor to ^4.0.0 and dedup for zod ≥4.0 hosts now happens via the added peerDependencies range, consider updating those doc sections in this PR.

Extended reasoning...

What is stale. This PR changes two things about how the SDK declares zod: the catalog floor drops from ^4.2.0 to ^4.0.0 (pnpm-workspace.yaml runtimeShared catalog), and zod is added to peerDependencies of @modelcontextprotocol/client, core, server, and server-legacy so hosts already on zod 4 share one hoisted copy instead of nesting one under each package. However, the docs that explain exactly this dedup story were not touched, so after merge they describe behavior that no longer matches the shipped manifests.

Specific stale locations.

  • docs/migration/upgrade-to-v2.md:265 — "The v2 packages depend on zod ^4.2.0": factually wrong post-merge; the declared floor becomes ^4.0.0 and the packages additionally declare a zod peer dependency.
  • docs/migration/upgrade-to-v2.md:264–278 (the "nested zod copies in zod@3-pinned monorepos" section) — "Upgrading the workspace to zod ^4.2.0 re-dedupes and removes the duplication": with the new peer range, dedup now happens for any host on zod ≥ 4.0.0, so both the version number and the dedup mechanism described here are stale. The section also never mentions the peer-dependency pattern, which is now the primary sharing mechanism.
  • docs/migration/upgrade-to-v2.md:284 — "add the v2 packages (and the zod ^4.2.0 bump)" repeats the old floor.
  • docs/migration/upgrade-to-v2.md:693 — the "zod-v4": "npm:zod@^4.2.0" alias example repeats the old floor (note: the surrounding ~standard.jsonSchema discussion at lines 679–693 is about a genuine zod 4.2 feature and remains intentionally correct — only the floor/dedup prose is affected).
  • docs/troubleshooting.md:44 — recommends an overrides entry of "zod": "^4.2.0", which is now over-restrictive relative to the ^4.0.0 floor.

Why nothing else prevents this. The PR touches only the four package.json files, pnpm-workspace.yaml, and the lockfile — no docs/**/*.md file — while the PR checklist claims documentation was updated as needed. There is no automated sync between the catalog version and these prose sections, so the drift will persist until someone edits the docs by hand.

Impact. Low. Following the stale advice still works: ^4.2.0 satisfies ^4.0.0 and still dedupes, and the zod@3-pinned nesting story remains accurate. But readers on zod 4.0/4.1 are told they must bump to 4.2 to dedupe when the new peer range already dedupes at 4.0, and the docs never explain the peer-dependency sharing behavior that is the whole point of this change (including the note from the PR description that on shared installs host-level z.config/locale settings affect SDK error text). That's misleading rather than harmful, so this is a non-blocking docs-drift nit.

How to fix. In this PR, update the "nested zod copies" section of docs/migration/upgrade-to-v2.md to state the new ^4.0.0 floor and explain that the packages declare zod as both a regular and a peer dependency (mirroring the comment added to pnpm-workspace.yaml), adjust lines 284 and 693 accordingly, and relax the docs/troubleshooting.md:44 override recommendation to ^4.0.0 (or note that any zod ≥ 4.0 dedupes via the peer range).

Step-by-step proof.

  1. This PR changes pnpm-workspace.yaml catalog entry zod: ^4.2.0 → zod: ^4.0.0 and adds "peerDependencies": { "zod": "catalog:runtimeShared" } to packages/{client,core,server,server-legacy}/package.json — visible in the diff.
  2. After merge, a reader consults docs/migration/upgrade-to-v2.md:265 and reads "The v2 packages depend on zod ^4.2.0" — but the published manifests now declare zod: ^4.0.0 (dependency and peer), so the sentence is false.
  3. A host monorepo pinned to zod 4.1 reads line 278 ("Upgrading the workspace to zod ^4.2.0 re-dedupes") and concludes it must upgrade zod to 4.2 to avoid nested copies — yet with the new peer range, its 4.1 copy already satisfies the peer requirement and package managers hoist a single shared copy without any upgrade.
  4. Similarly, docs/troubleshooting.md:44 tells that host to add an override forcing ^4.2.0, an unnecessary constraint under the new floor.
  5. The advice is merely over-restrictive (following it still yields a working, deduped install), so nothing breaks — hence nit severity — but the prose contradicts the change this PR ships and omits its central mechanism.

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Aug 18, 2026
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.

1 participant