mcp-browser-dev-tools 0.0.1-beta.1 → 0.0.1-beta.10
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 +234 -13
- package/package.json +4 -1
- package/src/browser-adapter.mjs +275 -0
- package/src/browser-launcher.mjs +35 -0
- package/src/cdp-client.mjs +524 -85
- package/src/cli.mjs +212 -9
- package/src/config.mjs +24 -9
- package/src/doctor.mjs +25 -5
- package/src/firefox-bidi-client.mjs +509 -100
- package/src/index.mjs +13 -1
- package/src/json-rpc-stdio.mjs +130 -21
- package/src/mcp-server.mjs +360 -5
- package/src/page-context.mjs +734 -0
- package/src/session-events.mjs +16 -2
- package/src/tcp-relay.mjs +194 -0
package/src/session-events.mjs
CHANGED
|
@@ -17,12 +17,18 @@ function createEmptyNetworkRequest(requestId) {
|
|
|
17
17
|
url: null,
|
|
18
18
|
method: null,
|
|
19
19
|
resourceType: null,
|
|
20
|
+
initiatorType: null,
|
|
20
21
|
status: null,
|
|
21
22
|
statusText: null,
|
|
22
23
|
mimeType: null,
|
|
23
24
|
startedAt: null,
|
|
24
25
|
completedAt: null,
|
|
25
26
|
updatedAt: null,
|
|
27
|
+
duration: null,
|
|
28
|
+
transferSize: null,
|
|
29
|
+
encodedBodySize: null,
|
|
30
|
+
decodedBodySize: null,
|
|
31
|
+
source: null,
|
|
26
32
|
finished: false,
|
|
27
33
|
failed: false,
|
|
28
34
|
canceled: false,
|
|
@@ -49,17 +55,25 @@ export function summarizeNetworkRequests(events = [], limit = 50) {
|
|
|
49
55
|
next.url = event.url ?? next.url;
|
|
50
56
|
next.method = event.method ?? next.method;
|
|
51
57
|
next.resourceType = event.resourceType ?? next.resourceType;
|
|
58
|
+
next.initiatorType = event.initiatorType ?? next.initiatorType;
|
|
52
59
|
next.status = event.status ?? next.status;
|
|
53
60
|
next.statusText = event.statusText ?? next.statusText;
|
|
54
61
|
next.mimeType = event.mimeType ?? next.mimeType;
|
|
62
|
+
next.duration = event.duration ?? next.duration;
|
|
63
|
+
next.transferSize = event.transferSize ?? next.transferSize;
|
|
64
|
+
next.encodedBodySize = event.encodedBodySize ?? next.encodedBodySize;
|
|
65
|
+
next.decodedBodySize = event.decodedBodySize ?? next.decodedBodySize;
|
|
66
|
+
next.source = event.source ?? next.source;
|
|
55
67
|
next.updatedAt = timestamp ?? next.updatedAt;
|
|
56
68
|
|
|
57
|
-
if (
|
|
69
|
+
if (event.startedAt !== undefined && event.startedAt !== null) {
|
|
70
|
+
next.startedAt = event.startedAt;
|
|
71
|
+
} else if (!next.startedAt || event.phase === "request") {
|
|
58
72
|
next.startedAt = next.startedAt ?? timestamp;
|
|
59
73
|
}
|
|
60
74
|
|
|
61
75
|
if (event.completed) {
|
|
62
|
-
next.completedAt = timestamp ?? next.completedAt;
|
|
76
|
+
next.completedAt = event.completedAt ?? timestamp ?? next.completedAt;
|
|
63
77
|
next.finished = !event.failed;
|
|
64
78
|
}
|
|
65
79
|
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
|
|
5
|
+
import { isLoopbackHost, isTruthyFlag } from "./config.mjs";
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_RELAY_LISTEN_HOST = "127.0.0.1";
|
|
8
|
+
export const DEFAULT_RELAY_LISTEN_PORT = 9223;
|
|
9
|
+
export const DEFAULT_RELAY_TARGET_HOST = "127.0.0.1";
|
|
10
|
+
export const DEFAULT_RELAY_TARGET_PORT = 9222;
|
|
11
|
+
|
|
12
|
+
function allowsRemoteEndpoints(env = process.env) {
|
|
13
|
+
return (
|
|
14
|
+
isTruthyFlag(env.MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS) ||
|
|
15
|
+
isTruthyFlag(env.MCP_BROWSER_ALLOW_REMOTE_CDP)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizeHost(value, fallback) {
|
|
20
|
+
return typeof value === "string" && value.trim() ? value.trim() : fallback;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parseRelayPort(value, fallback, optionName) {
|
|
24
|
+
const normalized =
|
|
25
|
+
typeof value === "number"
|
|
26
|
+
? String(value)
|
|
27
|
+
: typeof value === "string"
|
|
28
|
+
? value.trim()
|
|
29
|
+
: "";
|
|
30
|
+
|
|
31
|
+
if (!normalized) {
|
|
32
|
+
return fallback;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!/^\d+$/.test(normalized)) {
|
|
36
|
+
throw new Error(`${optionName} must be an integer between 1 and 65535`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const parsed = Number.parseInt(normalized, 10);
|
|
40
|
+
if (parsed < 1 || parsed > 65535) {
|
|
41
|
+
throw new Error(`${optionName} must be an integer between 1 and 65535`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function getWindowsWslHostCandidates(
|
|
48
|
+
networkInterfaces = os.networkInterfaces(),
|
|
49
|
+
) {
|
|
50
|
+
const candidates = [];
|
|
51
|
+
|
|
52
|
+
for (const [name, addresses] of Object.entries(networkInterfaces)) {
|
|
53
|
+
if (!/wsl/i.test(name)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const addressInfo of addresses ?? []) {
|
|
58
|
+
if (addressInfo.internal || addressInfo.family !== "IPv4") {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
candidates.push(addressInfo.address);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return [...new Set(candidates)];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolveRelayOptions(
|
|
70
|
+
options = {},
|
|
71
|
+
{
|
|
72
|
+
env = process.env,
|
|
73
|
+
platform = process.platform,
|
|
74
|
+
networkInterfaces = null,
|
|
75
|
+
} = {},
|
|
76
|
+
) {
|
|
77
|
+
const useWslBridge = options.wsl === true;
|
|
78
|
+
let listenHost;
|
|
79
|
+
|
|
80
|
+
if (useWslBridge) {
|
|
81
|
+
if (platform !== "win32") {
|
|
82
|
+
throw new Error("--wsl is only supported on Windows");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const candidates = getWindowsWslHostCandidates(
|
|
86
|
+
networkInterfaces ?? os.networkInterfaces(),
|
|
87
|
+
);
|
|
88
|
+
if (candidates.length === 0) {
|
|
89
|
+
throw new Error("Could not detect a Windows WSL host address");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
[listenHost] = candidates;
|
|
93
|
+
} else {
|
|
94
|
+
listenHost = normalizeHost(
|
|
95
|
+
options["listen-host"],
|
|
96
|
+
DEFAULT_RELAY_LISTEN_HOST,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (!isLoopbackHost(listenHost) && !allowsRemoteEndpoints(env)) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
"--listen-host must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
listenHost,
|
|
108
|
+
listenPort: parseRelayPort(
|
|
109
|
+
options["listen-port"],
|
|
110
|
+
DEFAULT_RELAY_LISTEN_PORT,
|
|
111
|
+
"--listen-port",
|
|
112
|
+
),
|
|
113
|
+
targetHost: normalizeHost(
|
|
114
|
+
options["target-host"],
|
|
115
|
+
DEFAULT_RELAY_TARGET_HOST,
|
|
116
|
+
),
|
|
117
|
+
targetPort: parseRelayPort(
|
|
118
|
+
options["target-port"],
|
|
119
|
+
DEFAULT_RELAY_TARGET_PORT,
|
|
120
|
+
"--target-port",
|
|
121
|
+
),
|
|
122
|
+
useWslBridge,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function startTcpRelay({
|
|
127
|
+
listenHost,
|
|
128
|
+
listenPort,
|
|
129
|
+
targetHost,
|
|
130
|
+
targetPort,
|
|
131
|
+
errorOutput = process.stderr,
|
|
132
|
+
} = {}) {
|
|
133
|
+
const openSockets = new Set();
|
|
134
|
+
|
|
135
|
+
const server = net.createServer((clientSocket) => {
|
|
136
|
+
const upstreamSocket = net.connect({
|
|
137
|
+
host: targetHost,
|
|
138
|
+
port: targetPort,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
openSockets.add(clientSocket);
|
|
142
|
+
openSockets.add(upstreamSocket);
|
|
143
|
+
|
|
144
|
+
const cleanup = () => {
|
|
145
|
+
openSockets.delete(clientSocket);
|
|
146
|
+
openSockets.delete(upstreamSocket);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
clientSocket.on("close", cleanup);
|
|
150
|
+
upstreamSocket.on("close", cleanup);
|
|
151
|
+
|
|
152
|
+
clientSocket.on("error", () => {
|
|
153
|
+
upstreamSocket.destroy();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
upstreamSocket.on("error", (error) => {
|
|
157
|
+
errorOutput.write(`[relay] target connection failed: ${error.message}\n`);
|
|
158
|
+
clientSocket.destroy();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
clientSocket.pipe(upstreamSocket);
|
|
162
|
+
upstreamSocket.pipe(clientSocket);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
await new Promise((resolve, reject) => {
|
|
166
|
+
server.once("error", reject);
|
|
167
|
+
server.listen(listenPort, listenHost, () => {
|
|
168
|
+
server.off("error", reject);
|
|
169
|
+
resolve();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
async function close() {
|
|
174
|
+
for (const socket of openSockets) {
|
|
175
|
+
socket.destroy();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
await new Promise((resolve, reject) => {
|
|
179
|
+
server.close((error) => {
|
|
180
|
+
if (error) {
|
|
181
|
+
reject(error);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
resolve();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
server,
|
|
192
|
+
close,
|
|
193
|
+
};
|
|
194
|
+
}
|