@webbrain/mcp-server 0.1.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 +158 -0
- package/dist/bridge.d.ts +71 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +243 -0
- package/dist/bridge.js.map +1 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +62 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +258 -0
- package/dist/index.js.map +1 -0
- package/dist/runs.d.ts +48 -0
- package/dist/runs.d.ts.map +1 -0
- package/dist/runs.js +122 -0
- package/dist/runs.js.map +1 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# WebBrain MCP Server
|
|
2
|
+
|
|
3
|
+
Give any MCP client — Claude Code, Codex, Cursor, OpenClaw — the ability to run tasks in **your real browser session**: already signed in, cookies present, MFA already passed.
|
|
4
|
+
|
|
5
|
+
That session is the whole point. A headless automation framework starts logged out of everything and hits a login wall on the first useful page. WebBrain is already inside the browser you use.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
Claude Code ──stdio──▶ webbrain-mcp ──ws://127.0.0.1:17374──▶ WebBrain extension ──▶ your tabs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install -g @webbrain/mcp-server
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or run from a checkout:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
cd mcp-server && npm install && npm run build
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Connect the browser
|
|
24
|
+
|
|
25
|
+
> **Chromium only.** Chrome, Edge, Brave, Opera, Vivaldi. The bridge runs from the extension's **offscreen document**, and the Firefox build has none — `cloud-bridge.js` and `cloud-runs.js` live only under `src/chrome/`. See [`src/firefox/ARCHITECTURE.md`](../src/firefox/ARCHITECTURE.md).
|
|
26
|
+
|
|
27
|
+
The MCP server hosts the listener; the extension dials out to it. A Manifest V3 extension cannot listen on a socket, so the direction is fixed.
|
|
28
|
+
|
|
29
|
+
1. Install the [WebBrain extension](https://webbrain.one) and open your browser.
|
|
30
|
+
2. In **WebBrain → Settings → General → Advanced → Cloud bridge**, set the URL to `ws://127.0.0.1:17374/extension` and enable it.
|
|
31
|
+
3. Ask your MCP client to call `webbrain_connection` to confirm.
|
|
32
|
+
|
|
33
|
+
> **One bridge at a time.** The extension holds exactly one outbound bridge socket. Pointing it here means it is *not* pointed at WebBrain Cloud (`17373`) or the LM Studio plugin (`17375`). Switch it under **Settings → General → Advanced → Cloud bridge**.
|
|
34
|
+
|
|
35
|
+
## Register with a client
|
|
36
|
+
|
|
37
|
+
**Claude Code**
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
claude mcp add webbrain -- npx -y @webbrain/mcp-server
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Codex / Cursor / anything reading `mcp.json`**
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"mcpServers": {
|
|
48
|
+
"webbrain": {
|
|
49
|
+
"command": "npx",
|
|
50
|
+
"args": ["-y", "@webbrain/mcp-server"]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The MCP client launches this command as a child process when it starts the
|
|
57
|
+
configured server; you do not need to keep a second copy running in a terminal.
|
|
58
|
+
After adding the configuration, restart or reconnect the MCP client if WebBrain
|
|
59
|
+
does not appear in its tool list.
|
|
60
|
+
|
|
61
|
+
## Launch manually
|
|
62
|
+
|
|
63
|
+
For a direct launch or connection test, run:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npx -y @webbrain/mcp-server
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Leave that terminal open while using the bridge. The server owns the local
|
|
70
|
+
listener on `127.0.0.1:17374`, and exiting it closes the bridge. Press `Ctrl+C`
|
|
71
|
+
to stop it.
|
|
72
|
+
|
|
73
|
+
From a source checkout, build and launch the same server with:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cd mcp-server
|
|
77
|
+
npm install
|
|
78
|
+
npm run build
|
|
79
|
+
npm start
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
After it is running, enable `ws://127.0.0.1:17374/extension` under **WebBrain →
|
|
83
|
+
Settings → General → Advanced → Cloud bridge**. Ask the MCP client to call
|
|
84
|
+
`webbrain_connection` to verify the complete connection.
|
|
85
|
+
|
|
86
|
+
## Troubleshooting
|
|
87
|
+
|
|
88
|
+
**Connection error: WebSocket error** normally means that no process is
|
|
89
|
+
listening at the URL selected in WebBrain. Confirm the MCP server is still
|
|
90
|
+
running and that Settings uses port `17374`, then check the listener:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
lsof -nP -iTCP:17374 -sTCP:LISTEN
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
No output means the MCP server is not listening. If it is listening but the
|
|
97
|
+
extension does not connect, make sure another bridge destination is not selected:
|
|
98
|
+
WebBrain Cloud uses `17373`, this MCP server uses `17374`, and the LM Studio
|
|
99
|
+
plugin uses `17375`. Only one can be selected at a time. The bridge is available
|
|
100
|
+
in Chromium browsers only, not Firefox.
|
|
101
|
+
|
|
102
|
+
## Tools
|
|
103
|
+
|
|
104
|
+
| Tool | Purpose |
|
|
105
|
+
|---|---|
|
|
106
|
+
| `webbrain_run` | Delegate a task. `mode='ask'` is read-only; `mode='act'` can click and type, gated by in-browser approval. |
|
|
107
|
+
| `webbrain_status` | Poll a run, or list every run. |
|
|
108
|
+
| `webbrain_respond` | Answer a run sitting at `needs_user_input`. |
|
|
109
|
+
| `webbrain_abort` | Stop a run. Actions already taken are not undone. |
|
|
110
|
+
| `webbrain_connection` | Report whether the extension is attached, and how to fix it if not. |
|
|
111
|
+
|
|
112
|
+
### Example
|
|
113
|
+
|
|
114
|
+
> "Open my Stripe dashboard and list last week's failed payments."
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
webbrain_run(task: "open the Stripe dashboard and list last week's failed
|
|
118
|
+
payments with amounts and customer emails", mode: "ask")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Read-only, in the tab you are already authenticated in. No API key, no headless login dance.
|
|
122
|
+
|
|
123
|
+
## Why task-level and not 50 browser tools
|
|
124
|
+
|
|
125
|
+
WebBrain exposes roughly fifty primitives internally — `click_ax`, `type_ax`, `extract_data`, `iframe_read` and so on. This server deliberately does **not** surface them.
|
|
126
|
+
|
|
127
|
+
**Safety.** WebBrain's capability × origin permission gate runs in the agent loop (`_executeToolBatch`), not inside `executeTool()`. An MCP layer calling primitives directly would sit *below* the gate and bypass every approval prompt the product is built on. Delegating a goal keeps the trust boundary in the browser, where the human is.
|
|
128
|
+
|
|
129
|
+
**Cost.** Driving a UI one primitive at a time over a socket costs a round trip and a slab of tokens per click. Handing over a goal costs one call.
|
|
130
|
+
|
|
131
|
+
## Configuration
|
|
132
|
+
|
|
133
|
+
| Variable | Default | Meaning |
|
|
134
|
+
|---|---|---|
|
|
135
|
+
| `WEBBRAIN_BRIDGE_PORT` | `17374` | Port the extension connects to. |
|
|
136
|
+
| `WEBBRAIN_BRIDGE_PATH` | `/extension` | Path segment; must match the URL in Settings. |
|
|
137
|
+
| `WEBBRAIN_COMMAND_TIMEOUT_MS` | `30000` | Per-command reply timeout. |
|
|
138
|
+
| `WEBBRAIN_RUN_TIMEOUT_MS` | `300000` | Default ceiling for `webbrain_run` polling. |
|
|
139
|
+
| `WEBBRAIN_POLL_INTERVAL_MS` | `1000` | Status poll interval. |
|
|
140
|
+
|
|
141
|
+
## Security notes
|
|
142
|
+
|
|
143
|
+
- The listener binds `127.0.0.1` only. Anything that can reach this port can drive your signed-in browser — never expose it to a network or a container bridge.
|
|
144
|
+
- Connections from HTTP(S) pages and other non-extension browser origins are rejected before they can replace the extension socket. Accepted connections must also present the extension's `hello` frame with `client: "webbrain-extension"`; anything else is closed. This is **not** authentication. The shipping extension sends no shared secret, so a local process could impersonate it. Treat the port as trusted-local, and see [`docs/security-model.md`](../docs/security-model.md).
|
|
145
|
+
- A `webbrain_run` timeout does **not** abort the run. A task that already submitted a form should not be silently killed — the browser keeps going and `webbrain_status` picks it back up.
|
|
146
|
+
- `allow_api_mutations` lifts WebBrain's UI-first rule and is off by default. It is accepted only with `mode: "act"`; Ask runs remain read-only. The UI path is visible and stoppable; direct API mutations are neither.
|
|
147
|
+
|
|
148
|
+
## Tests
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
npm run build && node --test test/bridge.test.mjs
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The suite stands up the real listener and connects a fake extension speaking the exact frames `src/chrome/src/offscreen/cloud-bridge.js` emits — handshake, id correlation under concurrency, error propagation, disconnect mid-command, and the poll/timeout/clarify paths. If the extension's wire format changes, these fail. That is intentional.
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge server — the local endpoint the WebBrain extension connects OUT to.
|
|
3
|
+
*
|
|
4
|
+
* Direction matters: a Manifest V3 extension cannot listen on a socket, so
|
|
5
|
+
* `src/chrome/src/offscreen/cloud-bridge.js` dials out from an offscreen
|
|
6
|
+
* document and we host the listener. That also means every command below is
|
|
7
|
+
* initiated by us and answered by the extension.
|
|
8
|
+
*
|
|
9
|
+
* Wire protocol (already shipping in the extension, unchanged here):
|
|
10
|
+
* extension -> us {type:'hello', client, protocolVersion, capabilities, status}
|
|
11
|
+
* us -> extension {id, action, payload}
|
|
12
|
+
* extension -> us {id, ok:true, result}
|
|
13
|
+
* {id, ok:false, error, status?}
|
|
14
|
+
*
|
|
15
|
+
* The extension spreads `payload` over the message it forwards to its own
|
|
16
|
+
* background worker, so payload keys become top-level fields there. Send the
|
|
17
|
+
* exact field names `cloud-runs.js` reads.
|
|
18
|
+
*/
|
|
19
|
+
/** Actions already present in the extension's ALLOWED_BRIDGE_ACTIONS set. */
|
|
20
|
+
export type BridgeAction = "cloud_run" | "cloud_status" | "cloud_respond" | "cloud_abort";
|
|
21
|
+
export interface CloudSnapshot {
|
|
22
|
+
runId: string;
|
|
23
|
+
status: "running" | "needs_user_input" | "aborting" | "completed" | "failed" | "aborted";
|
|
24
|
+
mode?: "ask" | "act";
|
|
25
|
+
tabId?: number;
|
|
26
|
+
task?: string;
|
|
27
|
+
structured?: boolean;
|
|
28
|
+
pendingInput?: {
|
|
29
|
+
clarifyId?: string;
|
|
30
|
+
clarify_id?: string;
|
|
31
|
+
question?: string;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
} | null;
|
|
34
|
+
result?: unknown;
|
|
35
|
+
summary?: string;
|
|
36
|
+
content?: string;
|
|
37
|
+
finalUrl?: string;
|
|
38
|
+
error?: string;
|
|
39
|
+
createdAt?: string;
|
|
40
|
+
updatedAt?: string;
|
|
41
|
+
completedAt?: string | null;
|
|
42
|
+
updates?: unknown[];
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
export declare const TERMINAL_STATUSES: Set<string>;
|
|
46
|
+
export declare class BridgeError extends Error {
|
|
47
|
+
readonly status?: number;
|
|
48
|
+
readonly code?: "COMMAND_TIMEOUT" | "COMMAND_INTERRUPTED";
|
|
49
|
+
constructor(message: string, status?: number, code?: "COMMAND_TIMEOUT" | "COMMAND_INTERRUPTED");
|
|
50
|
+
}
|
|
51
|
+
export declare class WebBrainBridge {
|
|
52
|
+
private wss;
|
|
53
|
+
private socket;
|
|
54
|
+
private pending;
|
|
55
|
+
private nextId;
|
|
56
|
+
private extensionCapabilities;
|
|
57
|
+
private extensionProtocol;
|
|
58
|
+
private handshakenSocket;
|
|
59
|
+
private waiters;
|
|
60
|
+
start(): Promise<void>;
|
|
61
|
+
private handleMessage;
|
|
62
|
+
private failAllPending;
|
|
63
|
+
isConnected(): boolean;
|
|
64
|
+
capabilities(): string[];
|
|
65
|
+
/** Resolve once the extension has connected and completed its handshake. */
|
|
66
|
+
waitForExtension(timeoutMs: number): Promise<boolean>;
|
|
67
|
+
/** Send one command and await the extension's reply. */
|
|
68
|
+
request<T = unknown>(action: BridgeAction, payload?: Record<string, unknown>, timeoutMs?: number): Promise<T>;
|
|
69
|
+
stop(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAKH,6EAA6E;AAC7E,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,cAAc,GACd,eAAe,GACf,aAAa,CAAC;AAElB,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,SAAS,GAAG,kBAAkB,GAAG,UAAU,GAAG,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAC;IACzF,IAAI,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,YAAY,CAAC,EAAE;QACb,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,GAAG,IAAI,CAAC;IACT,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,C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A0B,GAClC,OAAO,CAAC,CAAC,CAAC;IA0CP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAgB5B"}
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge server — the local endpoint the WebBrain extension connects OUT to.
|
|
3
|
+
*
|
|
4
|
+
* Direction matters: a Manifest V3 extension cannot listen on a socket, so
|
|
5
|
+
* `src/chrome/src/offscreen/cloud-bridge.js` dials out from an offscreen
|
|
6
|
+
* document and we host the listener. That also means every command below is
|
|
7
|
+
* initiated by us and answered by the extension.
|
|
8
|
+
*
|
|
9
|
+
* Wire protocol (already shipping in the extension, unchanged here):
|
|
10
|
+
* extension -> us {type:'hello', client, protocolVersion, capabilities, status}
|
|
11
|
+
* us -> extension {id, action, payload}
|
|
12
|
+
* extension -> us {id, ok:true, result}
|
|
13
|
+
* {id, ok:false, error, status?}
|
|
14
|
+
*
|
|
15
|
+
* The extension spreads `payload` over the message it forwards to its own
|
|
16
|
+
* background worker, so payload keys become top-level fields there. Send the
|
|
17
|
+
* exact field names `cloud-runs.js` reads.
|
|
18
|
+
*/
|
|
19
|
+
import { WebSocketServer } from "ws";
|
|
20
|
+
import { config } from "./config.js";
|
|
21
|
+
export const TERMINAL_STATUSES = new Set(["completed", "failed", "aborted"]);
|
|
22
|
+
export class BridgeError extends Error {
|
|
23
|
+
status;
|
|
24
|
+
code;
|
|
25
|
+
constructor(message, status, code) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = "BridgeError";
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.code = code;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Log to stderr only — stdout is the MCP stdio transport and must stay clean. */
|
|
33
|
+
function log(...args) {
|
|
34
|
+
console.error("[webbrain-mcp]", ...args);
|
|
35
|
+
}
|
|
36
|
+
function isAllowedBridgeOrigin(origin) {
|
|
37
|
+
if (origin == null)
|
|
38
|
+
return true;
|
|
39
|
+
if (Array.isArray(origin))
|
|
40
|
+
return false;
|
|
41
|
+
try {
|
|
42
|
+
const protocol = new URL(origin).protocol;
|
|
43
|
+
return protocol === "chrome-extension:" || protocol === "moz-extension:";
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class WebBrainBridge {
|
|
50
|
+
wss = null;
|
|
51
|
+
socket = null;
|
|
52
|
+
pending = new Map();
|
|
53
|
+
nextId = 1;
|
|
54
|
+
extensionCapabilities = [];
|
|
55
|
+
extensionProtocol = null;
|
|
56
|
+
handshakenSocket = null;
|
|
57
|
+
waiters = [];
|
|
58
|
+
async start() {
|
|
59
|
+
if (this.wss)
|
|
60
|
+
return;
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
// Bind to loopback explicitly. Never expose this listener to the network:
|
|
63
|
+
// anything that can reach it can drive the user's logged-in browser.
|
|
64
|
+
const wss = new WebSocketServer({ host: "127.0.0.1", port: config.bridgePort }, () => resolve());
|
|
65
|
+
wss.on("error", reject);
|
|
66
|
+
this.wss = wss;
|
|
67
|
+
});
|
|
68
|
+
this.wss.on("connection", (socket, request) => {
|
|
69
|
+
const url = request.url || "";
|
|
70
|
+
const origin = request.headers.origin;
|
|
71
|
+
// Browser WebSocket clients always send Origin. Only extension pages may
|
|
72
|
+
// reach this trusted-local command channel; native clients send none.
|
|
73
|
+
if (!isAllowedBridgeOrigin(origin)) {
|
|
74
|
+
log(`rejected WebSocket origin: ${String(origin)}`);
|
|
75
|
+
socket.close(1008, "Untrusted Origin");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (!url.startsWith(config.bridgePath)) {
|
|
79
|
+
log(`rejected connection on unexpected path: ${url}`);
|
|
80
|
+
socket.close(1008, "Unexpected path");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// Latest connection wins. The extension reconnects with backoff after a
|
|
84
|
+
// browser restart or an offscreen-document teardown, and the stale socket
|
|
85
|
+
// is never reused.
|
|
86
|
+
if (this.socket) {
|
|
87
|
+
this.failAllPending(new BridgeError("WebBrain extension connection was superseded mid-command.", undefined, "COMMAND_INTERRUPTED"));
|
|
88
|
+
try {
|
|
89
|
+
this.socket.close(1000, "Superseded by a newer extension connection");
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
/* already gone */
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.socket = socket;
|
|
96
|
+
this.handshakenSocket = null;
|
|
97
|
+
this.extensionCapabilities = [];
|
|
98
|
+
this.extensionProtocol = null;
|
|
99
|
+
log(`extension connected on ${config.bridgePath}`);
|
|
100
|
+
socket.on("message", (raw) => this.handleMessage(socket, raw.toString()));
|
|
101
|
+
socket.on("close", () => {
|
|
102
|
+
if (this.socket !== socket)
|
|
103
|
+
return;
|
|
104
|
+
this.socket = null;
|
|
105
|
+
this.handshakenSocket = null;
|
|
106
|
+
this.extensionCapabilities = [];
|
|
107
|
+
this.extensionProtocol = null;
|
|
108
|
+
log("extension disconnected");
|
|
109
|
+
this.failAllPending(new BridgeError("WebBrain extension disconnected mid-command.", undefined, "COMMAND_INTERRUPTED"));
|
|
110
|
+
});
|
|
111
|
+
socket.on("error", (error) => {
|
|
112
|
+
log("socket error:", error instanceof Error ? error.message : String(error));
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
log(`listening on ws://127.0.0.1:${config.bridgePort}${config.bridgePath}`);
|
|
116
|
+
}
|
|
117
|
+
handleMessage(socket, data) {
|
|
118
|
+
if (this.socket !== socket)
|
|
119
|
+
return;
|
|
120
|
+
let msg;
|
|
121
|
+
try {
|
|
122
|
+
msg = JSON.parse(data);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
log("dropped non-JSON frame from extension");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
// Handshake frame — no id, nothing to correlate.
|
|
129
|
+
if (msg.type === "hello") {
|
|
130
|
+
if (msg.client !== "webbrain-extension") {
|
|
131
|
+
log(`rejecting unknown bridge client: ${String(msg.client)}`);
|
|
132
|
+
socket.close(1008, "Unknown client");
|
|
133
|
+
if (this.socket === socket) {
|
|
134
|
+
this.socket = null;
|
|
135
|
+
this.handshakenSocket = null;
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.handshakenSocket = socket;
|
|
140
|
+
this.extensionProtocol = typeof msg.protocolVersion === "number" ? msg.protocolVersion : null;
|
|
141
|
+
this.extensionCapabilities = Array.isArray(msg.capabilities)
|
|
142
|
+
? msg.capabilities
|
|
143
|
+
: [];
|
|
144
|
+
log(`handshake ok — protocol v${this.extensionProtocol}, capabilities: ` +
|
|
145
|
+
(this.extensionCapabilities.join(", ") || "none"));
|
|
146
|
+
const waiters = this.waiters;
|
|
147
|
+
this.waiters = [];
|
|
148
|
+
for (const wake of waiters)
|
|
149
|
+
wake();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const id = typeof msg.id === "number" ? msg.id : null;
|
|
153
|
+
if (id == null)
|
|
154
|
+
return;
|
|
155
|
+
const entry = this.pending.get(id);
|
|
156
|
+
if (!entry)
|
|
157
|
+
return;
|
|
158
|
+
this.pending.delete(id);
|
|
159
|
+
clearTimeout(entry.timer);
|
|
160
|
+
if (msg.ok === false) {
|
|
161
|
+
const status = typeof msg.status === "number" ? msg.status : undefined;
|
|
162
|
+
entry.reject(new BridgeError(String(msg.error || "Unknown bridge error"), status));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
entry.resolve(msg.result);
|
|
166
|
+
}
|
|
167
|
+
failAllPending(error) {
|
|
168
|
+
for (const [, entry] of this.pending) {
|
|
169
|
+
clearTimeout(entry.timer);
|
|
170
|
+
entry.reject(error);
|
|
171
|
+
}
|
|
172
|
+
this.pending.clear();
|
|
173
|
+
}
|
|
174
|
+
isConnected() {
|
|
175
|
+
return (this.socket !== null &&
|
|
176
|
+
this.socket.readyState === 1 &&
|
|
177
|
+
this.handshakenSocket === this.socket);
|
|
178
|
+
}
|
|
179
|
+
capabilities() {
|
|
180
|
+
return [...this.extensionCapabilities];
|
|
181
|
+
}
|
|
182
|
+
/** Resolve once the extension has connected and completed its handshake. */
|
|
183
|
+
waitForExtension(timeoutMs) {
|
|
184
|
+
if (this.isConnected())
|
|
185
|
+
return Promise.resolve(true);
|
|
186
|
+
return new Promise((resolve) => {
|
|
187
|
+
const timer = setTimeout(() => {
|
|
188
|
+
this.waiters = this.waiters.filter((w) => w !== wake);
|
|
189
|
+
resolve(false);
|
|
190
|
+
}, timeoutMs);
|
|
191
|
+
const wake = () => {
|
|
192
|
+
clearTimeout(timer);
|
|
193
|
+
resolve(true);
|
|
194
|
+
};
|
|
195
|
+
this.waiters.push(wake);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/** Send one command and await the extension's reply. */
|
|
199
|
+
async request(action, payload = {}, timeoutMs = config.commandTimeoutMs) {
|
|
200
|
+
const socket = this.socket;
|
|
201
|
+
if (!socket || !this.isConnected()) {
|
|
202
|
+
throw new BridgeError("No WebBrain extension is connected. Open the browser, then set " +
|
|
203
|
+
"WebBrain → Settings → General → Advanced → Cloud bridge to " +
|
|
204
|
+
`ws://127.0.0.1:${config.bridgePort}${config.bridgePath} and enable it.`);
|
|
205
|
+
}
|
|
206
|
+
const id = this.nextId++;
|
|
207
|
+
const frame = JSON.stringify({ id, action, payload });
|
|
208
|
+
const responseTimeoutMs = Math.max(1, Math.min(config.commandTimeoutMs, timeoutMs));
|
|
209
|
+
return new Promise((resolve, reject) => {
|
|
210
|
+
const timer = setTimeout(() => {
|
|
211
|
+
this.pending.delete(id);
|
|
212
|
+
reject(new BridgeError(`WebBrain did not answer '${action}' within ${responseTimeoutMs}ms.`, undefined, "COMMAND_TIMEOUT"));
|
|
213
|
+
}, responseTimeoutMs);
|
|
214
|
+
this.pending.set(id, { resolve: resolve, reject, timer });
|
|
215
|
+
try {
|
|
216
|
+
socket.send(frame);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
this.pending.delete(id);
|
|
220
|
+
clearTimeout(timer);
|
|
221
|
+
reject(new BridgeError(`Failed to send '${action}': ${error instanceof Error ? error.message : String(error)}`));
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
async stop() {
|
|
226
|
+
this.failAllPending(new BridgeError("Bridge shutting down."));
|
|
227
|
+
if (this.socket) {
|
|
228
|
+
try {
|
|
229
|
+
this.socket.close(1001, "Server shutting down");
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
/* ignore */
|
|
233
|
+
}
|
|
234
|
+
this.socket = null;
|
|
235
|
+
this.handshakenSocket = null;
|
|
236
|
+
}
|
|
237
|
+
if (this.wss) {
|
|
238
|
+
await new Promise((resolve) => this.wss.close(() => resolve()));
|
|
239
|
+
this.wss = null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,eAAe,EAAkB,MAAM,IAAI,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAkCrC,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;AAQ7E,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,MAAM,CAAU;IAChB,IAAI,CAA6C;IAC1D,YACE,OAAe,EACf,MAAe,EACf,IAAgD;QAEhD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI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MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,sBAAsB,CAAC,CAAC;YAClD,CAAC;YAAC,MAAM,CAAC;gBACP,YAAY;YACd,CAAC;YACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACnB,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC/B,CAAC;QACD,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,GAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;YACvE,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;QAClB,CAAC;IACH,CAAC;CACF"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime configuration, all environment-driven.
|
|
3
|
+
*
|
|
4
|
+
* The bridge port intentionally defaults to 17374, NOT 17373. WebBrain Cloud's
|
|
5
|
+
* sidecar owns 17373, and `cloud-bridge.js` holds exactly one outbound socket —
|
|
6
|
+
* so the extension can be pointed at Cloud or at this server, never both. Using
|
|
7
|
+
* a distinct port keeps the failure mode obvious ("nothing connected") instead
|
|
8
|
+
* of two processes fighting over one listener.
|
|
9
|
+
*/
|
|
10
|
+
export declare const config: {
|
|
11
|
+
/** Port this process listens on for the extension's outbound bridge socket. */
|
|
12
|
+
readonly bridgePort: number;
|
|
13
|
+
/** Path segment the extension connects to. Must match the URL set in WebBrain settings. */
|
|
14
|
+
readonly bridgePath: string;
|
|
15
|
+
/**
|
|
16
|
+
* How long to wait for the extension to answer a single bridge command.
|
|
17
|
+
* Starting a run returns immediately; this is not the task timeout.
|
|
18
|
+
*/
|
|
19
|
+
readonly commandTimeoutMs: number;
|
|
20
|
+
/** Default ceiling for how long `webbrain_run` will poll before giving up. */
|
|
21
|
+
readonly defaultRunTimeoutMs: number;
|
|
22
|
+
/** Interval between `cloud_status` polls while a run is in flight. */
|
|
23
|
+
readonly pollIntervalMs: number;
|
|
24
|
+
};
|
|
25
|
+
/** The URL the user must paste into Settings → General → Advanced → Cloud bridge. */
|
|
26
|
+
export declare function bridgeUrl(): string;
|
|
27
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAoCH,eAAO,MAAM,MAAM;IACjB,+EAA+E;;IAG/E,2FAA2F;;IAG3F;;;OAGG;;IAGH,8EAA8E;;IAG9E,sEAAsE;;CAE9D,CAAC;AAEX,qFAAqF;AACrF,wBAAgB,SAAS,IAAI,MAAM,CAElC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime configuration, all environment-driven.
|
|
3
|
+
*
|
|
4
|
+
* The bridge port intentionally defaults to 17374, NOT 17373. WebBrain Cloud's
|
|
5
|
+
* sidecar owns 17373, and `cloud-bridge.js` holds exactly one outbound socket —
|
|
6
|
+
* so the extension can be pointed at Cloud or at this server, never both. Using
|
|
7
|
+
* a distinct port keeps the failure mode obvious ("nothing connected") instead
|
|
8
|
+
* of two processes fighting over one listener.
|
|
9
|
+
*/
|
|
10
|
+
function parseIntFromEnv(name, raw) {
|
|
11
|
+
const parsed = Number.parseInt(raw, 10);
|
|
12
|
+
if (!Number.isFinite(parsed) || String(parsed) !== raw.trim()) {
|
|
13
|
+
throw new Error(`${name} must be an integer, got: ${raw}`);
|
|
14
|
+
}
|
|
15
|
+
return parsed;
|
|
16
|
+
}
|
|
17
|
+
/** Ports carry a 16-bit ceiling. Durations must NOT — see durationFromEnv. */
|
|
18
|
+
function portFromEnv(name, fallback) {
|
|
19
|
+
const raw = process.env[name];
|
|
20
|
+
if (!raw)
|
|
21
|
+
return fallback;
|
|
22
|
+
const parsed = parseIntFromEnv(name, raw);
|
|
23
|
+
if (parsed <= 0 || parsed > 65535) {
|
|
24
|
+
throw new Error(`${name} must be a valid TCP port (1-65535), got: ${raw}`);
|
|
25
|
+
}
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Millisecond durations. Deliberately separate from portFromEnv: reusing the
|
|
30
|
+
* port validator here rejected every timeout above 65535ms, including the
|
|
31
|
+
* five-minute default this file itself documents.
|
|
32
|
+
*/
|
|
33
|
+
function durationFromEnv(name, fallback) {
|
|
34
|
+
const raw = process.env[name];
|
|
35
|
+
if (!raw)
|
|
36
|
+
return fallback;
|
|
37
|
+
const parsed = parseIntFromEnv(name, raw);
|
|
38
|
+
if (parsed <= 0) {
|
|
39
|
+
throw new Error(`${name} must be a positive duration in milliseconds, got: ${raw}`);
|
|
40
|
+
}
|
|
41
|
+
return parsed;
|
|
42
|
+
}
|
|
43
|
+
export const config = {
|
|
44
|
+
/** Port this process listens on for the extension's outbound bridge socket. */
|
|
45
|
+
bridgePort: portFromEnv("WEBBRAIN_BRIDGE_PORT", 17374),
|
|
46
|
+
/** Path segment the extension connects to. Must match the URL set in WebBrain settings. */
|
|
47
|
+
bridgePath: process.env.WEBBRAIN_BRIDGE_PATH || "/extension",
|
|
48
|
+
/**
|
|
49
|
+
* How long to wait for the extension to answer a single bridge command.
|
|
50
|
+
* Starting a run returns immediately; this is not the task timeout.
|
|
51
|
+
*/
|
|
52
|
+
commandTimeoutMs: durationFromEnv("WEBBRAIN_COMMAND_TIMEOUT_MS", 30_000),
|
|
53
|
+
/** Default ceiling for how long `webbrain_run` will poll before giving up. */
|
|
54
|
+
defaultRunTimeoutMs: durationFromEnv("WEBBRAIN_RUN_TIMEOUT_MS", 300_000),
|
|
55
|
+
/** Interval between `cloud_status` polls while a run is in flight. */
|
|
56
|
+
pollIntervalMs: durationFromEnv("WEBBRAIN_POLL_INTERVAL_MS", 1_000),
|
|
57
|
+
};
|
|
58
|
+
/** The URL the user must paste into Settings → General → Advanced → Cloud bridge. */
|
|
59
|
+
export function bridgeUrl() {
|
|
60
|
+
return `ws://127.0.0.1:${config.bridgePort}${config.bridgePath}`;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,SAAS,eAAe,CAAC,IAAY,EAAE,GAAW;IAChD,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QAC9D,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,6BAA6B,GAAG,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,8EAA8E;AAC9E,SAAS,WAAW,CAAC,IAAY,EAAE,QAAgB;IACjD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,CAAC,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC1B,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC1C,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,GAAG,KAAK,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,6CAA6C,GAAG,EAAE,CAAC,CAAC;IAC7E,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,SAAS,eAAe,CAAC,IAAY,EAAE,QAAgB;IACrD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,CAAC,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC1B,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC1C,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,sDAAsD,GAAG,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,+EAA+E;IAC/E,UAAU,EAAE,WAAW,CAAC,sBAAsB,EAAE,KAAK,CAAC;IAEtD,2FAA2F;IAC3F,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,YAAY;IAE5D;;;OAGG;IACH,gBAAgB,EAAE,eAAe,CAAC,6BAA6B,EAAE,MAAM,CAAC;IAExE,8EAA8E;IAC9E,mBAAmB,EAAE,eAAe,CAAC,yBAAyB,EAAE,OAAO,CAAC;IAExE,sEAAsE;IACtE,cAAc,EAAE,eAAe,CAAC,2BAA2B,EAAE,KAAK,CAAC;CAC3D,CAAC;AAEX,qFAAqF;AACrF,MAAM,UAAU,SAAS;IACvB,OAAO,kBAAkB,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;AACnE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* WebBrain MCP server.
|
|
4
|
+
*
|
|
5
|
+
* Gives any MCP client — Claude Code, Codex, Cursor, OpenClaw — the ability to
|
|
6
|
+
* delegate a browser task to the user's REAL browser session: already logged
|
|
7
|
+
* in, cookies present, MFA already passed. That session is the thing a headless
|
|
8
|
+
* automation framework cannot reproduce, and it is the only reason this server
|
|
9
|
+
* is interesting.
|
|
10
|
+
*
|
|
11
|
+
* Scope is deliberately coarse. We expose task delegation, not the ~50
|
|
12
|
+
* low-level browser primitives, because:
|
|
13
|
+
* 1. the permission gate lives in the extension's agent loop, not in
|
|
14
|
+
* `executeTool()`, so per-primitive access would bypass every safety
|
|
15
|
+
* property WebBrain advertises; and
|
|
16
|
+
* 2. driving 50 primitives over a socket costs a round trip and a pile of
|
|
17
|
+
* tokens per click. Delegation is both safer and cheaper.
|
|
18
|
+
*
|
|
19
|
+
* stdout belongs to the MCP stdio transport. All logging goes to stderr.
|
|
20
|
+
*/
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;GAkBG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* WebBrain MCP server.
|
|
4
|
+
*
|
|
5
|
+
* Gives any MCP client — Claude Code, Codex, Cursor, OpenClaw — the ability to
|
|
6
|
+
* delegate a browser task to the user's REAL browser session: already logged
|
|
7
|
+
* in, cookies present, MFA already passed. That session is the thing a headless
|
|
8
|
+
* automation framework cannot reproduce, and it is the only reason this server
|
|
9
|
+
* is interesting.
|
|
10
|
+
*
|
|
11
|
+
* Scope is deliberately coarse. We expose task delegation, not the ~50
|
|
12
|
+
* low-level browser primitives, because:
|
|
13
|
+
* 1. the permission gate lives in the extension's agent loop, not in
|
|
14
|
+
* `executeTool()`, so per-primitive access would bypass every safety
|
|
15
|
+
* property WebBrain advertises; and
|
|
16
|
+
* 2. driving 50 primitives over a socket costs a round trip and a pile of
|
|
17
|
+
* tokens per click. Delegation is both safer and cheaper.
|
|
18
|
+
*
|
|
19
|
+
* stdout belongs to the MCP stdio transport. All logging goes to stderr.
|
|
20
|
+
*/
|
|
21
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
22
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
23
|
+
import { randomUUID } from "node:crypto";
|
|
24
|
+
import { z } from "zod";
|
|
25
|
+
import { BridgeError, WebBrainBridge } from "./bridge.js";
|
|
26
|
+
import { bridgeUrl, config } from "./config.js";
|
|
27
|
+
import { abort, awaitSettled, describeSnapshot, getStatus, respond, startRun } from "./runs.js";
|
|
28
|
+
const bridge = new WebBrainBridge();
|
|
29
|
+
const server = new McpServer({
|
|
30
|
+
name: "webbrain",
|
|
31
|
+
version: "0.1.0",
|
|
32
|
+
});
|
|
33
|
+
const ok = (text) => ({ content: [{ type: "text", text }] });
|
|
34
|
+
const fail = (text) => ({ content: [{ type: "text", text }], isError: true });
|
|
35
|
+
function toolError(error) {
|
|
36
|
+
if (error instanceof BridgeError)
|
|
37
|
+
return fail(error.message);
|
|
38
|
+
return fail(error instanceof Error ? error.message : String(error));
|
|
39
|
+
}
|
|
40
|
+
server.registerTool("webbrain_run", {
|
|
41
|
+
title: "Run a browser task in the user's real session",
|
|
42
|
+
description: "Delegate a web task to WebBrain running in the user's actual browser — already " +
|
|
43
|
+
"signed in, with existing cookies and sessions. Use this when a task needs a page " +
|
|
44
|
+
"the caller cannot reach: an authenticated dashboard, a webmail account, an admin " +
|
|
45
|
+
"panel, a SaaS report behind SSO. Describe the goal in plain language, the way you " +
|
|
46
|
+
"would to a colleague sharing the screen.\n\n" +
|
|
47
|
+
"mode='ask' is read-only: it reads, extracts and summarises, and cannot click, type " +
|
|
48
|
+
"or submit. mode='act' allows interaction, and the user is prompted in-browser to " +
|
|
49
|
+
"approve consequential actions. Prefer 'ask' whenever you only need to read.\n\n" +
|
|
50
|
+
"If the run stops with status 'needs_user_input', relay the question to the user and " +
|
|
51
|
+
"answer with webbrain_respond — never guess on their behalf.",
|
|
52
|
+
inputSchema: {
|
|
53
|
+
task: z
|
|
54
|
+
.string()
|
|
55
|
+
.min(1)
|
|
56
|
+
.describe("The goal, in plain language. e.g. 'open the Stripe dashboard and list last " +
|
|
57
|
+
"week's failed payments with amounts and customer emails'."),
|
|
58
|
+
mode: z
|
|
59
|
+
.enum(["ask", "act"])
|
|
60
|
+
.default("ask")
|
|
61
|
+
.describe("'ask' is read-only and cannot modify the page. 'act' permits clicking, typing " +
|
|
62
|
+
"and navigation, gated by in-browser approval. Default 'ask'."),
|
|
63
|
+
tab_id: z
|
|
64
|
+
.number()
|
|
65
|
+
.int()
|
|
66
|
+
.optional()
|
|
67
|
+
.describe("Target a specific browser tab. Omit to use the active tab, which is almost " +
|
|
68
|
+
"always what you want."),
|
|
69
|
+
allow_api_mutations: z
|
|
70
|
+
.boolean()
|
|
71
|
+
.default(false)
|
|
72
|
+
.describe("Lift WebBrain's UI-first rule so the agent may issue mutating HTTP requests " +
|
|
73
|
+
"directly instead of clicking through the interface. Off by default and rarely " +
|
|
74
|
+
"correct — the UI path is visible and stoppable. Only valid when mode is 'act'."),
|
|
75
|
+
timeout_seconds: z
|
|
76
|
+
.number()
|
|
77
|
+
.int()
|
|
78
|
+
.positive()
|
|
79
|
+
.max(3600)
|
|
80
|
+
.optional()
|
|
81
|
+
.describe("How long to wait before returning control. The run keeps going in the browser " +
|
|
82
|
+
"past this point; poll webbrain_status to pick it back up."),
|
|
83
|
+
wait: z
|
|
84
|
+
.boolean()
|
|
85
|
+
.default(true)
|
|
86
|
+
.describe("Wait for the run to settle. Set false to start it and return the run_id " +
|
|
87
|
+
"immediately."),
|
|
88
|
+
},
|
|
89
|
+
}, async ({ task, mode, tab_id, allow_api_mutations, timeout_seconds, wait }) => {
|
|
90
|
+
const timeoutMs = timeout_seconds ? timeout_seconds * 1000 : config.defaultRunTimeoutMs;
|
|
91
|
+
const deadline = Date.now() + timeoutMs;
|
|
92
|
+
const runId = `mcp_${randomUUID()}`;
|
|
93
|
+
try {
|
|
94
|
+
let started;
|
|
95
|
+
try {
|
|
96
|
+
started = await startRun(bridge, {
|
|
97
|
+
runId,
|
|
98
|
+
task,
|
|
99
|
+
mode,
|
|
100
|
+
tabId: tab_id,
|
|
101
|
+
apiMutationsAllowed: allow_api_mutations,
|
|
102
|
+
}, Math.max(1, deadline - Date.now()));
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
if (error instanceof BridgeError &&
|
|
106
|
+
(error.code === "COMMAND_TIMEOUT" || error.code === "COMMAND_INTERRUPTED")) {
|
|
107
|
+
return ok(describeSnapshot({ runId, status: "running" }, true));
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
if (!wait) {
|
|
112
|
+
return ok(`Started in the background.\n${describeSnapshot(started)}\n\n` +
|
|
113
|
+
"Poll webbrain_status with this run_id for progress.");
|
|
114
|
+
}
|
|
115
|
+
const { snapshot, timedOut } = await awaitSettled(bridge, started.runId, {
|
|
116
|
+
timeoutMs: Math.max(0, deadline - Date.now()),
|
|
117
|
+
});
|
|
118
|
+
return ok(describeSnapshot(snapshot, timedOut));
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
return toolError(error);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
server.registerTool("webbrain_status", {
|
|
125
|
+
title: "Check a browser run",
|
|
126
|
+
description: "Fetch the current state of a WebBrain run, including its result once finished. " +
|
|
127
|
+
"Omit run_id to list every run this browser knows about.",
|
|
128
|
+
inputSchema: {
|
|
129
|
+
run_id: z
|
|
130
|
+
.string()
|
|
131
|
+
.optional()
|
|
132
|
+
.describe("The run to inspect. Omit to list all runs."),
|
|
133
|
+
},
|
|
134
|
+
}, async ({ run_id }) => {
|
|
135
|
+
try {
|
|
136
|
+
const result = await getStatus(bridge, run_id);
|
|
137
|
+
const runs = result.runs;
|
|
138
|
+
if (runs) {
|
|
139
|
+
if (!runs.length)
|
|
140
|
+
return ok("No WebBrain runs on record.");
|
|
141
|
+
return ok(runs
|
|
142
|
+
.map((run) => `${run.runId} ${run.status.padEnd(16)} ${run.task ?? ""}`)
|
|
143
|
+
.join("\n"));
|
|
144
|
+
}
|
|
145
|
+
return ok(describeSnapshot(result));
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
return toolError(error);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
server.registerTool("webbrain_respond", {
|
|
152
|
+
title: "Answer a question from a browser run",
|
|
153
|
+
description: "Supply the user's answer to a run sitting at status 'needs_user_input', then keep " +
|
|
154
|
+
"waiting for it to settle. The answer must come from the user — WebBrain pauses " +
|
|
155
|
+
"precisely because a human decision is required.",
|
|
156
|
+
inputSchema: {
|
|
157
|
+
run_id: z.string().describe("The run that is waiting."),
|
|
158
|
+
clarify_id: z.string().describe("The clarify_id reported alongside the question."),
|
|
159
|
+
answer: z.string().min(1).describe("The user's answer, passed through verbatim."),
|
|
160
|
+
timeout_seconds: z
|
|
161
|
+
.number()
|
|
162
|
+
.int()
|
|
163
|
+
.positive()
|
|
164
|
+
.max(3600)
|
|
165
|
+
.optional()
|
|
166
|
+
.describe("How long to wait after answering before returning control."),
|
|
167
|
+
},
|
|
168
|
+
}, async ({ run_id, clarify_id, answer, timeout_seconds }) => {
|
|
169
|
+
const timeoutMs = timeout_seconds ? timeout_seconds * 1000 : config.defaultRunTimeoutMs;
|
|
170
|
+
const deadline = Date.now() + timeoutMs;
|
|
171
|
+
try {
|
|
172
|
+
try {
|
|
173
|
+
await respond(bridge, run_id, clarify_id, answer, Math.max(1, deadline - Date.now()));
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (error instanceof BridgeError &&
|
|
177
|
+
(error.code === "COMMAND_TIMEOUT" || error.code === "COMMAND_INTERRUPTED")) {
|
|
178
|
+
return ok(describeSnapshot({ runId: run_id, status: "running" }, true));
|
|
179
|
+
}
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
const { snapshot, timedOut } = await awaitSettled(bridge, run_id, {
|
|
183
|
+
timeoutMs: Math.max(0, deadline - Date.now()),
|
|
184
|
+
});
|
|
185
|
+
return ok(describeSnapshot(snapshot, timedOut));
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
return toolError(error);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
server.registerTool("webbrain_abort", {
|
|
192
|
+
title: "Stop a browser run",
|
|
193
|
+
description: "Halt a run that is going wrong or is no longer needed. Note that actions already " +
|
|
194
|
+
"taken in the browser are not undone.",
|
|
195
|
+
inputSchema: {
|
|
196
|
+
run_id: z.string().describe("The run to stop."),
|
|
197
|
+
},
|
|
198
|
+
}, async ({ run_id }) => {
|
|
199
|
+
try {
|
|
200
|
+
const snapshot = await abort(bridge, run_id);
|
|
201
|
+
return ok(describeSnapshot(snapshot));
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
return toolError(error);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
server.registerTool("webbrain_connection", {
|
|
208
|
+
title: "Check the WebBrain browser connection",
|
|
209
|
+
description: "Report whether a WebBrain extension is currently attached. Call this first when a " +
|
|
210
|
+
"browser tool fails, so you can tell the user what to fix instead of retrying blindly.",
|
|
211
|
+
inputSchema: {},
|
|
212
|
+
}, async () => {
|
|
213
|
+
if (bridge.isConnected()) {
|
|
214
|
+
const caps = bridge.capabilities();
|
|
215
|
+
return ok(`Connected. Listening on ${bridgeUrl()}.` +
|
|
216
|
+
(caps.length ? `\nExtension capabilities: ${caps.join(", ")}` : ""));
|
|
217
|
+
}
|
|
218
|
+
return ok(`Not connected. Listening on ${bridgeUrl()}, but no extension has dialled in.\n\n` +
|
|
219
|
+
"To connect: open a Chromium browser (Chrome, Edge, Brave), then in " +
|
|
220
|
+
"WebBrain → Settings → General → Advanced → Cloud bridge set the URL to\n" +
|
|
221
|
+
` ${bridgeUrl()}\n` +
|
|
222
|
+
"and enable it. The extension holds one bridge socket at a time, so this cannot " +
|
|
223
|
+
"run at the same time as WebBrain Cloud on port 17373.\n\n" +
|
|
224
|
+
"Firefox cannot host the bridge — that build has no offscreen document. If the " +
|
|
225
|
+
"user is on Firefox, say so rather than suggesting settings changes.");
|
|
226
|
+
});
|
|
227
|
+
async function main() {
|
|
228
|
+
await bridge.start();
|
|
229
|
+
// Give an already-open extension a bounded chance to reconnect before the
|
|
230
|
+
// stdio server advertises browser tools. If this promise is discarded, the
|
|
231
|
+
// grace period is illusory and the first tool call can race the reconnect.
|
|
232
|
+
await bridge.waitForExtension(3_000);
|
|
233
|
+
const transport = new StdioServerTransport();
|
|
234
|
+
await server.connect(transport);
|
|
235
|
+
console.error("[webbrain-mcp] ready on stdio");
|
|
236
|
+
}
|
|
237
|
+
let shuttingDown = false;
|
|
238
|
+
async function shutdown() {
|
|
239
|
+
if (shuttingDown)
|
|
240
|
+
return;
|
|
241
|
+
shuttingDown = true;
|
|
242
|
+
await bridge.stop().catch((error) => {
|
|
243
|
+
console.error("[webbrain-mcp] shutdown error:", error);
|
|
244
|
+
});
|
|
245
|
+
process.exit(0);
|
|
246
|
+
}
|
|
247
|
+
process.on("SIGINT", () => void shutdown());
|
|
248
|
+
process.on("SIGTERM", () => void shutdown());
|
|
249
|
+
// MCP hosts commonly stop stdio servers by closing the child's stdin rather
|
|
250
|
+
// than sending a signal. Without these handlers the WebSocket listener keeps
|
|
251
|
+
// the event loop alive and leaves port 17374 occupied by an orphan process.
|
|
252
|
+
process.stdin.once("end", () => void shutdown());
|
|
253
|
+
process.stdin.once("close", () => void shutdown());
|
|
254
|
+
main().catch((error) => {
|
|
255
|
+
console.error("[webbrain-mcp] fatal:", error);
|
|
256
|
+
process.exit(1);
|
|
257
|
+
});
|
|
258
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAA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EAAE,CAAC,CAAC;AAEnD,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,uBAAuB,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
package/dist/runs.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run orchestration.
|
|
3
|
+
*
|
|
4
|
+
* `cloud_run` returns as soon as the run is registered — the actual work happens
|
|
5
|
+
* in a detached async IIFE inside the extension (`cloud-runs.js`). So starting a
|
|
6
|
+
* task gives us a snapshot with status 'running', and we poll `cloud_status`
|
|
7
|
+
* until it reaches a terminal state or stops to ask the user something.
|
|
8
|
+
*
|
|
9
|
+
* Important: every tool call still travels the full agent loop inside the
|
|
10
|
+
* extension (`agent.processMessage` -> `_executeToolBatch`), which means the
|
|
11
|
+
* capability x origin permission gate is enforced exactly as it is for a human
|
|
12
|
+
* driving the side panel. This server deliberately does NOT expose the
|
|
13
|
+
* individual browser primitives — `executeTool()` has no gate of its own, and
|
|
14
|
+
* calling it directly would move the trust boundary out of the browser.
|
|
15
|
+
*/
|
|
16
|
+
import { WebBrainBridge, type CloudSnapshot } from "./bridge.js";
|
|
17
|
+
export interface StartRunOptions {
|
|
18
|
+
runId?: string;
|
|
19
|
+
task: string;
|
|
20
|
+
mode: "ask" | "act";
|
|
21
|
+
tabId?: number;
|
|
22
|
+
apiMutationsAllowed?: boolean;
|
|
23
|
+
outputSchema?: unknown;
|
|
24
|
+
}
|
|
25
|
+
export interface AwaitOptions {
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
}
|
|
28
|
+
export declare function startRun(bridge: WebBrainBridge, options: StartRunOptions, timeoutMs?: number): Promise<CloudSnapshot>;
|
|
29
|
+
export declare function getStatus(bridge: WebBrainBridge, runId?: string, timeoutMs?: number): Promise<CloudSnapshot | {
|
|
30
|
+
runs: CloudSnapshot[];
|
|
31
|
+
}>;
|
|
32
|
+
export declare function respond(bridge: WebBrainBridge, runId: string, clarifyId: string, answer: string, timeoutMs?: number): Promise<CloudSnapshot>;
|
|
33
|
+
export declare function abort(bridge: WebBrainBridge, runId: string): Promise<CloudSnapshot>;
|
|
34
|
+
/**
|
|
35
|
+
* Poll until the run finishes, needs the user, or we run out of patience.
|
|
36
|
+
*
|
|
37
|
+
* A timeout here does NOT abort the run — the browser keeps working and the
|
|
38
|
+
* caller can resume with `webbrain_status`. Silently killing a half-finished
|
|
39
|
+
* task that may have already submitted a form would be worse than reporting
|
|
40
|
+
* that it is still going.
|
|
41
|
+
*/
|
|
42
|
+
export declare function awaitSettled(bridge: WebBrainBridge, runId: string, { timeoutMs }: AwaitOptions): Promise<{
|
|
43
|
+
snapshot: CloudSnapshot;
|
|
44
|
+
timedOut: boolean;
|
|
45
|
+
}>;
|
|
46
|
+
/** Render a snapshot as the text an calling agent actually needs to read. */
|
|
47
|
+
export declare function describeSnapshot(snapshot: CloudSnapshot, timedOut?: boolean): string;
|
|
48
|
+
//# sourceMappingURL=runs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runs.d.ts","sourceRoot":"","sources":["../src/runs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAkC,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAGjG,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,KAAK,GAAG,KAAK,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;CACnB;AAID,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,eAAe,EACxB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,aAAa,CAAC,CAexB;AAED,wBAAsB,SAAS,CAC7B,MAAM,EAAE,cAAc,EACtB,KAAK,CAAC,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,aAAa,GAAG;IAAE,IAAI,EAAE,aAAa,EAAE,CAAA;CAAE,CAAC,CAOpD;AAED,wBAAsB,OAAO,CAC3B,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,aAAa,CAAC,CAMxB;AAED,wBAAsB,KAAK,CAAC,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAEzF;AAED;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,MAAM,EACb,EAAE,SAAS,EAAE,EAAE,YAAY,GAC1B,OAAO,CAAC;IAAE,QAAQ,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC,CAoCzD;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,UAAQ,GAAG,MAAM,CAuClF"}
|
package/dist/runs.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run orchestration.
|
|
3
|
+
*
|
|
4
|
+
* `cloud_run` returns as soon as the run is registered — the actual work happens
|
|
5
|
+
* in a detached async IIFE inside the extension (`cloud-runs.js`). So starting a
|
|
6
|
+
* task gives us a snapshot with status 'running', and we poll `cloud_status`
|
|
7
|
+
* until it reaches a terminal state or stops to ask the user something.
|
|
8
|
+
*
|
|
9
|
+
* Important: every tool call still travels the full agent loop inside the
|
|
10
|
+
* extension (`agent.processMessage` -> `_executeToolBatch`), which means the
|
|
11
|
+
* capability x origin permission gate is enforced exactly as it is for a human
|
|
12
|
+
* driving the side panel. This server deliberately does NOT expose the
|
|
13
|
+
* individual browser primitives — `executeTool()` has no gate of its own, and
|
|
14
|
+
* calling it directly would move the trust boundary out of the browser.
|
|
15
|
+
*/
|
|
16
|
+
import { BridgeError, TERMINAL_STATUSES } from "./bridge.js";
|
|
17
|
+
import { config } from "./config.js";
|
|
18
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
export async function startRun(bridge, options, timeoutMs) {
|
|
20
|
+
if (options.apiMutationsAllowed && options.mode !== "act") {
|
|
21
|
+
throw new BridgeError("API mutation permission requires mode 'act'.", 400);
|
|
22
|
+
}
|
|
23
|
+
const payload = {
|
|
24
|
+
task: options.task,
|
|
25
|
+
mode: options.mode,
|
|
26
|
+
};
|
|
27
|
+
if (options.runId)
|
|
28
|
+
payload.runId = options.runId;
|
|
29
|
+
if (options.tabId != null)
|
|
30
|
+
payload.tabId = options.tabId;
|
|
31
|
+
if (options.apiMutationsAllowed)
|
|
32
|
+
payload.apiMutationsAllowed = true;
|
|
33
|
+
if (options.outputSchema != null)
|
|
34
|
+
payload.outputSchema = options.outputSchema;
|
|
35
|
+
return await bridge.request("cloud_run", payload, timeoutMs);
|
|
36
|
+
}
|
|
37
|
+
export async function getStatus(bridge, runId, timeoutMs) {
|
|
38
|
+
const payload = runId ? { runId } : {};
|
|
39
|
+
return await bridge.request("cloud_status", payload, timeoutMs);
|
|
40
|
+
}
|
|
41
|
+
export async function respond(bridge, runId, clarifyId, answer, timeoutMs) {
|
|
42
|
+
return await bridge.request("cloud_respond", { runId, clarifyId, answer }, timeoutMs);
|
|
43
|
+
}
|
|
44
|
+
export async function abort(bridge, runId) {
|
|
45
|
+
return await bridge.request("cloud_abort", { runId });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Poll until the run finishes, needs the user, or we run out of patience.
|
|
49
|
+
*
|
|
50
|
+
* A timeout here does NOT abort the run — the browser keeps working and the
|
|
51
|
+
* caller can resume with `webbrain_status`. Silently killing a half-finished
|
|
52
|
+
* task that may have already submitted a form would be worse than reporting
|
|
53
|
+
* that it is still going.
|
|
54
|
+
*/
|
|
55
|
+
export async function awaitSettled(bridge, runId, { timeoutMs }) {
|
|
56
|
+
const deadline = Date.now() + timeoutMs;
|
|
57
|
+
let last = { runId, status: "running" };
|
|
58
|
+
while (Date.now() < deadline) {
|
|
59
|
+
const remainingMs = deadline - Date.now();
|
|
60
|
+
if (remainingMs <= 0)
|
|
61
|
+
break;
|
|
62
|
+
let result;
|
|
63
|
+
try {
|
|
64
|
+
result = await getStatus(bridge, runId, remainingMs);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (Date.now() >= deadline ||
|
|
68
|
+
(error instanceof BridgeError &&
|
|
69
|
+
(error.code === "COMMAND_TIMEOUT" || error.code === "COMMAND_INTERRUPTED"))) {
|
|
70
|
+
return { snapshot: last, timedOut: true };
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
const snapshot = result.runs
|
|
75
|
+
? null
|
|
76
|
+
: result;
|
|
77
|
+
if (!snapshot)
|
|
78
|
+
continue;
|
|
79
|
+
last = snapshot;
|
|
80
|
+
if (TERMINAL_STATUSES.has(snapshot.status) || snapshot.status === "needs_user_input") {
|
|
81
|
+
return { snapshot, timedOut: false };
|
|
82
|
+
}
|
|
83
|
+
await sleep(Math.min(config.pollIntervalMs, Math.max(0, deadline - Date.now())));
|
|
84
|
+
}
|
|
85
|
+
return { snapshot: last, timedOut: true };
|
|
86
|
+
}
|
|
87
|
+
/** Render a snapshot as the text an calling agent actually needs to read. */
|
|
88
|
+
export function describeSnapshot(snapshot, timedOut = false) {
|
|
89
|
+
const lines = [];
|
|
90
|
+
lines.push(`run_id: ${snapshot.runId}`);
|
|
91
|
+
lines.push(`status: ${snapshot.status}${timedOut ? " (still running — poll webbrain_status)" : ""}`);
|
|
92
|
+
if (snapshot.mode)
|
|
93
|
+
lines.push(`mode: ${snapshot.mode}`);
|
|
94
|
+
if (snapshot.finalUrl)
|
|
95
|
+
lines.push(`final_url: ${snapshot.finalUrl}`);
|
|
96
|
+
if (snapshot.status === "needs_user_input" && snapshot.pendingInput) {
|
|
97
|
+
const clarifyId = snapshot.pendingInput.clarifyId || snapshot.pendingInput.clarify_id || "";
|
|
98
|
+
const question = snapshot.pendingInput.question || "(no question text supplied)";
|
|
99
|
+
lines.push("");
|
|
100
|
+
lines.push("WebBrain is waiting on a human decision before it continues.");
|
|
101
|
+
lines.push(`question: ${question}`);
|
|
102
|
+
lines.push(`clarify_id: ${clarifyId}`);
|
|
103
|
+
lines.push("Relay this to the user and send their answer with webbrain_respond. " +
|
|
104
|
+
"Do not invent an answer on their behalf.");
|
|
105
|
+
}
|
|
106
|
+
if (snapshot.error) {
|
|
107
|
+
lines.push("");
|
|
108
|
+
lines.push(`error: ${snapshot.error}`);
|
|
109
|
+
}
|
|
110
|
+
const body = snapshot.result !== undefined && snapshot.result !== null
|
|
111
|
+
? typeof snapshot.result === "string"
|
|
112
|
+
? snapshot.result
|
|
113
|
+
: JSON.stringify(snapshot.result, null, 2)
|
|
114
|
+
: snapshot.content || snapshot.summary || "";
|
|
115
|
+
if (body) {
|
|
116
|
+
lines.push("");
|
|
117
|
+
lines.push("--- result ---");
|
|
118
|
+
lines.push(body);
|
|
119
|
+
}
|
|
120
|
+
return lines.join("\n");
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=runs.js.map
|
package/dist/runs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runs.js","sourceRoot":"","sources":["../src/runs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAsC,MAAM,aAAa,CAAC;AACjG,OAAO,E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KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webbrain/mcp-server",
|
|
3
|
+
"displayName": "WebBrain MCP Server",
|
|
4
|
+
"description": "Model Context Protocol server that lets any MCP client (Claude Code, Codex, Cursor, OpenClaw) delegate browser tasks to a running WebBrain extension — in the user's real, already-authenticated browser session.",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"webbrain-mcp": "dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://webbrain.one",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/webbrain-one/webbrain.git",
|
|
17
|
+
"directory": "mcp-server"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc --watch",
|
|
22
|
+
"start": "node dist/index.js",
|
|
23
|
+
"test": "npm run build && node --test test/bridge.test.mjs test/config.test.mjs test/runs-request-timeout.test.mjs test/runs-timeout.test.mjs test/stdio-shutdown.test.mjs",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"prepack": "npm run build",
|
|
26
|
+
"prepare": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"mcp",
|
|
30
|
+
"model-context-protocol",
|
|
31
|
+
"webbrain",
|
|
32
|
+
"browser-agent",
|
|
33
|
+
"browser-automation",
|
|
34
|
+
"claude-code",
|
|
35
|
+
"codex",
|
|
36
|
+
"cursor"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
40
|
+
"ws": "^8.18.0",
|
|
41
|
+
"zod": "^3.23.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^20.11.0",
|
|
45
|
+
"@types/ws": "^8.5.10",
|
|
46
|
+
"typescript": "^5.5.0"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"README.md"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|