-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(server): surface raw-shape schema failures at registration time, restore mixed-zod guard #2243
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
base: main
Are you sure you want to change the base?
Changes from all commits
bfd0262
7e9e3a8
9242683
35303b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@modelcontextprotocol/core': patch | ||
| '@modelcontextprotocol/server': patch | ||
| --- | ||
|
|
||
| Raw-shape `inputSchema`/`outputSchema`/`argsSchema` failures now surface at registration time instead of crashing `tools/list` later. Shapes whose field schemas come from a different zod build than the SDK bundles (e.g. an application's own zod 4.0/4.1 instance) previously | ||
| registered fine and then threw `[toJSONSchema]: Non-representable type encountered` when listing; they now throw an actionable `TypeError` from `registerTool`/`registerPrompt`. Shapes mixing zod v3 and v4 fields throw `Error('Mixed Zod versions detected in object shape.')` — the | ||
| same registration-time error v1 threw — instead of being misreported as all-v3. The all-v3 error message now also mentions the `fromJsonSchema()` alternative. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,3 +87,80 @@ | |
| expect(() => normalizeRawShapeSchema(null as never)).toThrow(/must be a Standard Schema/); | ||
| }); | ||
| }); | ||
|
|
||
| // Minimal structural stand-in for a v4-detected field schema whose internals | ||
| // the SDK-bundled `z.toJSONSchema()` cannot walk. Deterministic and | ||
| // build-independent; the real-instance equivalent (an actual second zod | ||
| // install) is covered in the real-instance tests below. | ||
| function mockForeignZodV4String(): unknown { | ||
| return { | ||
| _zod: { def: { type: 'string' }, version: { major: 4, minor: 0 } }, | ||
| '~standard': { version: 1, vendor: 'zod', validate: (v: unknown) => ({ value: v }) } | ||
| }; | ||
| } | ||
|
|
||
| describe('normalizeRawShapeSchema v1-compat dispatch', () => { | ||
| test('throws the v1 error for a shape mixing zod versions (exact message)', () => { | ||
| const shape = { a: z.string(), b: mockZodV3String() }; | ||
| expect(() => normalizeRawShapeSchema(shape as never)).toThrow('Mixed Zod versions detected in object shape.'); | ||
| }); | ||
|
|
||
| test('mixed-version error is not misreported as the all-v3 error', () => { | ||
| const shape = { a: z.string(), b: mockZodV3String() }; | ||
| expect(() => normalizeRawShapeSchema(shape as never)).not.toThrow(/must be Zod v4 schemas/); | ||
| }); | ||
|
|
||
| test('fails at normalization time when raw-shape fields cannot be converted to JSON Schema', () => { | ||
| // Without the eager check this registers fine and `tools/list` crashes later. | ||
| const shape = { a: mockForeignZodV4String() }; | ||
| expect(() => normalizeRawShapeSchema(shape as never)).toThrow(/could not be converted to JSON Schema/); | ||
| }); | ||
|
|
||
| test('conversion failure preserves the underlying error as cause', () => { | ||
| const shape = { a: mockForeignZodV4String() }; | ||
| try { | ||
| normalizeRawShapeSchema(shape as never); | ||
| expect.unreachable('should have thrown'); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(TypeError); | ||
| expect((e as TypeError).cause).toBeDefined(); | ||
| } | ||
| }); | ||
|
|
||
| test('wraps and converts a well-formed raw shape for the output position', () => { | ||
| const wrapped = normalizeRawShapeSchema({ result: z.string().default('x') }, 'output'); | ||
| expect(wrapped).toBeDefined(); | ||
| expect(standardSchemaToJsonSchema(wrapped!, 'output').type).toBe('object'); | ||
| }); | ||
| }); | ||
|
|
||
| // Real second zod instance (npm alias zod-v40 -> zod@4.0.17): implements | ||
| // StandardSchemaV1 but not `~standard.jsonSchema`, like most currently | ||
| // installed zod versions. The SDK-bundled converter cannot walk schemas from | ||
| // this build, so raw shapes built from it are exactly the input that used to | ||
| // register fine and then crash every `tools/list`. | ||
| import * as zV40 from 'zod-v40'; | ||
|
|
||
| describe('normalizeRawShapeSchema real-instance coverage', () => { | ||
| test('native z.custom raw-shape field fails at normalization time (version-independent construct)', () => { | ||
| const shape = { c: z.custom<string>(v => typeof v === 'string') }; | ||
| expect(() => normalizeRawShapeSchema(shape as never)).toThrow(/could not be converted to JSON Schema/); | ||
| // The thrown message itself carries the converter's diagnosis, so the | ||
| // bundled-zod non-representable case is distinguishable from a foreign | ||
| // zod build without unwrapping `cause`. | ||
| expect(() => normalizeRawShapeSchema(shape as never)).toThrow(/Non-representable type|custom/i); | ||
| }); | ||
|
Check warning on line 152 in packages/core/test/util/zodCompat.test.ts
|
||
|
Comment on lines
+145
to
+152
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 second Extended reasoning...What the assertion claims to verify vs. what it actually verifies. The test |
||
|
|
||
| test('raw shape from a real foreign zod build fails at normalization time, not serve time', () => { | ||
| const shape = { a: zV40.z.string(), o: zV40.z.string().optional() }; | ||
| try { | ||
| normalizeRawShapeSchema(shape as never); | ||
| expect.unreachable('should have thrown'); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(TypeError); | ||
| expect(String(e)).toMatch(/could not be converted to JSON Schema/); | ||
| // the underlying converter error is preserved for debugging | ||
| expect(String((e as TypeError).cause)).toMatch(/Non-representable type/); | ||
| } | ||
| }); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.