File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,22 @@ export type SchemaOutput<T extends AnySchema> = z.output<T>;
2626 * Converts a Zod schema to JSON Schema.
2727 */
2828export 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/**
You can’t perform that action at this time.
0 commit comments