fix(core): make fromJsonSchema() use runtime-aware default validator … (#1825)
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
parent
2fd7f5ff26
commit
5f32a90f0d
6 changed files with 74 additions and 6 deletions
9
packages/client/src/fromJsonSchema.ts
Normal file
9
packages/client/src/fromJsonSchema.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/client/_shims';
|
||||
import type { JsonSchemaType, jsonSchemaValidator, StandardSchemaWithJSON } from '@modelcontextprotocol/core';
|
||||
import { fromJsonSchema as coreFromJsonSchema } from '@modelcontextprotocol/core';
|
||||
|
||||
let _defaultValidator: jsonSchemaValidator | undefined;
|
||||
|
||||
export function fromJsonSchema<T = unknown>(schema: JsonSchemaType, validator?: jsonSchemaValidator): StandardSchemaWithJSON<T, T> {
|
||||
return coreFromJsonSchema<T>(schema, validator ?? (_defaultValidator ??= new DefaultJsonSchemaValidator()));
|
||||
}
|
||||
|
|
@ -73,5 +73,8 @@ export { StreamableHTTPClientTransport } from './client/streamableHttp.js';
|
|||
// experimental exports
|
||||
export { ExperimentalClientTasks } from './experimental/tasks/client.js';
|
||||
|
||||
// runtime-aware wrapper (shadows core/public's fromJsonSchema with optional validator)
|
||||
export { fromJsonSchema } from './fromJsonSchema.js';
|
||||
|
||||
// re-export curated public API from core
|
||||
export * from '@modelcontextprotocol/core/public';
|
||||
|
|
|
|||
|
|
@ -138,5 +138,6 @@ export type { StandardSchemaWithJSON } from '../../util/standardSchema.js';
|
|||
export { AjvJsonSchemaValidator } from '../../validators/ajvProvider.js';
|
||||
export type { CfWorkerSchemaDraft } from '../../validators/cfWorkerProvider.js';
|
||||
export { CfWorkerJsonSchemaValidator } from '../../validators/cfWorkerProvider.js';
|
||||
export { fromJsonSchema } from '../../validators/fromJsonSchema.js';
|
||||
// fromJsonSchema is intentionally NOT exported here — the server and client packages
|
||||
// provide runtime-aware wrappers that default to the appropriate validator via _shims.
|
||||
export type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator, JsonSchemaValidatorResult } from '../../validators/types.js';
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ import type { JsonSchemaType, jsonSchemaValidator } from './types.js';
|
|||
* The callback arguments will be typed `unknown` (raw JSON Schema has no TypeScript
|
||||
* types attached). Cast at the call site, or use the generic `fromJsonSchema<MyType>(...)`.
|
||||
*
|
||||
* @param schema - A JSON Schema object describing the expected shape
|
||||
* @param validator - A validator provider. When importing `fromJsonSchema` from
|
||||
* `@modelcontextprotocol/server` or `@modelcontextprotocol/client`, a runtime-appropriate
|
||||
* default is provided automatically (AJV on Node.js, CfWorker on edge runtimes).
|
||||
*
|
||||
* @example
|
||||
* ```ts source="./fromJsonSchema.examples.ts#fromJsonSchema_basicUsage"
|
||||
* const inputSchema = fromJsonSchema<{ name: string }>(
|
||||
|
|
|
|||
9
packages/server/src/fromJsonSchema.ts
Normal file
9
packages/server/src/fromJsonSchema.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import type { JsonSchemaType, jsonSchemaValidator, StandardSchemaWithJSON } from '@modelcontextprotocol/core';
|
||||
import { fromJsonSchema as coreFromJsonSchema } from '@modelcontextprotocol/core';
|
||||
import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';
|
||||
|
||||
let _defaultValidator: jsonSchemaValidator | undefined;
|
||||
|
||||
export function fromJsonSchema<T = unknown>(schema: JsonSchemaType, validator?: jsonSchemaValidator): StandardSchemaWithJSON<T, T> {
|
||||
return coreFromJsonSchema<T>(schema, validator ?? (_defaultValidator ??= new DefaultJsonSchemaValidator()));
|
||||
}
|
||||
|
|
@ -43,5 +43,8 @@ export type { CreateTaskRequestHandler, TaskRequestHandler, ToolTaskHandler } fr
|
|||
export { ExperimentalMcpServerTasks } from './experimental/tasks/mcpServer.js';
|
||||
export { ExperimentalServerTasks } from './experimental/tasks/server.js';
|
||||
|
||||
// runtime-aware wrapper (shadows core/public's fromJsonSchema with optional validator)
|
||||
export { fromJsonSchema } from './fromJsonSchema.js';
|
||||
|
||||
// re-export curated public API from core
|
||||
export * from '@modelcontextprotocol/core/public';
|
||||
|
|
|
|||
Loading…
Reference in a new issue