@mcp-b/global 4.1.0-beta.20260702224704 → 5.0.1

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/README.md CHANGED
@@ -14,14 +14,14 @@
14
14
 
15
15
  ## Why Use @mcp-b/global?
16
16
 
17
- | Feature | Benefit |
18
- | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
19
- | **W3C Standard** | Implements the emerging WebMCP API specification |
20
- | **Drop-in IIFE** | Add AI capabilities with a single `<script>` tag - no build step |
21
- | **Native Chromium Support** | Auto-detects and uses native browser implementation when available |
22
- | **Dual Transport** | Works with both same-window clients AND parent pages (iframe support) |
23
- | **Spec-Aware Compatibility** | Tracks the current WebMCP draft (`document.modelContext`, `registerTool(tool, { signal })`, `getTools()`, and `executeTool(...)`). Deprecated `unregisterTool(name)` remains for existing MCP-B integrations and will be removed in the next major version. |
24
- | **Works with Any AI** | Claude, ChatGPT, Gemini, Cursor, Copilot, and any MCP client |
17
+ | Feature | Benefit |
18
+ | ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
19
+ | **W3C Standard** | Implements the emerging WebMCP API specification |
20
+ | **Drop-in IIFE** | Add AI capabilities with a single `<script>` tag - no build step |
21
+ | **Native Chromium Support** | Auto-detects and uses native browser implementation when available |
22
+ | **Dual Transport** | Works with both same-window clients AND parent pages (iframe support) |
23
+ | **Spec-Aware Compatibility** | Tracks the current WebMCP draft (`document.modelContext`, `registerTool(tool, { signal })`, and `getTools()`) plus Chromium's optional `executeTool(...)` extension. |
24
+ | **Works with Any AI** | Claude, ChatGPT, Gemini, Cursor, Copilot, and any MCP client |
25
25
 
26
26
  ## Package Selection
27
27
 
@@ -43,16 +43,18 @@
43
43
  <h1>My AI-Powered App</h1>
44
44
 
45
45
  <script>
46
- document.modelContext.registerTool({
47
- name: 'get-page-title',
48
- description: 'Get the current page title',
49
- inputSchema: { type: 'object', properties: {} },
50
- async execute() {
51
- return {
52
- content: [{ type: 'text', text: document.title }],
53
- };
54
- },
55
- });
46
+ void document.modelContext
47
+ .registerTool({
48
+ name: 'get-page-title',
49
+ description: 'Get the current page title',
50
+ inputSchema: { type: 'object', properties: {} },
51
+ async execute() {
52
+ return {
53
+ content: [{ type: 'text', text: document.title }],
54
+ };
55
+ },
56
+ })
57
+ .catch(console.error);
56
58
  </script>
57
59
  </body>
58
60
  </html>
@@ -67,7 +69,7 @@
67
69
  ```html
68
70
  <script type="module">
69
71
  import '@mcp-b/global';
70
- document.modelContext.registerTool({
72
+ await document.modelContext.registerTool({
71
73
  /* your tool */
72
74
  });
73
75
  </script>
@@ -84,7 +86,7 @@ npm install @mcp-b/global
84
86
  ```javascript
85
87
  import '@mcp-b/global';
86
88
 
