Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .changeset/empty-zod-required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@modelcontextprotocol/core": patch
"@modelcontextprotocol/server": patch
---

Ensure object schemas always include required field for OpenAI strict JSON schema compatibility
8 changes: 7 additions & 1 deletion packages/core/src/util/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export type SchemaOutput<T extends AnySchema> = z.output<T>;
* Converts a Zod schema to JSON Schema.
*/
export function schemaToJson(schema: AnySchema, options?: { io?: 'input' | 'output' }): Record<string, unknown> {
return z.toJSONSchema(schema, options) as Record<string, unknown>;
const jsonSchema = z.toJSONSchema(schema, options) as Record<string, unknown>;
// Ensure object schemas always include 'required' (even if empty)
// for compatibility with OpenAI strict JSON schema mode
if (jsonSchema.type === 'object' && !('required' in jsonSchema)) {
jsonSchema.required = [];
}
return jsonSchema;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,8 @@ function createToolExecutor(inputSchema: AnySchema | undefined, handler: AnyTool

const EMPTY_OBJECT_JSON_SCHEMA = {
type: 'object' as const,
properties: {}
properties: {},
required: [] as string[]
};

/**
Expand Down
3 changes: 2 additions & 1 deletion test/integration/test/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ describe('Zod v4', () => {
expect(result.tools[0]!.name).toBe('test');
expect(result.tools[0]!.inputSchema).toEqual({
type: 'object',
properties: {}
properties: {},
required: []
});

// Adding the tool before the connection was established means no notification was sent
Expand Down
Loading