mcp-browser-dev-tools 0.0.1 → 0.0.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 +21 -0
- package/README.md +102 -140
- package/package.json +6 -1
- package/src/app.mjs +31 -2
- package/src/browser-adapter.mjs +57 -0
- package/src/browser-launch-service.mjs +515 -0
- package/src/cdp-client.mjs +420 -5
- package/src/cli.mjs +69 -204
- package/src/config.mjs +26 -1
- package/src/firefox-bidi-client.mjs +251 -16
- package/src/index.mjs +3 -0
- package/src/logger.mjs +53 -0
- package/src/mcp-server.mjs +709 -12
- package/src/page-context.mjs +217 -0
- package/src/serve-bootstrap.mjs +290 -0
- package/src/session-events.mjs +83 -0
- package/src/tcp-relay.mjs +7 -1
- package/src/wait-for.mjs +201 -0
package/src/cli.mjs
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { spawn } from "node:child_process";
|
|
3
|
-
import { mkdir, readFile } from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
2
|
import process from "node:process";
|
|
6
3
|
import { pathToFileURL } from "node:url";
|
|
7
4
|
|
|
8
5
|
import { createBrowserDevToolsApp } from "./app.mjs";
|
|
9
|
-
import {
|
|
10
|
-
buildBrowserLaunchArgs,
|
|
11
|
-
findBrowserExecutable,
|
|
12
|
-
} from "./browser-launcher.mjs";
|
|
13
|
-
import { isLoopbackHost, loadConfig } from "./config.mjs";
|
|
6
|
+
import { loadConfig, loadLoggingConfig } from "./config.mjs";
|
|
14
7
|
import { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|
|
8
|
+
import { createLogger } from "./logger.mjs";
|
|
15
9
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
10
|
+
import { bootstrapWslRelay } from "./serve-bootstrap.mjs";
|
|
16
11
|
import { resolveRelayOptions, startTcpRelay } from "./tcp-relay.mjs";
|
|
12
|
+
import { launchBrowser } from "./browser-launch-service.mjs";
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
parseFirefoxBidiServerInfo,
|
|
16
|
+
resolveFirefoxDoctorEndpoint,
|
|
17
|
+
} from "./browser-launch-service.mjs";
|
|
17
18
|
|
|
18
19
|
export function parseCliArgs(argv) {
|
|
19
20
|
const args = [...argv];
|
|
@@ -66,6 +67,11 @@ function printUsage() {
|
|
|
66
67
|
" --help Print usage information",
|
|
67
68
|
" --version Print the package version",
|
|
68
69
|
"",
|
|
70
|
+
"Serve options:",
|
|
71
|
+
" --bootstrap-wsl-relay in WSL, start a Windows-side CDP relay and point the broker at it",
|
|
72
|
+
" --bridge-port <port> relay port exposed back to WSL (defaults to the CDP target port)",
|
|
73
|
+
" --target-port <port> Windows browser CDP target port for the relay",
|
|
74
|
+
"",
|
|
69
75
|
"Open options:",
|
|
70
76
|
" --family <name> chromium, edge, or firefox",
|
|
71
77
|
" --port <port> remote debugging port",
|
|
@@ -89,111 +95,39 @@ function printVersion() {
|
|
|
89
95
|
process.stdout.write(`${PACKAGE_VERSION}\n`);
|
|
90
96
|
}
|
|
91
97
|
|
|
92
|
-
function
|
|
93
|
-
|
|
94
|
-
setTimeout(resolve, ms);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const FIREFOX_BIDI_SERVER_FILENAME = "WebDriverBiDiServer.json";
|
|
98
|
+
async function runServe(options = {}) {
|
|
99
|
+
let relayBootstrap = null;
|
|
99
100
|
|
|
100
|
-
function isHttpLikeUrl(value) {
|
|
101
101
|
try {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return host;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function normalizeFirefoxDoctorPath(value) {
|
|
119
|
-
if (typeof value !== "string") {
|
|
120
|
-
return "/";
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const trimmed = value.trim();
|
|
124
|
-
if (!trimmed) {
|
|
125
|
-
return "/";
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function parseFirefoxBidiServerInfo(contents) {
|
|
132
|
-
const parsed = JSON.parse(contents);
|
|
133
|
-
if (!parsed || typeof parsed !== "object") {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const directUrl =
|
|
138
|
-
parsed.webSocketUrl ??
|
|
139
|
-
parsed.websocketUrl ??
|
|
140
|
-
parsed.wsUrl ??
|
|
141
|
-
parsed.ws_url ??
|
|
142
|
-
null;
|
|
143
|
-
if (typeof directUrl === "string" && directUrl.trim()) {
|
|
144
|
-
return directUrl.trim().replace(/\/+$/, "");
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const port =
|
|
148
|
-
parsed.ws_port ?? parsed.port ?? parsed.remoteDebuggingPort ?? null;
|
|
149
|
-
if (!Number.isInteger(port) && !/^\d+$/.test(String(port ?? "").trim())) {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const host = normalizeFirefoxDoctorHost(
|
|
154
|
-
parsed.ws_host ?? parsed.host ?? parsed.hostname ?? "127.0.0.1",
|
|
155
|
-
);
|
|
156
|
-
const protocol =
|
|
157
|
-
parsed.protocol === "https" || parsed.protocol === "wss" ? "wss:" : "ws:";
|
|
158
|
-
const endpoint = new URL(`${protocol}//${host}`);
|
|
159
|
-
endpoint.port = String(port);
|
|
160
|
-
endpoint.pathname = normalizeFirefoxDoctorPath(
|
|
161
|
-
parsed.path ?? parsed.ws_path ?? "/",
|
|
162
|
-
);
|
|
163
|
-
return endpoint.toString().replace(/\/+$/, "");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export async function resolveFirefoxDoctorEndpoint({
|
|
167
|
-
userDataDir,
|
|
168
|
-
fallbackPort,
|
|
169
|
-
}) {
|
|
170
|
-
if (!userDataDir) {
|
|
171
|
-
return `ws://127.0.0.1:${fallbackPort}`;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
const serverInfo = await readFile(
|
|
176
|
-
path.join(userDataDir, FIREFOX_BIDI_SERVER_FILENAME),
|
|
177
|
-
"utf8",
|
|
178
|
-
);
|
|
179
|
-
return (
|
|
180
|
-
parseFirefoxBidiServerInfo(serverInfo) ?? `ws://127.0.0.1:${fallbackPort}`
|
|
181
|
-
);
|
|
182
|
-
} catch (error) {
|
|
183
|
-
if (error?.code === "ENOENT") {
|
|
184
|
-
return `ws://127.0.0.1:${fallbackPort}`;
|
|
102
|
+
let env = process.env;
|
|
103
|
+
if (options["bootstrap-wsl-relay"] === true) {
|
|
104
|
+
const config = loadConfig(env);
|
|
105
|
+
relayBootstrap = await bootstrapWslRelay({
|
|
106
|
+
config,
|
|
107
|
+
relayListenPort: options["bridge-port"],
|
|
108
|
+
relayTargetPort: options["target-port"],
|
|
109
|
+
env,
|
|
110
|
+
});
|
|
111
|
+
env = relayBootstrap.env;
|
|
112
|
+
process.stderr.write(
|
|
113
|
+
`bootstrapped WSL relay: ${relayBootstrap.bridge.cdpBaseUrl} -> 127.0.0.1:${relayBootstrap.bridge.targetPort}\n`,
|
|
114
|
+
);
|
|
185
115
|
}
|
|
186
116
|
|
|
187
|
-
|
|
117
|
+
const app = createBrowserDevToolsApp({
|
|
118
|
+
env,
|
|
119
|
+
extraCloseHandlers: relayBootstrap
|
|
120
|
+
? [() => relayBootstrap.close()]
|
|
121
|
+
: undefined,
|
|
122
|
+
});
|
|
123
|
+
app.installSignalHandlers();
|
|
124
|
+
app.start();
|
|
125
|
+
} catch (error) {
|
|
126
|
+
await relayBootstrap?.close().catch(() => {});
|
|
127
|
+
throw error;
|
|
188
128
|
}
|
|
189
129
|
}
|
|
190
130
|
|
|
191
|
-
async function runServe() {
|
|
192
|
-
const app = createBrowserDevToolsApp();
|
|
193
|
-
app.installSignalHandlers();
|
|
194
|
-
app.start();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
131
|
async function runDoctor(options) {
|
|
198
132
|
const report = await collectDoctorReport({
|
|
199
133
|
url: typeof options.url === "string" ? options.url : null,
|
|
@@ -214,122 +148,53 @@ async function runOpen(positional, options) {
|
|
|
214
148
|
}
|
|
215
149
|
|
|
216
150
|
const config = loadConfig();
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
(family === "chromium" || family === "edge") &&
|
|
221
|
-
typeof options.address === "string"
|
|
222
|
-
? options.address.trim()
|
|
223
|
-
: null;
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
requestedAddress &&
|
|
227
|
-
!isLoopbackHost(requestedAddress) &&
|
|
228
|
-
!config.allowRemoteEndpoints
|
|
229
|
-
) {
|
|
230
|
-
throw new Error(
|
|
231
|
-
"--address must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1",
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const executable = await findBrowserExecutable(family);
|
|
236
|
-
if (!executable) {
|
|
237
|
-
throw new Error(`No local ${family} browser executable found`);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const defaultPort =
|
|
241
|
-
family === "firefox"
|
|
242
|
-
? new URL(config.firefoxBidiWsUrl).port || "9222"
|
|
243
|
-
: new URL(config.cdpBaseUrl).port || "9222";
|
|
244
|
-
const resolvedPort =
|
|
245
|
-
typeof options.port === "string" ? options.port : defaultPort;
|
|
246
|
-
const userDataDir =
|
|
247
|
-
typeof options["user-data-dir"] === "string"
|
|
248
|
-
? options["user-data-dir"]
|
|
249
|
-
: undefined;
|
|
250
|
-
|
|
251
|
-
if (family === "firefox" && userDataDir) {
|
|
252
|
-
await mkdir(userDataDir, { recursive: true });
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const args = buildBrowserLaunchArgs({
|
|
256
|
-
family,
|
|
151
|
+
const launch = await launchBrowser({
|
|
152
|
+
config,
|
|
153
|
+
browserFamily: typeof options.family === "string" ? options.family : null,
|
|
257
154
|
url,
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
userDataDir,
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const child = spawn(executable, args, {
|
|
264
|
-
detached: true,
|
|
265
|
-
stdio: "ignore",
|
|
155
|
+
port: options.port,
|
|
156
|
+
address: options.address,
|
|
157
|
+
userDataDir: options["user-data-dir"],
|
|
158
|
+
waitMs: options["wait-ms"],
|
|
159
|
+
skipDoctor: options["no-doctor"] === true,
|
|
266
160
|
});
|
|
267
|
-
child.unref();
|
|
268
161
|
|
|
269
162
|
process.stdout.write(
|
|
270
|
-
`launched ${
|
|
163
|
+
`launched ${launch.browserFamily} browser: ${launch.executable} ${launch.args.join(" ")}\n`,
|
|
271
164
|
);
|
|
272
165
|
|
|
273
|
-
if (
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const waitMs =
|
|
278
|
-
typeof options["wait-ms"] === "string"
|
|
279
|
-
? Number.parseInt(options["wait-ms"], 10)
|
|
280
|
-
: 5000;
|
|
281
|
-
const timeoutMs = Number.isInteger(waitMs) && waitMs >= 0 ? waitMs : 5000;
|
|
282
|
-
const startedAt = Date.now();
|
|
283
|
-
const doctorEnv = {
|
|
284
|
-
...process.env,
|
|
285
|
-
MCP_BROWSER_FAMILY: family,
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
if (family === "firefox") {
|
|
289
|
-
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
290
|
-
userDataDir,
|
|
291
|
-
fallbackPort: resolvedPort,
|
|
292
|
-
});
|
|
293
|
-
} else {
|
|
294
|
-
const address = requestedAddress || "127.0.0.1";
|
|
295
|
-
doctorEnv.CDP_BASE_URL = `http://${address}:${resolvedPort}`;
|
|
166
|
+
if (launch.profileStrategy === "temporary" && launch.userDataDir) {
|
|
167
|
+
process.stdout.write(
|
|
168
|
+
`using temporary browser profile: ${launch.userDataDir}\n`,
|
|
169
|
+
);
|
|
296
170
|
}
|
|
297
171
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
url: isHttpLikeUrl(url) ? url : null,
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
while (
|
|
304
|
-
!report.browserStatus.available &&
|
|
305
|
-
Date.now() - startedAt < timeoutMs
|
|
306
|
-
) {
|
|
307
|
-
await sleep(250);
|
|
308
|
-
if (family === "firefox") {
|
|
309
|
-
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
310
|
-
userDataDir,
|
|
311
|
-
fallbackPort: resolvedPort,
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
report = await collectDoctorReport({
|
|
315
|
-
env: doctorEnv,
|
|
316
|
-
url: isHttpLikeUrl(url) ? url : null,
|
|
317
|
-
});
|
|
172
|
+
if (!launch.doctorReport) {
|
|
173
|
+
return;
|
|
318
174
|
}
|
|
319
175
|
|
|
320
|
-
process.stdout.write(`${renderDoctorReport(
|
|
176
|
+
process.stdout.write(`${renderDoctorReport(launch.doctorReport)}\n`);
|
|
321
177
|
|
|
322
|
-
if (!
|
|
178
|
+
if (!launch.doctorReport.browserStatus.available) {
|
|
323
179
|
process.exitCode = 1;
|
|
324
180
|
}
|
|
325
181
|
}
|
|
326
182
|
|
|
327
183
|
async function runRelay(options) {
|
|
184
|
+
const logging = loadLoggingConfig(process.env);
|
|
185
|
+
const logger = createLogger({
|
|
186
|
+
level: logging.logLevel,
|
|
187
|
+
output: process.stderr,
|
|
188
|
+
name: PACKAGE_NAME,
|
|
189
|
+
});
|
|
328
190
|
const relayOptions = resolveRelayOptions(options);
|
|
329
|
-
const relay = await startTcpRelay(
|
|
191
|
+
const relay = await startTcpRelay({
|
|
192
|
+
...relayOptions,
|
|
193
|
+
logger,
|
|
194
|
+
});
|
|
330
195
|
|
|
331
196
|
const close = async (signal) => {
|
|
332
|
-
|
|
197
|
+
logger.info(`relay shutting down after ${signal}`);
|
|
333
198
|
await relay.close();
|
|
334
199
|
};
|
|
335
200
|
|
|
@@ -367,7 +232,7 @@ export async function runCli(argv = process.argv.slice(2)) {
|
|
|
367
232
|
|
|
368
233
|
switch (command) {
|
|
369
234
|
case "serve":
|
|
370
|
-
await runServe();
|
|
235
|
+
await runServe(options);
|
|
371
236
|
return;
|
|
372
237
|
case "doctor":
|
|
373
238
|
await runDoctor(options);
|
package/src/config.mjs
CHANGED
|
@@ -2,7 +2,8 @@ export const DEFAULT_CDP_BASE_URL = "http://127.0.0.1:9222";
|
|
|
2
2
|
export const DEFAULT_FIREFOX_BIDI_WS_URL = "ws://127.0.0.1:9222";
|
|
3
3
|
export const DEFAULT_EVENT_BUFFER_SIZE = 200;
|
|
4
4
|
export const DEFAULT_PROTOCOL_VERSION = "2024-11-05";
|
|
5
|
-
export const DEFAULT_BROWSER_FAMILY = "
|
|
5
|
+
export const DEFAULT_BROWSER_FAMILY = "auto";
|
|
6
|
+
export const DEFAULT_LOG_LEVEL = "error";
|
|
6
7
|
export const CDP_BROWSER_FAMILIES = ["chromium", "edge"];
|
|
7
8
|
|
|
8
9
|
export function isLoopbackHost(value) {
|
|
@@ -59,11 +60,33 @@ export function parseBrowserFamily(value) {
|
|
|
59
60
|
return DEFAULT_BROWSER_FAMILY;
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
export function parseLogLevel(value) {
|
|
64
|
+
const normalized = value?.trim().toLowerCase();
|
|
65
|
+
if (
|
|
66
|
+
normalized === "error" ||
|
|
67
|
+
normalized === "warn" ||
|
|
68
|
+
normalized === "info" ||
|
|
69
|
+
normalized === "debug"
|
|
70
|
+
) {
|
|
71
|
+
return normalized;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return DEFAULT_LOG_LEVEL;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function loadLoggingConfig(env = process.env) {
|
|
78
|
+
return {
|
|
79
|
+
logLevel: parseLogLevel(env.MCP_BROWSER_LOG_LEVEL),
|
|
80
|
+
debugStdio: isTruthyFlag(env.MCP_BROWSER_DEBUG_STDIO),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
62
84
|
export function loadConfig(env = process.env) {
|
|
63
85
|
const browserFamily = parseBrowserFamily(env.MCP_BROWSER_FAMILY);
|
|
64
86
|
const allowRemoteEndpoints = allowsRemoteEndpoints(env);
|
|
65
87
|
const cdpBaseUrl = normalizeBaseUrl(env.CDP_BASE_URL);
|
|
66
88
|
const firefoxBidiWsUrl = normalizeWebSocketUrl(env.FIREFOX_BIDI_WS_URL);
|
|
89
|
+
const logging = loadLoggingConfig(env);
|
|
67
90
|
const endpointsToValidate =
|
|
68
91
|
browserFamily === "firefox"
|
|
69
92
|
? [["FIREFOX_BIDI_WS_URL", firefoxBidiWsUrl]]
|
|
@@ -95,6 +118,8 @@ export function loadConfig(env = process.env) {
|
|
|
95
118
|
env.MCP_BROWSER_EVENT_BUFFER_SIZE,
|
|
96
119
|
DEFAULT_EVENT_BUFFER_SIZE,
|
|
97
120
|
),
|
|
121
|
+
logLevel: logging.logLevel,
|
|
122
|
+
debugStdio: logging.debugStdio,
|
|
98
123
|
protocolVersion:
|
|
99
124
|
env.MCP_PROTOCOL_VERSION?.trim() || DEFAULT_PROTOCOL_VERSION,
|
|
100
125
|
};
|