Drop the zod 3 JSON Schema fallback
The zod peer range is ^4.2.0, whose schemas implement ~standard.jsonSchema, so the lazy zod/v4 toJSONSchema fallback for zod 3.25 is dead code. Re-export StandardSchemaWithJSON from the client package instead of keeping a structurally identical local copy.
This commit is contained in:
parent
0a477bb25d
commit
09bf2aae3c
1 changed files with 11 additions and 33 deletions
|
|
@ -4,36 +4,22 @@ import type {
|
||||||
StandardTypedV1,
|
StandardTypedV1,
|
||||||
} from "@standard-schema/spec";
|
} from "@standard-schema/spec";
|
||||||
|
|
||||||
export type { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 };
|
import type { StandardSchemaWithJSON } from "@modelcontextprotocol/client";
|
||||||
|
|
||||||
// TODO(sdk-v2): once @modelcontextprotocol/core v2 is stable, import
|
|
||||||
// StandardSchemaWithJSON / standardSchemaToJsonSchema / validateStandardSchema
|
|
||||||
// from there and delete this file. At that point decide whether to tighten
|
|
||||||
// App.registerTool to StandardSchemaWithJSON (drops zod 3 from the peer range
|
|
||||||
// and the lazy z.toJSONSchema fallback below).
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A schema that implements both Standard Schema (validation) and Standard JSON
|
* A schema that implements both Standard Schema (validation) and Standard JSON
|
||||||
* Schema (serialization). Zod v4, ArkType, and Valibot (via
|
* Schema (serialization). Zod v4, ArkType, and Valibot (via
|
||||||
* `@valibot/to-json-schema`) all satisfy this.
|
* `@valibot/to-json-schema`) all satisfy this. Re-exported from the SDK so
|
||||||
*
|
* View authors can import it alongside {@link app!App `App`}.
|
||||||
* Mirrors the type of the same name in `@modelcontextprotocol/core` v2 so that
|
|
||||||
* bumping to that package later is a drop-in import swap.
|
|
||||||
*
|
*
|
||||||
* @see https://standardschema.dev/
|
* @see https://standardschema.dev/
|
||||||
* @see https://github.com/modelcontextprotocol/typescript-sdk/pull/1689
|
|
||||||
*/
|
*/
|
||||||
export interface StandardSchemaWithJSON<Input = unknown, Output = Input> {
|
export type {
|
||||||
readonly "~standard": StandardSchemaV1.Props<Input, Output> &
|
StandardJSONSchemaV1,
|
||||||
StandardJSONSchemaV1.Props<Input, Output>;
|
StandardSchemaV1,
|
||||||
}
|
StandardSchemaWithJSON,
|
||||||
|
StandardTypedV1,
|
||||||
export namespace StandardSchemaWithJSON {
|
};
|
||||||
export type InferInput<S extends StandardTypedV1> =
|
|
||||||
StandardTypedV1.InferInput<S>;
|
|
||||||
export type InferOutput<S extends StandardTypedV1> =
|
|
||||||
StandardTypedV1.InferOutput<S>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** JSON-Schema target draft used for tool input/output schemas (matches core MCP). */
|
/** JSON-Schema target draft used for tool input/output schemas (matches core MCP). */
|
||||||
const TARGET = { target: "draft-2020-12" } as const;
|
const TARGET = { target: "draft-2020-12" } as const;
|
||||||
|
|
@ -41,11 +27,8 @@ const TARGET = { target: "draft-2020-12" } as const;
|
||||||
/**
|
/**
|
||||||
* Serialize a Standard Schema to JSON Schema for the given direction.
|
* Serialize a Standard Schema to JSON Schema for the given direction.
|
||||||
*
|
*
|
||||||
* Uses `~standard.jsonSchema` when present (zod v4, ArkType, Valibot, …).
|
* Requires `~standard.jsonSchema` (zod v4, ArkType, Valibot, …); schemas
|
||||||
* Falls back to a lazy `zod/v4` import for zod v3.25.x — which implements
|
* without it throw.
|
||||||
* `~standard.validate` but not yet `~standard.jsonSchema` — so the existing
|
|
||||||
* `^3.25.0 || ^4.0.0` peer range keeps working. Non-zod schemas without
|
|
||||||
* `jsonSchema` throw.
|
|
||||||
*/
|
*/
|
||||||
export async function standardSchemaToJsonSchema(
|
export async function standardSchemaToJsonSchema(
|
||||||
schema: StandardSchemaV1,
|
schema: StandardSchemaV1,
|
||||||
|
|
@ -57,11 +40,6 @@ export async function standardSchemaToJsonSchema(
|
||||||
if (std.jsonSchema) {
|
if (std.jsonSchema) {
|
||||||
return std.jsonSchema[io](TARGET);
|
return std.jsonSchema[io](TARGET);
|
||||||
}
|
}
|
||||||
if (std.vendor === "zod") {
|
|
||||||
const { z } = await import("zod/v4");
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- bridging StandardSchemaV1 → zod's $ZodType for the v3.25 fallback
|
|
||||||
return z.toJSONSchema(schema as any, { io });
|
|
||||||
}
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Schema (vendor: ${std.vendor}) does not implement Standard JSON Schema (~standard.jsonSchema). ` +
|
`Schema (vendor: ${std.vendor}) does not implement Standard JSON Schema (~standard.jsonSchema). ` +
|
||||||
`Use a library that does (zod v4, ArkType, Valibot) or wrap your schema accordingly.`,
|
`Use a library that does (zod v4, ArkType, Valibot) or wrap your schema accordingly.`,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue