@mcp-b/global 5.1.0 → 6.0.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 +69 -65
- package/dist/index.d.ts +2 -11
- package/dist/index.iife.js +11 -6
- package/dist/index.js +24 -73
- package/dist/index.js.map +1 -1
- package/package.json +5 -8
package/dist/index.js
CHANGED
|
@@ -1,87 +1,40 @@
|
|
|
1
1
|
import { IframeChildTransport, TabServerTransport } from "@mcp-b/transports";
|
|
2
|
-
import {
|
|
2
|
+
import { installWebMCP } from "@mcp-b/webmcp-polyfill";
|
|
3
3
|
import { BrowserMcpServer, isBrowserMcpServer } from "@mcp-b/webmcp-ts-sdk";
|
|
4
4
|
|
|
5
5
|
//#region src/global.ts
|
|
6
6
|
let runtime = null;
|
|
7
|
-
function isBrowserEnvironment() {
|
|
8
|
-
return typeof window !== "undefined" && typeof window.navigator !== "undefined";
|
|
9
|
-
}
|
|
10
|
-
function readCurrentModelContext() {
|
|
11
|
-
return document.modelContext ?? navigator.modelContext;
|
|
12
|
-
}
|
|
13
|
-
function canReplaceModelContext(target) {
|
|
14
|
-
const descriptor = Object.getOwnPropertyDescriptor(target, "modelContext");
|
|
15
|
-
return descriptor ? descriptor.configurable === true : Object.isExtensible(target);
|
|
16
|
-
}
|
|
17
|
-
function replaceDocumentModelContext(value) {
|
|
18
|
-
Object.defineProperty(document, "modelContext", {
|
|
19
|
-
configurable: true,
|
|
20
|
-
enumerable: true,
|
|
21
|
-
writable: false,
|
|
22
|
-
value
|
|
23
|
-
});
|
|
24
|
-
if (document.modelContext !== value) console.error("[WebModelContext] Failed to replace document.modelContext.", "Descriptor:", Object.getOwnPropertyDescriptor(document, "modelContext"));
|
|
25
|
-
}
|
|
26
|
-
function replaceNavigatorModelContext(value) {
|
|
27
|
-
Object.defineProperty(navigator, "modelContext", {
|
|
28
|
-
configurable: true,
|
|
29
|
-
enumerable: true,
|
|
30
|
-
writable: false,
|
|
31
|
-
value
|
|
32
|
-
});
|
|
33
|
-
if (navigator.modelContext !== value) console.error("[WebModelContext] Failed to replace navigator.modelContext.", "Descriptor:", Object.getOwnPropertyDescriptor(navigator, "modelContext"));
|
|
34
|
-
}
|
|
35
|
-
function restoreProperty(target, key, descriptor) {
|
|
36
|
-
if (descriptor) Object.defineProperty(target, key, descriptor);
|
|
37
|
-
else Reflect.deleteProperty(target, key);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Replace both modelContext surfaces with the given value.
|
|
41
|
-
*
|
|
42
|
-
* document.modelContext is canonical. @mcp-b/global still supports old
|
|
43
|
-
* navigator-first users, so the bridge exposes the BrowserMcpServer wrapper
|
|
44
|
-
* through both properties.
|
|
45
|
-
*/
|
|
46
|
-
function replaceModelContext(value, previousDocumentDescriptor, previousNavigatorDescriptor) {
|
|
47
|
-
try {
|
|
48
|
-
replaceDocumentModelContext(value);
|
|
49
|
-
replaceNavigatorModelContext(value);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
restoreProperty(document, "modelContext", previousDocumentDescriptor);
|
|
52
|
-
restoreProperty(navigator, "modelContext", previousNavigatorDescriptor);
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
7
|
function createTransport(config) {
|
|
57
|
-
if (window.parent !== window && config?.iframeServer !== false) return new IframeChildTransport(
|
|
8
|
+
if (window.parent !== window && config?.iframeServer !== false) return new IframeChildTransport(config?.iframeServer || { allowedOrigins: ["*"] });
|
|
58
9
|
if (config?.tabServer === false) throw new Error("tabServer transport is disabled and iframe transport was not selected");
|
|
59
|
-
return new TabServerTransport(
|
|
10
|
+
return new TabServerTransport(config?.tabServer || { allowedOrigins: ["*"] });
|
|
60
11
|
}
|
|
61
12
|
/** Installs the global bridge on `document.modelContext`. */
|
|
62
13
|
function initializeWebModelContext(options) {
|
|
63
|
-
if (!
|
|
14
|
+
if (!globalThis.window || !globalThis.document || globalThis.isSecureContext === false) return;
|
|
64
15
|
if (runtime) return;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const native = readCurrentModelContext();
|
|
16
|
+
if (isBrowserMcpServer(document.modelContext)) return;
|
|
17
|
+
installWebMCP();
|
|
18
|
+
const native = document.modelContext;
|
|
69
19
|
if (!native) throw new Error("modelContext is not available");
|
|
70
|
-
|
|
20
|
+
const previousDescriptor = Object.getOwnPropertyDescriptor(document, "modelContext");
|
|
21
|
+
if (previousDescriptor ? !previousDescriptor.configurable : !Object.isExtensible(document)) return;
|
|
71
22
|
const transport = createTransport(options?.transport);
|
|
72
23
|
const server = new BrowserMcpServer({
|
|
73
24
|
name: `${window.location.hostname || "localhost"}-webmcp`,
|
|
74
25
|
version: "1.0.0"
|
|
75
26
|
}, { native });
|
|
76
|
-
const previousDocumentModelContextDescriptor = Object.getOwnPropertyDescriptor(document, "modelContext");
|
|
77
|
-
const previousNavigatorModelContextDescriptor = Object.getOwnPropertyDescriptor(navigator, "modelContext");
|
|
78
27
|
try {
|
|
79
|
-
|
|
28
|
+
Object.defineProperty(document, "modelContext", {
|
|
29
|
+
configurable: true,
|
|
30
|
+
enumerable: true,
|
|
31
|
+
writable: false,
|
|
32
|
+
value: server
|
|
33
|
+
});
|
|
80
34
|
runtime = {
|
|
81
35
|
server,
|
|
82
36
|
transport,
|
|
83
|
-
|
|
84
|
-
previousNavigatorModelContextDescriptor
|
|
37
|
+
previousDescriptor
|
|
85
38
|
};
|
|
86
39
|
} catch (error) {
|
|
87
40
|
server.close();
|
|
@@ -105,23 +58,21 @@ function initializeWebModelContext(options) {
|
|
|
105
58
|
}
|
|
106
59
|
function cleanupWebModelContext() {
|
|
107
60
|
if (!runtime) return;
|
|
108
|
-
const { server, transport,
|
|
61
|
+
const { server, transport, previousDescriptor } = runtime;
|
|
109
62
|
runtime = null;
|
|
110
63
|
server.close();
|
|
111
64
|
transport.close();
|
|
112
|
-
|
|
113
|
-
|
|
65
|
+
if (previousDescriptor) Object.defineProperty(document, "modelContext", previousDescriptor);
|
|
66
|
+
else Reflect.deleteProperty(document, "modelContext");
|
|
114
67
|
}
|
|
115
68
|
|
|
116
69
|
//#endregion
|
|
117
70
|
//#region src/index.ts
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
console.error("[WebModelContext] Auto-initialization failed:", error);
|
|
124
|
-
}
|
|
71
|
+
const options = globalThis.window?.__webModelContextOptions;
|
|
72
|
+
if (options?.autoInitialize !== false) try {
|
|
73
|
+
initializeWebModelContext(options);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error("[WebModelContext] Auto-initialization failed:", error);
|
|
125
76
|
}
|
|
126
77
|
|
|
127
78
|
//#endregion
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/global.ts","../src/index.ts"],"sourcesContent":["import { IframeChildTransport, TabServerTransport } from '@mcp-b/transports';\nimport { initializeWebMCPPolyfill } from '@mcp-b/webmcp-polyfill';\nimport { BrowserMcpServer, isBrowserMcpServer } from '@mcp-b/webmcp-ts-sdk';\nimport type { ModelContext } from '@mcp-b/webmcp-types';\nimport type { Transport } from '@modelcontextprotocol/server';\nimport type { WebModelContextInitOptions } from './types.js';\n\ninterface RuntimeState {\n server: BrowserMcpServer;\n transport: Transport;\n previousDocumentModelContextDescriptor: PropertyDescriptor | undefined;\n previousNavigatorModelContextDescriptor: PropertyDescriptor | undefined;\n}\n\nlet runtime: RuntimeState | null = null;\n\nfunction isBrowserEnvironment(): boolean {\n return typeof window !== 'undefined' && typeof window.navigator !== 'undefined';\n}\n\nfunction readCurrentModelContext(): ModelContext | undefined {\n return document.modelContext ?? navigator.modelContext;\n}\n\nfunction canReplaceModelContext(target: Document | Navigator): boolean {\n const descriptor = Object.getOwnPropertyDescriptor(target, 'modelContext');\n return descriptor ? descriptor.configurable === true : Object.isExtensible(target);\n}\n\nfunction replaceDocumentModelContext(value: unknown): void {\n Object.defineProperty(document, 'modelContext', {\n configurable: true,\n enumerable: true,\n writable: false,\n value,\n });\n\n if (document.modelContext !== value) {\n console.error(\n '[WebModelContext] Failed to replace document.modelContext.',\n 'Descriptor:',\n Object.getOwnPropertyDescriptor(document, 'modelContext')\n );\n }\n}\n\nfunction replaceNavigatorModelContext(value: unknown): void {\n Object.defineProperty(navigator, 'modelContext', {\n configurable: true,\n enumerable: true,\n writable: false,\n value,\n });\n\n if (navigator.modelContext !== value) {\n console.error(\n '[WebModelContext] Failed to replace navigator.modelContext.',\n 'Descriptor:',\n Object.getOwnPropertyDescriptor(navigator, 'modelContext')\n );\n }\n}\n\nfunction restoreProperty(\n target: Document | Navigator,\n key: 'modelContext',\n descriptor: PropertyDescriptor | undefined\n): void {\n if (descriptor) Object.defineProperty(target, key, descriptor);\n else Reflect.deleteProperty(target, key);\n}\n\n/**\n * Replace both modelContext surfaces with the given value.\n *\n * document.modelContext is canonical. @mcp-b/global still supports old\n * navigator-first users, so the bridge exposes the BrowserMcpServer wrapper\n * through both properties.\n */\nfunction replaceModelContext(\n value: unknown,\n previousDocumentDescriptor: PropertyDescriptor | undefined,\n previousNavigatorDescriptor: PropertyDescriptor | undefined\n): void {\n try {\n replaceDocumentModelContext(value);\n replaceNavigatorModelContext(value);\n } catch (error) {\n restoreProperty(document, 'modelContext', previousDocumentDescriptor);\n restoreProperty(navigator, 'modelContext', previousNavigatorDescriptor);\n throw error;\n }\n}\n\nfunction createTransport(config: WebModelContextInitOptions['transport']): Transport {\n const inIframe = window.parent !== window;\n\n if (inIframe && config?.iframeServer !== false) {\n return new IframeChildTransport(\n typeof config?.iframeServer === 'object' ? config.iframeServer : { allowedOrigins: ['*'] }\n );\n }\n\n if (config?.tabServer === false) {\n throw new Error('tabServer transport is disabled and iframe transport was not selected');\n }\n\n return new TabServerTransport(\n typeof config?.tabServer === 'object' ? config.tabServer : { allowedOrigins: ['*'] }\n );\n}\n\n/** Installs the global bridge on `document.modelContext`. */\nexport function initializeWebModelContext(options?: WebModelContextInitOptions): void {\n if (!isBrowserEnvironment() || globalThis.isSecureContext === false) {\n return;\n }\n\n if (runtime) {\n return;\n }\n\n // Cross-bundle guard: if modelContext is already a BrowserMcpServer\n // (set by another bundle in this window), skip initialization.\n const existingContext = readCurrentModelContext();\n if (existingContext && isBrowserMcpServer(existingContext)) {\n return;\n }\n\n // 1. Install polyfill (provides modelContext + modelContextTesting)\n initializeWebMCPPolyfill({\n installTestingShim: options?.installTestingShim ?? true,\n });\n\n // 2. Save reference to the polyfill's (or native) context\n const native = readCurrentModelContext();\n if (!native) {\n throw new Error('modelContext is not available');\n }\n\n // Some browser hosts expose a frozen native context through non-configurable\n // own properties. It is already usable and cannot legally be wrapped.\n if (!canReplaceModelContext(document) || !canReplaceModelContext(navigator)) {\n return;\n }\n\n // 3. Resolve transport before mutating either browser surface.\n const transport = createTransport(options?.transport);\n\n // 4. Create server with native mirroring\n const hostname = window.location.hostname || 'localhost';\n const server = new BrowserMcpServer({ name: `${hostname}-webmcp`, version: '1.0.0' }, { native });\n\n // 5. Replace both the canonical document surface and compatibility alias.\n const previousDocumentModelContextDescriptor = Object.getOwnPropertyDescriptor(\n document,\n 'modelContext'\n );\n const previousNavigatorModelContextDescriptor = Object.getOwnPropertyDescriptor(\n navigator,\n 'modelContext'\n );\n try {\n replaceModelContext(\n server,\n previousDocumentModelContextDescriptor,\n previousNavigatorModelContextDescriptor\n );\n runtime = {\n server,\n transport,\n previousDocumentModelContextDescriptor,\n previousNavigatorModelContextDescriptor,\n };\n } catch (error) {\n void server.close();\n void transport.close();\n throw error;\n }\n\n void (async () => {\n try {\n await server.syncNativeTools();\n } catch (error) {\n console.warn('[WebModelContext] Native WebMCP tool synchronization failed:', error);\n }\n\n if (runtime?.server !== server) {\n return;\n }\n\n try {\n await server.connect(transport);\n } catch (error) {\n console.error('[WebModelContext] Failed to connect MCP transport:', error);\n if (runtime?.server === server) {\n cleanupWebModelContext();\n }\n }\n })();\n}\n\nexport function cleanupWebModelContext(): void {\n if (!runtime) {\n return;\n }\n\n const {\n server,\n transport,\n previousDocumentModelContextDescriptor,\n previousNavigatorModelContextDescriptor,\n } = runtime;\n runtime = null;\n\n void server.close();\n void transport.close();\n\n // Restore the descriptors that existed before we wrapped with BrowserMcpServer.\n // We intentionally do NOT call cleanupWebMCPPolyfill() here — the polyfill\n // manages its own lifecycle (auto-init, testing shim) independently.\n restoreProperty(document, 'modelContext', previousDocumentModelContextDescriptor);\n restoreProperty(navigator, 'modelContext', previousNavigatorModelContextDescriptor);\n}\n","import { cleanupWebModelContext, initializeWebModelContext } from './global.js';\n\nexport { cleanupWebModelContext, initializeWebModelContext };\n\nexport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\n\nif (typeof window !== 'undefined' && typeof document !== 'undefined') {\n const options = window.__webModelContextOptions;\n const shouldAutoInitialize = options?.autoInitialize !== false;\n\n if (shouldAutoInitialize) {\n try {\n initializeWebModelContext(options);\n } catch (error) {\n console.error('[WebModelContext] Auto-initialization failed:', error);\n }\n }\n}\n"],"mappings":";;;;;AAcA,IAAI,UAA+B;AAEnC,SAAS,uBAAgC;CACvC,OAAO,OAAO,WAAW,eAAe,OAAO,OAAO,cAAc;AACtE;AAEA,SAAS,0BAAoD;CAC3D,OAAO,SAAS,gBAAgB,UAAU;AAC5C;AAEA,SAAS,uBAAuB,QAAuC;CACrE,MAAM,aAAa,OAAO,yBAAyB,QAAQ,cAAc;CACzE,OAAO,aAAa,WAAW,iBAAiB,OAAO,OAAO,aAAa,MAAM;AACnF;AAEA,SAAS,4BAA4B,OAAsB;CACzD,OAAO,eAAe,UAAU,gBAAgB;EAC9C,cAAc;EACd,YAAY;EACZ,UAAU;EACV;CACF,CAAC;CAED,IAAI,SAAS,iBAAiB,OAC5B,QAAQ,MACN,8DACA,eACA,OAAO,yBAAyB,UAAU,cAAc,CAC1D;AAEJ;AAEA,SAAS,6BAA6B,OAAsB;CAC1D,OAAO,eAAe,WAAW,gBAAgB;EAC/C,cAAc;EACd,YAAY;EACZ,UAAU;EAC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GAD3B,OAAO,SAAS,YAAY,YACW;EAAU,SAAS;CAAQ,GAAG,EAAE,OAAO,CAAC;CAGhG,MAAM,yCAAyC,OAAO,yBACpD,UACA,cACF;CACA,MAAM,0CAA0C,OAAO,yBACrD,WACA,cACF;CACA,IAAI;EACF,oBACE,QACA,wCACA,uCACF;EACA,UAAU;GACR;GACA;GACA;GACA;EACF;CACF,SAAS,OAAO;EACd,AAAK,OAAO,MAAM;EAClB,AAAK,UAAU,MAAM;EACrB,MAAM;CACR;CAEA,CAAM,YAAY;EAChB,IAAI;GACF,MAAM,OAAO,gBAAgB;EAC/B,SAAS,OAAO;GACd,QAAQ,KAAK,gEAAgE,KAAK;EACpF;EAEA,IAAI,SAAS,WAAW,QACtB;EAGF,IAAI;GACF,MAAM,OAAO,QAAQ,SAAS;EAChC,SAAS,OAAO;GACd,QAAQ,MAAM,sDAAsD,KAAK;GACzE,IAAI,SAAS,WAAW,QACtB,uBAAuB;EAE3B;CACF,GAAG;AACL;AAEA,SAAgB,yBAA+B;CAC7C,IAAI,CAAC,SACH;CAGF,MAAM,EACJ,QACA,WACA,wCACA,4CACE;CACJ,UAAU;CAEV,AAAK,OAAO,MAAM;CAClB,AAAK,UAAU,MAAM;CAKrB,gBAAgB,UAAU,gBAAgB,sCAAsC;CAChF,gBAAgB,WAAW,gBAAgB,uCAAuC;AACpF;;;;ACzNA,IAAI,OAAO,WAAW,eAAe,OAAO,aAAa,aAAa;CACpE,MAAM,UAAU,OAAO;CAGvB,IAF6B,SAAS,mBAAmB,OAGvD,IAAI;EACF,0BAA0B,OAAO;CACnC,SAAS,OAAO;EACd,QAAQ,MAAM,iDAAiD,KAAK;CACtE;AAEJ"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/global.ts","../src/index.ts"],"sourcesContent":["import { IframeChildTransport, TabServerTransport } from '@mcp-b/transports';\nimport { installWebMCP } from '@mcp-b/webmcp-polyfill';\nimport { BrowserMcpServer, isBrowserMcpServer } from '@mcp-b/webmcp-ts-sdk';\nimport type { Transport } from '@modelcontextprotocol/server';\nimport type { WebModelContextInitOptions } from './types.js';\n\ninterface RuntimeState {\n server: BrowserMcpServer;\n transport: Transport;\n previousDescriptor: PropertyDescriptor | undefined;\n}\n\nlet runtime: RuntimeState | null = null;\n\nfunction createTransport(config: WebModelContextInitOptions['transport']): Transport {\n const inIframe = window.parent !== window;\n\n if (inIframe && config?.iframeServer !== false) {\n return new IframeChildTransport(config?.iframeServer || { allowedOrigins: ['*'] });\n }\n\n if (config?.tabServer === false) {\n throw new Error('tabServer transport is disabled and iframe transport was not selected');\n }\n\n return new TabServerTransport(config?.tabServer || { allowedOrigins: ['*'] });\n}\n\n/** Installs the global bridge on `document.modelContext`. */\nexport function initializeWebModelContext(options?: WebModelContextInitOptions): void {\n if (!globalThis.window || !globalThis.document || globalThis.isSecureContext === false) {\n return;\n }\n\n if (runtime) {\n return;\n }\n\n // Cross-bundle guard: another bundle in this window already installed the bridge.\n if (isBrowserMcpServer(document.modelContext)) {\n return;\n }\n\n // Preserve native/core contexts and add declarative support when it is missing.\n installWebMCP();\n // Capture the upstream context before installing MCP-B extensions.\n const native = document.modelContext;\n if (!native) {\n throw new Error('modelContext is not available');\n }\n\n // Some browser hosts expose a frozen native context through non-configurable\n // own properties. It is already usable and cannot legally be wrapped.\n const previousDescriptor = Object.getOwnPropertyDescriptor(document, 'modelContext');\n if (previousDescriptor ? !previousDescriptor.configurable : !Object.isExtensible(document)) {\n return;\n }\n\n // Resolve transport before replacing the document context.\n const transport = createTransport(options?.transport);\n\n // Create the MCP server with native mirroring.\n const hostname = window.location.hostname || 'localhost';\n const server = new BrowserMcpServer({ name: `${hostname}-webmcp`, version: '1.0.0' }, { native });\n\n try {\n Object.defineProperty(document, 'modelContext', {\n configurable: true,\n enumerable: true,\n writable: false,\n value: server,\n });\n runtime = { server, transport, previousDescriptor };\n } catch (error) {\n void server.close();\n void transport.close();\n throw error;\n }\n\n void (async () => {\n try {\n await server.syncNativeTools();\n } catch (error) {\n console.warn('[WebModelContext] Native WebMCP tool synchronization failed:', error);\n }\n\n if (runtime?.server !== server) {\n return;\n }\n\n try {\n await server.connect(transport);\n } catch (error) {\n console.error('[WebModelContext] Failed to connect MCP transport:', error);\n if (runtime?.server === server) {\n cleanupWebModelContext();\n }\n }\n })();\n}\n\nexport function cleanupWebModelContext(): void {\n if (!runtime) {\n return;\n }\n\n const { server, transport, previousDescriptor } = runtime;\n runtime = null;\n\n void server.close();\n void transport.close();\n\n // The polyfill and its declarative layer remain installed for the lifetime of the document.\n if (previousDescriptor) {\n Object.defineProperty(document, 'modelContext', previousDescriptor);\n } else {\n Reflect.deleteProperty(document, 'modelContext');\n }\n}\n","import { cleanupWebModelContext, initializeWebModelContext } from './global.js';\n\nexport { cleanupWebModelContext, initializeWebModelContext };\nexport type { WebMCP } from '@mcp-b/webmcp-polyfill';\n\nexport type { TransportConfiguration, WebModelContextInitOptions } from './types.js';\n\nconst options = globalThis.window?.__webModelContextOptions;\nif (options?.autoInitialize !== false) {\n try {\n initializeWebModelContext(options);\n } catch (error) {\n console.error('[WebModelContext] Auto-initialization failed:', error);\n }\n}\n"],"mappings":";;;;;AAYA,IAAI,UAA+B;AAEnC,SAAS,gBAAgB,QAA4D;CAGnF,IAFiB,OAAO,WAAW,UAEnB,QAAQ,iBAAiB,OACvC,OAAO,IAAI,qBAAqB,QAAQ,gBAAgB,EAAE,gBAAgB,CAAC,GAAG,EAAE,CAAC;CAGnF,IAAI,QAAQ,cAAc,OACxB,MAAM,IAAI,MAAM,uEAAuE;CAGzF,OAAO,IAAI,mBAAmB,QAAQ,aAAa,EAAE,gBAAgB,CAAC,GAAG,EAAE,CAAC;AAC9E;;AAGA,SAAgB,0BAA0B,SAA4C;CACpF,IAAI,CAAC,WAAW,UAAU,CAAC,WAAW,YAAY,WAAW,oBAAoB,OAC/E;CAGF,IAAI,SACF;CAIF,IAAI,mBAAmB,SAAS,YAAY,GAC1C;CAIF,cAAc;CAEd,MAAM,SAAS,SA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;AACtE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-b/global",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "W3C Web Model Context Protocol (WebMCP) API polyfill. Let AI agents like Claude, ChatGPT, and Gemini interact with your website via document.modelContext.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai-agents",
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"llm",
|
|
21
21
|
"mcp",
|
|
22
22
|
"model-context-protocol",
|
|
23
|
-
"navigator-modelcontext",
|
|
24
23
|
"openai",
|
|
25
24
|
"polyfill",
|
|
26
25
|
"tool-registration",
|
|
@@ -67,10 +66,9 @@
|
|
|
67
66
|
},
|
|
68
67
|
"dependencies": {
|
|
69
68
|
"@modelcontextprotocol/server": "2.0.0",
|
|
70
|
-
"@mcp-b/transports": "
|
|
71
|
-
"@mcp-b/webmcp-
|
|
72
|
-
"@mcp-b/webmcp-
|
|
73
|
-
"@mcp-b/webmcp-ts-sdk": "5.1.0"
|
|
69
|
+
"@mcp-b/transports": "6.0.0",
|
|
70
|
+
"@mcp-b/webmcp-ts-sdk": "6.0.0",
|
|
71
|
+
"@mcp-b/webmcp-polyfill": "6.0.0"
|
|
74
72
|
},
|
|
75
73
|
"devDependencies": {
|
|
76
74
|
"@modelcontextprotocol/client": "2.0.0",
|
|
@@ -94,9 +92,8 @@
|
|
|
94
92
|
"publish:npm": "pnpm publish --access public",
|
|
95
93
|
"test": "vp test run",
|
|
96
94
|
"test:conformance:global": "vp test run conformance/global-runtime.e2e.test.ts --browser.headless",
|
|
97
|
-
"test:conformance:matrix": "pnpm
|
|
95
|
+
"test:conformance:matrix": "pnpm --filter @mcp-b/webmcp-polyfill test --browser.headless && pnpm run test:conformance:global && pnpm run test:conformance:native",
|
|
98
96
|
"test:conformance:native": "vp test run --config vitest.conformance.native.config.ts --browser.headless",
|
|
99
|
-
"test:conformance:polyfill": "pnpm --filter @mcp-b/webmcp-polyfill run test:conformance",
|
|
100
97
|
"test:coverage": "vp test run --coverage",
|
|
101
98
|
"test:watch": "vp test",
|
|
102
99
|
"typecheck": "tsc --noEmit",
|