Skip to content

zod listed in both dependencies and peerDependencies causes duplicate installs and TS type-incompatibility #2011

Open

Description

@Euaell

Describe the bug
@modelcontextprotocol/[email protected] declares zod in BOTH dependencies and
peerDependencies. With bun (and pnpm under default hoisting, npm in some
configs) this causes a nested node_modules/@modelcontextprotocol/sdk/node_modules/zod
to be installed alongside the consumer's top-level zod, even when the
consumer's version satisfies the SDK's range.

The result: TypeScript sees two distinct $ZodType class declarations (one per
copy) and treats them as nominally incompatible. Schemas built from the
consumer's zod (e.g. z.string().optional()) fail to satisfy the
AnySchema = z3.ZodTypeAny | z4.$ZodType type that registerTool's
inputSchema parameter expects — even though both copies are the same library
at the same major version.

To Reproduce
Steps to reproduce the behavior:

  1. bun init -y
  2. bun add @modelcontextprotocol/[email protected] [email protected] typescript
  3. Create src/server.ts:
    import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
    import { z } from 'zod'
    
    const server = new McpServer({ name: 'demo', version: '0.0.1' })
    server.registerTool(
      'echo',
      { inputSchema: { msg: z.string() } },
      async ({ msg }) => ({ content: [{ type: 'text', text: msg }] }),
    )
  4. bun x tsc --noEmit

Expected behavior

Typecheck passes. The consumer's zod should satisfy the AnySchema slot
because the SDK's compat layer already accepts both z3.ZodTypeAny and
z4.$ZodType, and the consumer's schemas are valid z4.$ZodType values.

Logs
src/server.ts:7:24 - error TS2322: Type 'ZodString' is not assignable to type 'AnySchema'.
Type 'ZodString' is missing the following properties from type
'ZodType<any, any, any>': _type, _parse, _getType, _getOrReturnCtx, and 7 more.

7 { inputSchema: { msg: z.string() } },
~~~~~~~~~~

ls node_modules/@modelcontextprotocol/sdk/node_modules/zod/package.json
shows the nested copy is installed (e.g. 4.3.6) alongside the top-level (4.4.2).

Additional context
Workaround consumers have to apply (this is what bit us):
// package.json
"overrides": { "zod": "" }
followed by deleting bun.lock + reinstalling so the existing resolution is
re-evaluated.

Proposed fix: remove "zod" from dependencies and keep it only in
peerDependencies. The compat shim (dist/esm/server/zod-compat.d.ts) already
handles both v3 and v4 via the consumer-provided copy, so the SDK doesn't
need its own bundled zod.

Repo: https://github.com/modelcontextprotocol/typescript-sdk
SDK version: 1.26.0
Bun version: 1.3.12
TypeScript: 5.9.3
Zod (top): 4.4.2
Zod (nested): 4.3.6

Activity

  1. added
    bug
    on May 2, 2026
  2. added
    triage
    on Jul 9, 2026
  3. ghost added
    ready for work
    on Jul 9, 2026
  4. ghost added
    P2
    on Jul 9, 2026
  5. ghost added
    fix proposed
    on Jul 9, 2026
  6. ghost removed
    triage
    on Jul 9, 2026
  7. maribostashvili-93 commented on Nov 10, 2026

    @maribostashvili-93

    On main this is resolved (v2 lists zod once, per #2014), but v1.x still declares zod with the same range in both dependencies and peerDependencies. Would a small PR against v1.x that keeps zod only in peerDependencies be welcome? Happy to take it and verify with npm, pnpm and bun installs.

  8. nithishreddy5771 commented on Nov 12, 2026

    @nithishreddy5771

    On v1.x (sdk 1.32.2) I could not reproduce duplicate zod copies with fresh installs. Installing the SDK and a host zod together gave one copy on npm 10.9, pnpm 12.18 and bun 1.4, for zod 4.1.12 and 3.25.76.

    It does reproduce when the SDK is installed first and the host's zod is added afterwards. With bun that leaves node_modules/@modelcontextprotocol/sdk/node_modules/zod (4.6.7) next to the host's copy, and tsc 5.9 fails on the registerTool example: TS2322 with a zod 4.1.12 host, TS2589 with a zod 3.25.76 host. pnpm also ends up with two copies in that order, though its tsc check still passes. npm dedupes.

    With zod removed from dependencies (peerDependencies unchanged), the bun case gives one copy and tsc passes for both hosts. I checked that on an edited copy of the published 1.32.2 tarball and on a package built from a local branch. It does not change pnpm's two copies. Hosts without zod still get it as a peer on npm, pnpm and bun, but npm with --legacy-peer-deps and no host zod ends up without zod and the import fails. A host on zod 3.24 would get an ERESOLVE error instead of a nested copy.

    #2460 treated the dual declaration as the intended v1.x pattern, so I'm asking before doing anything else: is peer-only acceptable on v1.x, or should it stay as it is? There is a small local patch (package.json, lockfile, a manifest guard test (a package.json assertion, not a behavior test) and a changeset). I'm not opening a PR unless you want one.

    These checks were run by an AI assistant on my behalf; I have not rerun them by hand.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2bugfix proposedready for work

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions