@mcp-b/global 1.0.15 → 1.1.0

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
@@ -103,6 +103,117 @@ window.navigator.modelContext.provideContext({
103
103
  });
104
104
  ```
105
105
 
106
+ ## ⚙️ Configuration
107
+
108
+ The polyfill exposes `initializeWebModelContext(options?: WebModelContextInitOptions)` to let you control transport behaviour. When you import `@mcp-b/global` as a module it auto-initializes by default, but you can customise or defer initialization:
109
+
110
+ - **Disable auto init**: Set `window.__webModelContextOptions = { autoInitialize: false }` before importing, then call `initializeWebModelContext()` manually.
111
+ - **Configure via script tag**: When using the IIFE build, pass options through data attributes:
112
+ ```html
113
+ <script
114
+ src="https://unpkg.com/@mcp-b/global@latest/dist/index.iife.js"
115
+ data-webmcp-auto-initialize="false"
116
+ data-webmcp-allowed-origins="https://example.com,https://docs.example.com"
117
+ ></script>
118
+ <!-- Later in the page -->
119
+ <script>
120
+ window.navigator.modelContext.provideContext({ tools: [] });
121
+ </script>
122
+ ```
123
+ Use `data-webmcp-options='{"transport":{"tabServer":{"allowedOrigins":["https://example.com"]}}}'` for advanced JSON configuration.
124
+ - **Supported data attributes**
125
+ - `data-webmcp-auto-initialize="false"`: Skip automatic setup.
126
+ - `data-webmcp-allowed-origins="https://a.com,https://b.com"`: Override `tabServer.allowedOrigins`.
127
+ - `data-webmcp-channel-id="custom-channel"`: Set the Tab transport channel.
128
+
129
+ ### Dual-Server Mode (Tab + Iframe)
130
+
131
+ By default, the global package runs **two MCP servers** that share the same tool registry:
132
+
133
+ 1. **Tab Server** (`TabServerTransport`) - For same-window communication
134
+ 2. **Iframe Server** (`IframeChildTransport`) - Auto-enabled when running in an iframe (when `window.parent !== window`)
135
+
136
+ Both servers expose the same tools (Bucket A + Bucket B), allowing your tools to be accessed from:
137
+ - Same-window clients (e.g., browser extension content scripts)
138
+ - Parent page (when running in an iframe)
139
+
140
+ **Example: Running in an Iframe**
141
+
142
+ When your app runs in an iframe, both servers are automatically enabled:
143
+
144
+ ```ts
145
+ // In iframe: Auto-initializes with both servers
146
+ import '@mcp-b/global';
147
+
148
+ // Register tools - they're automatically available to:
149
+ // 1. Same-window clients (via TabServerTransport)
150
+ // 2. Parent page (via IframeChildTransport)
151
+ window.navigator.modelContext.provideContext({
152
+ tools: [
153
+ {
154
+ name: "iframe-action",
155
+ description: "Action from iframe",
156
+ inputSchema: { type: "object", properties: {} },
157
+ async execute() {
158
+ return {
159
+ content: [{ type: "text", text: "Hello from iframe!" }]
160
+ };
161
+ }
162
+ }
163
+ ]
164
+ });
165
+ ```
166
+
167
+ **Configure Iframe Server**
168
+
169
+ You can customize or disable the iframe server:
170
+
171
+ ```ts
172
+ import { initializeWebModelContext } from '@mcp-b/global';
173
+
174
+ // Customize iframe server
175
+ initializeWebModelContext({
176
+ transport: {
177
+ iframeServer: {
178
+ allowedOrigins: ['https://parent-app.com'], // Only allow specific parent
179
+ channelId: 'custom-iframe-channel',
180
+ },
181
+ },
182
+ });
183
+
184
+ // Disable iframe server (only Tab server runs)
185
+ initializeWebModelContext({
186
+ transport: {
187
+ iframeServer: false, // Disable iframe server
188
+ },
189
+ });
190
+
191
+ // Disable tab server (only Iframe server runs)
192
+ initializeWebModelContext({
193
+ transport: {
194
+ tabServer: false, // Disable tab server
195
+ iframeServer: {
196
+ allowedOrigins: ['https://parent-app.com'],
197
+ },
198
+ },
199
+ });
200
+ ```
201
+
202
+ **Custom Transport Factory**
203
+
204
+ Provide `transport.create` to supply any MCP `Transport` implementation instead of the built-in dual-server mode:
205
+
206
+ ```ts
207
+ import { initializeWebModelContext } from '@mcp-b/global';
208
+ import { CustomTransport } from './my-transport';
209
+
210
+ initializeWebModelContext({
211
+ transport: {
212
+ create: () => new CustomTransport(),
213
+ },
214
+ });
215
+ ```
216
+
106
217
  ## 📖 API Reference
107
218
 
108
219
  ### Two-Bucket Tool Management System
package/dist/index.d.ts CHANGED
@@ -1,17 +1,9 @@
1
- import { CallToolResult, Server, ToolAnnotations } from "@mcp-b/webmcp-ts-sdk";
1
+ import { IframeChildTransportOptions, TabServerTransportOptions } from "@mcp-b/transports";
2
+ import { CallToolResult, Server, ToolAnnotations, Transport } from "@mcp-b/webmcp-ts-sdk";
2
3
  import { z } from "zod";
3
4
 
4
- //#region src/global.d.ts
5
- /**
6
- * Initialize the Web Model Context API (window.navigator.modelContext)
7
- */
8
- declare function initializeWebModelContext(): void;
9
- /**
10
- * Cleanup function (for testing/development)
11
- */
12
- declare function cleanupWebModelContext(): void;
13
- //#endregion
14
5
  //#region src/types.d.ts
6
+
15
7
  /**
16
8
  * JSON Schema definition for tool input parameters
17
9
  */
@@ -35,6 +27,40 @@ type ZodSchemaObject = Record<string, z.ZodTypeAny>;
35
27
  * This is compatible with MCP SDK's CallToolResult
36
28
  */
37
29
  type ToolResponse = CallToolResult;
30
+ /**
31
+ * Transport configuration for initializing the Web Model Context polyfill.
32
+ */
33
+ interface TransportConfiguration {
34
+ /**
35
+ * Provide a custom transport factory. When set, tabServer and iframeServer options are ignored.
36
+ */
37
+ create?: () => Transport;
38
+ /**
39
+ * Options passed to the built-in TabServerTransport when no custom factory is provided.
40
+ * Set to false to disable the tab server.
41
+ * Default: enabled with allowedOrigins: ['*']
42
+ */
43
+ tabServer?: Partial<TabServerTransportOptions> | false;
44
+ /**
45
+ * Options passed to the built-in IframeChildTransport when no custom factory is provided.
46
+ * Set to false to disable the iframe server.
47
+ * Default: auto-enabled when running in an iframe (window.parent !== window)
48
+ */
49
+ iframeServer?: Partial<IframeChildTransportOptions> | false;
50
+ }
51
+ /**
52
+ * Initialization options for the Web Model Context polyfill.
53
+ */
54
+ interface WebModelContextInitOptions {
55
+ /**
56
+ * Configure the transport used to expose the MCP server in the browser.
57
+ */
58
+ transport?: TransportConfiguration;
59
+ /**
60
+ * When set to false, automatic initialization on module load is skipped.
61
+ */
62
+ autoInitialize?: boolean;
63
+ }
38
64
  /**
39
65
  * Tool descriptor for Web Model Context API
40
66
  * Extended with full MCP protocol support
@@ -180,7 +206,8 @@ interface InternalModelContext extends ModelContext {
180
206
  * Internal MCP Bridge state
181
207
  */
182
208
  interface MCPBridge {
183
- server: Server;
209
+ tabServer: Server;
210
+ iframeServer?: Server;
184
211
  tools: Map<string, ValidatedToolDescriptor>;
185
212
  modelContext: InternalModelContext;
186
213
  isInitialized: boolean;
@@ -202,5 +229,20 @@ declare global {
202
229
  }
203
230
  //# sourceMappingURL=types.d.ts.map
204
231
  //#endregion
205
- export { type CallToolResult, InputSchema, InternalModelContext, MCPBridge, ModelContext, ModelContextInput, type ToolAnnotations, ToolCallEvent, ToolDescriptor, ToolResponse, ValidatedToolDescriptor, ZodSchemaObject, cleanupWebModelContext, initializeWebModelContext };
232
+ //#region src/global.d.ts
233
+ declare global {
234
+ interface Window {
235
+ __webModelContextOptions?: WebModelContextInitOptions;
236
+ }
237
+ }
238
+ /**
239
+ * Initialize the Web Model Context API (window.navigator.modelContext)
240
+ */
241
+ declare function initializeWebModelContext(options?: WebModelContextInitOptions): void;
242
+ /**
243
+ * Cleanup function (for testing/development)
244
+ */
245
+ declare function cleanupWebModelContext(): void;
246
+ //#endregion
247
+ export { type CallToolResult, InputSchema, InternalModelContext, MCPBridge, ModelContext, ModelContextInput, type ToolAnnotations, ToolCallEvent, ToolDescriptor, ToolResponse, TransportConfiguration, ValidatedToolDescriptor, WebModelContextInitOptions, ZodSchemaObject, cleanupWebModelContext, initializeWebModelContext };
206
248
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/global.ts","../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;;iBAifgB,yBAAA,CAAA;AAAhB;AAyCA;;iBAAgB,sBAAA,CAAA;;;;;AAzChB;AAyCgB,UCjhBC,WAAA,CDihBqB;;eC/gBvB;;IAFE,WAAW,CAAA,EAAA,MAAA;IAkBhB,CAAA,GAAA,EAAA,MAAA,CAAA,EAAe,OAAA;EAgBf,CA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