Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-output-schema-wrappers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/sdk': patch
---

Fix v1.x server tool output validation for optional, nullable, nullish, and union Zod output schemas.
8 changes: 5 additions & 3 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ export class McpServer {
);
}

// if the tool has an output schema, validate structured content
const outputObj = normalizeObjectSchema(tool.outputSchema) as AnyObjectSchema;
const parseResult = await safeParseAsync(outputObj, result.structuredContent);
// Try to normalize to object schema first (for raw shapes and object schemas)
// If that fails, use the schema directly (for union/optional/nullable/etc)
const outputObj = normalizeObjectSchema(tool.outputSchema);
const schemaToParse = outputObj ?? tool.outputSchema;
const parseResult = await safeParseAsync(schemaToParse, result.structuredContent);
if (!parseResult.success) {
const error = 'error' in parseResult ? parseResult.error : 'Unknown error';
const errorMessage = getParseErrorMessage(error);
Expand Down
68 changes: 68 additions & 0 deletions test/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,74 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
expect(JSON.parse(textContent.text)).toEqual(result.structuredContent);
});

test('should validate structuredContent against non-object-wrapper output schemas', async () => {
const mcpServer = new McpServer({
name: 'test server',
version: '1.0'
});

const client = new Client({
name: 'test client',
version: '1.0'
});

const schemaCases = [
{
name: 'optional',
outputSchema: z.object({ data: z.string() }).optional(),
structuredContent: { data: 'hello' }
},
{
name: 'nullable',
outputSchema: z.object({ data: z.string() }).nullable(),
structuredContent: { data: 'hello' }
},
{
name: 'nullish',
outputSchema: z.object({ data: z.string() }).nullish(),
structuredContent: { data: 'hello' }
},
{
name: 'union',
outputSchema: z.union([z.object({ data: z.string() }), z.object({ value: z.string() })]),
structuredContent: { value: 'hello' }
}
];

for (const { name, outputSchema, structuredContent } of schemaCases) {
mcpServer.registerTool(
`test-${name}`,
{
description: `Test tool with ${name} output schema`,
outputSchema
},
async () => ({
structuredContent,
content: [
{
type: 'text',
text: JSON.stringify(structuredContent)
}
]
})
);
}

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();

await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);

for (const { name, structuredContent } of schemaCases) {
const result = await client.callTool({
name: `test-${name}`,
arguments: {}
});

expect(result.isError).not.toBe(true);
expect(result.structuredContent).toEqual(structuredContent);
}
});

/***
* Test: Tool with Output Schema Must Provide Structured Content
*/
Expand Down
Loading