Repository navigation
build: declare zod as a peer dependency alongside the regular dependency; floor ^4.0.0 - #2460
felixweinberger wants to merge 1 commit into
Conversation
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.
|
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
| # 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 |
There was a problem hiding this comment.
🟡 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.0and 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 anoverridesentry of"zod": "^4.2.0", which is now over-restrictive relative to the^4.0.0floor.
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.
- This PR changes
pnpm-workspace.yamlcatalog entryzod: ^4.2.0→zod: ^4.0.0and adds"peerDependencies": { "zod": "catalog:runtimeShared" }topackages/{client,core,server,server-legacy}/package.json— visible in the diff. - After merge, a reader consults
docs/migration/upgrade-to-v2.md:265and reads "The v2 packages depend on zod ^4.2.0" — but the published manifests now declarezod: ^4.0.0(dependency and peer), so the sentence is false. - 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.
- Similarly,
docs/troubleshooting.md:44tells that host to add an override forcing^4.2.0, an unnecessary constraint under the new floor. - 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.
Declares zod in
peerDependenciesalongside the existing regular dependency (the v1.x pattern), and lowers the floor from^4.2.0to^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 differentglobalRegistrythan 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;
toJSONSchemaoutput at 4.0.0 is byte-identical to 4.4.3 for ordinary shapes under the explicitiooption the SDK always passes, and~standard.validatebehavior is invariant across the line (87/87 corpus agreement, identical output values). The single known emission divergence below 4.3 (exhaustive-enum records missingrequired) 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
Checklist