diff --git a/README.md b/README.md index 43b62ac60..edb2ebf08 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,27 @@ const server = new McpServer({ }); ``` +#### JSON Schema Specification + +You can configure the JSON Schema target specification for better compatibility with different systems, particularly OpenAI endpoints: + +```typescript +const server = new McpServer({ + name: "my-app", + version: "1.0.0" +}, { + jsonSchemaSpec: 'openApi3' // For OpenAI compatibility +}); +``` + +Supported values include: +- `'jsonSchema7'` (default) - JSON Schema Draft 7 +- `'openApi3'` - OpenAPI 3.0 specification (recommended for OpenAI compatibility) +- `'jsonSchema4'` - JSON Schema Draft 4 +- `'jsonSchema2019-09'` - JSON Schema Draft 2019-09 + +This setting affects how tool input schemas are generated from Zod schemas, ensuring they match the expected format for your target system. + ### Resources Resources are how you expose data to LLMs. They're similar to GET endpoints in a REST API - they provide data but shouldn't perform significant computation or have side effects: diff --git a/src/server/index.ts b/src/server/index.ts index 970657358..9fb862d53 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -37,6 +37,7 @@ import { LoggingLevelSchema } from "../types.js"; import Ajv from "ajv"; +import type { Targets } from "zod-to-json-schema"; export type ServerOptions = ProtocolOptions & { /** @@ -48,6 +49,11 @@ export type ServerOptions = ProtocolOptions & { * Optional instructions describing how to use the server and its features. */ instructions?: string; + + /** + * Optional spec for JSON Schema to allow fully compatible use with OpenAI endpoints. + */ + jsonSchemaSpec?: Targets; }; /** @@ -88,6 +94,7 @@ export class Server< private _clientVersion?: Implementation; private _capabilities: ServerCapabilities; private _instructions?: string; + public _jsonSchemaSpec?: Targets = 'jsonSchema7'; /** * Callback for when initialization has fully completed (i.e., the client has sent an `initialized` notification). @@ -104,6 +111,7 @@ export class Server< super(options); this._capabilities = options?.capabilities ?? {}; this._instructions = options?.instructions; + this._jsonSchemaSpec = options?.jsonSchemaSpec; this.setRequestHandler(InitializeRequestSchema, (request) => this._oninitialize(request), diff --git a/src/server/mcp.ts b/src/server/mcp.ts index ac4880c99..b281275f2 100644 --- a/src/server/mcp.ts +++ b/src/server/mcp.ts @@ -119,6 +119,7 @@ export class McpServer { description: tool.description, inputSchema: tool.inputSchema ? (zodToJsonSchema(tool.inputSchema, { + target: this.server._jsonSchemaSpec, strictUnions: true, }) as Tool["inputSchema"]) : EMPTY_OBJECT_JSON_SCHEMA,