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
5 changes: 5 additions & 0 deletions .changeset/fix-openai-strict-required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/sdk": patch
---

fix: include required field in JSON Schema when Zod .optional() is absent
17 changes: 16 additions & 1 deletion packages/core/src/util/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ 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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was removed, probably needs to go to standardSchemaToJsonSchema now

return z.toJSONSchema(schema, options) as Record<string, unknown>;
const jsonSchema = z.toJSONSchema(schema, options) as Record<string, unknown>;

// OpenAI strict JSON schema mode requires the `required` field to always
// be present on object schemas, even when empty. Zod's toJSONSchema omits
// it for empty objects (e.g. z.object({}).strict()), causing tool
// registration to fail with OpenAI's strict mode.
// See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1659
if (
jsonSchema.type === 'object' &&
jsonSchema.properties !== undefined &&
!Array.isArray(jsonSchema.required)
) {
jsonSchema.required = [];
}

return jsonSchema;
}

/**
Expand Down
Loading