@mcp-b/global 2.0.3-canary.1 → 2.0.3-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { IframeChildTransport, TabServerTransport } from "@mcp-b/transports";
2
2
  import { CallToolRequestSchema, GetPromptRequestSchema, ListPromptsRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, Server } from "@mcp-b/webmcp-ts-sdk";
3
3
  import { z } from "zod";
4
- import { fromJSONSchema, toJSONSchema } from "zod/v4";
4
+ import { zodToJsonSchema as zodToJsonSchema$1 } from "zod-to-json-schema";
5
5
 
6
6
  //#region src/logger.ts
7
7
  /**
@@ -92,14 +92,50 @@ function createLogger(namespace) {
92
92
  //#region src/validation.ts
93
93
  const logger$2 = createLogger("WebModelContext");
94
94
  /**
95
+ * Check if a schema is a Zod 4 schema.
96
+ * Zod 4 schemas have a `_zod` property, Zod 3 schemas have `_def`.
97
+ */
98
+ function isZod4Schema(schema) {
99
+ if (typeof schema !== "object" || schema === null) return false;
100
+ return "_zod" in schema;
101
+ }
102
+ /**
103
+ * Check if a schema is a Zod 3 schema.
104
+ */
105
+ function isZod3Schema(schema) {
106
+ if (typeof schema !== "object" || schema === null) return false;
107
+ return "_def" in schema && !("_zod" in schema);
108
+ }
109
+ let zodV4Module = null;
110
+ let zodV4LoadAttempted = false;
111
+ /**
112
+ * Attempt to load zod/v4 module. Returns null if not available (Zod 3.x).
113
+ */
114
+ async function getZodV4Module() {
115
+ if (zodV4LoadAttempted) return zodV4Module;
116
+ zodV4LoadAttempted = true;
117
+ try {
118
+ const mod = await import("zod/v4");
119
+ zodV4Module = {
120
+ toJSONSchema: mod.toJSONSchema,
121
+ fromJSONSchema: "fromJSONSchema" in mod ? mod.fromJSONSchema : void 0
122
+ };
123
+ } catch {
124
+ zodV4Module = null;
125
+ }
126
+ return zodV4Module;
127
+ }
128
+ function getZodV4ModuleSync() {
129
+ return zodV4Module;
130
+ }
131
+ getZodV4Module().catch(() => {});
132
+ /**
95
133
  * Detect if a schema is a Zod schema object (Record<string, ZodType>)
96
134
  * or a JSON Schema object.
97
135
  *
98
136
  * Uses duck-typing to detect Zod schemas:
99
137
  * - Zod 4 schemas have `_zod` property
100
138
  * - Zod 3 schemas have `_def` property
101
- *
102
- * Both are supported as of Zod 3.25+ (which includes Zod 4 under the hood).
103
139
  */
104
140
  function isZodSchema(schema) {
105
141
  if (typeof schema !== "object" || schema === null) return false;
@@ -108,44 +144,66 @@ function isZodSchema(schema) {
108
144
  if (values.length === 0) return false;
109
145
  for (const val of values) {
110
146
  if (val == null || typeof val !== "object") continue;
111
- const obj = val;
112
- if ("_zod" in obj || "_def" in obj) return true;
147
+ if (isZod4Schema(val) || isZod3Schema(val)) return true;
113
148
  }
114
149
  return false;
115
150
  }
116
151
  /**
117
- * Convert JSON Schema to Zod validator.
118
- * Uses fromJSONSchema from 'zod/v4' which is available in Zod 3.25+ and Zod 4.
152
+ * Convert Zod schema object to JSON Schema.
153
+ * Uses the appropriate method based on detected Zod version:
154
+ * - Zod 4: Built-in toJSONSchema from zod/v4
155
+ * - Zod 3: zod-to-json-schema external library
156
+ *
157
+ * @param schema - Record of Zod type definitions (e.g., { name: z.string(), age: z.number() })
158
+ * @returns JSON Schema object compatible with MCP InputSchema
119
159
  */
120
- function jsonSchemaToZod(jsonSchema) {
160
+ function zodToJsonSchema(schema) {
161
+ const zodObject = z.object(schema);
162
+ const zodV4 = getZodV4ModuleSync();
163
+ if (isZod4Schema(zodObject) && zodV4?.toJSONSchema) try {
164
+ const { $schema: _,...rest } = zodV4.toJSONSchema(zodObject, { target: "draft-7" });
165
+ return rest;
166
+ } catch (error) {
167
+ logger$2.warn("Zod 4 toJSONSchema failed, falling back to zod-to-json-schema:", error);
168
+ }
121
169
  try {
122
- return fromJSONSchema(jsonSchema);
170
+ const { $schema: _,...rest } = zodToJsonSchema$1(zodObject, {
171
+ strictUnions: true,
172
+ $refStrategy: "none"
173
+ });
174
+ return rest;
123
175
  } catch (error) {
124
- logger$2.warn("Failed to convert JSON Schema to Zod:", error);
125
- return z.object({}).passthrough();
176
+ logger$2.warn("zod-to-json-schema failed:", error);
177
+ return {
178
+ type: "object",
179
+ properties: {}
180
+ };
126
181
  }
127
182
  }
128
183
  /**
129
- * Convert Zod schema object to JSON Schema.
130
- * Uses toJSONSchema from 'zod/v4' which is available in Zod 3.25+ and Zod 4.
131
- *
132
- * Works with schemas created from both `import { z } from 'zod'` (Zod 3.25+ compat)
133
- * and `import { z } from 'zod/v4'` (native Zod 4).
184
+ * Convert JSON Schema to Zod validator.
185
+ * Only available in Zod 4.x via fromJSONSchema.
186
+ * Falls back to a passthrough validator in Zod 3.x.
134
187
  *
135
- * @param schema - Record of Zod type definitions (e.g., { name: z.string(), age: z.number() })
136
- * @returns JSON Schema object compatible with MCP InputSchema
188
+ * Note: When using Zod 3.x with JSON Schema input, server-side validation
189
+ * is skipped. The MCP client still validates against the JSON Schema.
137
190
  */
138
- function zodToJsonSchema(schema) {
139
- const { $schema: _,...rest } = toJSONSchema(z.object(schema));
140
- return rest;
191
+ function jsonSchemaToZod(jsonSchema) {
192
+ const zodV4 = getZodV4ModuleSync();
193
+ if (zodV4?.fromJSONSchema) try {
194
+ return zodV4.fromJSONSchema(jsonSchema);
195
+ } catch (error) {
196
+ logger$2.warn("fromJSONSchema failed:", error);
197
+ }
198
+ else logger$2.debug("fromJSONSchema not available (Zod 3.x). Server-side validation will be skipped for JSON Schema input. Upgrade to Zod 4.x for full JSON Schema validation support.");
199
+ return z.object({}).passthrough();
141
200
  }
142
201
  /**
143
202
  * Normalize a schema to both JSON Schema and Zod formats.
144
203
  * Detects which format is provided and converts to the other.
145
204
  *
146
205
  * Supports:
147
- * - Zod schemas from `import { z } from 'zod'` (Zod 3.25+ compat layer)
148
- * - Zod schemas from `import { z } from 'zod/v4'` (native Zod 4)
206
+ * - Zod schemas from Zod 3.x or Zod 4.x
149
207
  * - Plain JSON Schema objects
150
208
  */
151
209
  function normalizeSchema(schema) {