@jobshimo/browser-link 0.1.0 → 0.4.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 +58 -21
- package/dist/bridge/client.d.ts +101 -0
- package/dist/bridge/client.js +435 -0
- package/dist/bridge/client.js.map +1 -0
- package/dist/bridge/dispatch.d.ts +29 -0
- package/dist/bridge/dispatch.js +39 -0
- package/dist/bridge/dispatch.js.map +1 -0
- package/dist/bridge/events.d.ts +39 -0
- package/dist/bridge/events.js +47 -0
- package/dist/bridge/events.js.map +1 -0
- package/dist/bridge/protocol.d.ts +80 -0
- package/dist/bridge/protocol.js +79 -0
- package/dist/bridge/protocol.js.map +1 -0
- package/dist/bridge/server.d.ts +42 -0
- package/dist/bridge/server.js +336 -0
- package/dist/bridge/server.js.map +1 -0
- package/dist/bridge/token.d.ts +17 -0
- package/dist/bridge/token.js +79 -0
- package/dist/bridge/token.js.map +1 -0
- package/dist/cli.js +132 -39
- package/dist/cli.js.map +1 -1
- package/dist/commands/about.d.ts +3 -6
- package/dist/commands/about.js +2 -18
- package/dist/commands/about.js.map +1 -1
- package/dist/commands/doctor.d.ts +12 -1
- package/dist/commands/doctor.js +90 -20
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/extension.d.ts +3 -2
- package/dist/commands/extension.js +53 -28
- package/dist/commands/extension.js.map +1 -1
- package/dist/commands/multi-agent.d.ts +7 -0
- package/dist/commands/multi-agent.js +109 -0
- package/dist/commands/multi-agent.js.map +1 -0
- package/dist/commands/tools.d.ts +11 -0
- package/dist/commands/tools.js +168 -0
- package/dist/commands/tools.js.map +1 -0
- package/dist/commands/updates.d.ts +20 -0
- package/dist/commands/updates.js +100 -0
- package/dist/commands/updates.js.map +1 -0
- package/dist/commands/welcome.d.ts +2 -1
- package/dist/commands/welcome.js +11 -47
- package/dist/commands/welcome.js.map +1 -1
- package/dist/config.d.ts +25 -3
- package/dist/config.js +35 -2
- package/dist/config.js.map +1 -1
- package/dist/installers/copilot.d.ts +2 -0
- package/dist/installers/copilot.js +72 -0
- package/dist/installers/copilot.js.map +1 -0
- package/dist/installers/index.js +2 -1
- package/dist/installers/index.js.map +1 -1
- package/dist/installers/types.d.ts +1 -1
- package/dist/messages.d.ts +7 -0
- package/dist/permissions.d.ts +37 -0
- package/dist/permissions.js +156 -0
- package/dist/permissions.js.map +1 -0
- package/dist/server.d.ts +7 -3
- package/dist/server.js +160 -32
- package/dist/server.js.map +1 -1
- package/dist/tools/browser-definitions.js +18 -0
- package/dist/tools/browser-definitions.js.map +1 -1
- package/dist/tools/browser-dispatch.d.ts +7 -0
- package/dist/tools/browser-dispatch.js +11 -0
- package/dist/tools/browser-dispatch.js.map +1 -1
- package/dist/tools/server-instructions.d.ts +1 -1
- package/dist/tools/server-instructions.js +4 -0
- package/dist/tools/server-instructions.js.map +1 -1
- package/dist/ui/app.d.ts +7 -0
- package/dist/ui/app.js +77 -0
- package/dist/ui/app.js.map +1 -0
- package/dist/ui/components.d.ts +18 -0
- package/dist/ui/components.js +27 -0
- package/dist/ui/components.js.map +1 -0
- package/dist/ui/screens.d.ts +61 -0
- package/dist/ui/screens.js +603 -0
- package/dist/ui/screens.js.map +1 -0
- package/dist/ui/start.d.ts +6 -0
- package/dist/ui/start.js +19 -0
- package/dist/ui/start.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +15 -0
- package/dist/version.js.map +1 -0
- package/package.json +10 -4
- package/dist/commands/menu.d.ts +0 -25
- package/dist/commands/menu.js +0 -167
- package/dist/commands/menu.js.map +0 -1
package/README.md
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
> ⚠️ **Read this before installing**
|
|
4
4
|
>
|
|
5
5
|
> This package opens a bridge between your MCP client (Claude Code,
|
|
6
|
-
> OpenCode, …) and the Chrome tabs you explicitly
|
|
7
|
-
> companion extension. On every tab where you press
|
|
8
|
-
> extension popup, the agent can read its DOM, click,
|
|
9
|
-
> arbitrary JavaScript, and follow links — including any
|
|
10
|
-
> session, saved card, wallet, banking page or admin panel
|
|
11
|
-
> currently showing.
|
|
6
|
+
> OpenCode, GitHub Copilot CLI…) and the Chrome tabs you explicitly
|
|
7
|
+
> enable through a companion extension. On every tab where you press
|
|
8
|
+
> "Conectar" in the extension popup, the agent can read its DOM, click,
|
|
9
|
+
> type, run arbitrary JavaScript, and follow links — including any
|
|
10
|
+
> logged-in session, saved card, wallet, banking page or admin panel
|
|
11
|
+
> that tab is currently showing.
|
|
12
12
|
>
|
|
13
13
|
> Treat the agent like a junior dev with remote control of those tabs.
|
|
14
14
|
> Only enable tabs where you would let an automated process act on your
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
> every action the agent performs on the tabs you explicitly enable.
|
|
17
17
|
|
|
18
18
|
MCP server that bridges any MCP-compatible client (Claude Code, OpenCode,
|
|
19
|
-
and friends) to the Chrome tabs you grant access to,
|
|
20
|
-
WebSocket relay and a companion Chrome extension. Ships
|
|
21
|
-
UI map so the agent remembers selectors, flows and
|
|
22
|
-
about each app, across sessions.
|
|
19
|
+
GitHub Copilot CLI, and friends) to the Chrome tabs you grant access to,
|
|
20
|
+
through a small WebSocket relay and a companion Chrome extension. Ships
|
|
21
|
+
with a persistent UI map so the agent remembers selectors, flows and
|
|
22
|
+
gotchas it learned about each app, across sessions.
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
@@ -31,18 +31,16 @@ This puts the `browser-link` binary on your PATH on macOS, Linux and Windows.
|
|
|
31
31
|
|
|
32
32
|
## Set it up
|
|
33
33
|
|
|
34
|
-
The fastest path is the interactive
|
|
35
|
-
flicker-free in PowerShell, Windows Terminal, macOS Terminal, iTerm, every
|
|
36
|
-
Linux TTY):
|
|
34
|
+
The fastest path is the interactive UI — a full-screen [Ink](https://github.com/vadimdemedes/ink)-based app with a pinned header, live status of every MCP client, and sub-screens that swap in place (no flicker, no scroll-off):
|
|
37
35
|
|
|
38
36
|
```bash
|
|
39
37
|
browser-link
|
|
40
38
|
```
|
|
41
39
|
|
|
42
40
|
That opens the welcome / disclaimer screen (English or Spanish), and then
|
|
43
|
-
the setup menu where you can register browser-link with **Claude Code
|
|
44
|
-
**OpenCode**, see the Chrome extension install
|
|
45
|
-
diagnose, and open the about / help page.
|
|
41
|
+
the setup menu where you can register `browser-link` with **Claude Code**,
|
|
42
|
+
**OpenCode**, or **GitHub Copilot CLI**, see the Chrome extension install
|
|
43
|
+
steps, run a doctor diagnose, and open the about / help page.
|
|
46
44
|
|
|
47
45
|
If you prefer direct commands:
|
|
48
46
|
|
|
@@ -50,12 +48,50 @@ If you prefer direct commands:
|
|
|
50
48
|
browser-link install # register in every detected client
|
|
51
49
|
browser-link install --client claude # register only in Claude Code
|
|
52
50
|
browser-link install --client opencode # register only in OpenCode
|
|
51
|
+
browser-link install --client copilot # register only in GitHub Copilot CLI
|
|
53
52
|
browser-link uninstall --client opencode # remove from one client
|
|
54
53
|
browser-link extension # show the Chrome extension assets path + steps
|
|
55
54
|
browser-link doctor # diagnose current setup
|
|
55
|
+
browser-link tools # show which MCP tools are enabled / disabled
|
|
56
|
+
browser-link tools disable browser.evaluate
|
|
57
|
+
browser-link tools preset readonly # all | readonly | no-eval | no-map
|
|
58
|
+
browser-link updates # check the npm registry for a newer version
|
|
56
59
|
browser-link about # what this is, how it works, every tool
|
|
57
60
|
```
|
|
58
61
|
|
|
62
|
+
## Per-tool permissions
|
|
63
|
+
|
|
64
|
+
`browser-link` exposes 17 MCP tools by default — 10 browser-bridge tools,
|
|
65
|
+
6 UI-map tools, and `browser.events` for bridge traceability. You can
|
|
66
|
+
disable any subset per machine, either through the **Permissions** screen
|
|
67
|
+
in the interactive menu (toggle with Space, apply a preset with Enter,
|
|
68
|
+
save with `s`) or through the scriptable `browser-link tools` subcommand.
|
|
69
|
+
Available presets: `all` (default), `readonly`, `no-eval`, `no-map`.
|
|
70
|
+
Changes take effect the next time your MCP client starts the server.
|
|
71
|
+
|
|
72
|
+
## Multi-agent mode
|
|
73
|
+
|
|
74
|
+
By default only one MCP client can have browser-link active at a time
|
|
75
|
+
(EADDRINUSE on the second). Enable multi-agent mode and the second
|
|
76
|
+
`browser-link` spawn becomes a proxy that forwards MCP requests to the
|
|
77
|
+
first via `127.0.0.1:17530`, with the same kernel-level process binding
|
|
78
|
+
the WS port already uses. All connected clients share the same Chrome
|
|
79
|
+
tabs and persistent UI map.
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
browser-link multi-agent enable
|
|
83
|
+
browser-link multi-agent auto-reelect enable # optional
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
With `auto-reelect` on, secondary proxies survive the primary closing:
|
|
87
|
+
they enter a 5-second reconnect window, return `-32001 "temporarily
|
|
88
|
+
unavailable"` for in-flight requests, and hot-swap to the fresh primary
|
|
89
|
+
once it appears. The agent self-recovers from stale tab ids via the new
|
|
90
|
+
`browser.events` tool, which surfaces a ring buffer of bridge lifecycle
|
|
91
|
+
events (`primary-elected`, `tab-registered`, `tab-disconnected`,
|
|
92
|
+
`tab-renamed`). The Chrome extension preserves the per-tab id across
|
|
93
|
+
primary swaps via `chrome.storage.session`.
|
|
94
|
+
|
|
59
95
|
After `install`, restart the MCP client so it picks up the new entry.
|
|
60
96
|
After `extension`, follow the printed steps to load the unpacked extension
|
|
61
97
|
in Chrome. Then click "Conectar" on every tab you want the agent to reach
|
|
@@ -63,12 +99,13 @@ in Chrome. Then click "Conectar" on every tab you want the agent to reach
|
|
|
63
99
|
|
|
64
100
|
## Supported MCP clients
|
|
65
101
|
|
|
66
|
-
| Client
|
|
67
|
-
|
|
|
68
|
-
| [Claude Code](https://docs.claude.com/claude-code)
|
|
69
|
-
| [OpenCode](https://opencode.ai)
|
|
102
|
+
| Client | Config file written |
|
|
103
|
+
| -------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
|
104
|
+
| [Claude Code](https://docs.claude.com/claude-code) | `~/.claude.json` (`%USERPROFILE%\.claude.json` on Windows) |
|
|
105
|
+
| [OpenCode](https://opencode.ai) | `~/.config/opencode/opencode.json` on **every** OS (Win incl.) |
|
|
106
|
+
| [GitHub Copilot CLI](https://docs.github.com/en/copilot/copilot-cli) | `~/.copilot/mcp-config.json` (override via `COPILOT_HOME` env) |
|
|
70
107
|
|
|
71
|
-
|
|
108
|
+
Every registration is idempotent — re-running `install` updates the
|
|
72
109
|
entry instead of duplicating it. `uninstall --client <id>` removes it
|
|
73
110
|
cleanly without touching anything else in the file.
|
|
74
111
|
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { type Interface } from 'node:readline';
|
|
2
|
+
/** Raised when the proxy cannot complete the IPC handshake. */
|
|
3
|
+
export declare class HandshakeError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
export interface ConnectOptions {
|
|
7
|
+
host?: string;
|
|
8
|
+
port?: number;
|
|
9
|
+
/** How long to wait for a hello-ack after sending the hello. */
|
|
10
|
+
handshakeTimeoutMs?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ConnectionInfo {
|
|
13
|
+
sessionId: string;
|
|
14
|
+
version: string;
|
|
15
|
+
}
|
|
16
|
+
/** Minimal IPC client. Owns the TCP socket, handles framing, runs the
|
|
17
|
+
* handshake, dispatches mcp.response frames back to whoever sent the
|
|
18
|
+
* matching mcp.request. */
|
|
19
|
+
export declare class IpcClient {
|
|
20
|
+
private socket;
|
|
21
|
+
private buffer;
|
|
22
|
+
private nextRequestId;
|
|
23
|
+
private pendingRequests;
|
|
24
|
+
private closeListeners;
|
|
25
|
+
private notificationListeners;
|
|
26
|
+
private closed;
|
|
27
|
+
/** Open the TCP connection, perform the handshake, and resolve with the
|
|
28
|
+
* session info from the primary's hello-ack. On any handshake failure,
|
|
29
|
+
* rejects with HandshakeError and tears down the socket. */
|
|
30
|
+
connect(token: string, opts?: ConnectOptions): Promise<ConnectionInfo>;
|
|
31
|
+
private handshakeReceiver;
|
|
32
|
+
/** Forward a JSON-RPC request as an mcp.request frame. Resolves with the
|
|
33
|
+
* primary's JSON-RPC response payload. Rejects if the socket closes. */
|
|
34
|
+
sendMcpRequest(jsonRpcPayload: unknown): Promise<unknown>;
|
|
35
|
+
/** Forward a JSON-RPC notification as an mcp.notification frame. Fire-
|
|
36
|
+
* and-forget — notifications have no response in JSON-RPC. */
|
|
37
|
+
sendMcpNotification(jsonRpcPayload: unknown): void;
|
|
38
|
+
/** Register a callback invoked when the IPC connection drops. The
|
|
39
|
+
* `reason` argument distinguishes a primary-closing broadcast from a
|
|
40
|
+
* plain remote close so callers can decide to re-elect vs exit. */
|
|
41
|
+
onClose(cb: (reason: 'remote' | 'local' | 'primary-closing') => void): void;
|
|
42
|
+
/** Register a callback for unsolicited notifications from the primary
|
|
43
|
+
* (e.g. tools/list_changed in the future). */
|
|
44
|
+
onNotification(cb: (payload: unknown) => void): void;
|
|
45
|
+
/** Close the IPC connection. */
|
|
46
|
+
disconnect(): Promise<void>;
|
|
47
|
+
private ingest;
|
|
48
|
+
private handleFrame;
|
|
49
|
+
private onSocketClose;
|
|
50
|
+
private notifyClose;
|
|
51
|
+
}
|
|
52
|
+
export interface ProxyOptions {
|
|
53
|
+
input?: NodeJS.ReadableStream;
|
|
54
|
+
output?: NodeJS.WritableStream;
|
|
55
|
+
/** Called when the IPC connection drops and (if reelect was enabled)
|
|
56
|
+
* could not be re-established. After this fires the proxy is dead;
|
|
57
|
+
* callers usually want to exit the process. */
|
|
58
|
+
onClose?: (reason: 'remote' | 'local' | 'primary-closing') => void;
|
|
59
|
+
/** Override the token used in the hello. Tests pass this directly so
|
|
60
|
+
* they don't need to write a token file. Production reads it from disk. */
|
|
61
|
+
token?: string;
|
|
62
|
+
/** Override IPC endpoint. */
|
|
63
|
+
host?: string;
|
|
64
|
+
port?: number;
|
|
65
|
+
/** When true, instead of giving up on the first IPC drop, the proxy
|
|
66
|
+
* tries to reconnect to a fresh primary at the same IPC port for up to
|
|
67
|
+
* reelectTimeoutMs. During that window, any incoming JSON-RPC request
|
|
68
|
+
* from stdin gets an immediate "bridge unavailable" error response so
|
|
69
|
+
* the MCP client doesn't hang. Default: false. */
|
|
70
|
+
autoReelect?: boolean;
|
|
71
|
+
/** Total budget (ms) to wait for a new primary before giving up.
|
|
72
|
+
* Default: 5000. */
|
|
73
|
+
reelectTimeoutMs?: number;
|
|
74
|
+
/** Per-attempt interval (ms) between reconnect tries. Default: 200. */
|
|
75
|
+
reelectIntervalMs?: number;
|
|
76
|
+
/** Hook for tests: called whenever the proxy starts a reconnect
|
|
77
|
+
* attempt cycle. Production callers ignore. */
|
|
78
|
+
onReelectStart?: () => void;
|
|
79
|
+
/** Hook for tests: called when a reconnect cycle succeeds. */
|
|
80
|
+
onReelectSuccess?: () => void;
|
|
81
|
+
/** Hook for tests: called when a reconnect cycle exhausts the budget. */
|
|
82
|
+
onReelectExhausted?: () => void;
|
|
83
|
+
}
|
|
84
|
+
export interface ProxyHandle {
|
|
85
|
+
client: IpcClient;
|
|
86
|
+
stop(): Promise<void>;
|
|
87
|
+
/** Resolves once the IPC connection drops (any reason). */
|
|
88
|
+
closed: Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
/** Read the token from disk and connect to the running primary, then plug
|
|
91
|
+
* stdin → IPC mcp.request and IPC mcp.response → stdout.
|
|
92
|
+
*
|
|
93
|
+
* When `autoReelect: true` is passed, the proxy survives IPC drops by
|
|
94
|
+
* waiting for a fresh primary to appear at the same port. During the
|
|
95
|
+
* wait, incoming JSON-RPC requests get an immediate error response so
|
|
96
|
+
* the MCP client never hangs on a missing reply. */
|
|
97
|
+
export declare function runProxy(opts?: ProxyOptions): Promise<ProxyHandle>;
|
|
98
|
+
/** Convenience: open a readline-style line iterator over a stream. Currently
|
|
99
|
+
* unused (we use a hand-rolled line buffer for symmetry with the server)
|
|
100
|
+
* but exported for future tooling. */
|
|
101
|
+
export declare function readlines(stream: NodeJS.ReadableStream): Interface;
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { connect } from 'node:net';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { IPC_HOST, IPC_PORT, IPC_PROTOCOL_VERSION, encodeFrame, parseFrame, } from './protocol.js';
|
|
4
|
+
import { readToken } from './token.js';
|
|
5
|
+
/**
|
|
6
|
+
* Proxy's side of the IPC bridge. The proxy is a thin browser-link process
|
|
7
|
+
* spawned by an MCP client (Claude / Copilot / OpenCode) that finds the WS
|
|
8
|
+
* port already taken — it forwards every MCP frame it gets on stdin to the
|
|
9
|
+
* primary via the IPC socket, and pipes responses back to stdout.
|
|
10
|
+
*
|
|
11
|
+
* Two flavours:
|
|
12
|
+
* - IpcClient is the bare connection (used by tests and re-election).
|
|
13
|
+
* - runProxy() wires IpcClient to stdin/stdout so the MCP client thinks
|
|
14
|
+
* it is talking to a real server.
|
|
15
|
+
*/
|
|
16
|
+
function log(msg) {
|
|
17
|
+
console.error(`[browser-link proxy] ${msg}`);
|
|
18
|
+
}
|
|
19
|
+
/** Raised when the proxy cannot complete the IPC handshake. */
|
|
20
|
+
export class HandshakeError extends Error {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'HandshakeError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Minimal IPC client. Owns the TCP socket, handles framing, runs the
|
|
27
|
+
* handshake, dispatches mcp.response frames back to whoever sent the
|
|
28
|
+
* matching mcp.request. */
|
|
29
|
+
export class IpcClient {
|
|
30
|
+
socket = null;
|
|
31
|
+
buffer = '';
|
|
32
|
+
nextRequestId = 1;
|
|
33
|
+
pendingRequests = new Map();
|
|
34
|
+
closeListeners = [];
|
|
35
|
+
notificationListeners = [];
|
|
36
|
+
closed = false;
|
|
37
|
+
/** Open the TCP connection, perform the handshake, and resolve with the
|
|
38
|
+
* session info from the primary's hello-ack. On any handshake failure,
|
|
39
|
+
* rejects with HandshakeError and tears down the socket. */
|
|
40
|
+
async connect(token, opts = {}) {
|
|
41
|
+
const host = opts.host ?? IPC_HOST;
|
|
42
|
+
const port = opts.port ?? IPC_PORT;
|
|
43
|
+
const handshakeTimeoutMs = opts.handshakeTimeoutMs ?? 4000;
|
|
44
|
+
const socket = await new Promise((resolve, reject) => {
|
|
45
|
+
const s = connect({ host, port });
|
|
46
|
+
s.once('connect', () => resolve(s));
|
|
47
|
+
s.once('error', (err) => reject(err));
|
|
48
|
+
});
|
|
49
|
+
this.socket = socket;
|
|
50
|
+
socket.on('data', (chunk) => this.ingest(chunk));
|
|
51
|
+
socket.on('close', () => this.onSocketClose('remote'));
|
|
52
|
+
socket.on('error', (err) => log(`Socket error: ${err.message}`));
|
|
53
|
+
// Send hello, wait for ack or reject or timeout.
|
|
54
|
+
const ack = await new Promise((resolve, reject) => {
|
|
55
|
+
const timer = setTimeout(() => {
|
|
56
|
+
reject(new HandshakeError('Handshake timed out — primary did not reply.'));
|
|
57
|
+
}, handshakeTimeoutMs);
|
|
58
|
+
const onFirstFrame = (frame) => {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
if (frame.kind === 'hello-ack') {
|
|
61
|
+
resolve({ sessionId: frame.sessionId, version: frame.version });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (frame.kind === 'hello-reject') {
|
|
65
|
+
reject(new HandshakeError(`Primary rejected: ${frame.reason}`));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
reject(new HandshakeError(`Unexpected first frame: ${frame.kind}`));
|
|
69
|
+
};
|
|
70
|
+
this.handshakeReceiver = onFirstFrame;
|
|
71
|
+
try {
|
|
72
|
+
socket.write(encodeFrame({ kind: 'hello', version: IPC_PROTOCOL_VERSION, token }));
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
77
|
+
}
|
|
78
|
+
}).catch((err) => {
|
|
79
|
+
this.disconnect().catch(() => {
|
|
80
|
+
/* ignore */
|
|
81
|
+
});
|
|
82
|
+
throw err;
|
|
83
|
+
});
|
|
84
|
+
this.handshakeReceiver = null;
|
|
85
|
+
log(`Handshake ok — session=${ack.sessionId}`);
|
|
86
|
+
return ack;
|
|
87
|
+
}
|
|
88
|
+
handshakeReceiver = null;
|
|
89
|
+
/** Forward a JSON-RPC request as an mcp.request frame. Resolves with the
|
|
90
|
+
* primary's JSON-RPC response payload. Rejects if the socket closes. */
|
|
91
|
+
sendMcpRequest(jsonRpcPayload) {
|
|
92
|
+
if (!this.socket || this.closed) {
|
|
93
|
+
return Promise.reject(new Error('Proxy is not connected.'));
|
|
94
|
+
}
|
|
95
|
+
const requestId = this.nextRequestId++;
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
this.pendingRequests.set(requestId, resolve);
|
|
98
|
+
const onCloseHandler = () => {
|
|
99
|
+
if (this.pendingRequests.delete(requestId)) {
|
|
100
|
+
reject(new Error('Primary connection closed while a request was in flight.'));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
this.onClose(onCloseHandler);
|
|
104
|
+
try {
|
|
105
|
+
this.socket.write(encodeFrame({ kind: 'mcp.request', requestId, payload: jsonRpcPayload }));
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
this.pendingRequests.delete(requestId);
|
|
109
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** Forward a JSON-RPC notification as an mcp.notification frame. Fire-
|
|
114
|
+
* and-forget — notifications have no response in JSON-RPC. */
|
|
115
|
+
sendMcpNotification(jsonRpcPayload) {
|
|
116
|
+
if (!this.socket || this.closed)
|
|
117
|
+
return;
|
|
118
|
+
try {
|
|
119
|
+
this.socket.write(encodeFrame({ kind: 'mcp.notification', payload: jsonRpcPayload }));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
/* socket gone; close handler will fire */
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Register a callback invoked when the IPC connection drops. The
|
|
126
|
+
* `reason` argument distinguishes a primary-closing broadcast from a
|
|
127
|
+
* plain remote close so callers can decide to re-elect vs exit. */
|
|
128
|
+
onClose(cb) {
|
|
129
|
+
this.closeListeners.push(cb);
|
|
130
|
+
}
|
|
131
|
+
/** Register a callback for unsolicited notifications from the primary
|
|
132
|
+
* (e.g. tools/list_changed in the future). */
|
|
133
|
+
onNotification(cb) {
|
|
134
|
+
this.notificationListeners.push(cb);
|
|
135
|
+
}
|
|
136
|
+
/** Close the IPC connection. */
|
|
137
|
+
async disconnect() {
|
|
138
|
+
if (this.closed)
|
|
139
|
+
return;
|
|
140
|
+
this.closed = true;
|
|
141
|
+
if (this.socket) {
|
|
142
|
+
const s = this.socket;
|
|
143
|
+
this.socket = null;
|
|
144
|
+
try {
|
|
145
|
+
s.end();
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
/* ignore */
|
|
149
|
+
}
|
|
150
|
+
s.destroy();
|
|
151
|
+
// Fire close listeners with reason=local (we initiated it).
|
|
152
|
+
this.notifyClose('local');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
ingest(chunk) {
|
|
156
|
+
this.buffer += chunk.toString('utf8');
|
|
157
|
+
let nl;
|
|
158
|
+
while ((nl = this.buffer.indexOf('\n')) >= 0) {
|
|
159
|
+
const line = this.buffer.slice(0, nl);
|
|
160
|
+
this.buffer = this.buffer.slice(nl + 1);
|
|
161
|
+
if (line.length === 0)
|
|
162
|
+
continue;
|
|
163
|
+
const frame = parseFrame(line);
|
|
164
|
+
if (!frame) {
|
|
165
|
+
log('Invalid frame from primary; dropping.');
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
this.handleFrame(frame);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
handleFrame(frame) {
|
|
172
|
+
if (this.handshakeReceiver) {
|
|
173
|
+
this.handshakeReceiver(frame);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
switch (frame.kind) {
|
|
177
|
+
case 'mcp.response': {
|
|
178
|
+
const resolve = this.pendingRequests.get(frame.requestId);
|
|
179
|
+
if (resolve) {
|
|
180
|
+
this.pendingRequests.delete(frame.requestId);
|
|
181
|
+
resolve(frame.payload);
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
case 'mcp.notification': {
|
|
186
|
+
for (const cb of this.notificationListeners)
|
|
187
|
+
cb(frame.payload);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
case 'ping': {
|
|
191
|
+
try {
|
|
192
|
+
this.socket?.write(encodeFrame({ kind: 'pong' }));
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
/* socket gone */
|
|
196
|
+
}
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
case 'pong': {
|
|
200
|
+
// Future: we can send our own pings if needed. Today the primary
|
|
201
|
+
// initiates the heartbeat; pong from us → primary.
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
case 'primary-closing': {
|
|
205
|
+
log(`Primary signalled close (${frame.reason ?? 'no reason'}).`);
|
|
206
|
+
this.notifyClose('primary-closing');
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
default:
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
onSocketClose(reason) {
|
|
214
|
+
if (this.closed)
|
|
215
|
+
return;
|
|
216
|
+
this.closed = true;
|
|
217
|
+
log('Socket closed by primary.');
|
|
218
|
+
this.notifyClose(reason);
|
|
219
|
+
}
|
|
220
|
+
notifyClose(reason) {
|
|
221
|
+
// Reject every in-flight request first so callers don't hang.
|
|
222
|
+
for (const resolve of this.pendingRequests.values()) {
|
|
223
|
+
// We resolve with a JSON-RPC error envelope so the MCP client gets
|
|
224
|
+
// a tool error instead of a stuck promise.
|
|
225
|
+
resolve({
|
|
226
|
+
jsonrpc: '2.0',
|
|
227
|
+
id: null,
|
|
228
|
+
error: { code: -32000, message: 'browser-link primary disconnected.' },
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
this.pendingRequests.clear();
|
|
232
|
+
for (const cb of this.closeListeners.splice(0)) {
|
|
233
|
+
try {
|
|
234
|
+
cb(reason);
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
log(`Close listener threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** Read the token from disk and connect to the running primary, then plug
|
|
243
|
+
* stdin → IPC mcp.request and IPC mcp.response → stdout.
|
|
244
|
+
*
|
|
245
|
+
* When `autoReelect: true` is passed, the proxy survives IPC drops by
|
|
246
|
+
* waiting for a fresh primary to appear at the same port. During the
|
|
247
|
+
* wait, incoming JSON-RPC requests get an immediate error response so
|
|
248
|
+
* the MCP client never hangs on a missing reply. */
|
|
249
|
+
export async function runProxy(opts = {}) {
|
|
250
|
+
const input = opts.input ?? process.stdin;
|
|
251
|
+
const output = opts.output ?? process.stdout;
|
|
252
|
+
const autoReelect = opts.autoReelect === true;
|
|
253
|
+
const reelectTimeoutMs = opts.reelectTimeoutMs ?? 5000;
|
|
254
|
+
const reelectIntervalMs = opts.reelectIntervalMs ?? 200;
|
|
255
|
+
const initialToken = opts.token ?? readToken();
|
|
256
|
+
if (!initialToken) {
|
|
257
|
+
throw new Error('Multi-agent token not found. The primary browser-link instance is not running with multi-agent enabled.');
|
|
258
|
+
}
|
|
259
|
+
let client = new IpcClient();
|
|
260
|
+
await client.connect(initialToken, { host: opts.host, port: opts.port });
|
|
261
|
+
/* When the IPC drops AND autoReelect is on, we enter "reconnecting"
|
|
262
|
+
* mode. While reconnecting, the data pipe stays attached but requests
|
|
263
|
+
* are answered immediately with an error envelope so the MCP client
|
|
264
|
+
* does not stall on a missing reply. */
|
|
265
|
+
let reconnecting = false;
|
|
266
|
+
let stopped = false;
|
|
267
|
+
let closedResolve = () => { };
|
|
268
|
+
const closed = new Promise((resolve) => {
|
|
269
|
+
closedResolve = resolve;
|
|
270
|
+
});
|
|
271
|
+
const fail = (reason) => {
|
|
272
|
+
if (stopped)
|
|
273
|
+
return;
|
|
274
|
+
stopped = true;
|
|
275
|
+
input.off?.('data', onData);
|
|
276
|
+
if (opts.onClose) {
|
|
277
|
+
try {
|
|
278
|
+
opts.onClose(reason);
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
log(`onClose handler threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
closedResolve();
|
|
285
|
+
};
|
|
286
|
+
// Set up the input pipe. We can't use readline.createInterface on a raw
|
|
287
|
+
// stream that has already been data-event-attached, but we can use a
|
|
288
|
+
// simple line buffer like the server side.
|
|
289
|
+
let lineBuffer = '';
|
|
290
|
+
const onData = (chunk) => {
|
|
291
|
+
const text = typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
292
|
+
lineBuffer += text;
|
|
293
|
+
let nl;
|
|
294
|
+
while ((nl = lineBuffer.indexOf('\n')) >= 0) {
|
|
295
|
+
const line = lineBuffer.slice(0, nl).replace(/\r$/, '');
|
|
296
|
+
lineBuffer = lineBuffer.slice(nl + 1);
|
|
297
|
+
if (line.length === 0)
|
|
298
|
+
continue;
|
|
299
|
+
handleLine(line);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
const handleLine = (line) => {
|
|
303
|
+
let msg;
|
|
304
|
+
try {
|
|
305
|
+
msg = JSON.parse(line);
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
output.write(JSON.stringify({
|
|
309
|
+
jsonrpc: '2.0',
|
|
310
|
+
id: null,
|
|
311
|
+
error: { code: -32700, message: 'Parse error in proxy input.' },
|
|
312
|
+
}) + '\n');
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (!msg || typeof msg !== 'object')
|
|
316
|
+
return;
|
|
317
|
+
if (msg.id === undefined || msg.id === null) {
|
|
318
|
+
// Notification — fire and forget when connected; drop during reconnect.
|
|
319
|
+
if (!reconnecting)
|
|
320
|
+
client.sendMcpNotification(msg);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (reconnecting) {
|
|
324
|
+
// Fail fast so the MCP client can decide to retry.
|
|
325
|
+
output.write(JSON.stringify({
|
|
326
|
+
jsonrpc: '2.0',
|
|
327
|
+
id: msg.id,
|
|
328
|
+
error: {
|
|
329
|
+
code: -32001,
|
|
330
|
+
message: 'browser-link bridge temporarily unavailable (primary just closed; reconnecting).',
|
|
331
|
+
},
|
|
332
|
+
}) + '\n');
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
client
|
|
336
|
+
.sendMcpRequest(msg)
|
|
337
|
+
.then((responsePayload) => {
|
|
338
|
+
output.write(JSON.stringify(responsePayload) + '\n');
|
|
339
|
+
})
|
|
340
|
+
.catch((err) => {
|
|
341
|
+
output.write(JSON.stringify({
|
|
342
|
+
jsonrpc: '2.0',
|
|
343
|
+
id: msg.id,
|
|
344
|
+
error: { code: -32000, message: err instanceof Error ? err.message : String(err) },
|
|
345
|
+
}) + '\n');
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
input.on('data', onData);
|
|
349
|
+
const wireCloseHandler = (c) => {
|
|
350
|
+
c.onClose(async (reason) => {
|
|
351
|
+
if (stopped)
|
|
352
|
+
return;
|
|
353
|
+
if (!autoReelect) {
|
|
354
|
+
fail(reason);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
// Enter reconnect mode and try to find a new primary.
|
|
358
|
+
reconnecting = true;
|
|
359
|
+
opts.onReelectStart?.();
|
|
360
|
+
log(`Primary closed (${reason}); reelect window opened for ${reelectTimeoutMs}ms.`);
|
|
361
|
+
const newClient = await reconnectLoop(opts.host, opts.port, reelectTimeoutMs, reelectIntervalMs);
|
|
362
|
+
if (stopped) {
|
|
363
|
+
if (newClient)
|
|
364
|
+
await newClient.disconnect();
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (!newClient) {
|
|
368
|
+
log('Reelect window exhausted; closing proxy.');
|
|
369
|
+
opts.onReelectExhausted?.();
|
|
370
|
+
reconnecting = false;
|
|
371
|
+
fail(reason);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
// Hot-swap clients. The old one is already torn down by its close
|
|
375
|
+
// handler chain; we just install the new one and wire it up.
|
|
376
|
+
log('Reconnected to new primary.');
|
|
377
|
+
opts.onReelectSuccess?.();
|
|
378
|
+
client = newClient;
|
|
379
|
+
reconnecting = false;
|
|
380
|
+
wireCloseHandler(client);
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
wireCloseHandler(client);
|
|
384
|
+
const stop = async () => {
|
|
385
|
+
stopped = true;
|
|
386
|
+
input.off?.('data', onData);
|
|
387
|
+
await client.disconnect();
|
|
388
|
+
closedResolve();
|
|
389
|
+
};
|
|
390
|
+
return {
|
|
391
|
+
get client() {
|
|
392
|
+
return client;
|
|
393
|
+
},
|
|
394
|
+
stop,
|
|
395
|
+
closed,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
/** Repeatedly try to connect to the IPC port until success or budget
|
|
399
|
+
* expires. Re-reads the token on every attempt because a new primary
|
|
400
|
+
* rotates it on startup. Returns the connected client, or null on
|
|
401
|
+
* timeout. */
|
|
402
|
+
async function reconnectLoop(host, port, totalBudgetMs, intervalMs) {
|
|
403
|
+
const deadline = Date.now() + totalBudgetMs;
|
|
404
|
+
// Generous handshake budget — Windows under load can take >200ms for a
|
|
405
|
+
// local socket round-trip and a too-tight ceiling makes reconnects flap.
|
|
406
|
+
const handshakeTimeoutMs = Math.min(1500, Math.max(800, intervalMs * 4));
|
|
407
|
+
while (Date.now() < deadline) {
|
|
408
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
409
|
+
const token = readToken();
|
|
410
|
+
if (!token)
|
|
411
|
+
continue;
|
|
412
|
+
const c = new IpcClient();
|
|
413
|
+
try {
|
|
414
|
+
await c.connect(token, { host, port, handshakeTimeoutMs });
|
|
415
|
+
return c;
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
try {
|
|
419
|
+
await c.disconnect();
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
/* ignore */
|
|
423
|
+
}
|
|
424
|
+
// Retry until deadline.
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
/** Convenience: open a readline-style line iterator over a stream. Currently
|
|
430
|
+
* unused (we use a hand-rolled line buffer for symmetry with the server)
|
|
431
|
+
* but exported for future tooling. */
|
|
432
|
+
export function readlines(stream) {
|
|
433
|
+
return createInterface({ input: stream, crlfDelay: Infinity });
|
|
434
|
+
}
|
|
435
|
+
//# sourceMappingURL=client.js.map
|