@mcp-b/global 2.0.2-beta.20260125195244 → 2.0.2-canary.20260125211849

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
@@ -91,17 +91,42 @@ function createLogger(namespace) {
91
91
  //#region src/validation.ts
92
92
  const logger$2 = createLogger("WebModelContext");
93
93
  /**
94
- * Detect if a schema is a Zod schema object (Record<string, ZodType>)
95
- * or a JSON Schema object.
94
+ * Detect if a schema is a Zod schema object and which version.
96
95
  *
97
- * Uses duck-typing to detect Zod 4 schemas by checking for the `_zod` property.
96
+ * Uses duck-typing to detect Zod schemas:
97
+ * - Zod 4 schemas have `_zod` property
98
+ * - Zod 3 schemas have `_def` property (but not `_zod`)
98
99
  */
99
- function isZodSchema(schema) {
100
- if (typeof schema !== "object" || schema === null) return false;
101
- if ("type" in schema && typeof schema.type === "string") return false;
100
+ function detectZodSchema(schema) {
101
+ if (typeof schema !== "object" || schema === null) return {
102
+ isZodSchema: false,
103
+ hasZod4: false,
104
+ hasZod3: false
105
+ };
106
+ if ("type" in schema && typeof schema.type === "string") return {
107
+ isZodSchema: false,
108
+ hasZod4: false,
109
+ hasZod3: false
110
+ };
102
111
  const values = Object.values(schema);
103
- if (values.length === 0) return false;
104
- return values.some((val) => val != null && typeof val === "object" && "_zod" in val);
112
+ if (values.length === 0) return {
113
+ isZodSchema: false,
114
+ hasZod4: false,
115
+ hasZod3: false
116
+ };
117
+ let hasZod4 = false;
118
+ let hasZod3 = false;
119
+ for (const val of values) {
120
+ if (val == null || typeof val !== "object") continue;
121
+ const obj = val;
122
+ if ("_zod" in obj) hasZod4 = true;
123
+ else if ("_def" in obj) hasZod3 = true;
124
+ }
125
+ return {
126
+ isZodSchema: hasZod4 || hasZod3,
127
+ hasZod4,
128
+ hasZod3
129
+ };
105
130
  }
106
131
  /**
107
132
  * Convert JSON Schema to Zod validator
@@ -128,11 +153,25 @@ function zodToJsonSchema(schema) {
128
153
  return rest;
129
154
  }
130
155
  /**
156
+ * Error thrown when Zod 3 schemas are detected.
157
+ * Zod 4 is required for schema conversion.
158
+ */
159
+ var Zod3SchemaError = class extends Error {
160
+ constructor() {
161
+ super("Zod 3 schema detected. This package requires Zod 4 for schema support.\n\nSolutions:\n 1. Upgrade to zod@4.x: pnpm add zod@4\n 2. If using zod@3.25+, import from the v4 subpath:\n import { z } from \"zod/v4\"\n 3. Use JSON Schema instead of Zod schemas\n\nSee https://zod.dev/v4/versioning for more information.");
162
+ this.name = "Zod3SchemaError";
163
+ }
164
+ };
165
+ /**
131
166
  * Normalize a schema to both JSON Schema and Zod formats
132
167
  * Detects which format is provided and converts to the other
168
+ *
169
+ * @throws {Zod3SchemaError} If Zod 3 schemas are detected
133
170
  */
134
171
  function normalizeSchema(schema) {
135
- if (isZodSchema(schema)) return {
172
+ const detection = detectZodSchema(schema);
173
+ if (detection.hasZod3 && !detection.hasZod4) throw new Zod3SchemaError();
174
+ if (detection.isZodSchema) return {
136
175
  jsonSchema: zodToJsonSchema(schema),
137
176
  zodValidator: z.object(schema)
138
177
  };
@@ -181,6 +220,7 @@ const POLYFILL_MARKER_PROPERTY = "__isWebMCPPolyfill";
181
220
  * @returns Detection result with flags for native context and testing API availability
182
221
  */
183
222
  function detectNativeAPI() {
223
+ /* c8 ignore next 2 */
184
224
  if (typeof window === "undefined" || typeof navigator === "undefined") return {
185
225
  hasNativeContext: false,
186
226
  hasNativeTesting: false
@@ -188,8 +228,8 @@ function detectNativeAPI() {
188
228
  const modelContext = navigator.modelContext;
189
229
  const modelContextTesting = navigator.modelContextTesting;
190
230
  if (!modelContext || !modelContextTesting) return {
191
- hasNativeContext: false,
192
- hasNativeTesting: false
231
+ hasNativeContext: Boolean(modelContext),
232
+ hasNativeTesting: Boolean(modelContextTesting)
193
233
  };
194
234
  if (POLYFILL_MARKER_PROPERTY in modelContextTesting && modelContextTesting[POLYFILL_MARKER_PROPERTY] === true) return {
195
235
  hasNativeContext: false,
@@ -902,7 +942,7 @@ var WebModelContext = class {
902
942
  const templateParams = [];
903
943
  for (const match of resource.uri.matchAll(templateParamRegex)) {
904
944
  const paramName = match[1];
905
- if (paramName) templateParams.push(paramName);
945
+ templateParams.push(paramName);
906
946
  }
907
947
  return {
908
948
  uri: resource.uri,
@@ -1373,8 +1413,7 @@ var WebModelContext = class {
1373
1413
  const params = {};
1374
1414
  for (let i = 0; i < paramNames.length; i++) {
1375
1415
  const paramName = paramNames[i];
1376
- const paramValue = match[i + 1];
1377
- if (paramName !== void 0 && paramValue !== void 0) params[paramName] = paramValue;
1416
+ params[paramName] = match[i + 1];
1378
1417
  }
1379
1418
  return params;
1380
1419
  }
@@ -1652,6 +1691,7 @@ function initializeMCPBridge(options) {
1652
1691
  * ```
1653
1692
  */
1654
1693
  function initializeWebModelContext(options) {
1694
+ /* c8 ignore next 4 */
1655
1695
  if (typeof window === "undefined") {
1656
1696
  logger$1.warn("Not in browser environment, skipping initialization");
1657
1697
  return;
@@ -1745,6 +1785,7 @@ function initializeWebModelContext(options) {
1745
1785
  * ```
1746
1786
  */
1747
1787
  function cleanupWebModelContext() {
1788
+ /* c8 ignore next */
1748
1789
  if (typeof window === "undefined") return;
1749
1790
  if (window.__mcpBridge) try {
1750
1791
  window.__mcpBridge.tabServer.close();