-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Describe the bug
Tool output validation fails with the following error when the tool's outputSchema is an optional, nullable, nullish, or union schema (or, most likely anything besides a plain object schema).
Cannot read properties of undefined (reading '_zod')
When validating output the SDK attempts to use normalizeObjectSchema, which returns undefined for anything besides object schemas.
To Reproduce
Steps to reproduce the behavior:
- Define an
outputSchemawith something other than a plain object orz.objectschema. - Implement the tool so that it returns
structuredContentthat conforms to this schema. - Invoke the tool.
// Works:
outputSchema: z.object({ data: z.string() })
// Fails:
outputSchema: z.object({ data: z.string() }).optional()
outputSchema: z.object({ data: z.string() }).nullable()
outputSchema: z.object({ data: z.string() }).nullish()
outputSchema: z.union([z.object({ data: z.string() }), z.object({ value: z.string() })])
Expected behavior
The SDK should be able to validate output based on Zod schemas which represent a JSON object, even if it is optional or a union of possible object schemas. Based on the spec, null is not a valid value for structuredContent, so nullable and nullish schemas should not be allowed, either through more restrictive typing of outputSchema or runtime validation with a clear error message.
Logs
at isZ4Schema ($PROJECT/node_modules/@modelcontextprotocol/sdk/dist/cjs/server/zod-compat.js:46:21)
at safeParseAsync ($PROJECT/node_modules/@modelcontextprotocol/sdk/dist/cjs/server/zod-compat.js:80:9)
at McpServer.validateToolOutput ($PROJECT/node_modules/@modelcontextprotocol/sdk/dist/cjs/server/mcp.js:205:70)
at $PROJECT/node_modules/@modelcontextprotocol/sdk/dist/cjs/server/mcp.js:135:28
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async wrappedHandler ($PROJECT/node_modules/@modelcontextprotocol/sdk/dist/cjs/server/index.js:131:32)
Additional context
"@modelcontextprotocol/sdk": "1.24.3",
"zod": "4.1.13"
I was expecting this to work based on #816 but it has possibly regressed with #1040