@mcp-b/global 1.0.10 → 1.0.13
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/LICENSE +21 -0
- package/dist/index.js +24 -3
- package/package.json +45 -52
- package/dist/index.cjs +0 -3
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -23
- package/dist/index.d.ts +0 -23
- package/dist/index.js.map +0 -1
- package/dist/index.umd.js +0 -11
- package/dist/index.umd.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,74 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-b/global",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"author": "Alex Nahas",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/MiguelsPizza/WebMCP.git",
|
|
10
|
-
"directory": "packages/global"
|
|
11
|
-
},
|
|
12
|
-
"homepage": "https://github.com/MiguelsPizza/WebMCP#readme",
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/MiguelsPizza/WebMCP/issues"
|
|
15
|
-
},
|
|
3
|
+
"version": "1.0.13",
|
|
4
|
+
"description": "ESM bundle for MCP-B browser injection via Chrome MV3 scripting",
|
|
16
5
|
"keywords": [
|
|
17
6
|
"mcp",
|
|
18
7
|
"model-context-protocol",
|
|
19
8
|
"browser",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
9
|
+
"chrome-extension",
|
|
10
|
+
"mv3",
|
|
11
|
+
"injection",
|
|
12
|
+
"esm"
|
|
24
13
|
],
|
|
14
|
+
"homepage": "https://github.com/MiguelsPizza/WebMCP#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/MiguelsPizza/WebMCP/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/MiguelsPizza/WebMCP.git",
|
|
21
|
+
"directory": "packages/global"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Alex Nahas",
|
|
25
|
+
"sideEffects": true,
|
|
25
26
|
"type": "module",
|
|
26
|
-
"main": "./dist/index.cjs",
|
|
27
|
-
"module": "./dist/index.js",
|
|
28
|
-
"browser": "./dist/index.umd.js",
|
|
29
|
-
"unpkg": "./dist/index.umd.js",
|
|
30
|
-
"types": "./dist/index.d.ts",
|
|
31
27
|
"exports": {
|
|
32
|
-
".":
|
|
33
|
-
"types": "./dist/index.d.ts",
|
|
34
|
-
"import": "./dist/index.js",
|
|
35
|
-
"require": "./dist/index.cjs",
|
|
36
|
-
"browser": "./dist/index.umd.js"
|
|
37
|
-
},
|
|
38
|
-
"./umd": "./dist/index.umd.js"
|
|
28
|
+
".": "./dist/index.js"
|
|
39
29
|
},
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"browser": "./dist/index.js",
|
|
40
33
|
"files": [
|
|
41
34
|
"dist"
|
|
42
35
|
],
|
|
43
|
-
"scripts": {
|
|
44
|
-
"build": "tsup",
|
|
45
|
-
"typecheck": "tsc --noEmit",
|
|
46
|
-
"clean": "rm -rf dist .turbo",
|
|
47
|
-
"prepublishOnly": "pnpm run build",
|
|
48
|
-
"publish:npm": "npm publish --access public --no-git-checks",
|
|
49
|
-
"publish:dry": "pnpm publish --access public --no-git-checks --dry-run",
|
|
50
|
-
"version:patch": "pnpm version patch --no-git-tag-version",
|
|
51
|
-
"version:minor": "pnpm version minor --no-git-tag-version",
|
|
52
|
-
"version:major": "pnpm version major --no-git-tag-version",
|
|
53
|
-
"lint": "biome lint --write .",
|
|
54
|
-
"format": "biome format --write .",
|
|
55
|
-
"check": "biome check --write ."
|
|
56
|
-
},
|
|
57
36
|
"dependencies": {
|
|
58
|
-
"@
|
|
59
|
-
"@
|
|
37
|
+
"@modelcontextprotocol/sdk": "1.15.0",
|
|
38
|
+
"@mcp-b/transports": "1.0.3"
|
|
60
39
|
},
|
|
61
40
|
"devDependencies": {
|
|
62
|
-
"@types/node": "
|
|
63
|
-
"
|
|
64
|
-
"
|
|
41
|
+
"@types/node": "^22.15.21",
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"vite": "^7.1.1",
|
|
44
|
+
"vite-plugin-monkey": "^6.0.1"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18"
|
|
65
48
|
},
|
|
66
49
|
"publishConfig": {
|
|
67
50
|
"access": "public",
|
|
68
51
|
"registry": "https://registry.npmjs.org/"
|
|
69
52
|
},
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
"
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "vite build",
|
|
55
|
+
"check": "biome check --write .",
|
|
56
|
+
"clean": "rm -rf dist .turbo",
|
|
57
|
+
"format": "biome format --write .",
|
|
58
|
+
"lint": "biome lint --write .",
|
|
59
|
+
"preview": "vite preview",
|
|
60
|
+
"publish:dry": "pnpm publish --access public --dry-run",
|
|
61
|
+
"publish:npm": "pnpm publish --access public",
|
|
62
|
+
"typecheck": "tsc --noEmit",
|
|
63
|
+
"version:major": "pnpm version major --no-git-tag-version",
|
|
64
|
+
"version:minor": "pnpm version minor --no-git-tag-version",
|
|
65
|
+
"version:patch": "pnpm version patch --no-git-tag-version"
|
|
73
66
|
}
|
|
74
|
-
}
|
|
67
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var transports=require('@mcp-b/transports'),mcp_js=require('@modelcontextprotocol/sdk/server/mcp.js');// @mcp-b/global - MIT License
|
|
2
|
-
var t=null,i=false;function c(){if(!t)try{let e=window.location.hostname||"localhost";t=new mcp_js.McpServer({name:e,version:"1.0.0"},{capabilities:{tools:{listChanged:!0},debouncedNotificationMethods:["notifications/tools/list_changed"]}}),t.registerTool("get_current_website_title",{description:"Get the title of the current website"},async()=>{try{return {content:[{type:"text",text:document.title||"Untitled"}]}}catch(n){return console.error("Error in get_current_website_title tool:",n),{content:[{type:"text",text:`Error getting website title ${n instanceof Error?n.message:String(n)}`}],isError:!0}}});let o=new transports.TabServerTransport({allowedOrigins:["*"]});t.connect(o);}catch(e){t=null;let o=e instanceof Error?e.message:"Unknown error";throw new Error(`Failed to initialize MCP server: ${o}`)}}function r(){if(typeof window=="undefined"){console.warn("initializeGlobalMCP called in non-browser environment; skipping.");return}if(!(i&&window.mcp&&t))try{c(),t&&(window.mcp=t,i=!0);}catch(e){throw console.error("Failed to initialize global MCP:",e),e}}function s(){if(t)try{t.close();}catch(e){console.warn("Error closing MCP server:",e);}finally{t=null;}typeof window!="undefined"&&"mcp"in window&&delete window.mcp,i=false;}if(typeof window!="undefined"&&typeof document!="undefined"){let e=()=>{try{r();}catch(o){console.error("Auto-initialization of global MCP failed:",o);}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",e):e();}var p=r;exports.cleanupGlobalMCP=s;exports.default=p;exports.initializeGlobalMCP=r;//# sourceMappingURL=index.cjs.map
|
|
3
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/global.ts","../src/index.ts"],"names":["serverInstance","isInitialized","initializeMCP","hostname","McpServer","error","transport","TabServerTransport","errorMessage","initializeGlobalMCP","cleanupGlobalMCP","autoInitialize","index_default"],"mappings":";AAKA,IAAIA,CAAmC,CAAA,IAAA,CACnCC,CAAgB,CAAA,KAAA,CAOpB,SAASC,CAAAA,EAAsB,CAC7B,GAAI,CAAAF,CAAAA,CAIJ,GAAI,CACF,IAAMG,CAAAA,CAAW,MAAO,CAAA,QAAA,CAAS,QAAY,EAAA,WAAA,CAE7CH,CAAiB,CAAA,IAAII,gBACnB,CAAA,CACE,IAAMD,CAAAA,CAAAA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CAAA,GAAA,EAAOD,CAInC,CAAA,CAAA,GAAI,CACFE,CAAAA,EACIF,CAAAA,CAAAA,GACF,MAAO,CAAA,GAAA,CAAMA,CACbC,CAAAA,CAAAA,CAAgB,CAEpB,CAAA,EAAA,CAAA,MAASI,CAAO,CAAA,CACd,MAAQ,OAAA,CAAA,KAAA,CAAM,kCAAoCA,CAAAA,CAAK,CACjDA,CAAAA,CACR,CACF,CAOO,SAASK,CAAAA,EAAyB,CACvC,GAAIV,CACF,CAAA,GAAI,CACFA,CAAe,CAAA,KAAA,GACjB,CAAA,MAASK,CAAO,CAAA,CACd,OAAQ,CAAA,IAAA,CAAK,2BAA6BA,CAAAA,CAAK,EACjD,CAAA,OAAE,CACAL,CAAAA,CAAiB,KACnB,CAGE,OAAO,MAAA,EAAW,WAAe,EAAA,KAAA,GAAS,MAC5C,EAAA,OAAQ,MAAwC,CAAA,GAAA,CAGlDC,CAAgB,CAAA,MAClB,CClHA,GAAI,OAAO,MAAA,EAAW,WAAe,EAAA,OAAO,QAAa,EAAA,WAAA,CAAa,CACpE,IAAMU,CAAiB,CAAA,IAAM,CAC3B,GAAI,CACFF,CAAAA,GACF,CAAA,MAASJ,CAAO,CAAA,CACd,OAAQ,CAAA,KAAA,CAAM,2CAA6CA,CAAAA,CAAK,EAClE,CACF,CAEI,CAAA,QAAA,CAAS,UAAe,GAAA,SAAA,CAC1B,QAAS,CAAA,gBAAA,CAAiB,kBAAoBM,CAAAA,CAAc,CAE5DA,CAAAA,CAAAA,GAEJ,KAGOC,CAAQH,CAAAA","file":"index.cjs","sourcesContent":["// global.ts\n\nimport { TabServerTransport } from '@mcp-b/transports';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\n\nlet serverInstance: McpServer | null = null;\nlet isInitialized = false;\n\n/**\n * Internal initialization function that creates and configures the MCP server.\n * This function is idempotent and handles errors gracefully.\n * @throws {Error} If initialization fails\n */\nfunction initializeMCP(): void {\n if (serverInstance) {\n return;\n }\n\n try {\n const hostname = window.location.hostname || 'localhost';\n\n serverInstance = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\n debouncedNotificationMethods: ['notifications/tools/list_changed'],\n },\n }\n );\n\n // Register default tool with proper error handling in the callback\n serverInstance.registerTool(\n 'get_current_website_title',\n {\n description: 'Get the title of the current website',\n },\n async () => {\n try {\n const title = document.title || 'Untitled';\n return { content: [{ type: 'text', text: title }] };\n } catch (error) {\n console.error('Error in get_current_website_title tool:', error);\n return {\n content: [\n {\n type: 'text',\n text: `Error getting website title ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n );\n\n // Configure transport with restricted origins if possible; '*' is insecure, consider environment-specific origins\n // For best security practices, replace '*' with specific allowed origins in production\n const transport = new TabServerTransport({\n allowedOrigins: ['*'],\n });\n\n serverInstance.connect(transport);\n } catch (error) {\n serverInstance = null;\n const errorMessage = error instanceof Error ? error.message : 'Unknown error';\n throw new Error(`Failed to initialize MCP server: ${errorMessage}`);\n }\n}\n\n/**\n * Initializes the global MCP server and exposes it on window.mcp.\n * This function is safe to call multiple times - it will only initialize once.\n * It performs environment checks and handles browser-only execution.\n */\nexport function initializeGlobalMCP(): void {\n if (typeof window === 'undefined') {\n console.warn('initializeGlobalMCP called in non-browser environment; skipping.');\n return;\n }\n\n if (isInitialized && window.mcp && serverInstance) {\n return;\n }\n\n try {\n initializeMCP();\n if (serverInstance) {\n window.mcp = serverInstance;\n isInitialized = true;\n }\n } catch (error) {\n console.error('Failed to initialize global MCP:', error);\n throw error;\n }\n}\n\n/**\n * Cleanup function to properly dispose of the MCP server and remove global reference.\n * This is useful for testing, hot module replacement, or resetting the state.\n * It handles errors during cleanup gracefully.\n */\nexport function cleanupGlobalMCP(): void {\n if (serverInstance) {\n try {\n serverInstance.close();\n } catch (error) {\n console.warn('Error closing MCP server:', error);\n } finally {\n serverInstance = null;\n }\n }\n\n if (typeof window !== 'undefined' && 'mcp' in window) {\n delete (window as unknown as { mcp?: unknown }).mcp;\n }\n\n isInitialized = false;\n}\n","// index.ts\n\nimport { initializeGlobalMCP } from './global.js';\n\nexport * from './types.js';\nexport { initializeGlobalMCP, cleanupGlobalMCP } from './global.js';\n\n// Auto-initialize when script loads in browser environments\n// Using DOMContentLoaded for better timing instead of setTimeout(0), ensuring DOM is ready\nif (typeof window !== 'undefined' && typeof document !== 'undefined') {\n const autoInitialize = () => {\n try {\n initializeGlobalMCP();\n } catch (error) {\n console.error('Auto-initialization of global MCP failed:', error);\n }\n };\n\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', autoInitialize);\n } else {\n autoInitialize();\n }\n}\n\n// For manual initialization (when using as ES module)\nexport default initializeGlobalMCP;\n"]}
|
package/dist/index.d.cts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
-
export { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Initializes the global MCP server and exposes it on window.mcp.
|
|
6
|
-
* This function is safe to call multiple times - it will only initialize once.
|
|
7
|
-
* It performs environment checks and handles browser-only execution.
|
|
8
|
-
*/
|
|
9
|
-
declare function initializeGlobalMCP(): void;
|
|
10
|
-
/**
|
|
11
|
-
* Cleanup function to properly dispose of the MCP server and remove global reference.
|
|
12
|
-
* This is useful for testing, hot module replacement, or resetting the state.
|
|
13
|
-
* It handles errors during cleanup gracefully.
|
|
14
|
-
*/
|
|
15
|
-
declare function cleanupGlobalMCP(): void;
|
|
16
|
-
|
|
17
|
-
declare global {
|
|
18
|
-
interface Window {
|
|
19
|
-
mcp: McpServer;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { cleanupGlobalMCP, initializeGlobalMCP as default, initializeGlobalMCP };
|
package/dist/index.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
-
export { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Initializes the global MCP server and exposes it on window.mcp.
|
|
6
|
-
* This function is safe to call multiple times - it will only initialize once.
|
|
7
|
-
* It performs environment checks and handles browser-only execution.
|
|
8
|
-
*/
|
|
9
|
-
declare function initializeGlobalMCP(): void;
|
|
10
|
-
/**
|
|
11
|
-
* Cleanup function to properly dispose of the MCP server and remove global reference.
|
|
12
|
-
* This is useful for testing, hot module replacement, or resetting the state.
|
|
13
|
-
* It handles errors during cleanup gracefully.
|
|
14
|
-
*/
|
|
15
|
-
declare function cleanupGlobalMCP(): void;
|
|
16
|
-
|
|
17
|
-
declare global {
|
|
18
|
-
interface Window {
|
|
19
|
-
mcp: McpServer;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { cleanupGlobalMCP, initializeGlobalMCP as default, initializeGlobalMCP };
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/global.ts","../src/index.ts"],"names":["serverInstance","isInitialized","initializeMCP","hostname","McpServer","error","transport","TabServerTransport","errorMessage","initializeGlobalMCP","cleanupGlobalMCP","autoInitialize","index_default"],"mappings":";AAKA,IAAIA,CAAmC,CAAA,IAAA,CACnCC,CAAgB,CAAA,KAAA,CAOpB,SAASC,CAAAA,EAAsB,CAC7B,GAAI,CAAAF,CAAAA,CAIJ,GAAI,CACF,IAAMG,CAAAA,CAAW,MAAO,CAAA,QAAA,CAAS,QAAY,EAAA,WAAA,CAE7CH,CAAiB,CAAA,IAAII,SACnB,CAAA,CACE,IAAMD,CAAAA,CAAAA,CACN,OAAS,CAAA,OACX,CACA,CAAA,CACE,YAAc,CAAA,CACZ,KAAO,CAAA,CACL,WAAa,CAAA,CAAA,CACf,CACA,CAAA,4BAAA,CAA8B,CAAC,kCAAkC,C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EpB,CAAA,EAAA,CAAA,MAASI,CAAO,CAAA,CACd,MAAQ,OAAA,CAAA,KAAA,CAAM,kCAAoCA,CAAAA,CAAK,CACjDA,CAAAA,CACR,CACF,CAOO,SAASK,CAAAA,EAAyB,CACvC,GAAIV,CACF,CAAA,GAAI,CACFA,CAAe,CAAA,KAAA,GACjB,CAAA,MAASK,CAAO,CAAA,CACd,OAAQ,CAAA,IAAA,CAAK,2BAA6BA,CAAAA,CAAK,EACjD,CAAA,OAAE,CACAL,CAAAA,CAAiB,KACnB,CAGE,OAAO,MAAA,EAAW,WAAe,EAAA,KAAA,GAAS,MAC5C,EAAA,OAAQ,MAAwC,CAAA,GAAA,CAGlDC,CAAgB,CAAA,MAClB,CClHA,GAAI,OAAO,MAAA,EAAW,WAAe,EAAA,OAAO,QAAa,EAAA,WAAA,CAAa,CACpE,IAAMU,CAAiB,CAAA,IAAM,CAC3B,GAAI,CACFF,CAAAA,GACF,CAAA,MAASJ,CAAO,CAAA,CACd,OAAQ,CAAA,KAAA,CAAM,2CAA6CA,CAAAA,CAAK,EAClE,CACF,CAEI,CAAA,QAAA,CAAS,UAAe,GAAA,SAAA,CAC1B,QAAS,CAAA,gBAAA,CAAiB,kBAAoBM,CAAAA,CAAc,CAE5DA,CAAAA,CAAAA,GAEJ,KAGOC,CAAQH,CAAAA","file":"index.js","sourcesContent":["// global.ts\n\nimport { TabServerTransport } from '@mcp-b/transports';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\n\nlet serverInstance: McpServer | null = null;\nlet isInitialized = false;\n\n/**\n * Internal initialization function that creates and configures the MCP server.\n * This function is idempotent and handles errors gracefully.\n * @throws {Error} If initialization fails\n */\nfunction initializeMCP(): void {\n if (serverInstance) {\n return;\n }\n\n try {\n const hostname = window.location.hostname || 'localhost';\n\n serverInstance = new McpServer(\n {\n name: hostname,\n version: '1.0.0',\n },\n {\n capabilities: {\n tools: {\n listChanged: true,\n },\n debouncedNotificationMethods: ['notifications/tools/list_changed'],\n },\n }\n );\n\n // Register default tool with proper error handling in the callback\n serverInstance.registerTool(\n 'get_current_website_title',\n {\n description: 'Get the title of the current website',\n },\n async () => {\n try {\n const title = document.title || 'Untitled';\n return { content: [{ type: 'text', text: title }] };\n } catch (error) {\n console.error('Error in get_current_website_title tool:', error);\n return {\n content: [\n {\n type: 'text',\n text: `Error getting website title ${error instanceof Error ? error.message : String(error)}`,\n },\n ],\n isError: true,\n };\n }\n }\n );\n\n // Configure transport with restricted origins if possible; '*' is insecure, consider environment-specific origins\n // For best security practices, replace '*' with specific allowed origins in production\n const transport = new TabServerTransport({\n allowedOrigins: ['*'],\n });\n\n serverInstance.connect(transport);\n } catch (error) {\n serverInstance = null;\n const errorMessage = error instanceof Error ? error.message : 'Unknown error';\n throw new Error(`Failed to initialize MCP server: ${errorMessage}`);\n }\n}\n\n/**\n * Initializes the global MCP server and exposes it on window.mcp.\n * This function is safe to call multiple times - it will only initialize once.\n * It performs environment checks and handles browser-only execution.\n */\nexport function initializeGlobalMCP(): void {\n if (typeof window === 'undefined') {\n console.warn('initializeGlobalMCP called in non-browser environment; skipping.');\n return;\n }\n\n if (isInitialized && window.mcp && serverInstance) {\n return;\n }\n\n try {\n initializeMCP();\n if (serverInstance) {\n window.mcp = serverInstance;\n isInitialized = true;\n }\n } catch (error) {\n console.error('Failed to initialize global MCP:', error);\n throw error;\n }\n}\n\n/**\n * Cleanup function to properly dispose of the MCP server and remove global reference.\n * This is useful for testing, hot module replacement, or resetting the state.\n * It handles errors during cleanup gracefully.\n */\nexport function cleanupGlobalMCP(): void {\n if (serverInstance) {\n try {\n serverInstance.close();\n } catch (error) {\n console.warn('Error closing MCP server:', error);\n } finally {\n serverInstance = null;\n }\n }\n\n if (typeof window !== 'undefined' && 'mcp' in window) {\n delete (window as unknown as { mcp?: unknown }).mcp;\n }\n\n isInitialized = false;\n}\n","// index.ts\n\nimport { initializeGlobalMCP } from './global.js';\n\nexport * from './types.js';\nexport { initializeGlobalMCP, cleanupGlobalMCP } from './global.js';\n\n// Auto-initialize when script loads in browser environments\n// Using DOMContentLoaded for better timing instead of setTimeout(0), ensuring DOM is ready\nif (typeof window !== 'undefined' && typeof document !== 'undefined') {\n const autoInitialize = () => {\n try {\n initializeGlobalMCP();\n } catch (error) {\n console.error('Auto-initialization of global MCP failed:', error);\n }\n };\n\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', autoInitialize);\n } else {\n autoInitialize();\n }\n}\n\n// For manual initialization (when using as ES module)\nexport default initializeGlobalMCP;\n"]}
|