mcp-browser-dev-tools 0.0.2-beta.0 → 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/README.md +93 -139
- package/package.json +3 -1
- package/src/app.mjs +22 -1
- package/src/browser-adapter.mjs +27 -0
- package/src/browser-launch-service.mjs +515 -0
- package/src/cdp-client.mjs +261 -5
- package/src/cli.mjs +57 -202
- package/src/config.mjs +1 -1
- package/src/firefox-bidi-client.mjs +180 -16
- package/src/mcp-server.mjs +553 -0
- package/src/page-context.mjs +217 -0
- package/src/serve-bootstrap.mjs +290 -0
- package/src/session-events.mjs +83 -0
package/src/cdp-client.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
+
exportHarLikeSummary,
|
|
2
3
|
filterConsoleMessages,
|
|
3
4
|
summarizeNetworkRequests,
|
|
4
5
|
} from "./session-events.mjs";
|
|
6
|
+
import { DEFAULT_CDP_BASE_URL } from "./config.mjs";
|
|
5
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
6
8
|
import { buildPageContextExpression } from "./page-context.mjs";
|
|
7
9
|
|
|
@@ -37,6 +39,36 @@ function cdpBrowserFamily(config) {
|
|
|
37
39
|
return config.browserFamily === "edge" ? "edge" : "chromium";
|
|
38
40
|
}
|
|
39
41
|
|
|
42
|
+
function buildDiscoveryBaseUrls(configuredBaseUrl) {
|
|
43
|
+
const configured = new URL(configuredBaseUrl);
|
|
44
|
+
const candidatePorts = [
|
|
45
|
+
configured.port || "9222",
|
|
46
|
+
"9223",
|
|
47
|
+
"9224",
|
|
48
|
+
"9225",
|
|
49
|
+
"9226",
|
|
50
|
+
];
|
|
51
|
+
const seen = new Set();
|
|
52
|
+
const candidates = [];
|
|
53
|
+
|
|
54
|
+
for (const port of candidatePorts) {
|
|
55
|
+
const candidate = new URL(configured.toString());
|
|
56
|
+
candidate.port = port;
|
|
57
|
+
candidate.pathname = "/";
|
|
58
|
+
candidate.search = "";
|
|
59
|
+
candidate.hash = "";
|
|
60
|
+
const normalized = candidate.toString().replace(/\/+$/, "");
|
|
61
|
+
if (seen.has(normalized)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
seen.add(normalized);
|
|
66
|
+
candidates.push(normalized);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return candidates;
|
|
70
|
+
}
|
|
71
|
+
|
|
40
72
|
function summarizeRemoteObject(object) {
|
|
41
73
|
if (!object) {
|
|
42
74
|
return null;
|
|
@@ -669,6 +701,82 @@ export class CdpSession {
|
|
|
669
701
|
return summarizeNetworkRequests(this.bufferedEvents, limit);
|
|
670
702
|
}
|
|
671
703
|
|
|
704
|
+
async getCookies() {
|
|
705
|
+
return this.runPageAction({
|
|
706
|
+
action: "cookie_snapshot",
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
async getStorage() {
|
|
711
|
+
return this.runPageAction({
|
|
712
|
+
action: "storage_snapshot",
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
async captureDebugReport(options = {}) {
|
|
717
|
+
const consoleLimit =
|
|
718
|
+
Number.isInteger(options.consoleLimit) && options.consoleLimit > 0
|
|
719
|
+
? options.consoleLimit
|
|
720
|
+
: 20;
|
|
721
|
+
const networkLimit =
|
|
722
|
+
Number.isInteger(options.networkLimit) && options.networkLimit > 0
|
|
723
|
+
? options.networkLimit
|
|
724
|
+
: 20;
|
|
725
|
+
const includeScreenshot = options.includeScreenshot !== false;
|
|
726
|
+
const screenshotFormat = options.screenshotFormat ?? "png";
|
|
727
|
+
const snapshot = await this.runPageAction({
|
|
728
|
+
action: "debug_report",
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
return {
|
|
732
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
733
|
+
capturedAt: new Date().toISOString(),
|
|
734
|
+
page: {
|
|
735
|
+
...(await this.getPageState()),
|
|
736
|
+
},
|
|
737
|
+
cookies: snapshot.cookies,
|
|
738
|
+
storage: snapshot.storage,
|
|
739
|
+
console: this.getConsoleMessages(consoleLimit),
|
|
740
|
+
network: this.getNetworkRequests(networkLimit),
|
|
741
|
+
screenshot: includeScreenshot
|
|
742
|
+
? await this.takeScreenshot(screenshotFormat)
|
|
743
|
+
: null,
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
getHar(options = {}) {
|
|
748
|
+
return exportHarLikeSummary(this.bufferedEvents, {
|
|
749
|
+
limit: options.limit ?? 50,
|
|
750
|
+
page: {
|
|
751
|
+
title: this.target.title,
|
|
752
|
+
url: this.target.url,
|
|
753
|
+
},
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async captureSessionSnapshot() {
|
|
758
|
+
return {
|
|
759
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
760
|
+
capturedAt: new Date().toISOString(),
|
|
761
|
+
page: await this.getPageState(),
|
|
762
|
+
...(await this.getCookies()),
|
|
763
|
+
...(await this.getStorage()),
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
async restoreSessionSnapshot(snapshot, options = {}) {
|
|
768
|
+
const result = await this.runPageAction({
|
|
769
|
+
action: "restore_snapshot",
|
|
770
|
+
snapshot,
|
|
771
|
+
clearStorage: options.clearStorage === true,
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
return {
|
|
775
|
+
...result,
|
|
776
|
+
page: await this.getPageState(),
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
|
|
672
780
|
async inspectElement(selector) {
|
|
673
781
|
return this.runPageAction({
|
|
674
782
|
action: "inspect",
|
|
@@ -897,14 +1005,116 @@ export class CdpSessionManager {
|
|
|
897
1005
|
constructor(config) {
|
|
898
1006
|
this.config = config;
|
|
899
1007
|
this.sessions = new Map();
|
|
1008
|
+
this.resolvedBaseUrl = null;
|
|
1009
|
+
this.lastAttemptedBaseUrl = null;
|
|
1010
|
+
this.lastKnownBaseUrl = null;
|
|
1011
|
+
this.failedBaseUrlCooldownMs = 5_000;
|
|
1012
|
+
this.failedBaseUrlDeadlines = new Map();
|
|
1013
|
+
this.fetchTimeoutMs = 1_000;
|
|
900
1014
|
}
|
|
901
1015
|
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1016
|
+
shouldAutoDiscoverBaseUrl() {
|
|
1017
|
+
return this.config.cdpBaseUrl === DEFAULT_CDP_BASE_URL;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
markBaseUrlFailed(baseUrl) {
|
|
1021
|
+
this.failedBaseUrlDeadlines.set(
|
|
1022
|
+
baseUrl,
|
|
1023
|
+
Date.now() + this.failedBaseUrlCooldownMs,
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
invalidateBaseUrl(baseUrl) {
|
|
1028
|
+
this.markBaseUrlFailed(baseUrl);
|
|
1029
|
+
|
|
1030
|
+
if (this.resolvedBaseUrl === baseUrl) {
|
|
1031
|
+
this.resolvedBaseUrl = null;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
if (this.lastKnownBaseUrl === baseUrl) {
|
|
1035
|
+
this.lastKnownBaseUrl = null;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
pruneFailedBaseUrls(now = Date.now()) {
|
|
1040
|
+
for (const [baseUrl, deadline] of this.failedBaseUrlDeadlines.entries()) {
|
|
1041
|
+
if (deadline <= now) {
|
|
1042
|
+
this.failedBaseUrlDeadlines.delete(baseUrl);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
async fetchJsonAt(baseUrl, pathname) {
|
|
1048
|
+
this.lastAttemptedBaseUrl = baseUrl;
|
|
1049
|
+
const url = new URL(pathname, `${baseUrl}/`);
|
|
1050
|
+
const response = await fetch(url, {
|
|
1051
|
+
signal: AbortSignal.timeout(this.fetchTimeoutMs),
|
|
1052
|
+
});
|
|
905
1053
|
return parseJson(response);
|
|
906
1054
|
}
|
|
907
1055
|
|
|
1056
|
+
async resolveBaseUrl(excludedBaseUrls = new Set()) {
|
|
1057
|
+
if (this.resolvedBaseUrl) {
|
|
1058
|
+
return this.resolvedBaseUrl;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
const candidates = this.shouldAutoDiscoverBaseUrl()
|
|
1062
|
+
? buildDiscoveryBaseUrls(this.config.cdpBaseUrl)
|
|
1063
|
+
: [this.config.cdpBaseUrl];
|
|
1064
|
+
this.pruneFailedBaseUrls();
|
|
1065
|
+
const preferredCandidates = candidates.filter(
|
|
1066
|
+
(candidate) =>
|
|
1067
|
+
!excludedBaseUrls.has(candidate) &&
|
|
1068
|
+
!this.failedBaseUrlDeadlines.has(candidate),
|
|
1069
|
+
);
|
|
1070
|
+
const probeCandidates =
|
|
1071
|
+
preferredCandidates.length > 0
|
|
1072
|
+
? preferredCandidates
|
|
1073
|
+
: candidates.filter((candidate) => !excludedBaseUrls.has(candidate));
|
|
1074
|
+
let lastError = null;
|
|
1075
|
+
|
|
1076
|
+
for (const candidate of probeCandidates) {
|
|
1077
|
+
try {
|
|
1078
|
+
const info = await this.fetchJsonAt(candidate, "/json/version");
|
|
1079
|
+
if (!info.webSocketDebuggerUrl) {
|
|
1080
|
+
this.invalidateBaseUrl(candidate);
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
this.failedBaseUrlDeadlines.delete(candidate);
|
|
1085
|
+
this.resolvedBaseUrl = candidate;
|
|
1086
|
+
this.lastKnownBaseUrl = candidate;
|
|
1087
|
+
return candidate;
|
|
1088
|
+
} catch (error) {
|
|
1089
|
+
this.invalidateBaseUrl(candidate);
|
|
1090
|
+
lastError = error;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
throw lastError ?? new Error("Unable to resolve a CDP browser endpoint");
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
async fetchJson(pathname) {
|
|
1098
|
+
const baseUrl = await this.resolveBaseUrl();
|
|
1099
|
+
|
|
1100
|
+
try {
|
|
1101
|
+
return await this.fetchJsonAt(baseUrl, pathname);
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
if (!this.shouldAutoDiscoverBaseUrl() || !this.resolvedBaseUrl) {
|
|
1104
|
+
throw error;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
this.invalidateBaseUrl(baseUrl);
|
|
1108
|
+
const rediscoveredBaseUrl = await this.resolveBaseUrl(new Set([baseUrl]));
|
|
1109
|
+
try {
|
|
1110
|
+
return await this.fetchJsonAt(rediscoveredBaseUrl, pathname);
|
|
1111
|
+
} catch (rediscoveredError) {
|
|
1112
|
+
this.invalidateBaseUrl(rediscoveredBaseUrl);
|
|
1113
|
+
throw rediscoveredError;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
|
|
908
1118
|
async sendBrowserCommand(method, params = {}) {
|
|
909
1119
|
const info = await this.fetchJson("/json/version");
|
|
910
1120
|
if (!info.webSocketDebuggerUrl) {
|
|
@@ -988,6 +1198,12 @@ export class CdpSessionManager {
|
|
|
988
1198
|
websocket.addEventListener("message", handleMessage);
|
|
989
1199
|
websocket.addEventListener("error", handleError, { once: true });
|
|
990
1200
|
websocket.addEventListener("close", handleClose, { once: true });
|
|
1201
|
+
}).catch((error) => {
|
|
1202
|
+
if (this.shouldAutoDiscoverBaseUrl() && this.resolvedBaseUrl) {
|
|
1203
|
+
this.invalidateBaseUrl(this.resolvedBaseUrl);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
throw error;
|
|
991
1207
|
});
|
|
992
1208
|
}
|
|
993
1209
|
|
|
@@ -996,7 +1212,15 @@ export class CdpSessionManager {
|
|
|
996
1212
|
const info = await this.fetchJson("/json/version");
|
|
997
1213
|
return {
|
|
998
1214
|
available: true,
|
|
999
|
-
endpoint:
|
|
1215
|
+
endpoint:
|
|
1216
|
+
this.resolvedBaseUrl ??
|
|
1217
|
+
this.lastKnownBaseUrl ??
|
|
1218
|
+
this.config.cdpBaseUrl,
|
|
1219
|
+
attemptedEndpoint:
|
|
1220
|
+
this.lastAttemptedBaseUrl ??
|
|
1221
|
+
this.resolvedBaseUrl ??
|
|
1222
|
+
this.lastKnownBaseUrl ??
|
|
1223
|
+
this.config.cdpBaseUrl,
|
|
1000
1224
|
sessionCount: this.sessions.size,
|
|
1001
1225
|
browser: info.Browser,
|
|
1002
1226
|
protocolVersion: info["Protocol-Version"],
|
|
@@ -1005,7 +1229,15 @@ export class CdpSessionManager {
|
|
|
1005
1229
|
} catch (error) {
|
|
1006
1230
|
return {
|
|
1007
1231
|
available: false,
|
|
1008
|
-
endpoint:
|
|
1232
|
+
endpoint:
|
|
1233
|
+
this.resolvedBaseUrl ??
|
|
1234
|
+
this.lastKnownBaseUrl ??
|
|
1235
|
+
this.config.cdpBaseUrl,
|
|
1236
|
+
attemptedEndpoint:
|
|
1237
|
+
this.lastAttemptedBaseUrl ??
|
|
1238
|
+
this.resolvedBaseUrl ??
|
|
1239
|
+
this.lastKnownBaseUrl ??
|
|
1240
|
+
this.config.cdpBaseUrl,
|
|
1009
1241
|
sessionCount: this.sessions.size,
|
|
1010
1242
|
error: toErrorMessage(error),
|
|
1011
1243
|
};
|
|
@@ -1182,6 +1414,30 @@ export class CdpSessionManager {
|
|
|
1182
1414
|
return this.getSession(sessionId).getNetworkRequests(limit);
|
|
1183
1415
|
}
|
|
1184
1416
|
|
|
1417
|
+
async getCookies(sessionId) {
|
|
1418
|
+
return this.getSession(sessionId).getCookies();
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
async getStorage(sessionId) {
|
|
1422
|
+
return this.getSession(sessionId).getStorage();
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
async captureDebugReport(sessionId, options = {}) {
|
|
1426
|
+
return this.getSession(sessionId).captureDebugReport(options);
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
getHar(sessionId, options = {}) {
|
|
1430
|
+
return this.getSession(sessionId).getHar(options);
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
async captureSessionSnapshot(sessionId) {
|
|
1434
|
+
return this.getSession(sessionId).captureSessionSnapshot();
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
async restoreSessionSnapshot(sessionId, snapshot, options = {}) {
|
|
1438
|
+
return this.getSession(sessionId).restoreSessionSnapshot(snapshot, options);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1185
1441
|
async inspectElement(sessionId, selector) {
|
|
1186
1442
|
return this.getSession(sessionId).inspectElement(selector);
|
|
1187
1443
|
}
|
package/src/cli.mjs
CHANGED
|
@@ -1,20 +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, loadLoggingConfig } from "./config.mjs";
|
|
6
|
+
import { loadConfig, loadLoggingConfig } from "./config.mjs";
|
|
14
7
|
import { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|
|
15
8
|
import { createLogger } from "./logger.mjs";
|
|
16
9
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
10
|
+
import { bootstrapWslRelay } from "./serve-bootstrap.mjs";
|
|
17
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";
|
|
18
18
|
|
|
19
19
|
export function parseCliArgs(argv) {
|
|
20
20
|
const args = [...argv];
|
|
@@ -67,6 +67,11 @@ function printUsage() {
|
|
|
67
67
|
" --help Print usage information",
|
|
68
68
|
" --version Print the package version",
|
|
69
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
|
+
"",
|
|
70
75
|
"Open options:",
|
|
71
76
|
" --family <name> chromium, edge, or firefox",
|
|
72
77
|
" --port <port> remote debugging port",
|
|
@@ -90,111 +95,39 @@ function printVersion() {
|
|
|
90
95
|
process.stdout.write(`${PACKAGE_VERSION}\n`);
|
|
91
96
|
}
|
|
92
97
|
|
|
93
|
-
function
|
|
94
|
-
|
|
95
|
-
setTimeout(resolve, ms);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const FIREFOX_BIDI_SERVER_FILENAME = "WebDriverBiDiServer.json";
|
|
100
|
-
|
|
101
|
-
function isHttpLikeUrl(value) {
|
|
102
|
-
try {
|
|
103
|
-
const url = new URL(value);
|
|
104
|
-
return url.protocol === "http:" || url.protocol === "https:";
|
|
105
|
-
} catch {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function normalizeFirefoxDoctorHost(value) {
|
|
111
|
-
const host = typeof value === "string" ? value.trim() : "";
|
|
112
|
-
if (!host || host === "0.0.0.0" || host === "::" || host === "[::]") {
|
|
113
|
-
return "127.0.0.1";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return host;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function normalizeFirefoxDoctorPath(value) {
|
|
120
|
-
if (typeof value !== "string") {
|
|
121
|
-
return "/";
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const trimmed = value.trim();
|
|
125
|
-
if (!trimmed) {
|
|
126
|
-
return "/";
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function parseFirefoxBidiServerInfo(contents) {
|
|
133
|
-
const parsed = JSON.parse(contents);
|
|
134
|
-
if (!parsed || typeof parsed !== "object") {
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const directUrl =
|
|
139
|
-
parsed.webSocketUrl ??
|
|
140
|
-
parsed.websocketUrl ??
|
|
141
|
-
parsed.wsUrl ??
|
|
142
|
-
parsed.ws_url ??
|
|
143
|
-
null;
|
|
144
|
-
if (typeof directUrl === "string" && directUrl.trim()) {
|
|
145
|
-
return directUrl.trim().replace(/\/+$/, "");
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const port =
|
|
149
|
-
parsed.ws_port ?? parsed.port ?? parsed.remoteDebuggingPort ?? null;
|
|
150
|
-
if (!Number.isInteger(port) && !/^\d+$/.test(String(port ?? "").trim())) {
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const host = normalizeFirefoxDoctorHost(
|
|
155
|
-
parsed.ws_host ?? parsed.host ?? parsed.hostname ?? "127.0.0.1",
|
|
156
|
-
);
|
|
157
|
-
const protocol =
|
|
158
|
-
parsed.protocol === "https" || parsed.protocol === "wss" ? "wss:" : "ws:";
|
|
159
|
-
const endpoint = new URL(`${protocol}//${host}`);
|
|
160
|
-
endpoint.port = String(port);
|
|
161
|
-
endpoint.pathname = normalizeFirefoxDoctorPath(
|
|
162
|
-
parsed.path ?? parsed.ws_path ?? "/",
|
|
163
|
-
);
|
|
164
|
-
return endpoint.toString().replace(/\/+$/, "");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export async function resolveFirefoxDoctorEndpoint({
|
|
168
|
-
userDataDir,
|
|
169
|
-
fallbackPort,
|
|
170
|
-
}) {
|
|
171
|
-
if (!userDataDir) {
|
|
172
|
-
return `ws://127.0.0.1:${fallbackPort}`;
|
|
173
|
-
}
|
|
98
|
+
async function runServe(options = {}) {
|
|
99
|
+
let relayBootstrap = null;
|
|
174
100
|
|
|
175
101
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
+
);
|
|
186
115
|
}
|
|
187
116
|
|
|
188
|
-
|
|
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;
|
|
189
128
|
}
|
|
190
129
|
}
|
|
191
130
|
|
|
192
|
-
async function runServe() {
|
|
193
|
-
const app = createBrowserDevToolsApp();
|
|
194
|
-
app.installSignalHandlers();
|
|
195
|
-
app.start();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
131
|
async function runDoctor(options) {
|
|
199
132
|
const report = await collectDoctorReport({
|
|
200
133
|
url: typeof options.url === "string" ? options.url : null,
|
|
@@ -215,112 +148,34 @@ async function runOpen(positional, options) {
|
|
|
215
148
|
}
|
|
216
149
|
|
|
217
150
|
const config = loadConfig();
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
(family === "chromium" || family === "edge") &&
|
|
222
|
-
typeof options.address === "string"
|
|
223
|
-
? options.address.trim()
|
|
224
|
-
: null;
|
|
225
|
-
|
|
226
|
-
if (
|
|
227
|
-
requestedAddress &&
|
|
228
|
-
!isLoopbackHost(requestedAddress) &&
|
|
229
|
-
!config.allowRemoteEndpoints
|
|
230
|
-
) {
|
|
231
|
-
throw new Error(
|
|
232
|
-
"--address must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1",
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const executable = await findBrowserExecutable(family);
|
|
237
|
-
if (!executable) {
|
|
238
|
-
throw new Error(`No local ${family} browser executable found`);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const defaultPort =
|
|
242
|
-
family === "firefox"
|
|
243
|
-
? new URL(config.firefoxBidiWsUrl).port || "9222"
|
|
244
|
-
: new URL(config.cdpBaseUrl).port || "9222";
|
|
245
|
-
const resolvedPort =
|
|
246
|
-
typeof options.port === "string" ? options.port : defaultPort;
|
|
247
|
-
const userDataDir =
|
|
248
|
-
typeof options["user-data-dir"] === "string"
|
|
249
|
-
? options["user-data-dir"]
|
|
250
|
-
: undefined;
|
|
251
|
-
|
|
252
|
-
if (family === "firefox" && userDataDir) {
|
|
253
|
-
await mkdir(userDataDir, { recursive: true });
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const args = buildBrowserLaunchArgs({
|
|
257
|
-
family,
|
|
151
|
+
const launch = await launchBrowser({
|
|
152
|
+
config,
|
|
153
|
+
browserFamily: typeof options.family === "string" ? options.family : null,
|
|
258
154
|
url,
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
userDataDir,
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const child = spawn(executable, args, {
|
|
265
|
-
detached: true,
|
|
266
|
-
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,
|
|
267
160
|
});
|
|
268
|
-
child.unref();
|
|
269
161
|
|
|
270
162
|
process.stdout.write(
|
|
271
|
-
`launched ${
|
|
163
|
+
`launched ${launch.browserFamily} browser: ${launch.executable} ${launch.args.join(" ")}\n`,
|
|
272
164
|
);
|
|
273
165
|
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const waitMs =
|
|
279
|
-
typeof options["wait-ms"] === "string"
|
|
280
|
-
? Number.parseInt(options["wait-ms"], 10)
|
|
281
|
-
: 5000;
|
|
282
|
-
const timeoutMs = Number.isInteger(waitMs) && waitMs >= 0 ? waitMs : 5000;
|
|
283
|
-
const startedAt = Date.now();
|
|
284
|
-
const doctorEnv = {
|
|
285
|
-
...process.env,
|
|
286
|
-
MCP_BROWSER_FAMILY: family,
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
if (family === "firefox") {
|
|
290
|
-
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
291
|
-
userDataDir,
|
|
292
|
-
fallbackPort: resolvedPort,
|
|
293
|
-
});
|
|
294
|
-
} else {
|
|
295
|
-
const address = requestedAddress || "127.0.0.1";
|
|
296
|
-
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
|
+
);
|
|
297
170
|
}
|
|
298
171
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
url: isHttpLikeUrl(url) ? url : null,
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
while (
|
|
305
|
-
!report.browserStatus.available &&
|
|
306
|
-
Date.now() - startedAt < timeoutMs
|
|
307
|
-
) {
|
|
308
|
-
await sleep(250);
|
|
309
|
-
if (family === "firefox") {
|
|
310
|
-
doctorEnv.FIREFOX_BIDI_WS_URL = await resolveFirefoxDoctorEndpoint({
|
|
311
|
-
userDataDir,
|
|
312
|
-
fallbackPort: resolvedPort,
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
report = await collectDoctorReport({
|
|
316
|
-
env: doctorEnv,
|
|
317
|
-
url: isHttpLikeUrl(url) ? url : null,
|
|
318
|
-
});
|
|
172
|
+
if (!launch.doctorReport) {
|
|
173
|
+
return;
|
|
319
174
|
}
|
|
320
175
|
|
|
321
|
-
process.stdout.write(`${renderDoctorReport(
|
|
176
|
+
process.stdout.write(`${renderDoctorReport(launch.doctorReport)}\n`);
|
|
322
177
|
|
|
323
|
-
if (!
|
|
178
|
+
if (!launch.doctorReport.browserStatus.available) {
|
|
324
179
|
process.exitCode = 1;
|
|
325
180
|
}
|
|
326
181
|
}
|
|
@@ -377,7 +232,7 @@ export async function runCli(argv = process.argv.slice(2)) {
|
|
|
377
232
|
|
|
378
233
|
switch (command) {
|
|
379
234
|
case "serve":
|
|
380
|
-
await runServe();
|
|
235
|
+
await runServe(options);
|
|
381
236
|
return;
|
|
382
237
|
case "doctor":
|
|
383
238
|
await runDoctor(options);
|
package/src/config.mjs
CHANGED
|
@@ -2,7 +2,7 @@ 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
6
|
export const DEFAULT_LOG_LEVEL = "error";
|
|
7
7
|
export const CDP_BROWSER_FAMILIES = ["chromium", "edge"];
|
|
8
8
|
|