@mcp-b/global 1.0.15 → 1.1.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 +486 -0
- package/dist/index.d.ts +189 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.iife.js +4 -4
- package/dist/index.js +848 -79
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import {
|
|
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
|
|
@@ -103,7 +129,7 @@ interface ModelContextInput {
|
|
|
103
129
|
* Array of tool descriptors
|
|
104
130
|
* Supports both JSON Schema and Zod schema formats
|
|
105
131
|
*/
|
|
106
|
-
tools: ToolDescriptor
|
|
132
|
+
tools: ToolDescriptor[];
|
|
107
133
|
}
|
|
108
134
|
/**
|
|
109
135
|
* Tool call event
|
|
@@ -141,6 +167,16 @@ interface ModelContext {
|
|
|
141
167
|
registerTool<TInputSchema extends ZodSchemaObject = Record<string, never>, TOutputSchema extends ZodSchemaObject = Record<string, never>>(tool: ToolDescriptor<TInputSchema, TOutputSchema>): {
|
|
142
168
|
unregister: () => void;
|
|
143
169
|
};
|
|
170
|
+
/**
|
|
171
|
+
* Unregister a tool by name
|
|
172
|
+
* Available in Chromium's native implementation
|
|
173
|
+
*/
|
|
174
|
+
unregisterTool(name: string): void;
|
|
175
|
+
/**
|
|
176
|
+
* Clear all registered tools (both buckets)
|
|
177
|
+
* Available in Chromium's native implementation
|
|
178
|
+
*/
|
|
179
|
+
clearContext(): void;
|
|
144
180
|
/**
|
|
145
181
|
* Add event listener for tool calls
|
|
146
182
|
*/
|
|
@@ -180,11 +216,97 @@ interface InternalModelContext extends ModelContext {
|
|
|
180
216
|
* Internal MCP Bridge state
|
|
181
217
|
*/
|
|
182
218
|
interface MCPBridge {
|
|
183
|
-
|
|
219
|
+
tabServer: Server;
|
|
220
|
+
iframeServer?: Server;
|
|
184
221
|
tools: Map<string, ValidatedToolDescriptor>;
|
|
185
222
|
modelContext: InternalModelContext;
|
|
223
|
+
modelContextTesting?: ModelContextTesting;
|
|
186
224
|
isInitialized: boolean;
|
|
187
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Tool info returned by listTools() in testing API
|
|
228
|
+
* Note: inputSchema is a JSON string, not an object (matches Chromium implementation)
|
|
229
|
+
*/
|
|
230
|
+
interface ToolInfo {
|
|
231
|
+
name: string;
|
|
232
|
+
description: string;
|
|
233
|
+
inputSchema: string;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Testing API for Model Context
|
|
237
|
+
*
|
|
238
|
+
* **Native Support**: This API is available natively in Chromium-based browsers
|
|
239
|
+
* when the experimental "Model Context Testing" feature flag is enabled.
|
|
240
|
+
*
|
|
241
|
+
* **How to enable in Chromium**:
|
|
242
|
+
* - Navigate to `chrome://flags`
|
|
243
|
+
* - Search for "experimental web platform features" or "model context"
|
|
244
|
+
* - Enable the feature and restart the browser
|
|
245
|
+
* - Or launch with: `--enable-experimental-web-platform-features`
|
|
246
|
+
*
|
|
247
|
+
* **Polyfill**: If the native API is not available, this polyfill provides
|
|
248
|
+
* a compatible implementation for testing purposes.
|
|
249
|
+
*/
|
|
250
|
+
interface ModelContextTesting {
|
|
251
|
+
/**
|
|
252
|
+
* Execute a tool directly with JSON string input (Chromium native API)
|
|
253
|
+
* @param toolName - Name of the tool to execute
|
|
254
|
+
* @param inputArgsJson - JSON string of input arguments
|
|
255
|
+
* @returns Promise resolving to the tool's result
|
|
256
|
+
*/
|
|
257
|
+
executeTool(toolName: string, inputArgsJson: string): Promise<unknown>;
|
|
258
|
+
/**
|
|
259
|
+
* List all registered tools (Chromium native API)
|
|
260
|
+
* Returns tools with inputSchema as JSON string
|
|
261
|
+
*/
|
|
262
|
+
listTools(): ToolInfo[];
|
|
263
|
+
/**
|
|
264
|
+
* Register a callback that fires when the tools list changes (Chromium native API)
|
|
265
|
+
* Callback will fire on: registerTool, unregisterTool, provideContext, clearContext
|
|
266
|
+
*/
|
|
267
|
+
registerToolsChangedCallback(callback: () => void): void;
|
|
268
|
+
/**
|
|
269
|
+
* Get all tool calls that have been made (for testing/debugging)
|
|
270
|
+
* Polyfill-specific extension
|
|
271
|
+
*/
|
|
272
|
+
getToolCalls(): Array<{
|
|
273
|
+
toolName: string;
|
|
274
|
+
arguments: Record<string, unknown>;
|
|
275
|
+
timestamp: number;
|
|
276
|
+
}>;
|
|
277
|
+
/**
|
|
278
|
+
* Clear the history of tool calls
|
|
279
|
+
* Polyfill-specific extension
|
|
280
|
+
*/
|
|
281
|
+
clearToolCalls(): void;
|
|
282
|
+
/**
|
|
283
|
+
* Set a mock response for a specific tool (for testing)
|
|
284
|
+
* When set, the tool's execute function will be bypassed and the mock response returned
|
|
285
|
+
* Polyfill-specific extension
|
|
286
|
+
*/
|
|
287
|
+
setMockToolResponse(toolName: string, response: ToolResponse): void;
|
|
288
|
+
/**
|
|
289
|
+
* Clear mock response for a specific tool
|
|
290
|
+
* Polyfill-specific extension
|
|
291
|
+
*/
|
|
292
|
+
clearMockToolResponse(toolName: string): void;
|
|
293
|
+
/**
|
|
294
|
+
* Clear all mock tool responses
|
|
295
|
+
* Polyfill-specific extension
|
|
296
|
+
*/
|
|
297
|
+
clearAllMockToolResponses(): void;
|
|
298
|
+
/**
|
|
299
|
+
* Get the current tools registered in the system
|
|
300
|
+
* (same as modelContext.listTools but explicitly for testing)
|
|
301
|
+
* Polyfill-specific extension
|
|
302
|
+
*/
|
|
303
|
+
getRegisteredTools(): ReturnType<ModelContext['listTools']>;
|
|
304
|
+
/**
|
|
305
|
+
* Reset the entire testing state (clears tool calls and mock responses)
|
|
306
|
+
* Polyfill-specific extension
|
|
307
|
+
*/
|
|
308
|
+
reset(): void;
|
|
309
|
+
}
|
|
188
310
|
declare global {
|
|
189
311
|
interface Navigator {
|
|
190
312
|
/**
|
|
@@ -192,6 +314,18 @@ declare global {
|
|
|
192
314
|
* Provides tools and context to AI agents
|
|
193
315
|
*/
|
|
194
316
|
modelContext: ModelContext;
|
|
317
|
+
/**
|
|
318
|
+
* Model Context Testing API
|
|
319
|
+
*
|
|
320
|
+
* **IMPORTANT**: This API is only available in Chromium-based browsers
|
|
321
|
+
* with the experimental feature flag enabled:
|
|
322
|
+
* - `chrome://flags` → "Experimental Web Platform Features"
|
|
323
|
+
* - Or launch with: `--enable-experimental-web-platform-features`
|
|
324
|
+
*
|
|
325
|
+
* If not available natively, the @mcp-b/global polyfill provides
|
|
326
|
+
* a compatible implementation.
|
|
327
|
+
*/
|
|
328
|
+
modelContextTesting?: ModelContextTesting;
|
|
195
329
|
}
|
|
196
330
|
interface Window {
|
|
197
331
|
/**
|
|
@@ -202,5 +336,46 @@ declare global {
|
|
|
202
336
|
}
|
|
203
337
|
//# sourceMappingURL=types.d.ts.map
|
|
204
338
|
//#endregion
|
|
205
|
-
|
|
339
|
+
//#region src/global.d.ts
|
|
340
|
+
declare global {
|
|
341
|
+
interface Window {
|
|
342
|
+
__webModelContextOptions?: WebModelContextInitOptions;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Initializes the Web Model Context API on window.navigator.
|
|
347
|
+
* Creates and exposes navigator.modelContext and navigator.modelContextTesting.
|
|
348
|
+
* Automatically detects and uses native Chromium implementation if available.
|
|
349
|
+
*
|
|
350
|
+
* @param {WebModelContextInitOptions} [options] - Configuration options
|
|
351
|
+
* @throws {Error} If initialization fails
|
|
352
|
+
* @example
|
|
353
|
+
* ```typescript
|
|
354
|
+
* import { initializeWebModelContext } from '@mcp-b/global';
|
|
355
|
+
*
|
|
356
|
+
* initializeWebModelContext({
|
|
357
|
+
* transport: {
|
|
358
|
+
* tabServer: {
|
|
359
|
+
* allowedOrigins: ['https://example.com']
|
|
360
|
+
* }
|
|
361
|
+
* }
|
|
362
|
+
* });
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
declare function initializeWebModelContext(options?: WebModelContextInitOptions): void;
|
|
366
|
+
/**
|
|
367
|
+
* Cleans up the Web Model Context API.
|
|
368
|
+
* Closes all MCP servers and removes API from window.navigator.
|
|
369
|
+
* Useful for testing and hot module replacement.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```typescript
|
|
373
|
+
* import { cleanupWebModelContext } from '@mcp-b/global';
|
|
374
|
+
*
|
|
375
|
+
* cleanupWebModelContext();
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
declare function cleanupWebModelContext(): void;
|
|
379
|
+
//#endregion
|
|
380
|
+
export { type CallToolResult, InputSchema, InternalModelContext, MCPBridge, ModelContext, ModelContextInput, ModelContextTesting, type ToolAnnotations, ToolCallEvent, ToolDescriptor, ToolInfo, ToolResponse, TransportConfiguration, ValidatedToolDescriptor, WebModelContextInitOptions, ZodSchemaObject, cleanupWebModelContext, initializeWebModelContext };
|
|
206
381
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/global.ts"],"sourcesContent":[],"mappings":";;;;;;;;AAYA;AAkBY,UAlBK,WAAA,CAkBU;EAgBf,IAAA,EAAA,MAAA;EAKK,UAAA,CAAA,EArCF,MAq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