-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): fall back to z.toJSONSchema for zod schemas without ~standard.jsonSchema #1895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+79
−3
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1313b78
fix(core): fall back to z.toJSONSchema for zod schemas without ~stand…
felixweinberger 6dc7acb
Merge branch 'main' into fweinberger/v2-bc-zod-trap
KKonstantinov 889434a
chore(changeset): add core to frontmatter; fix warning prefix to [mcp…
felixweinberger 8b7a0e4
Merge branch 'main' into fweinberger/v2-bc-zod-trap
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.0–4.1. The SDK requires `~standard.jsonSchema` (StandardJSONSchemaV1, added in zod 4.2.0); previously a missing `jsonSchema` crashed at `undefined[io]`. `standardSchemaToJsonSchema` now detects zod 4 schemas lacking `jsonSchema` and falls back to the SDK-bundled `z.toJSONSchema()`, emitting a one-time console warning. zod 3 schemas (which the bundled zod 4 converter cannot introspect) and non-zod schema libraries without `jsonSchema` get a clear error pointing to `fromJsonSchema()`. The workspace zod catalog is also bumped to `^4.2.0`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/core/test/util/standardSchema.zodFallback.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 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 fallback paths', () => { | ||
| it('falls back to z.toJSONSchema for zod 4.0–4.1 (vendor=zod, no ~standard.jsonSchema, has _zod)', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const real = z.object({ a: z.string() }); | ||
| // Simulate zod 4.0–4.1: shadow `~standard` on the real instance with `jsonSchema` removed. | ||
| // Keeps the rest of the zod 4 object (including `_zod`) 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 zod 3 (vendor=zod, no ~standard.jsonSchema, no _zod)', () => { | ||
| // zod 3.24+ reports `~standard.vendor === 'zod'` but has no `_zod` internal marker. | ||
| const zod3ish = { _def: {}, '~standard': { version: 1, vendor: 'zod', validate: () => ({ value: {} }) } }; | ||
| expect(() => standardSchemaToJsonSchema(zod3ish as unknown as SchemaArg)).toThrow(/zod 3/); | ||
| expect(() => standardSchemaToJsonSchema(zod3ish as unknown as SchemaArg)).toThrow(/4\.2\.0/); | ||
| }); | ||
|
|
||
| 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/); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.