Uh oh!
There was an error while loading. Please reload this page.
registerTool: accept ZodType<object> for input and output schema - #816
Conversation
jjn1056
commented
Aug 21, 2025
+1 on merging this or at least getting feedback from the team. I ran into this problem myself just today. Some of the error conditions that the API I'm wrapping for LLM consumption are complex and the better I can hint the LLM in the response, the better we'll be able to properly recover. |
akshaylingamaneni
commented
Oct 8, 2025
guys, can we get this merged |
davidgilbertson
commented
Nov 4, 2025
It's disappointing that @ksinder went to the trouble of creating a PR for this and it's been ignored for many months. |
47678e4 to
d839335CompareUh oh!
There was an error while loading. Please reload this page.
demian85
commented
Nov 20, 2025
Can I get help with this please? #1148 |
This change was released in constschema=z.strictObject({key1: z.literal('value1'),key2: z.literal('value2'),});constmcpTool: Tool={name: 'Test tool',description: 'Test tool',title: 'Test tool',// just a dummy input and output schemainputSchema: schema,outputSchema: schema,};InputSchema and outputSchema have the following error. TLDR: 'type' is missing I think this pr is supposed to support the schema mentioned above directly. Please correct me if I am wrong constmcpTool: Tool={name: 'Test tool',description: 'Test tool',title: 'Test tool',// just a dummy input and output schemainputSchema: { ...schema,type: 'object'},outputSchema: { ...schema,type: 'object'},}; |
RahulLanjewar93
commented
Nov 27, 2025
The issue was due to a difference in exported types from the sdk. I was using low-level server, and the type I used for creating the tool was // GOOD import{RegisteredTool,RegisteredResource}from'@modelcontextprotocol/sdk/server/mcp';// BADimport{Tool,Resource}from'@modelcontextprotocol/sdk/types'; |
joshjg
commented
Dec 15, 2025
This appears to have regressed after the changes in #1040 - specifically because when validating output the SDK attempts to use https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/server/mcp.ts#L309 |
Hi @joshjg thanks for this - could you elaborate on what you mean that has regressed here? If you could file an issue that would be greatly appreciated! |
joshjg
commented
Dec 16, 2025
@felixweinberger Created #1308 with more details. |
Motivation and Context
See #588. This PR adds a backwards-compatible change to
registerToolonMcpServerto allow any Zod schema extendingZodType<object>instead of requiringinputSchemaandoutputSchemato conform toZodRawShape(:=Record<string, ZodType<any>>).The underlying schema validation and
zod-to-json-schemadependency already support this, so adding this plumbing expands SDK expressiveness and flexibility. Specifically, this unblocks MCP servers' ability to define tool schemas that usez.union(...)andz.intersection(...). Historically, some creators have had to wrap existing schemas in some wrapper object that has fixed keys, e.g.{bodyParams: z.union(...)}to work around this limitation.How Has This Been Tested?
Added automated tests in
mcp.test.tsand confirmed they pass locally withnpm run test.Also manually tried changing the test locally to use
inputSchemas for types that aren't objects at the top level -- e.g.z.number()andz.union([z.number(), z.object({})]), and confirmed typechecking fails (which is good, since I'm guessing we still want guardrails to push toward objects for params and responses. If this ever changes in the future it's easy enough to change allZodType<object>toZodTypeAny!)Breaking Changes
Should be backwards-compatible in the SDK with existing server code. This is purely additive since we can easily dynamically distinguish between existing
ZodRawShapes being passed in vs. directZodType<object>and branch accordingly.ZodType<object>is added as a union type variant forserver.registerTool, and isn't mandatory.Types of changes
Checklist
Additional context
N/A