-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix Zod v4 schema description extraction #1296
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ export interface ZodV4Internal { | |
| value?: unknown; | ||
| values?: unknown[]; | ||
| shape?: Record<string, AnySchema> | (() => Record<string, AnySchema>); | ||
| description?: string; | ||
| }; | ||
| }; | ||
| value?: unknown; | ||
|
|
@@ -220,15 +219,12 @@ export function getParseErrorMessage(error: unknown): string { | |
| /** | ||
| * Gets the description from a schema, if available. | ||
| * Works with both Zod v3 and v4. | ||
| * | ||
| * Both versions expose a `.description` getter that returns the description | ||
| * from their respective internal storage (v3: _def, v4: globalRegistry). | ||
| */ | ||
| export function getSchemaDescription(schema: AnySchema): string | undefined { | ||
| if (isZ4Schema(schema)) { | ||
| const v4Schema = schema as unknown as ZodV4Internal; | ||
| return v4Schema._zod?.def?.description; | ||
| } | ||
| const v3Schema = schema as unknown as ZodV3Internal; | ||
| // v3 may have description on the schema itself or in _def | ||
| return (schema as { description?: string }).description ?? v3Schema._def?.description; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| return (schema as { description?: string }).description; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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,65 @@ | ||
| /** | ||
| * Regression test for https://github.com/modelcontextprotocol/typescript-sdk/issues/1277 | ||
| * | ||
| * Zod v4 stores `.describe()` descriptions directly on the schema object, | ||
| * not in `._zod.def.description`. This test verifies that descriptions are | ||
| * correctly extracted for prompt arguments. | ||
| */ | ||
|
|
||
| import { Client } from '../../src/client/index.js'; | ||
| import { InMemoryTransport } from '../../src/inMemory.js'; | ||
| import { ListPromptsResultSchema } from '../../src/types.js'; | ||
| import { McpServer } from '../../src/server/mcp.js'; | ||
| import { zodTestMatrix, type ZodMatrixEntry } from '../../src/__fixtures__/zodTestMatrix.js'; | ||
|
|
||
| describe.each(zodTestMatrix)('Issue #1277: $zodVersionLabel', (entry: ZodMatrixEntry) => { | ||
| const { z } = entry; | ||
|
|
||
| test('should preserve argument descriptions from .describe()', async () => { | ||
| const mcpServer = new McpServer({ | ||
| name: 'test server', | ||
| version: '1.0' | ||
| }); | ||
| const client = new Client({ | ||
| name: 'test client', | ||
| version: '1.0' | ||
| }); | ||
|
|
||
| mcpServer.prompt( | ||
| 'test', | ||
| { | ||
| name: z.string().describe('The user name'), | ||
| value: z.string().describe('The value to set') | ||
| }, | ||
| async ({ name, value }) => ({ | ||
| messages: [ | ||
| { | ||
| role: 'assistant', | ||
| content: { | ||
| type: 'text', | ||
| text: `${name}: ${value}` | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| ); | ||
|
|
||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]); | ||
|
|
||
| const result = await client.request( | ||
| { | ||
| method: 'prompts/list' | ||
| }, | ||
| ListPromptsResultSchema | ||
| ); | ||
|
|
||
| expect(result.prompts).toHaveLength(1); | ||
| expect(result.prompts[0].name).toBe('test'); | ||
| expect(result.prompts[0].arguments).toEqual([ | ||
| { name: 'name', required: true, description: 'The user name' }, | ||
| { name: 'value', required: true, description: 'The value to set' } | ||
| ]); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In v4
.describe()stores inglobalRegistrywith the.descriptiongetter reading from it directly viav4Schema.descriptionrather than on_defso we don't need this.https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/classic/schemas.ts#L223-L230