Skip to content

Commit d75057f

Browse files
committed
fix: add required field to empty object schemas for OpenAI strict mode (#1659)
1 parent ccb78f2 commit d75057f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

packages/core/src/util/schema.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ export type SchemaOutput<T extends AnySchema> = z.output<T>;
2626
* Converts a Zod schema to JSON Schema.
2727
*/
2828
export function schemaToJson(schema: AnySchema, options?: { io?: 'input' | 'output' }): Record<string, unknown> {
29-
return z.toJSONSchema(schema, options) as Record<string, unknown>;
29+
const jsonSchema = z.toJSONSchema(schema, options) as Record<string, unknown>;
30+
31+
// OpenAI strict JSON schema mode requires the `required` field to always
32+
// be present on object schemas, even when empty. Zod's toJSONSchema omits
33+
// it for empty objects (e.g. z.object({}).strict()), causing tool
34+
// registration to fail with OpenAI's strict mode.
35+
// See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1659
36+
if (
37+
jsonSchema.type === 'object' &&
38+
jsonSchema.properties !== undefined &&
39+
!Array.isArray(jsonSchema.required)
40+
) {
41+
jsonSchema.required = [];
42+
}
43+
44+
return jsonSchema;
3045
}
3146

3247
/**

0 commit comments

Comments
 (0)