Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/zod-jsonschema-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelcontextprotocol/server': patch
'@modelcontextprotocol/client': patch
---

Fix runtime crash on `tools/list` when a tool's `inputSchema` comes from zod < 4.2.0. The SDK requires `~standard.jsonSchema` (StandardJSONSchemaV1, added in zod 4.2.0); previously a missing `jsonSchema` crashed at `undefined[io]`. `standardSchemaToJsonSchema` now detects `vendor: 'zod'` without `jsonSchema` and falls back to the SDK-bundled `z.toJSONSchema()`, emitting a one-time console warning. Non-zod schema libraries without `jsonSchema` get a clear error pointing to `fromJsonSchema()`. The workspace zod catalog is also bumped to `^4.2.0`.
26 changes: 25 additions & 1 deletion packages/core/src/util/standardSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/* eslint-disable @typescript-eslint/no-namespace */

import * as z from 'zod/v4';

// Standard Schema interfaces — vendored from https://standardschema.dev (spec v1, Jan 2025)

export interface StandardTypedV1<Input = unknown, Output = Input> {
Expand Down Expand Up @@ -148,8 +150,30 @@ export function isStandardSchemaWithJSON(schema: unknown): schema is StandardSch
* Throws if the schema has an explicit non-object `type` (e.g. `z.string()`),
* since that cannot satisfy the MCP spec.
*/
let warnedZodFallback = false;

export function standardSchemaToJsonSchema(schema: StandardJSONSchemaV1, io: 'input' | 'output' = 'input'): Record<string, unknown> {
const result = schema['~standard'].jsonSchema[io]({ target: 'draft-2020-12' });
const std = schema['~standard'];
let result: Record<string, unknown>;
if (std.jsonSchema) {
result = std.jsonSchema[io]({ target: 'draft-2020-12' });
} else if (std.vendor === 'zod') {
// zod <4.2.0 implements StandardSchemaV1 but not StandardJSONSchemaV1 (`~standard.jsonSchema`).
// The SDK already bundles zod, so fall back to its converter rather than crashing on tools/list.
if (!warnedZodFallback) {
warnedZodFallback = true;
console.warn(
'[@modelcontextprotocol/sdk] Your zod version does not implement `~standard.jsonSchema` (added in zod 4.2.0). ' +
'Falling back to z.toJSONSchema(). Upgrade to zod >=4.2.0 to silence this warning.'
);
}
result = z.toJSONSchema(schema as unknown as z.ZodType, { target: 'draft-2020-12', io }) as Record<string, unknown>;
} else {
throw new Error(
`Schema library "${std.vendor}" does not implement StandardJSONSchemaV1 (\`~standard.jsonSchema\`). ` +
`Upgrade to a version that does, or wrap your JSON Schema with fromJsonSchema().`
);
}
if (result.type !== undefined && result.type !== 'object') {
throw new Error(
`MCP tool and prompt schemas must describe objects (got type: ${JSON.stringify(result.type)}). ` +
Expand Down
30 changes: 30 additions & 0 deletions packages/core/test/util/standardSchema.zodFallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it, vi } from 'vitest';
import * as z from 'zod/v4';
import { standardSchemaToJsonSchema } from '../../src/util/standardSchema.js';

type SchemaArg = Parameters<typeof standardSchemaToJsonSchema>[0];

describe('standardSchemaToJsonSchema — zod <4.2.0 fallback', () => {
it('falls back to z.toJSONSchema when ~standard.jsonSchema is absent (vendor=zod)', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const real = z.object({ a: z.string() });
// Simulate zod <4.2.0: shadow `~standard` on the real instance with `jsonSchema` removed.
// Keeps the rest of the zod object intact so z.toJSONSchema can introspect it.
const { jsonSchema: _drop, ...stdNoJson } = real['~standard'] as unknown as Record<string, unknown>;
void _drop;
Object.defineProperty(real, '~standard', { value: { ...stdNoJson, vendor: 'zod' }, configurable: true });

const result = standardSchemaToJsonSchema(real as unknown as SchemaArg);
expect(result.type).toBe('object');
expect((result.properties as unknown as Record<string, unknown>)?.a).toBeDefined();
expect(warn).toHaveBeenCalledOnce();
expect(warn.mock.calls[0]?.[0]).toContain('zod 4.2.0');
warn.mockRestore();
});

it('throws a clear error for non-zod libraries without ~standard.jsonSchema', () => {
const fake = { '~standard': { version: 1, vendor: 'mylib', validate: () => ({ value: {} }) } };
expect(() => standardSchemaToJsonSchema(fake as unknown as SchemaArg)).toThrow(/mylib/);
expect(() => standardSchemaToJsonSchema(fake as unknown as SchemaArg)).toThrow(/fromJsonSchema/);
});
});
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ catalogs:
ajv-formats: ^3.0.1
json-schema-typed: ^8.0.2
pkce-challenge: ^5.0.0
zod: ^4.0
zod: ^4.2.0

enableGlobalVirtualStore: false

Expand Down
Loading