@mcp-b/global 2.0.3-canary.1 → 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,7 +1,8 @@
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
- import { fromJSONSchema, toJSONSchema } from "zod/v4";
5
+ import { zodToJsonSchema as zodToJsonSchema$1 } from "zod-to-json-schema";
5
6
 
6
7
  //#region src/logger.ts
7
8
  /**
@@ -91,87 +92,101 @@ function createLogger(namespace) {
91
92
  //#endregion
92
93
  //#region src/validation.ts
93
94
  const logger$2 = createLogger("WebModelContext");
94
- /**
95
- * Detect if a schema is a Zod schema object (Record<string, ZodType>)
96
- * or a JSON Schema object.
97
- *
98
- * Uses duck-typing to detect Zod schemas:
99
- * - Zod 4 schemas have `_zod` property
100
- * - Zod 3 schemas have `_def` property
101
- *
102
- * Both are supported as of Zod 3.25+ (which includes Zod 4 under the hood).
103
- */
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
+ };
104
108
  function isZodSchema(schema) {
105
- if (typeof schema !== "object" || schema === null) return false;
109
+ if (!isRecord(schema)) return false;
106
110
  if ("type" in schema && typeof schema.type === "string") return false;
107
111
  const values = Object.values(schema);
108
112
  if (values.length === 0) return false;
109
- for (const val of values) {
110
- if (val == null || typeof val !== "object") continue;
111
- const obj = val;
112
- if ("_zod" in obj || "_def" in obj) return true;
113
+ return values.some((value) => isZod4Schema(value) || isZod3Schema(value));
114
+ }
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" }));
120
+ } catch (error) {
121
+ logger$2.warn("z.toJSONSchema failed, falling back to zod-to-json-schema:", error);
122
+ return null;
123
+ }
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, {
130
+ strictUnions: true,
131
+ $refStrategy: "none"
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);
147
+ } catch (error) {
148
+ logger$2.warn("zodToJsonSchema failed:", error);
149
+ return {
150
+ type: "object",
151
+ properties: {}
152
+ };
113
153
  }
114
- return false;
115
154
  }
116
- /**
117
- * Convert JSON Schema to Zod validator.
118
- * Uses fromJSONSchema from 'zod/v4' which is available in Zod 3.25+ and Zod 4.
119
- */
120
- function jsonSchemaToZod(jsonSchema) {
155
+ function jsonSchemaToZod$1(jsonSchema) {
121
156
  try {
122
- return fromJSONSchema(jsonSchema);
157
+ return jsonSchemaToZod(jsonSchema);
123
158
  } catch (error) {
124
- logger$2.warn("Failed to convert JSON Schema to Zod:", error);
159
+ logger$2.warn("jsonSchemaToZod failed:", error);
125
160
  return z.object({}).passthrough();
126
161
  }
127
162
  }
128
- /**
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).
134
- *
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
137
- */
138
- function zodToJsonSchema(schema) {
139
- const { $schema: _,...rest } = toJSONSchema(z.object(schema));
140
- return rest;
141
- }
142
- /**
143
- * Normalize a schema to both JSON Schema and Zod formats.
144
- * Detects which format is provided and converts to the other.
145
- *
146
- * 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)
149
- * - Plain JSON Schema objects
150
- */
163
+ const buildZodValidator = (schema, jsonSchema) => {
164
+ if (hasZod4Schemas(schema) && hasNativeToJSONSchema) return z.object(schema);
165
+ return jsonSchemaToZod$1(jsonSchema);
166
+ };
151
167
  function normalizeSchema(schema) {
152
- if (isZodSchema(schema)) return {
153
- jsonSchema: zodToJsonSchema(schema),
154
- zodValidator: z.object(schema)
155
- };
156
- const jsonSchema = schema;
168
+ if (isZodSchema(schema)) {
169
+ const jsonSchema = zodToJsonSchema(schema);
170
+ return {
171
+ jsonSchema,
172
+ zodValidator: buildZodValidator(schema, jsonSchema)
173
+ };
174
+ }
157
175
  return {
158
- jsonSchema,
159
- zodValidator: jsonSchemaToZod(jsonSchema)
176
+ jsonSchema: schema,
177
+ zodValidator: jsonSchemaToZod$1(schema)
160
178
  };
161
179
  }
162
- /**
163
- * Validate data with Zod schema and return formatted result
164
- */
165
180
  function validateWithZod(data, validator) {
166
181
  const result = validator.safeParse(data);
167
- if (!result.success) return {
168
- success: false,
169
- error: `Validation failed:\n${result.error.issues.map((err) => ` - ${err.path.join(".") || "root"}: ${err.message}`).join("\n")}`
170
- };
171
- return {
182
+ if (result.success) return {
172
183
  success: true,
173
184
  data: result.data
174
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
+ };
175
190
  }
176
191
 
177
192
  //#endregion
@@ -276,7 +291,7 @@ var NativeModelContextAdapter = class {
276
291
  const result = await this.nativeTesting.executeTool(toolInfo.name, JSON.stringify(args));
277
292
  return this.convertToToolResponse(result);
278
293
  },
279
- inputValidator: jsonSchemaToZod(inputSchema)
294
+ inputValidator: jsonSchemaToZod$1(inputSchema)
280
295
  };
281
296
  this.bridge.tools.set(toolInfo.name, validatedTool);
282
297
  } catch (error) {