js-bridge-mcp 0.1.6 → 0.1.7
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/dist/client/main.js +42 -36
- package/package.json +1 -1
package/dist/client/main.js
CHANGED
|
@@ -1,56 +1,62 @@
|
|
|
1
|
-
function
|
|
2
|
-
const
|
|
1
|
+
function _(e) {
|
|
2
|
+
const n = [], o = /* @__PURE__ */ new Map();
|
|
3
3
|
for (const { fn: c, ...s } of e)
|
|
4
|
-
|
|
5
|
-
return { manifest:
|
|
4
|
+
n.push(s), o.set(s.name, c);
|
|
5
|
+
return { manifest: n, fnByName: o };
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
const f = 1e4, g = 3600 * 1e3;
|
|
8
|
+
function $(e, n = {}) {
|
|
9
|
+
let o, c = !1, s = 0, p;
|
|
10
|
+
const m = () => {
|
|
11
|
+
const a = n.tenant ?? (location.pathname.startsWith("/t/") ? location.pathname.slice(3).split("/")[0] : ""), y = a ? `/ws?tenant=${encodeURIComponent(a)}` : "/ws", d = `${n.serverUrl ? n.serverUrl.replace(/^http/, "ws") : `${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}`}${y}`;
|
|
12
|
+
console.log(`[mcp-ws] connecting (attempt ${s + 1}): ${d}`), o = new WebSocket(d), o.onopen = () => {
|
|
13
|
+
console.log(`[mcp-ws] connected${s > 0 ? ` after ${s} reconnect attempt(s)` : ""}`), s = 0, p = void 0, e.onConnect?.();
|
|
14
|
+
}, o.onclose = (i) => {
|
|
15
|
+
if (console.log(`[mcp-ws] disconnected: code=${i.code} reason=${i.reason || "(none)"} wasClean=${i.wasClean}`), e.onDisconnect?.(), !c) {
|
|
16
|
+
if (i.code === 4404) {
|
|
17
|
+
console.log("[mcp-ws] invalid tenant id (4404) — not retrying");
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
if (p ??= Date.now(), Date.now() - p > g) {
|
|
21
|
+
console.log(`[mcp-ws] giving up after retrying for over ${g / 6e4} minutes`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
s++, console.log(`[mcp-ws] retrying in ${f / 1e3}s (attempt ${s + 1})`), setTimeout(m, f);
|
|
20
25
|
}
|
|
21
26
|
}, o.onerror = () => {
|
|
22
27
|
console.log("[mcp-ws] socket error (see close event for details)");
|
|
23
|
-
}, o.onmessage = (
|
|
24
|
-
const
|
|
25
|
-
|
|
28
|
+
}, o.onmessage = (i) => {
|
|
29
|
+
const t = JSON.parse(i.data);
|
|
30
|
+
t.type === "init" && e.onInit?.(t.schema, t.state), t.type === "reinit" && e.onReinit?.(t.schema, t.state), t.type === "update" && e.onUpdate?.(t.field, t.value), t.type === "call" && e.onCall?.(t.id, t.name, t.args), t.type === "identify" && (e.onIdentify ? e.onIdentify(t.label) : alert(`Identify: this is the "${t.label ?? "unlabeled"}" connection`));
|
|
26
31
|
};
|
|
27
32
|
};
|
|
28
|
-
return
|
|
29
|
-
send(
|
|
30
|
-
o?.send(JSON.stringify(
|
|
33
|
+
return m(), {
|
|
34
|
+
send(a) {
|
|
35
|
+
o?.send(JSON.stringify(a));
|
|
31
36
|
},
|
|
32
37
|
close() {
|
|
33
38
|
c = !0, o?.close();
|
|
34
39
|
}
|
|
35
40
|
};
|
|
36
41
|
}
|
|
37
|
-
const
|
|
38
|
-
let
|
|
39
|
-
function
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
const e = prompt("Name this MCP connection (used to identify it to the agent):",
|
|
43
|
-
return e && (
|
|
42
|
+
const h = window.__mcpTools ?? [], { manifest: C, fnByName: b } = _(h), N = window.__mcpSummary ?? void 0;
|
|
43
|
+
let r = window.__mcpAppName ?? document.title ?? void 0, w = !1;
|
|
44
|
+
function v() {
|
|
45
|
+
if (w) return r;
|
|
46
|
+
w = !0;
|
|
47
|
+
const e = prompt("Name this MCP connection (used to identify it to the agent):", r ?? "");
|
|
48
|
+
return e && (r = e), r;
|
|
44
49
|
}
|
|
45
|
-
const
|
|
50
|
+
const u = new URL(import.meta.url), S = u.searchParams.get("server") ?? void 0, R = u.searchParams.get("tenant") ?? void 0, l = $(
|
|
46
51
|
{
|
|
47
52
|
onConnect() {
|
|
48
|
-
|
|
53
|
+
const e = v();
|
|
54
|
+
console.log(`[js-bridge-mcp] connected as "${e ?? "(unlabeled)"}"`), l.send({ type: "register_tools", tools: C, summary: N, appLabel: e });
|
|
49
55
|
},
|
|
50
|
-
async onCall(e,
|
|
56
|
+
async onCall(e, n, o) {
|
|
51
57
|
try {
|
|
52
|
-
const c =
|
|
53
|
-
if (!c) throw new Error(`no page tool named "${
|
|
58
|
+
const c = b.get(n);
|
|
59
|
+
if (!c) throw new Error(`no page tool named "${n}" — was it in window.__mcpTools when this script loaded?`);
|
|
54
60
|
const s = await c(o);
|
|
55
61
|
l.send({ type: "call_result", id: e, result: s });
|
|
56
62
|
} catch (c) {
|
|
@@ -61,9 +67,9 @@ const w = new URL(import.meta.url), b = w.searchParams.get("server") ?? void 0,
|
|
|
61
67
|
console.log("[js-bridge-mcp] disconnected, retrying...");
|
|
62
68
|
}
|
|
63
69
|
},
|
|
64
|
-
{ serverUrl:
|
|
70
|
+
{ serverUrl: S, tenant: R }
|
|
65
71
|
);
|
|
66
72
|
window.__mcpRename = (e) => {
|
|
67
|
-
const
|
|
68
|
-
|
|
73
|
+
const n = e ?? prompt("Rename this MCP connection:", r ?? "");
|
|
74
|
+
n && (r = n, l.send({ type: "rename_connection", appLabel: n }), console.log(`[js-bridge-mcp] renamed connection to "${n}"`));
|
|
69
75
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-bridge-mcp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Generic bridge: exposes MCP tools discovered from a connected page's own JSON tool manifest, dispatched over a cross-origin WebSocket. Ships a hello-world example page under legacy-page/.",
|
|
6
6
|
"repository": {
|