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

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,5 +1,6 @@
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
+ import { jsonSchemaToZod } from "@n8n/json-schema-to-zod";
3
4
  import { z } from "zod";
4
5
  import { zodToJsonSchema as zodToJsonSchema$1 } from "zod-to-json-schema";
5
6
 
@@ -91,145 +92,101 @@ function createLogger(namespace) {
91
92
  //#endregion
92
93
  //#region src/validation.ts
93
94
  const logger$2 = createLogger("WebModelContext");
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
- /**
133
- * Detect if a schema is a Zod schema object (Record<string, ZodType>)
134
- * or a JSON Schema object.
135
- *
136
- * Uses duck-typing to detect Zod schemas:
137
- * - Zod 4 schemas have `_zod` property
138
- * - Zod 3 schemas have `_def` property
139
- */
95
+ const nativeToJsonSchema = z.toJSONSchema;
96
+ const hasNativeToJSONSchema = typeof nativeToJsonSchema === "function";
97
+ const isRecord = (value) => typeof value === "object" && value !== null;
98
+ const isZod4Schema = (schema) => isRecord(schema) && "_zod" in schema;
99
+ const isZod3Schema = (schema) => isRecord(schema) && "_def" in schema && !("_zod" in schema);
100
+ const stripSchemaMeta = (schema) => {
101
+ const { $schema: _,...rest } = schema;
102
+ return rest;
103
+ };
104
+ const isOptionalSchema = (schema) => {
105
+ const typeName = schema._def?.typeName;
106
+ return typeName === "ZodOptional" || typeName === "ZodDefault";
107
+ };
140
108
  function isZodSchema(schema) {
141
- if (typeof schema !== "object" || schema === null) return false;
109
+ if (!isRecord(schema)) return false;
142
110
  if ("type" in schema && typeof schema.type === "string") return false;
143
111
  const values = Object.values(schema);
144
112
  if (values.length === 0) return false;
145
- for (const val of values) {
146
- if (val == null || typeof val !== "object") continue;
147
- if (isZod4Schema(val) || isZod3Schema(val)) return true;
148
- }
149
- return false;
113
+ return values.some((value) => isZod4Schema(value) || isZod3Schema(value));
150
114
  }
151
- /**
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
159
- */
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;
115
+ const hasZod4Schemas = (schema) => Object.values(schema).some((value) => isZod4Schema(value));
116
+ const tryNativeZodToJsonSchema = (schema) => {
117
+ if (!hasZod4Schemas(schema) || !hasNativeToJSONSchema) return null;
118
+ try {
119
+ return stripSchemaMeta(nativeToJsonSchema(z.object(schema), { target: "draft-7" }));
166
120
  } catch (error) {
167
- logger$2.warn("Zod 4 toJSONSchema failed, falling back to zod-to-json-schema:", error);
121
+ logger$2.warn("z.toJSONSchema failed, falling back to zod-to-json-schema:", error);
122
+ return null;
168
123
  }
169
- try {
170
- const { $schema: _,...rest } = zodToJsonSchema$1(zodObject, {
124
+ };
125
+ const fallbackZodToJsonSchema = (schema) => {
126
+ const properties = {};
127
+ const required = [];
128
+ for (const [key, zodSchema] of Object.entries(schema)) {
129
+ properties[key] = stripSchemaMeta(zodToJsonSchema$1(zodSchema, {
171
130
  strictUnions: true,
172
131
  $refStrategy: "none"
173
- });
174
- return rest;
132
+ }));
133
+ if (!isOptionalSchema(zodSchema)) required.push(key);
134
+ }
135
+ const result = {
136
+ type: "object",
137
+ properties
138
+ };
139
+ if (required.length > 0) result.required = required;
140
+ return result;
141
+ };
142
+ function zodToJsonSchema(schema) {
143
+ const nativeSchema = tryNativeZodToJsonSchema(schema);
144
+ if (nativeSchema) return nativeSchema;
145
+ try {
146
+ return fallbackZodToJsonSchema(schema);
175
147
  } catch (error) {
176
- logger$2.warn("zod-to-json-schema failed:", error);
148
+ logger$2.warn("zodToJsonSchema failed:", error);
177
149
  return {
178
150
  type: "object",
179
151
  properties: {}
180
152
  };
181
153
  }
182
154
  }
183
- /**
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.
187
- *
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.
190
- */
191
- function jsonSchemaToZod(jsonSchema) {
192
- const zodV4 = getZodV4ModuleSync();
193
- if (zodV4?.fromJSONSchema) try {
194
- return zodV4.fromJSONSchema(jsonSchema);
155
+ function jsonSchemaToZod$1(jsonSchema) {
156
+ try {
157
+ return jsonSchemaToZod(jsonSchema);
195
158
  } catch (error) {
196
- logger$2.warn("fromJSONSchema failed:", error);
159
+ logger$2.warn("jsonSchemaToZod failed:", error);
160
+ return z.object({}).passthrough();
197
161
  }
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();
200
162
  }
201
- /**
202
- * Normalize a schema to both JSON Schema and Zod formats.
203
- * Detects which format is provided and converts to the other.
204
- *
205
- * Supports:
206
- * - Zod schemas from Zod 3.x or Zod 4.x
207
- * - Plain JSON Schema objects
208
- */
163
+ const buildZodValidator = (schema, jsonSchema) => {
164
+ if (hasZod4Schemas(schema) && hasNativeToJSONSchema) return z.object(schema);
165
+ return jsonSchemaToZod$1(jsonSchema);
166
+ };
209
167
  function normalizeSchema(schema) {
210
- if (isZodSchema(schema)) return {
211
- jsonSchema: zodToJsonSchema(schema),
212
- zodValidator: z.object(schema)
213
- };
214
- const jsonSchema = schema;
168
+ if (isZodSchema(schema)) {
169
+ const jsonSchema = zodToJsonSchema(schema);
170
+ return {
171
+ jsonSchema,
172
+ zodValidator: buildZodValidator(schema, jsonSchema)
173
+ };
174
+ }
215
175
  return {
216
- jsonSchema,
217
- zodValidator: jsonSchemaToZod(jsonSchema)
176
+ jsonSchema: schema,
177
+ zodValidator: jsonSchemaToZod$1(schema)
218
178
  };
219
179
  }
220
- /**
221
- * Validate data with Zod schema and return formatted result
222
- */
223
180
  function validateWithZod(data, validator) {
224
181
  const result = validator.safeParse(data);
225
- if (!result.success) return {
226
- success: false,
227
- error: `Validation failed:\n${result.error.issues.map((err) => ` - ${err.path.join(".") || "root"}: ${err.message}`).join("\n")}`
228
- };
229
- return {
182
+ if (result.success) return {
230
183
  success: true,
231
184
  data: result.data
232
185
  };
186
+ return {
187
+ success: false,
188
+ error: `Validation failed:\n${result.error.issues.map((err) => ` - ${err.path.join(".") || "root"}: ${err.message}`).join("\n")}`
189
+ };
233
190
  }
234
191
 
235
192
  //#endregion
@@ -334,7 +291,7 @@ var NativeModelContextAdapter = class {
334
291
  const result = await this.nativeTesting.executeTool(toolInfo.name, JSON.stringify(args));
335
292
  return this.convertToToolResponse(result);
336
293
  },
337
- inputValidator: jsonSchemaToZod(inputSchema)
294
+ inputValidator: jsonSchemaToZod$1(inputSchema)
338
295
  };
339
296
  this.bridge.tools.set(toolInfo.name, validatedTool);
340
297
  } catch (error) {