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.
Files changed (2) hide show
  1. package/dist/client/main.js +42 -36
  2. package/package.json +1 -1
@@ -1,56 +1,62 @@
1
- function f(e) {
2
- const t = [], o = /* @__PURE__ */ new Map();
1
+ function _(e) {
2
+ const n = [], o = /* @__PURE__ */ new Map();
3
3
  for (const { fn: c, ...s } of e)
4
- t.push(s), o.set(s.name, c);
5
- return { manifest: t, fnByName: o };
4
+ n.push(s), o.set(s.name, c);
5
+ return { manifest: n, fnByName: o };
6
6
  }
7
- function u(e, t = {}) {
8
- let o, c = !1, s = 0;
9
- const p = () => {
10
- const i = t.tenant ?? (location.pathname.startsWith("/t/") ? location.pathname.slice(3).split("/")[0] : ""), g = i ? `/ws?tenant=${encodeURIComponent(i)}` : "/ws", m = `${t.serverUrl ? t.serverUrl.replace(/^http/, "ws") : `${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}`}${g}`;
11
- console.log(`[mcp-ws] connecting (attempt ${s + 1}): ${m}`), o = new WebSocket(m), o.onopen = () => {
12
- console.log(`[mcp-ws] connected${s > 0 ? ` after ${s} reconnect attempt(s)` : ""}`), s = 0, e.onConnect?.();
13
- }, o.onclose = (r) => {
14
- if (console.log(`[mcp-ws] disconnected: code=${r.code} reason=${r.reason || "(none)"} wasClean=${r.wasClean}`), e.onDisconnect?.(), !c) {
15
- if (r.code === 4404) {
16
- console.log("[mcp-ws] tenant unknown/expired (4404) — not retrying");
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
- s++, console.log(`[mcp-ws] retrying in 2s (attempt ${s + 1})`), setTimeout(p, 2e3);
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 = (r) => {
24
- const n = JSON.parse(r.data);
25
- n.type === "init" && e.onInit?.(n.schema, n.state), n.type === "reinit" && e.onReinit?.(n.schema, n.state), n.type === "update" && e.onUpdate?.(n.field, n.value), n.type === "call" && e.onCall?.(n.id, n.name, n.args);
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 p(), {
29
- send(i) {
30
- o?.send(JSON.stringify(i));
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 y = window.__mcpTools ?? [], { manifest: h, fnByName: $ } = f(y), _ = window.__mcpSummary ?? void 0;
38
- let a = window.__mcpAppName ?? document.title ?? void 0, d = !1;
39
- function C() {
40
- if (d) return a;
41
- d = !0;
42
- const e = prompt("Name this MCP connection (used to identify it to the agent):", a ?? "");
43
- return e && (a = e), a;
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 w = new URL(import.meta.url), b = w.searchParams.get("server") ?? void 0, S = w.searchParams.get("tenant") ?? void 0, l = u(
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
- console.log("[js-bridge-mcp] connected"), l.send({ type: "register_tools", tools: h, summary: _, appLabel: C() });
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, t, o) {
56
+ async onCall(e, n, o) {
51
57
  try {
52
- const c = $.get(t);
53
- if (!c) throw new Error(`no page tool named "${t}" — was it in window.__mcpTools when this script loaded?`);
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: b, tenant: S }
70
+ { serverUrl: S, tenant: R }
65
71
  );
66
72
  window.__mcpRename = (e) => {
67
- const t = e ?? prompt("Rename this MCP connection:", a ?? "");
68
- t && (a = t, l.send({ type: "rename_connection", appLabel: t }), console.log(`[js-bridge-mcp] renamed connection to "${t}"`));
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.6",
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": {