@mcp-b/global 2.0.3-canary.0 → 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,6 +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 { zodToJsonSchema as zodToJsonSchema$1 } from "zod-to-json-schema";
4
5
 
5
6
  //#region src/logger.ts
6
7
  /**
@@ -91,14 +92,50 @@ function createLogger(namespace) {
91
92
  //#region src/validation.ts
92
93
  const logger$2 = createLogger("WebModelContext");
93
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
+ /**
94
133
  * Detect if a schema is a Zod schema object (Record<string, ZodType>)
95
134
  * or a JSON Schema object.
96
135
  *
97
136
  * Uses duck-typing to detect Zod schemas:
98
137
  * - Zod 4 schemas have `_zod` property
99
138
  * - Zod 3 schemas have `_def` property
100
- *
101
- * Both are supported as of Zod 3.25+ (which includes Zod 4 under the hood).
102
139
  */
103
140
  function isZodSchema(schema) {
104
141
  if (typeof schema !== "object" || schema === null) return false;
@@ -107,47 +144,66 @@ function isZodSchema(schema) {
107
144
  if (values.length === 0) return false;
108
145
  for (const val of values) {
109
146
  if (val == null || typeof val !== "object") continue;
110
- const obj = val;
111
- if ("_zod" in obj || "_def" in obj) return true;
147
+ if (isZod4Schema(val) || isZod3Schema(val)) return true;
112
148
  }
113
149
  return false;
114
150
  }
115
151
  /**
116
- * Convert JSON Schema to Zod validator.
117
- * Uses z.fromJSONSchema() which is available in Zod 4.2+.
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
118
156
  *
119
- * Works with both Zod 3.25+ and Zod 4.
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
120
159
  */
121
- 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
+ }
122
169
  try {
123
- return z.fromJSONSchema(jsonSchema);
170
+ const { $schema: _,...rest } = zodToJsonSchema$1(zodObject, {
171
+ strictUnions: true,
172
+ $refStrategy: "none"
173
+ });
174
+ return rest;
124
175
  } catch (error) {
125
- logger$2.warn("Failed to convert JSON Schema to Zod:", error);
126
- return z.object({}).passthrough();
176
+ logger$2.warn("zod-to-json-schema failed:", error);
177
+ return {
178
+ type: "object",
179
+ properties: {}
180
+ };
127
181
  }
128
182
  }
129
183
  /**
130
- * Convert Zod schema object to JSON Schema.
131
- * Uses the toJSONSchema function from zod/v4.
132
- *
133
- * Works with schemas created from both `import { z } from 'zod'` (Zod 3.25+ compat)
134
- * 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.
135
187
  *
136
- * @param schema - Record of Zod type definitions (e.g., { name: z.string(), age: z.number() })
137
- * @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.
138
190
  */
139
- function zodToJsonSchema(schema) {
140
- const zodObject = z.object(schema);
141
- const { $schema: _,...rest } = z.toJSONSchema(zodObject);
142
- 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();
143
200
  }
144
201
  /**
145
202
  * Normalize a schema to both JSON Schema and Zod formats.
146
203
  * Detects which format is provided and converts to the other.
147
204
  *
148
205
  * Supports:
149
- * - Zod schemas from `import { z } from 'zod'` (Zod 3.25+ compat layer)
150
- * - Zod schemas from `import { z } from 'zod/v4'` (native Zod 4)
206
+ * - Zod schemas from Zod 3.x or Zod 4.x
151
207
  * - Plain JSON Schema objects
152
208
  */
153
209
  function normalizeSchema(schema) {