87
- document.modelContext.registerTool({
89
+ await document.modelContext.registerTool({
88
90
  /* your tool */
89
91
  });
90
92
  ```
@@ -98,26 +100,34 @@ document.modelContext.registerTool({
98
100
  Initializes the global adapter. Replaces `document.modelContext` with a `BrowserMcpServer` instance that bridges WebMCP tools to the MCP protocol layer.
99
101
 
100
102
  ```typescript
101
- import { initializeWebModelContext } from '@mcp-b/global';
103
+ window.__webModelContextOptions = { autoInitialize: false };
104
+
105
+ const { initializeWebModelContext } = await import('@mcp-b/global');
102
106
 
103
107
  initializeWebModelContext({
104
108
  transport: {
105
109
  tabServer: { allowedOrigins: ['https://example.com'] },
106
110
  },
107
- nativeModelContextBehavior: 'preserve',
111
+ });
112
+
113
+ await document.modelContext.registerTool({
114
+ /* your tool */
108
115
  });
109
116
  ```
110
117
 
111
118
  **Behavior:**
112
119
 
113
120
  - Only operates in browser environments
114
- - Idempotent - calling multiple times is a no-op after first initialization
115
- - Preserves native `document.modelContext` by default (configurable)
121
+ - Returns nothing; use `document.modelContext` after initialization
122
+ - Idempotent - repeated and cross-bundle calls are no-ops
123
+ - Wraps the native context when present; otherwise installs and wraps the polyfill
124
+ - Cleanup restores the captured native or polyfilled context
116
125
  - Auto-called on import unless `window.__webModelContextOptions.autoInitialize` is `false`
117
126
 
118
127
  #### `cleanupWebModelContext()`
119
128
 
120
- Tears down the adapter and restores `document.modelContext` to its original state. Allows re-initialization.
129
+ Tears down the adapter and restores `document.modelContext` to the captured native or polyfilled
130
+ context. Allows re-initialization.
121
131
 
122
132
  ```typescript
123
133
  import { cleanupWebModelContext, initializeWebModelContext } from '@mcp-b/global';
@@ -136,11 +146,13 @@ After initialization, `document.modelContext` exposes these methods:
136
146
 
137
147
  #### `registerTool(tool, options?)`
138
148
 
139
- Registers a single tool. The tool name must be unique, otherwise throws if a tool with the same name already exists. The recommended unregistration path is `options.signal` (`AbortSignal`):
149
+ Registers a single tool. The tool name must be unique. Duplicate and invalid
150
+ registrations reject the returned promise. The recommended unregistration path
151
+ is `options.signal` (`AbortSignal`):
140
152
 
141
153
  ```typescript
142
154
  const ac = new AbortController();
143
- document.modelContext.registerTool(
155
+ await document.modelContext.registerTool(
144
156
  {
145
157
  name: 'add-to-cart',
146
158
  description: 'Add a product to the shopping cart',
@@ -168,14 +180,6 @@ ac.abort();
168
180
 
169
181
  `registerTool` resolves `undefined`, matching current Chromium and the WebMCP spec. Use `AbortSignal` cleanup for dynamic tools.
170
182
 
171
- #### `unregisterTool(nameOrTool)` (deprecated)
172
-
173
- Removes a tool by name. The April 23, 2026 WebMCP draft removed `unregisterTool` from the spec in favor of `AbortSignal` on `registerTool`. `@mcp-b/global` keeps `unregisterTool` functional for compatibility with older native previews and existing MCP-B integrations, and emits a one-time deprecation warning when called. It will be removed in the next major version.
174
-
175
- ```typescript
176
- document.modelContext.unregisterTool('add-to-cart');
177
- ```
178
-
179
183
  #### `getTools()`
180
184
 
181
185
  Returns WebMCP tool descriptors for all registered tools.
@@ -187,14 +191,27 @@ const tools = await document.modelContext.getTools();
187
191
 
188
192
  #### `executeTool(tool, inputArgsJson)`
189
193
 
190
- Executes a tool descriptor returned from `getTools()`.
194
+ Chromium exposes an optional descriptor-based execution method. Feature-detect it
195
+ on the canonical document surface.
196
+
197
+ ```bash
198
+ npm install --save-dev @mcp-b/webmcp-types
199
+ ```
191
200
 
192
201
  ```typescript
193
- const tools = await document.modelContext.getTools();
202
+ import '@mcp-b/global';
203
+ import type { ChromeModelContext } from '@mcp-b/webmcp-types';
204
+
205
+ const modelContext = document.modelContext as ChromeModelContext;
206
+ if (typeof modelContext.executeTool !== 'function') {
207
+ throw new Error('Tool execution is unavailable');
208
+ }
209
+
210
+ const tools = await modelContext.getTools();
194
211
  const searchTool = tools.find((tool) => tool.name === 'search-products');
195
212
  if (!searchTool) throw new Error('search-products is not available');
196
213
 
197
- const resultJson = await document.modelContext.executeTool(
214
+ const resultJson = await modelContext.executeTool(
198
215
  searchTool,
199
216
  JSON.stringify({ query: 'laptop', limit: 5 })
200
217
  );
@@ -202,24 +219,34 @@ const result = resultJson === null ? null : JSON.parse(resultJson);
202
219
  // { content: [{ type: 'text', text: '...' }] }
203
220
  ```
204
221
 
205
- #### `listTools()` and `callTool(params)` (deprecated)
222
+ #### `listTools()`
223
+
224
+ This MCP-B helper exposes MCP metadata. In-page WebMCP consumers should use
225
+ `getTools()` and feature-detect `executeTool(tool, inputArgsJson)`.
226
+
227
+ Prompt, resource, and lower-level MCP helpers are MCP-B extensions. Narrow
228
+ `document.modelContext` with `isBrowserMcpServer()` from
229
+ `@mcp-b/webmcp-ts-sdk` before using them. Install that package directly when
230
+ importing the guard; do not add extensions to the standard global type.
206
231
 
207
- These MCP-B compatibility helpers remain available for older integrations. Prefer `getTools()` and `executeTool(tool, inputArgsJson)` for in-page WebMCP consumers.
232
+ ### `BrowserMcpServer` Tool Descriptor
208
233
 
209
- ### Tool Descriptor
234
+ The MCP-B runtime consumes `outputSchema` when a separately typed descriptor is
235
+ passed to `document.modelContext.registerTool()`. Native and strict polyfill
236
+ runtimes ignore the extra field.
210
237
 
211
- | Property | Type | Required | Description |
212
- | -------------- | ----------------------------------------- | -------- | --------------------------------------------------------------------------------------- |
213
- | `name` | `string` | Yes | Unique identifier for the tool |
214
- | `description` | `string` | Yes | Natural language description of what the tool does |
215
- | `inputSchema` | `InputSchema` | No | JSON Schema describing accepted input. Defaults to `{ type: 'object', properties: {} }` |
216
- | `outputSchema` | `InputSchema` | No | MCP-B helper metadata for output typing and structured MCP responses |
217
- | `annotations` | `ToolAnnotations` | No | Hints about tool behavior for LLM planners |
218
- | `execute` | `(args, client) => Promise<ToolResponse>` | Yes | Async function implementing the tool logic |
238
+ | Property | Type | Required | Description |
239
+ | -------------- | --------------------------------------- | -------- | --------------------------------------------------------------------------------------- |
240
+ | `name` | `string` | Yes | Unique identifier for the tool |
241
+ | `description` | `string` | Yes | Natural language description of what the tool does |
242
+ | `inputSchema` | `InputSchema` | No | JSON Schema describing accepted input. Defaults to `{ type: 'object', properties: {} }` |
243
+ | `outputSchema` | `InputSchema` | No | MCP-B helper metadata for output typing and structured MCP responses |
244
+ | `annotations` | `ToolAnnotations` | No | Hints about tool behavior for LLM planners |
245
+ | `execute` | `(args) => unknown \| Promise<unknown>` | Yes | Function implementing the tool logic |
219
246
 
220
247
  ### Tool Response Format
221
248
 
222
- Tools return a `ToolResponse` object:
249
+ Tools may return an MCP `CallToolResult` object:
223
250
 
224
251
  ```typescript
225
252
  // Success
@@ -242,17 +269,15 @@ Tools return a `ToolResponse` object:
242
269
  interface WebModelContextInitOptions {
243
270
  transport?: TransportConfiguration;
244
271
  autoInitialize?: boolean;
245
- nativeModelContextBehavior?: 'preserve' | 'patch';
246
- installTestingShim?: boolean | 'always' | 'if-missing';
272
+ installTestingShim?: boolean;
247
273
  }
248
274
  ```
249
275
 
250
- | Option | Default | Description |
251
- | ---------------------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
252
- | `transport` | Auto-detect | Transport layer configuration (tab server and/or iframe) |
253
- | `autoInitialize` | `true` | Whether to auto-initialize on import |
254
- | `nativeModelContextBehavior` | `'preserve'` | `'preserve'` keeps native implementation untouched. `'patch'` replaces it with a BrowserMcpServer that mirrors to the native object |
255
- | `installTestingShim` | `'if-missing'` | Controls `navigator.modelContextTesting` installation. Only installs when not already present natively |
276
+ | Option | Default | Description |
277
+ | -------------------- | ----------- | ------------------------------------------------------------------------------- |
278
+ | `transport` | Auto-detect | Transport layer configuration (tab server and/or iframe) |
279
+ | `autoInitialize` | `true` | Whether to auto-initialize on import |
280
+ | `installTestingShim` | `true` | Installs `navigator.modelContextTesting` only when no implementation is present |
256
281
 
257
282
  ### Transport Configuration
258
283
 
@@ -260,8 +285,8 @@ The transport is auto-selected based on context:
260
285
 
261
286
  ```typescript
262
287
  interface TransportConfiguration {
263
- tabServer?: Partial<TabServerTransportOptions> | false;
264
- iframeServer?: Partial<IframeChildTransportOptions> | false;
288
+ tabServer?: TabServerTransportOptions | false;
289
+ iframeServer?: IframeChildTransportOptions | false;
265
290
  }
266
291
  ```
267
292
 
@@ -270,21 +295,18 @@ interface TransportConfiguration {
270
295
  - Set either to `false` to disable it
271
296
 
272
297
  ```typescript
273
- // Restrict to specific origins
274
- initializeWebModelContext({
275
- transport: {
276
- tabServer: { allowedOrigins: ['https://myapp.com'] },
277
- },
278
- });
298
+ window.__webModelContextOptions = { autoInitialize: false };
279
299
 
280
- // Disable tab transport (iframe only)
300
+ const { initializeWebModelContext } = await import('@mcp-b/global');
281
301
  initializeWebModelContext({
282
302
  transport: {
283
- tabServer: false,
303
+ tabServer: { allowedOrigins: ['https://myapp.com'] },
284
304
  },
285
305
  });
286
306
  ```
287
307
 
308
+ Inside an iframe, pass `transport: { tabServer: false }` to require the iframe transport.
309
+
288
310
  ### Auto-Initialization
289
311
 
290
312
  The package auto-initializes on import in browser environments. To customize before initialization:
@@ -310,7 +332,7 @@ To prevent auto-initialization:
310
332
  <script src="https://unpkg.com/@mcp-b/global@latest/dist/index.iife.js"></script>
311
333
  <script>
312
334
  // Initialize manually later
313
- MCPB.initializeWebModelContext();
335
+ WebMCP.initializeWebModelContext();
314
336
  </script>
315
337
  ```
316
338
 
@@ -333,10 +355,12 @@ const result = await navigator.modelContextTesting?.executeTool(
333
355
  ## Feature Detection
334
356
 
335
357
  ```javascript
336
- if ('modelContext' in navigator) {
337
- document.modelContext.registerTool({
338
- /* your tool */
339
- });
358
+ if ('modelContext' in document) {
359
+ void document.modelContext
360
+ .registerTool({
361
+ /* your tool */
362
+ })
363
+ .catch(console.error);
340
364
  }
341
365
  ```
342
366
 
@@ -347,7 +371,7 @@ if ('modelContext' in navigator) {
347
371
  ```typescript
348
372
  import '@mcp-b/global';
349
373
 
350
- document.modelContext.registerTool({
374
+ await document.modelContext.registerTool({
351
375
  name: 'search-products',
352
376
  description: 'Search products by keyword, category, or price range',
353
377
  inputSchema: {
@@ -367,7 +391,7 @@ document.modelContext.registerTool({
367
391
  },
368
392
  });
369
393
 
370
- document.modelContext.registerTool({
394
+ await document.modelContext.registerTool({
371
395
  name: 'add-to-cart',
372
396
  description: 'Add a product to the shopping cart',
373
397
  inputSchema: {
@@ -395,7 +419,7 @@ import '@mcp-b/global';
395
419
 
396
420
  // Start with base tools
397
421
  const userToolController = new AbortController();
398
- document.modelContext.registerTool(
422
+ await document.modelContext.registerTool(
399
423
  {
400
424
  name: 'get-user',
401
425
  description: 'Get current user info',
@@ -409,11 +433,11 @@ document.modelContext.registerTool(
409
433
 
410
434
  let adminToolController: AbortController | undefined;
411
435
 
412
- function registerAdminTool() {
436
+ async function registerAdminTool() {
413
437
  adminToolController?.abort();
414
438
  adminToolController = new AbortController();
415
439
 
416
- document.modelContext.registerTool(
440
+ await document.modelContext.registerTool(
417
441
  {
418
442
  name: 'delete-user',
419
443
  description: 'Delete a user account (admin only)',
@@ -438,7 +462,7 @@ function unregisterAdminTool() {
438
462
 
439
463
  // Add tools dynamically based on user role
440
464
  if (currentUser.isAdmin) {
441
- registerAdminTool();
465
+ await registerAdminTool();
442
466
  }
443
467
 
444
468
  // Remove tools when permissions change
@@ -453,7 +477,7 @@ function onLogout() {
453
477
  ```typescript
454
478
  import '@mcp-b/global';
455
479
 
456
- document.modelContext.registerTool({
480
+ await document.modelContext.registerTool({
457
481
  name: 'fill-contact-form',
458
482
  description: 'Fill the contact form with provided details',
459
483
  inputSchema: {
@@ -473,7 +497,7 @@ document.modelContext.registerTool({
473
497
  },
474
498
  });
475
499
 
476
- document.modelContext.registerTool({
500
+ await document.modelContext.registerTool({
477
501
  name: 'submit-form',
478
502
  description: 'Submit the contact form',
479
503
  inputSchema: { type: 'object', properties: {} },
@@ -493,30 +517,22 @@ document.modelContext.registerTool({
493
517
  | Firefox | No | Yes |
494
518
  | Safari | No | Yes |
495
519
 
496
- ## Zod Version Compatibility
520
+ ## Schema Compatibility
497
521
 
498
- This package supports **Zod `^3.25 || ^4.0`** where Zod is used by higher-level helpers. Plain JSON Schema is also supported.
522
+ The browser API accepts plain JSON Schema and does not require Zod. Direct MCP SDK v2
523
+ registrations accept Standard Schema implementations that emit JSON Schema, including Zod 4.2 or
524
+ newer. Zod 3 is unsupported.
499
525
 
500
- ## Type Exports
526
+ ## Type exports
501
527
 
502
- All types are re-exported for TypeScript consumers:
528
+ This package exports its initialization options:
503
529
 
504
530
  ```typescript
505
- import type {
506
- CallToolResult,
507
- InputSchema,
508
- ModelContext,
509
- ModelContextCore,
510
- NativeModelContextBehavior,
511
- ToolAnnotations,
512
- ToolDescriptor,
513
- ToolListItem,
514
- ToolResponse,
515
- TransportConfiguration,
516
- WebModelContextInitOptions,
517
- } from '@mcp-b/global';
531
+ import type { TransportConfiguration, WebModelContextInitOptions } from '@mcp-b/global';
518
532
  ```
519
533
 
534
+ Import browser contract types from `@mcp-b/webmcp-types`.
535
+
520
536
  ## Tool Routing Contract
521
537
 
522
538
  - MCP `tools/list`, `tools/call`, and tool list update notifications are sourced from the `BrowserMcpServer` registry.
@@ -526,7 +542,6 @@ import type {
526
542
 
527
543
  - [`@mcp-b/transports`](https://docs.mcp-b.ai/packages/transports/reference) - MCP transport implementations
528
544
  - [`@mcp-b/react-webmcp`](https://docs.mcp-b.ai/packages/react-webmcp/reference) - React hooks for MCP
529
- - [`@mcp-b/extension-tools`](https://docs.mcp-b.ai/packages/extension-tools/reference) - Chrome Extension API tools
530
545
  - [`chrome-devtools-mcp`](https://github.com/ChromeDevTools/chrome-devtools-mcp) - Upstream Chrome DevTools MCP server
531
546
 
532
547
  ## Resources
package/dist/index.d.ts CHANGED
@@ -1,39 +1,38 @@
1
+ /// <reference types="@mcp-b/webmcp-types" preserve="true" />
1
2
  import { IframeChildTransportOptions, TabServerTransportOptions } from "@mcp-b/transports";
2
3
 
3
4
  //#region src/types.d.ts
4
5
  interface TransportConfiguration {
5
- tabServer?: Partial<TabServerTransportOptions> | false;
6
- iframeServer?: Partial<IframeChildTransportOptions> | false;
6
+ /**
7
+ * Configuring a transport means stating its origins: `allowedOrigins` is required
8
+ * here, so a wildcard is only ever reached by omitting the transport entirely.
9
+ */
10
+ tabServer?: TabServerTransportOptions | false;
11
+ iframeServer?: IframeChildTransportOptions | false;
7
12
  }
8
- type NativeModelContextBehavior = 'preserve' | 'patch';
9
13
  interface WebModelContextInitOptions {
10
14
  transport?: TransportConfiguration;
11
15
  autoInitialize?: boolean;
12
- /**
13
- * Behavior when navigator.modelContext already exists.
14
- * - 'preserve' (default): wrap native with BrowserMcpServer, mirroring
15
- * core operations to the native object while extending with prompts,
16
- * resources, and other MCP capabilities.
17
- * - 'patch': same wrapping behavior (kept for backward compatibility).
18
- */
19
- nativeModelContextBehavior?: NativeModelContextBehavior;
20
16
  /**
21
17
  * Forwarded to @mcp-b/webmcp-polyfill when polyfill installation is needed.
22
- * - true or 'if-missing' (default): install modelContextTesting only when missing.
23
- * - 'always': replace existing modelContextTesting.
24
- * - false: do not install modelContextTesting.
18
+ * Existing modelContextTesting implementations are never replaced.
19
+ *
20
+ * Deliberately inverts the polyfill's own default (false): this entry point is the
21
+ * batteries-included one, and the e2e suites rely on the shim being present.
22
+ * @default true
25
23
  */
26
- installTestingShim?: boolean | 'always' | 'if-missing';
24
+ installTestingShim?: boolean;
27
25
  }
28
26
  declare global {
29
27
  interface Window {
30
28
  __webModelContextOptions?: WebModelContextInitOptions;
31
29
  }
32
- } //# sourceMappingURL=types.d.ts.map
30
+ }
33
31
  //#endregion
34
32
  //#region src/global.d.ts
33
+ /** Installs the global bridge on `document.modelContext`. */
35
34
  declare function initializeWebModelContext(options?: WebModelContextInitOptions): void;
36
35
  declare function cleanupWebModelContext(): void;
37
36
  //#endregion
38
- export { type NativeModelContextBehavior, type TransportConfiguration, type WebModelContextInitOptions, cleanupWebModelContext, initializeWebModelContext };
37
+ export { type TransportConfiguration, type WebModelContextInitOptions, cleanupWebModelContext, initializeWebModelContext };
39
38
  //# sourceMappingURL=index.d.ts.map