@ricsam/browser-mcp 0.0.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -0
- package/README.md +51 -43
- package/THIRD_PARTY_NOTICES.md +17 -0
- package/dist/chunk-1sjcwr28.js +4 -0
- package/dist/chunk-2hzkme2s.js +41 -0
- package/dist/chunk-3kc8te5e.js +5006 -0
- package/dist/chunk-847prxk6.js +98 -0
- package/dist/chunk-cy1a8r79.js +255 -0
- package/dist/chunk-j6psfksr.js +52 -0
- package/dist/chunk-swy0qtx0.js +60 -0
- package/dist/chunk-xgk37h9a.js +77 -0
- package/dist/main.js +36 -0
- package/package.json +10 -7
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseSecrets,
|
|
3
|
+
serializeSecrets
|
|
4
|
+
} from "./chunk-j6psfksr.js";
|
|
5
|
+
import {
|
|
6
|
+
capabilityFetch,
|
|
7
|
+
exclusiveOutput,
|
|
8
|
+
readCapability
|
|
9
|
+
} from "./chunk-2hzkme2s.js";
|
|
10
|
+
import"./chunk-1sjcwr28.js";
|
|
11
|
+
|
|
12
|
+
// cli/src/agent/secrets.ts
|
|
13
|
+
import { spawn } from "node:child_process";
|
|
14
|
+
import { unlink } from "node:fs/promises";
|
|
15
|
+
async function receive(path, format) {
|
|
16
|
+
const cap = await readCapability(path);
|
|
17
|
+
if (cap.method !== "GET" || cap.limits.maxBytes > 1024 * 1024)
|
|
18
|
+
throw new Error("Secret export capability required");
|
|
19
|
+
const response = await capabilityFetch(cap);
|
|
20
|
+
if (!response.body)
|
|
21
|
+
throw new Error("Missing export body");
|
|
22
|
+
const reader = response.body.getReader();
|
|
23
|
+
const chunks = [];
|
|
24
|
+
let bytes = 0;
|
|
25
|
+
try {
|
|
26
|
+
for (;; ) {
|
|
27
|
+
const r = await reader.read();
|
|
28
|
+
if (r.done)
|
|
29
|
+
break;
|
|
30
|
+
bytes += r.value.length;
|
|
31
|
+
if (bytes > cap.limits.maxBytes)
|
|
32
|
+
throw new Error("Export limit");
|
|
33
|
+
chunks.push(r.value);
|
|
34
|
+
}
|
|
35
|
+
return parseSecrets(Buffer.concat(chunks).toString("utf8"), format);
|
|
36
|
+
} catch {
|
|
37
|
+
await reader.cancel().catch(() => {});
|
|
38
|
+
throw new Error("Secret export failed; request a new capability");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function exportSecrets(capabilityPath, outputPath, format = "json") {
|
|
42
|
+
const file = await exclusiveOutput(outputPath);
|
|
43
|
+
let ok = false;
|
|
44
|
+
try {
|
|
45
|
+
const values = await receive(capabilityPath, format);
|
|
46
|
+
await file.writeFile(serializeSecrets(values, format));
|
|
47
|
+
await file.sync();
|
|
48
|
+
ok = true;
|
|
49
|
+
return { exported: true };
|
|
50
|
+
} finally {
|
|
51
|
+
await file.close();
|
|
52
|
+
if (!ok)
|
|
53
|
+
await unlink(outputPath).catch(() => {});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function execSecrets(capabilityPath, command, args, format = "json") {
|
|
57
|
+
if (!command)
|
|
58
|
+
throw new Error("Executable required");
|
|
59
|
+
const values = await receive(capabilityPath, format);
|
|
60
|
+
if (Object.values(values).some((v) => v.includes("\x00")))
|
|
61
|
+
throw new Error("NUL is not supported in environment values");
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const child = spawn(command, args, { shell: false, env: { ...process.env, ...values }, stdio: "inherit" });
|
|
64
|
+
child.once("error", () => reject(new Error("Secret child could not start")));
|
|
65
|
+
child.once("exit", (code) => resolve(code ?? 1));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async function runSecretsCommand(args) {
|
|
69
|
+
if (!args.length || args[0] === "--help" || args[0] === "help" || args.indexOf("--help") >= 0 && (args.indexOf("--") < 0 || args.indexOf("--help") < args.indexOf("--"))) {
|
|
70
|
+
console.log(`Usage: secrets export --capability private.json|- --format json|dotenv --output PATH
|
|
71
|
+
secrets exec --capability private.json|- --format json|dotenv -- executable args`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const [op, ...rest] = args;
|
|
75
|
+
const value = (flag) => {
|
|
76
|
+
const i = rest.indexOf(flag);
|
|
77
|
+
if (i < 0 || !rest[i + 1])
|
|
78
|
+
throw new Error(`Required ${flag}`);
|
|
79
|
+
return rest[i + 1];
|
|
80
|
+
};
|
|
81
|
+
const format = rest.includes("--format") ? value("--format") : "json";
|
|
82
|
+
if (format !== "json" && format !== "dotenv")
|
|
83
|
+
throw new Error("Invalid secret format");
|
|
84
|
+
if (op === "export")
|
|
85
|
+
return exportSecrets(value("--capability"), value("--output"), format);
|
|
86
|
+
if (op === "exec") {
|
|
87
|
+
const i = rest.indexOf("--");
|
|
88
|
+
if (i < 0)
|
|
89
|
+
throw new Error("Executable must follow --");
|
|
90
|
+
return execSecrets(value("--capability"), rest[i + 1] ?? "", rest.slice(i + 2), format);
|
|
91
|
+
}
|
|
92
|
+
throw new Error("Usage: secrets export|exec --capability private.json|- --format json|dotenv --output PATH | -- executable args");
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
execSecrets,
|
|
96
|
+
exportSecrets,
|
|
97
|
+
runSecretsCommand
|
|
98
|
+
};
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// cli/src/forward/connector.ts
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
import WebSocket from "ws";
|
|
4
|
+
|
|
5
|
+
// shared/forwarding.ts
|
|
6
|
+
var FORWARD = { path: "/api/forward/v1", maxPayload: 16384, window: 65536, maxStreams: 32, maxBuffered: 2097152, maxLeases: 128, maxScopeLeases: 8, maxTtl: 900000, heartbeatMs: 1e4, deadMs: 35000 };
|
|
7
|
+
function frame(type, id = 0, data = new Uint8Array) {
|
|
8
|
+
const out = new Uint8Array(5 + data.length);
|
|
9
|
+
out[0] = type;
|
|
10
|
+
new DataView(out.buffer).setUint32(1, id);
|
|
11
|
+
out.set(data, 5);
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function decode(bytes) {
|
|
15
|
+
if (bytes.length < 5 || bytes.length > FORWARD.maxPayload + 5)
|
|
16
|
+
throw new Error("invalid forwarding frame");
|
|
17
|
+
const type = bytes[0];
|
|
18
|
+
const id = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(1);
|
|
19
|
+
const data = bytes.subarray(5);
|
|
20
|
+
if (type < 1 /* Open */ || type > 8 /* Pong */ || (type === 2 /* Data */ ? !id || !data.length : type === 5 /* Credit */ ? !id || data.length !== 4 : data.length !== 0))
|
|
21
|
+
throw new Error("invalid forwarding frame");
|
|
22
|
+
if (type >= 6 /* Ready */ !== (id === 0))
|
|
23
|
+
throw new Error("invalid stream identity");
|
|
24
|
+
return { type, id, data };
|
|
25
|
+
}
|
|
26
|
+
function credit(id, n) {
|
|
27
|
+
const b = new Uint8Array(4);
|
|
28
|
+
new DataView(b.buffer).setUint32(0, n);
|
|
29
|
+
return frame(5 /* Credit */, id, b);
|
|
30
|
+
}
|
|
31
|
+
function sameScope(a, b) {
|
|
32
|
+
return a.browserId === b.browserId && a.tokenId === b.tokenId && a.sessionKey === b.sessionKey && a.instanceId === b.instanceId;
|
|
33
|
+
}
|
|
34
|
+
function loopback(value) {
|
|
35
|
+
if (value !== "127.0.0.1" && value !== "::1")
|
|
36
|
+
throw new Error("explicit loopback address required");
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
function port(value) {
|
|
40
|
+
if (!Number.isInteger(value) || Number(value) < 1 || Number(value) > 65535)
|
|
41
|
+
throw new Error("invalid TCP port");
|
|
42
|
+
return Number(value);
|
|
43
|
+
}
|
|
44
|
+
function validateCapability(c) {
|
|
45
|
+
if (!c || c.version !== 1 || typeof c.id !== "string" || !/^[0-9a-f-]{36}$/.test(c.id) || !["browser", "agent"].includes(c.role) || !c.headers || Object.keys(c.headers).length !== 1 || typeof c.headers.Authorization !== "string" || !/^Bearer [A-Za-z0-9_-]{43}$/.test(c.headers.Authorization) || !c.scope || Object.keys(c.scope).length !== 4 || ["browserId", "tokenId", "sessionKey", "instanceId"].some((k) => typeof c.scope[k] !== "string" || !c.scope[k] || c.scope[k].length > 512))
|
|
46
|
+
throw new Error("invalid forward capability");
|
|
47
|
+
const u = new URL(c.url);
|
|
48
|
+
if (u.protocol !== "wss:" || u.username || u.password || u.search || u.hash || u.pathname !== FORWARD.path)
|
|
49
|
+
throw new Error("forward requires credential-free WSS URL");
|
|
50
|
+
if (!Number.isFinite(c.expiresAt) || c.expiresAt <= Date.now() || c.expiresAt > Date.now() + FORWARD.maxTtl)
|
|
51
|
+
throw new Error("forward capability expired or too long");
|
|
52
|
+
loopback(c.browserHost);
|
|
53
|
+
loopback(c.targetHost);
|
|
54
|
+
port(c.browserPort);
|
|
55
|
+
port(c.targetPort);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// cli/src/forward/connector.ts
|
|
59
|
+
async function connectForward(cap) {
|
|
60
|
+
validateCapability(cap);
|
|
61
|
+
const ws = new WebSocket(cap.url, { headers: cap.headers, maxPayload: FORWARD.maxPayload + 5, perMessageDeflate: false, handshakeTimeout: 1e4 });
|
|
62
|
+
const streams = new Map;
|
|
63
|
+
let listener;
|
|
64
|
+
let ready = false;
|
|
65
|
+
let ended = false;
|
|
66
|
+
let lastError = null;
|
|
67
|
+
let nextId = 0;
|
|
68
|
+
let lastSeen = Date.now();
|
|
69
|
+
let resolveClosed;
|
|
70
|
+
const closed = new Promise((r) => resolveClosed = r);
|
|
71
|
+
let resolveOpen;
|
|
72
|
+
let rejectOpen;
|
|
73
|
+
const opened = new Promise((r, j) => {
|
|
74
|
+
resolveOpen = r;
|
|
75
|
+
rejectOpen = j;
|
|
76
|
+
});
|
|
77
|
+
function close(reason) {
|
|
78
|
+
if (ended)
|
|
79
|
+
return;
|
|
80
|
+
ended = true;
|
|
81
|
+
ready = false;
|
|
82
|
+
lastError = reason ?? null;
|
|
83
|
+
clearInterval(heartbeat);
|
|
84
|
+
clearTimeout(expiry);
|
|
85
|
+
listener?.close();
|
|
86
|
+
for (const s of streams.values())
|
|
87
|
+
s.socket.destroy();
|
|
88
|
+
streams.clear();
|
|
89
|
+
ws.terminate();
|
|
90
|
+
rejectOpen(new Error(reason ?? "forward closed"));
|
|
91
|
+
resolveClosed();
|
|
92
|
+
}
|
|
93
|
+
function send(bytes) {
|
|
94
|
+
if (ended || ws.readyState !== WebSocket.OPEN || ws.bufferedAmount + bytes.byteLength > FORWARD.maxBuffered) {
|
|
95
|
+
close("forward transport unavailable");
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
ws.send(bytes, { binary: true }, (error) => {
|
|
99
|
+
if (error)
|
|
100
|
+
close("forward transport failed");
|
|
101
|
+
});
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
function reset(id, notify = true) {
|
|
105
|
+
const s = streams.get(id);
|
|
106
|
+
if (!s)
|
|
107
|
+
return;
|
|
108
|
+
streams.delete(id);
|
|
109
|
+
s.socket.destroy();
|
|
110
|
+
if (notify)
|
|
111
|
+
send(frame(4 /* Reset */, id));
|
|
112
|
+
}
|
|
113
|
+
function pump(id) {
|
|
114
|
+
const s = streams.get(id);
|
|
115
|
+
if (!s)
|
|
116
|
+
return;
|
|
117
|
+
while (s.credit > 0) {
|
|
118
|
+
const n = Math.min(FORWARD.maxPayload, s.credit, s.socket.readableLength);
|
|
119
|
+
const b = s.socket.read(n);
|
|
120
|
+
if (!b)
|
|
121
|
+
break;
|
|
122
|
+
s.credit -= b.length;
|
|
123
|
+
if (!send(frame(2 /* Data */, id, b)))
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function attach(id, socket) {
|
|
128
|
+
socket.setNoDelay(true);
|
|
129
|
+
socket.setTimeout(120000, () => reset(id));
|
|
130
|
+
const s = { socket, credit: 0, outstanding: FORWARD.window, localFin: false, remoteFin: false };
|
|
131
|
+
streams.set(id, s);
|
|
132
|
+
socket.on("readable", () => pump(id));
|
|
133
|
+
socket.on("end", () => {
|
|
134
|
+
if (!streams.has(id))
|
|
135
|
+
return;
|
|
136
|
+
s.localFin = true;
|
|
137
|
+
send(frame(3 /* Fin */, id));
|
|
138
|
+
if (s.remoteFin)
|
|
139
|
+
streams.delete(id);
|
|
140
|
+
});
|
|
141
|
+
socket.on("error", () => reset(id));
|
|
142
|
+
socket.on("close", () => {
|
|
143
|
+
if (streams.has(id))
|
|
144
|
+
reset(id);
|
|
145
|
+
});
|
|
146
|
+
send(credit(id, FORWARD.window));
|
|
147
|
+
}
|
|
148
|
+
const heartbeat = setInterval(() => {
|
|
149
|
+
if (Date.now() - lastSeen > FORWARD.deadMs)
|
|
150
|
+
close("heartbeat expired");
|
|
151
|
+
else
|
|
152
|
+
send(frame(7 /* Ping */));
|
|
153
|
+
}, FORWARD.heartbeatMs);
|
|
154
|
+
heartbeat.unref();
|
|
155
|
+
const expiry = setTimeout(() => close("forward expired"), Math.max(1, cap.expiresAt - Date.now()));
|
|
156
|
+
expiry.unref();
|
|
157
|
+
ws.on("open", () => {
|
|
158
|
+
if (cap.role === "agent") {
|
|
159
|
+
resolveOpen();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
listener = net.createServer({ allowHalfOpen: true, pauseOnConnect: true }, (socket) => {
|
|
163
|
+
if (!ready || streams.size >= FORWARD.maxStreams || nextId === 4294967295) {
|
|
164
|
+
socket.destroy();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const id = ++nextId;
|
|
168
|
+
send(frame(1 /* Open */, id));
|
|
169
|
+
attach(id, socket);
|
|
170
|
+
});
|
|
171
|
+
listener.on("error", () => close("browser loopback port unavailable"));
|
|
172
|
+
listener.listen({ host: cap.browserHost, port: cap.browserPort, exclusive: true }, () => resolveOpen());
|
|
173
|
+
});
|
|
174
|
+
ws.on("error", () => close("forward connection failed"));
|
|
175
|
+
ws.on("close", () => close("forward disconnected"));
|
|
176
|
+
ws.on("message", (raw, isBinary) => {
|
|
177
|
+
try {
|
|
178
|
+
if (!isBinary)
|
|
179
|
+
throw new Error("text frame forbidden");
|
|
180
|
+
const bytes = raw instanceof ArrayBuffer ? new Uint8Array(raw) : Array.isArray(raw) ? Buffer.concat(raw) : raw;
|
|
181
|
+
const f = decode(bytes);
|
|
182
|
+
lastSeen = Date.now();
|
|
183
|
+
if (f.type === 7 /* Ping */) {
|
|
184
|
+
send(frame(8 /* Pong */));
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (f.type === 8 /* Pong */)
|
|
188
|
+
return;
|
|
189
|
+
if (f.type === 6 /* Ready */) {
|
|
190
|
+
ready = true;
|
|
191
|
+
if (cap.role === "browser")
|
|
192
|
+
send(frame(6 /* Ready */));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (f.type === 1 /* Open */) {
|
|
196
|
+
if (cap.role !== "agent" || streams.has(f.id) || streams.size >= FORWARD.maxStreams)
|
|
197
|
+
throw new Error("invalid open");
|
|
198
|
+
const socket = net.createConnection({ host: cap.targetHost, port: cap.targetPort, allowHalfOpen: true });
|
|
199
|
+
attach(f.id, socket);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const s = streams.get(f.id);
|
|
203
|
+
if (!s) {
|
|
204
|
+
if (f.type === 4 /* Reset */ || f.type === 5 /* Credit */)
|
|
205
|
+
return;
|
|
206
|
+
throw new Error("unknown stream");
|
|
207
|
+
}
|
|
208
|
+
if (f.type === 4 /* Reset */) {
|
|
209
|
+
reset(f.id, false);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (f.type === 5 /* Credit */) {
|
|
213
|
+
const n = new DataView(f.data.buffer, f.data.byteOffset, 4).getUint32(0);
|
|
214
|
+
if (!n || s.credit + n > FORWARD.window)
|
|
215
|
+
throw new Error("invalid credit");
|
|
216
|
+
s.credit += n;
|
|
217
|
+
pump(f.id);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (f.type === 2 /* Data */) {
|
|
221
|
+
if (s.remoteFin || f.data.length > s.outstanding)
|
|
222
|
+
throw new Error("flow budget exceeded");
|
|
223
|
+
s.outstanding -= f.data.length;
|
|
224
|
+
const n = f.data.length;
|
|
225
|
+
s.socket.write(f.data, (error) => {
|
|
226
|
+
if (error) {
|
|
227
|
+
reset(f.id);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (streams.has(f.id)) {
|
|
231
|
+
s.outstanding += n;
|
|
232
|
+
send(credit(f.id, n));
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (f.type === 3 /* Fin */) {
|
|
238
|
+
if (s.remoteFin)
|
|
239
|
+
throw new Error("duplicate EOF");
|
|
240
|
+
s.remoteFin = true;
|
|
241
|
+
s.socket.end();
|
|
242
|
+
if (s.localFin)
|
|
243
|
+
streams.delete(f.id);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
throw new Error("invalid frame");
|
|
247
|
+
} catch {
|
|
248
|
+
close("forward protocol failure");
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
await opened;
|
|
252
|
+
return { closed, close: () => close(), status: () => ({ ready, activeConnections: streams.size, lastError }) };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export { FORWARD, sameScope, connectForward };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// cli/src/data/contracts.ts
|
|
2
|
+
var LIMITS = { file: 256 * 1024 * 1024, session: 512 * 1024 * 1024, browser: 1024 * 1024 * 1024, secret: 1024 * 1024, value: 65536, count: 128, ttl: 300000, duration: 120000 };
|
|
3
|
+
var sameScope = (a, b) => a.browserId === b.browserId && a.tokenId === b.tokenId && a.sessionKey === b.sessionKey && a.instanceId === b.instanceId;
|
|
4
|
+
var validName = (n) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(n);
|
|
5
|
+
function secureDataUrl(u, allowInsecureLoopback = false) {
|
|
6
|
+
return u.protocol === "https:" || allowInsecureLoopback && u.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(u.hostname);
|
|
7
|
+
}
|
|
8
|
+
function validateGrant(g, origin, resourceId, allowInsecureLoopback = false) {
|
|
9
|
+
const u = new URL(g.url);
|
|
10
|
+
if (!secureDataUrl(u, allowInsecureLoopback) || u.origin !== new URL(origin).origin || u.username || u.password || u.search || u.hash || !/^\/api\/native-data\/[a-zA-Z0-9-]+\/runtime$/.test(u.pathname) || g.resourceId !== resourceId || g.expiresAt <= Date.now() || g.expiresAt > Date.now() + 65000 || !/^Bearer [A-Za-z0-9_-]{43}$/.test(g.headers.Authorization) || !Number.isSafeInteger(g.limits.maxBytes) || g.limits.maxBytes < 0 || g.limits.maxBytes > LIMITS.file)
|
|
11
|
+
throw new Error("Invalid data grant");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// cli/src/data/serialization.ts
|
|
15
|
+
function validateValues(input) {
|
|
16
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
17
|
+
throw new Error("Invalid secret document");
|
|
18
|
+
const out = Object.create(null);
|
|
19
|
+
for (const [k, v] of Object.entries(input)) {
|
|
20
|
+
if (!validName(k) || typeof v !== "string")
|
|
21
|
+
throw new Error("Invalid secret document");
|
|
22
|
+
out[k] = v;
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function serializeSecrets(values, format) {
|
|
27
|
+
validateValues(values);
|
|
28
|
+
return format === "json" ? JSON.stringify(values) : Object.entries(values).map(([k, v]) => `${k}=${JSON.stringify(v)}`).join(`
|
|
29
|
+
`) + `
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
function parseSecrets(text, format) {
|
|
33
|
+
if (format === "json")
|
|
34
|
+
return validateValues(JSON.parse(text));
|
|
35
|
+
const result = Object.create(null);
|
|
36
|
+
for (const line of text.split(`
|
|
37
|
+
`)) {
|
|
38
|
+
if (!line)
|
|
39
|
+
continue;
|
|
40
|
+
const index = line.indexOf("=");
|
|
41
|
+
const key = line.slice(0, index);
|
|
42
|
+
if (index < 1 || !validName(key) || Object.hasOwn(result, key))
|
|
43
|
+
throw new Error("Invalid dotenv document");
|
|
44
|
+
const value = JSON.parse(line.slice(index + 1));
|
|
45
|
+
if (typeof value !== "string")
|
|
46
|
+
throw new Error("Invalid dotenv value");
|
|
47
|
+
result[key] = value;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { LIMITS, sameScope, validName, secureDataUrl, validateGrant, serializeSecrets, parseSecrets };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
connectForward
|
|
3
|
+
} from "./chunk-cy1a8r79.js";
|
|
4
|
+
import"./chunk-1sjcwr28.js";
|
|
5
|
+
|
|
6
|
+
// cli/src/agent/forward.ts
|
|
7
|
+
import { constants } from "node:fs";
|
|
8
|
+
import { open } from "node:fs/promises";
|
|
9
|
+
async function runForward(args) {
|
|
10
|
+
if (args.includes("--help")) {
|
|
11
|
+
console.log(`browser-mcp forward --capability-file <private-json-file> | --stdin
|
|
12
|
+
One lease, no reconnect. Capability bytes are sensitive; never pass a token in argv.`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
let text;
|
|
16
|
+
if (args.length === 1 && args[0] === "--stdin") {
|
|
17
|
+
const parts = [];
|
|
18
|
+
let size = 0;
|
|
19
|
+
for await (const chunk of process.stdin) {
|
|
20
|
+
const b = Buffer.from(chunk);
|
|
21
|
+
size += b.length;
|
|
22
|
+
if (size > 16384)
|
|
23
|
+
throw new Error("capability too large");
|
|
24
|
+
parts.push(b);
|
|
25
|
+
}
|
|
26
|
+
text = Buffer.concat(parts).toString("utf8");
|
|
27
|
+
} else if (args.length === 2 && args[0] === "--capability-file") {
|
|
28
|
+
const handle = await open(args[1], constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
29
|
+
try {
|
|
30
|
+
const stat = await handle.stat();
|
|
31
|
+
if (!stat.isFile() || stat.size > 16384 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.()))
|
|
32
|
+
throw new Error("capability file must be private, owned, regular, <=16 KiB");
|
|
33
|
+
text = await handle.readFile("utf8");
|
|
34
|
+
} finally {
|
|
35
|
+
await handle.close();
|
|
36
|
+
}
|
|
37
|
+
} else
|
|
38
|
+
throw new Error("use forward --capability-file <path> or --stdin; tokens in arguments are not accepted");
|
|
39
|
+
let cap;
|
|
40
|
+
try {
|
|
41
|
+
cap = JSON.parse(text);
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error("invalid capability JSON");
|
|
44
|
+
}
|
|
45
|
+
if (cap.role !== "agent")
|
|
46
|
+
throw new Error("agent capability required");
|
|
47
|
+
const connection = await connectForward(cap);
|
|
48
|
+
const stop = () => connection.close();
|
|
49
|
+
process.once("SIGINT", stop);
|
|
50
|
+
process.once("SIGTERM", stop);
|
|
51
|
+
try {
|
|
52
|
+
await connection.closed;
|
|
53
|
+
} finally {
|
|
54
|
+
process.removeListener("SIGINT", stop);
|
|
55
|
+
process.removeListener("SIGTERM", stop);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
runForward
|
|
60
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
capabilityFetch,
|
|
3
|
+
exclusiveOutput,
|
|
4
|
+
readCapability
|
|
5
|
+
} from "./chunk-2hzkme2s.js";
|
|
6
|
+
import"./chunk-1sjcwr28.js";
|
|
7
|
+
|
|
8
|
+
// cli/src/agent/files.ts
|
|
9
|
+
import { open, constants, unlink } from "node:fs/promises";
|
|
10
|
+
import { Readable, Transform } from "node:stream";
|
|
11
|
+
import { pipeline } from "node:stream/promises";
|
|
12
|
+
async function uploadFile(capabilityPath, inputPath) {
|
|
13
|
+
const cap = await readCapability(capabilityPath);
|
|
14
|
+
if (cap.method !== "PUT")
|
|
15
|
+
throw new Error("Upload capability required");
|
|
16
|
+
const file = await open(inputPath, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
17
|
+
try {
|
|
18
|
+
const stat = await file.stat();
|
|
19
|
+
if (!stat.isFile() || stat.size !== cap.limits.maxBytes)
|
|
20
|
+
throw new Error("Input size mismatch");
|
|
21
|
+
await capabilityFetch(cap, file.createReadStream({ autoClose: false }));
|
|
22
|
+
return { uploaded: true, bytes: stat.size };
|
|
23
|
+
} finally {
|
|
24
|
+
await file.close();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function downloadFile(capabilityPath, outputPath) {
|
|
28
|
+
const cap = await readCapability(capabilityPath);
|
|
29
|
+
if (cap.method !== "GET")
|
|
30
|
+
throw new Error("Download capability required");
|
|
31
|
+
const file = await exclusiveOutput(outputPath);
|
|
32
|
+
let success = false;
|
|
33
|
+
try {
|
|
34
|
+
const response = await capabilityFetch(cap);
|
|
35
|
+
if (!response.body)
|
|
36
|
+
throw new Error("Missing transfer body");
|
|
37
|
+
let bytes = 0;
|
|
38
|
+
const limiter = new Transform({ transform(chunk, _encoding, callback) {
|
|
39
|
+
bytes += chunk.length;
|
|
40
|
+
if (bytes > cap.limits.maxBytes)
|
|
41
|
+
return callback(new Error("Size exceeded"));
|
|
42
|
+
callback(null, chunk);
|
|
43
|
+
} });
|
|
44
|
+
await pipeline(Readable.fromWeb(response.body), limiter, file.createWriteStream({ autoClose: false }));
|
|
45
|
+
await file.sync();
|
|
46
|
+
success = true;
|
|
47
|
+
return { downloaded: true, bytes };
|
|
48
|
+
} finally {
|
|
49
|
+
await file.close();
|
|
50
|
+
if (!success)
|
|
51
|
+
await unlink(outputPath).catch(() => {});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function runFilesCommand(args) {
|
|
55
|
+
if (!args.length || args.includes("--help") || args[0] === "help") {
|
|
56
|
+
console.log(`Usage: files upload --capability private.json|- --input PATH
|
|
57
|
+
files download --capability private.json|- --output PATH`);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const [operation, ...rest] = args;
|
|
61
|
+
const value = (flag) => {
|
|
62
|
+
const i = rest.indexOf(flag);
|
|
63
|
+
if (i < 0 || !rest[i + 1])
|
|
64
|
+
throw new Error(`Required ${flag}`);
|
|
65
|
+
return rest[i + 1];
|
|
66
|
+
};
|
|
67
|
+
if (operation === "upload")
|
|
68
|
+
return uploadFile(value("--capability"), value("--input"));
|
|
69
|
+
if (operation === "download")
|
|
70
|
+
return downloadFile(value("--capability"), value("--output"));
|
|
71
|
+
throw new Error("Usage: files upload|download --capability private.json|- --input|--output PATH");
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
downloadFile,
|
|
75
|
+
runFilesCommand,
|
|
76
|
+
uploadFile
|
|
77
|
+
};
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
__require
|
|
4
|
+
} from "./chunk-1sjcwr28.js";
|
|
5
|
+
|
|
6
|
+
// cli/src/main.ts
|
|
7
|
+
var args = process.argv.slice(2);
|
|
8
|
+
var command = args.shift();
|
|
9
|
+
async function main() {
|
|
10
|
+
if (!command || command === "--help" || command === "help") {
|
|
11
|
+
console.log(`@ricsam/browser-mcp 0.3.2
|
|
12
|
+
Usage: browser-mcp browser start|status|pause|resume|stop [options]
|
|
13
|
+
browser-mcp files upload|download [options]
|
|
14
|
+
browser-mcp secrets export|exec [options]
|
|
15
|
+
browser-mcp forward --capability-file PATH
|
|
16
|
+
Browser dependencies load only for browser commands. No automatic Chrome downloads.`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (command === "--version") {
|
|
20
|
+
console.log("0.3.2");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (command === "browser")
|
|
24
|
+
return (await import("./chunk-3kc8te5e.js")).runBrowser(args);
|
|
25
|
+
if (command === "files")
|
|
26
|
+
return (await import("./chunk-xgk37h9a.js")).runFilesCommand(args);
|
|
27
|
+
if (command === "secrets")
|
|
28
|
+
return (await import("./chunk-847prxk6.js")).runSecretsCommand(args);
|
|
29
|
+
if (command === "forward")
|
|
30
|
+
return (await import("./chunk-swy0qtx0.js")).runForward(args);
|
|
31
|
+
throw new Error("Unknown command. Use --help.");
|
|
32
|
+
}
|
|
33
|
+
main().catch((error) => {
|
|
34
|
+
console.error(error instanceof Error ? error.message : "Command failed");
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ricsam/browser-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Dedicated native Chrome companion for Browser MCP",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": { "browser-mcp": "dist/main.js" },
|
|
7
|
+
"engines": { "node": ">=22.14.0" },
|
|
8
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
9
|
+
"repository": { "type": "git", "url": "https://github.com/ricsam/browser-mcp" },
|
|
10
|
+
"files": ["dist/", "README.md", "LICENSE", "THIRD_PARTY_NOTICES.md"],
|
|
11
|
+
"dependencies": { "playwright-core": "1.63.0", "ws": "8.22.0" },
|
|
12
|
+
"devDependencies": { "@types/ws": "8.18.1", "@types/node": "22.15.30", "typescript": "5.9.3" }
|
|
10
13
|
}
|