-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(compat): registerTool/registerPrompt accept raw Zod shape, auto-wrap with z.object() #1901
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 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
27e4ddf
feat(compat): registerTool/registerPrompt accept raw Zod shape (auto-…
felixweinberger 5266131
fix: isZodRawShape treats empty object as raw shape (matches v1)
felixweinberger f2fdbe7
docs: changeset wording aligns with @deprecated overloads (not first-…
felixweinberger 9576f20
docs: clarify isZodRawShape only supports Zod values for auto-wrap
felixweinberger 0152b26
fix(compat): narrow ZodRawShape to Zod-only (detector + type); add ou…
felixweinberger 1af9ed2
test(compat): add e2e raw-shape tools/call test; drop vestigial warn-…
felixweinberger 3155be7
Merge branch 'main' into fweinberger/v2-bc-register-rawshape
KKonstantinov aba1d39
feat(compat): widen completable() constraint to StandardSchemaV1
felixweinberger 0febd83
refactor(compat): move zod helpers to zodCompat.ts; throw on invalid …
felixweinberger 7e80880
Merge branch 'main' into fweinberger/v2-bc-register-rawshape
felixweinberger c75bc88
test(compat): move zod-compat tests to zodCompat.test.ts; tighten nor…
felixweinberger a6b25ee
fix(compat): reject Zod v3 fields in raw-shape auto-wrap with actiona…
felixweinberger b5854c1
fix(compat): require plain-object prototype in isZodRawShape; null-gu…
felixweinberger 9b7ee90
Merge branch 'main' into fweinberger/v2-bc-register-rawshape
felixweinberger 27e2c4b
fix(compat): pass StandardSchema without ~standard.jsonSchema through…
felixweinberger 617830b
Merge branch 'main' into fweinberger/v2-bc-register-rawshape
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/core': patch | ||
| '@modelcontextprotocol/server': patch | ||
| --- | ||
|
|
||
| `registerTool`/`registerPrompt` accept a raw Zod shape (`{ field: z.string() }`) for `inputSchema`/`outputSchema`/`argsSchema` in addition to a wrapped Standard Schema. Raw shapes are auto-wrapped with `z.object()`. The raw-shape overloads are `@deprecated`; prefer wrapping with `z.object()`. |
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
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
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
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
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,121 @@ | ||
| import type { JSONRPCMessage } from '@modelcontextprotocol/core'; | ||
| import { InMemoryTransport, isStandardSchema, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import * as z from 'zod/v4'; | ||
| import { McpServer } from '../../src/index.js'; | ||
| import { completable } from '../../src/server/completable.js'; | ||
|
|
||
| describe('registerTool/registerPrompt accept raw Zod shape (auto-wrapped)', () => { | ||
| it('registerTool accepts a raw shape for inputSchema and auto-wraps it', () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| server.registerTool('a', { inputSchema: { x: z.number() } }, async ({ x }) => ({ | ||
| content: [{ type: 'text' as const, text: String(x) }] | ||
| })); | ||
| server.registerTool('b', { inputSchema: { y: z.number() } }, async ({ y }) => ({ | ||
| content: [{ type: 'text' as const, text: String(y) }] | ||
| })); | ||
|
|
||
| const tools = (server as unknown as { _registeredTools: Record<string, { inputSchema?: unknown }> })._registeredTools; | ||
| expect(Object.keys(tools)).toEqual(['a', 'b']); | ||
| // raw shape was wrapped into a Standard Schema (z.object) | ||
| expect(isStandardSchema(tools['a']?.inputSchema)).toBe(true); | ||
| }); | ||
|
|
||
| it('registerTool accepts a raw shape for outputSchema and auto-wraps it', () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| server.registerTool('out', { inputSchema: { n: z.number() }, outputSchema: { result: z.string() } }, async ({ n }) => ({ | ||
| content: [{ type: 'text' as const, text: String(n) }], | ||
| structuredContent: { result: String(n) } | ||
| })); | ||
|
|
||
| const tools = (server as unknown as { _registeredTools: Record<string, { outputSchema?: unknown }> })._registeredTools; | ||
| expect(isStandardSchema(tools['out']?.outputSchema)).toBe(true); | ||
| }); | ||
|
|
||
| it('registerTool with z.object() inputSchema also works (passthrough, no auto-wrap)', () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| server.registerTool('c', { inputSchema: z.object({ x: z.number() }) }, async ({ x }) => ({ | ||
| content: [{ type: 'text' as const, text: String(x) }] | ||
| })); | ||
|
|
||
| const tools = (server as unknown as { _registeredTools: Record<string, { inputSchema?: unknown }> })._registeredTools; | ||
| expect(isStandardSchema(tools['c']?.inputSchema)).toBe(true); | ||
| }); | ||
|
|
||
| it('registerPrompt accepts a raw shape for argsSchema', () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| server.registerPrompt('p', { argsSchema: { topic: z.string() } }, async ({ topic }) => ({ | ||
| messages: [{ role: 'user' as const, content: { type: 'text' as const, text: topic } }] | ||
| })); | ||
|
|
||
| const prompts = (server as unknown as { _registeredPrompts: Record<string, { argsSchema?: unknown }> })._registeredPrompts; | ||
| expect(Object.keys(prompts)).toContain('p'); | ||
| expect(isStandardSchema(prompts['p']?.argsSchema)).toBe(true); | ||
| }); | ||
|
|
||
| it('registerPrompt raw shape accepts completable() fields (v1 pattern)', () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| server.registerPrompt( | ||
| 'p', | ||
| { | ||
| argsSchema: { | ||
| language: completable(z.string(), v => ['typescript', 'python'].filter(l => l.startsWith(v))) | ||
| } | ||
| }, | ||
| async ({ language }) => ({ | ||
| messages: [{ role: 'user' as const, content: { type: 'text' as const, text: language } }] | ||
| }) | ||
| ); | ||
|
|
||
| const prompts = (server as unknown as { _registeredPrompts: Record<string, { argsSchema?: unknown }> })._registeredPrompts; | ||
| expect(isStandardSchema(prompts['p']?.argsSchema)).toBe(true); | ||
| }); | ||
|
|
||
| it('callback receives validated, typed args end-to-end via tools/call', async () => { | ||
| const server = new McpServer({ name: 't', version: '1.0.0' }); | ||
|
|
||
| let received: { x: number } | undefined; | ||
| server.registerTool('echo', { inputSchema: { x: z.number() } }, async args => { | ||
| received = args; | ||
| return { content: [{ type: 'text' as const, text: String(args.x) }] }; | ||
| }); | ||
|
|
||
| const [client, srv] = InMemoryTransport.createLinkedPair(); | ||
| await server.connect(srv); | ||
| await client.start(); | ||
|
|
||
| const responses: JSONRPCMessage[] = []; | ||
| client.onmessage = m => responses.push(m); | ||
|
|
||
| await client.send({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'initialize', | ||
| params: { | ||
| protocolVersion: LATEST_PROTOCOL_VERSION, | ||
| capabilities: {}, | ||
| clientInfo: { name: 'c', version: '1.0.0' } | ||
| } | ||
| } as JSONRPCMessage); | ||
| await client.send({ jsonrpc: '2.0', method: 'notifications/initialized' } as JSONRPCMessage); | ||
| await client.send({ | ||
| jsonrpc: '2.0', | ||
| id: 2, | ||
| method: 'tools/call', | ||
| params: { name: 'echo', arguments: { x: 7 } } | ||
| } as JSONRPCMessage); | ||
|
|
||
| await vi.waitFor(() => expect(responses.some(r => 'id' in r && r.id === 2)).toBe(true)); | ||
|
|
||
| expect(received).toEqual({ x: 7 }); | ||
| const result = responses.find(r => 'id' in r && r.id === 2) as { result?: { content: Array<{ text: string }> } }; | ||
| expect(result.result?.content[0]?.text).toBe('7'); | ||
|
|
||
| await server.close(); | ||
| }); | ||
| }); |
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.