mcp-browser-dev-tools 0.5.4 → 0.5.7
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 +15 -5
- package/package.json +5 -2
- package/src/browser-launch-service.mjs +9 -18
- package/src/browser-launcher.mjs +25 -0
- package/src/cdp-client.mjs +29 -1
- package/src/config.mjs +34 -0
- package/src/doctor.mjs +9 -3
- package/src/firefox-bidi-client.mjs +7 -2
- package/src/tcp-relay.mjs +11 -4
- package/src/tools/browser-tools.mjs +55 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/mcp-browser-dev-tools)
|
|
4
4
|
[](https://www.npmjs.com/package/mcp-browser-dev-tools)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
An MCP server for browser use: let an AI agent work in a real browser with fewer back-and-forths. It sends a whole sequence of clicks, typing, waits, and checks in one tool call and gets every result back together, in your Chrome, Edge, or Firefox. Against Playwright MCP and Chrome DevTools MCP, on six browser tasks with six models, it took the fewest turns and the least time with every model ([comparison](https://hinaser.github.io/mcp-browser-dev-tools/comparison.html)).
|
|
7
7
|
|
|
8
8
|
## Quick Start
|
|
9
9
|
|
|
@@ -11,10 +11,10 @@ You need Node.js 24 or later. Register the server with your MCP client:
|
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
13
|
# Claude Code
|
|
14
|
-
claude mcp add browser-devtools --scope user -- npx -y mcp-browser-dev-tools serve
|
|
14
|
+
claude mcp add browser-devtools --scope user -- npx -y mcp-browser-dev-tools@latest serve
|
|
15
15
|
|
|
16
16
|
# Codex
|
|
17
|
-
codex mcp add browser-devtools -- npx -y mcp-browser-dev-tools serve
|
|
17
|
+
codex mcp add browser-devtools -- npx -y mcp-browser-dev-tools@latest serve
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
Cursor and other clients that use an `mcpServers` config:
|
|
@@ -24,12 +24,14 @@ Cursor and other clients that use an `mcpServers` config:
|
|
|
24
24
|
"mcpServers": {
|
|
25
25
|
"browser-devtools": {
|
|
26
26
|
"command": "npx",
|
|
27
|
-
"args": ["-y", "mcp-browser-dev-tools", "serve"]
|
|
27
|
+
"args": ["-y", "mcp-browser-dev-tools@latest", "serve"]
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
`@latest` makes npx check for the current release when the client starts the server, reusing its cached copy when nothing changed; the check takes a moment and needs the npm registry or a cached answer. Without a version, npx uses an already installed copy, such as one from `npm install -g`, however old.
|
|
34
|
+
|
|
33
35
|
Then ask the agent for browser work, for example "open example.com and check the console for errors" or "attach to my open tab and find out why the form won't submit". The agent starts with `ensure_browser`, which reuses a browser that is already running with a debugging port, or launches one. To make launches use a particular profile, set `MCP_BROWSER_USER_DATA_DIR`; see [Configuration](docs/configuration.md) for this, native Windows, WSL, and Firefox.
|
|
34
36
|
|
|
35
37
|
## What It Does
|
|
@@ -47,7 +49,7 @@ Your agent gets 46 browser tools over MCP: open or attach to tabs, click, type,
|
|
|
47
49
|
- **Right where you work.** It attaches to the browser you already use, with its logins, tabs, and extensions, and speaks to Chrome, Edge, and Firefox with the same tools.
|
|
48
50
|
- **A debugger at hand.** Console messages with stack traces, network requests and HAR export, cookies and storage, and a one-call debug report.
|
|
49
51
|
|
|
50
|
-
Against Playwright MCP and Chrome DevTools MCP, on the same tasks with six models, it took the fewest turns and the least time with every model and cost the least on average and with four of the six: see the [comparison](https://hinaser.github.io/mcp-browser-dev-tools/).
|
|
52
|
+
Against Playwright MCP and Chrome DevTools MCP, on the same tasks with six models, it took the fewest turns and the least time with every model and cost the least on average and with four of the six: see the [comparison](https://hinaser.github.io/mcp-browser-dev-tools/comparison.html).
|
|
51
53
|
|
|
52
54
|
## One Call, Start To Finish
|
|
53
55
|
|
|
@@ -184,6 +186,14 @@ Every setting, per-client examples, and how the server reuses or launches browse
|
|
|
184
186
|
- The server only connects to loopback browser endpoints unless you set `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1`.
|
|
185
187
|
- Tools both read and act on the page: they click, type, navigate, and run page JavaScript through `evaluate_js`, as whoever the browser profile is signed in as. Point the server at a profile you are willing to let an agent use.
|
|
186
188
|
- `MCP_BROWSER_ENABLE_EVAL=0` removes `evaluate_js` and the `expression` field of `wait_for` and `run_steps` conditions. This narrows the tool surface rather than drawing a security boundary: the other tools already read cookies and storage and can navigate anywhere. To approve page scripts one at a time, use your MCP client's tool permissions, and note that `wait_for` and `run_steps` calls can carry scripts too.
|
|
189
|
+
- `upload_file` gives a page files from the server's working directory, the temp directory, and `MCP_BROWSER_UPLOAD_DIRS`. Your MCP client chooses the working directory: if it starts the server in your home folder, every file there qualifies.
|
|
190
|
+
- Pages are not opened from `file://` unless you set `MCP_BROWSER_ALLOW_FILE_URLS=1`, since a local page can show any file the browser can read. Screenshots, traces, and videos are saved to any absolute path the agent names, and replace an existing file of the same type only when the call passes `overwrite`.
|
|
191
|
+
- `MCP_BROWSER_TOOLS` changes which tools are listed, not which can run: the others stay callable through `more_tools`.
|
|
192
|
+
- The install commands use `@latest`, so a new release runs on the next start. To run only a version you have checked, pin it, such as `mcp-browser-dev-tools@0.5.7`.
|
|
193
|
+
|
|
194
|
+
The server has no runtime dependencies and no install scripts, and npm releases are built by GitHub Actions from signed tags, with [provenance](https://docs.npmjs.com/generating-provenance-statements). To check it yourself before installing, give your coding agent this prompt:
|
|
195
|
+
|
|
196
|
+
> Check https://github.com/Hinaser/mcp-browser-dev-tools for malicious or risky code before I install it. Look for network requests that leave this machine, data sent or saved outside the browser, downloaded code that gets run, and whether the npm package mcp-browser-dev-tools matches the source.
|
|
187
197
|
|
|
188
198
|
## Compared With Playwright
|
|
189
199
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
3
|
"mcpName": "io.github.Hinaser/mcp-browser-dev-tools",
|
|
4
|
-
"version": "0.5.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "0.5.7",
|
|
5
|
+
"description": "MCP server for browser use by AI agents: batch actions, waits, and checks into one call on Chrome, Edge, or Firefox (CDP and WebDriver BiDi)",
|
|
6
6
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
@@ -78,6 +78,9 @@
|
|
|
78
78
|
"mcp-server",
|
|
79
79
|
"model-context-protocol",
|
|
80
80
|
"browser",
|
|
81
|
+
"ai-agents",
|
|
82
|
+
"browser-automation",
|
|
83
|
+
"browser-use",
|
|
81
84
|
"devtools",
|
|
82
85
|
"cdp",
|
|
83
86
|
"webdriver-bidi",
|
|
@@ -13,10 +13,11 @@ import path from "node:path";
|
|
|
13
13
|
import { promisify } from "node:util";
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
|
+
assertLaunchUrl,
|
|
16
17
|
buildBrowserLaunchArgs,
|
|
17
18
|
findBrowserExecutable,
|
|
18
19
|
} from "./browser-launcher.mjs";
|
|
19
|
-
import { isLoopbackHost } from "./config.mjs";
|
|
20
|
+
import { assertPageUrlAllowed, isLoopbackHost } from "./config.mjs";
|
|
20
21
|
import { collectDoctorReport } from "./doctor.mjs";
|
|
21
22
|
|
|
22
23
|
const FIREFOX_BIDI_SERVER_FILENAME = "WebDriverBiDiServer.json";
|
|
@@ -40,15 +41,6 @@ function sleep(ms) {
|
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
function isHttpLikeUrl(value) {
|
|
44
|
-
try {
|
|
45
|
-
const url = new URL(value);
|
|
46
|
-
return url.protocol === "http:" || url.protocol === "https:";
|
|
47
|
-
} catch {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
44
|
function normalizeFirefoxDoctorHost(value) {
|
|
53
45
|
const host = typeof value === "string" ? value.trim() : "";
|
|
54
46
|
if (!host || host === "0.0.0.0" || host === "::" || host === "[::]") {
|
|
@@ -591,6 +583,8 @@ export async function launchBrowser(
|
|
|
591
583
|
) {
|
|
592
584
|
const family = resolveLaunchFamily(config.browserFamily, browserFamily);
|
|
593
585
|
const targetUrl = normalizeStringValue(url) ?? "about:blank";
|
|
586
|
+
assertLaunchUrl(targetUrl);
|
|
587
|
+
assertPageUrlAllowed(targetUrl, config);
|
|
594
588
|
const resolvedPort =
|
|
595
589
|
normalizeIntegerValue(port, "port", { minimum: 1 }) ??
|
|
596
590
|
resolveDefaultPort(config, family);
|
|
@@ -718,10 +712,10 @@ export async function launchBrowser(
|
|
|
718
712
|
doctorEnv.CDP_BASE_URL = result.endpoint;
|
|
719
713
|
}
|
|
720
714
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
});
|
|
715
|
+
// The doctor report checks the browser's endpoint only. The browser loads
|
|
716
|
+
// the URL itself; requesting it from here too would hit the site again,
|
|
717
|
+
// outside the browser, once per retry.
|
|
718
|
+
let report = await collectDoctorReportFn({ env: doctorEnv });
|
|
725
719
|
|
|
726
720
|
while (
|
|
727
721
|
!report.browserStatus.available &&
|
|
@@ -735,10 +729,7 @@ export async function launchBrowser(
|
|
|
735
729
|
});
|
|
736
730
|
}
|
|
737
731
|
|
|
738
|
-
report = await collectDoctorReportFn({
|
|
739
|
-
env: doctorEnv,
|
|
740
|
-
url: isHttpLikeUrl(targetUrl) ? targetUrl : null,
|
|
741
|
-
});
|
|
732
|
+
report = await collectDoctorReportFn({ env: doctorEnv });
|
|
742
733
|
}
|
|
743
734
|
|
|
744
735
|
result.endpoint =
|
package/src/browser-launcher.mjs
CHANGED
|
@@ -168,6 +168,30 @@ export async function detectInstalledBrowsers(env = process.env) {
|
|
|
168
168
|
};
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
// The launch URL goes on the browser's command line, where a value such as
|
|
172
|
+
// --flag, -flag, or (on Windows) /flag is read as a switch. Only an absolute
|
|
173
|
+
// URL that starts with its scheme gets there, so its first character is a
|
|
174
|
+
// letter and it can never carry a switch past the unsafeArgs gate or the
|
|
175
|
+
// loopback check on the debugging address.
|
|
176
|
+
export function assertLaunchUrl(url) {
|
|
177
|
+
let protocol;
|
|
178
|
+
try {
|
|
179
|
+
protocol = new URL(url).protocol;
|
|
180
|
+
} catch {
|
|
181
|
+
protocol = null;
|
|
182
|
+
}
|
|
183
|
+
if (
|
|
184
|
+
typeof url !== "string" ||
|
|
185
|
+
!/^[a-z][a-z0-9+.-]*:/i.test(url) ||
|
|
186
|
+
!protocol ||
|
|
187
|
+
protocol === "javascript:"
|
|
188
|
+
) {
|
|
189
|
+
throw new Error(
|
|
190
|
+
"url must be an absolute URL that starts with its scheme, such as https://example.com or about:blank",
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
171
195
|
export function buildBrowserLaunchArgs({
|
|
172
196
|
family,
|
|
173
197
|
url,
|
|
@@ -179,6 +203,7 @@ export function buildBrowserLaunchArgs({
|
|
|
179
203
|
if (!url) {
|
|
180
204
|
throw new Error("A target URL is required");
|
|
181
205
|
}
|
|
206
|
+
assertLaunchUrl(url);
|
|
182
207
|
|
|
183
208
|
const args = [];
|
|
184
209
|
if (family === "firefox") {
|
package/src/cdp-client.mjs
CHANGED
|
@@ -9,7 +9,11 @@ import {
|
|
|
9
9
|
InFlightRequests,
|
|
10
10
|
summarizeNetworkRequests,
|
|
11
11
|
} from "./session-events.mjs";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
assertPageUrlAllowed,
|
|
14
|
+
DEFAULT_CDP_BASE_URL,
|
|
15
|
+
isLoopbackHost,
|
|
16
|
+
} from "./config.mjs";
|
|
13
17
|
import {
|
|
14
18
|
answerDialog,
|
|
15
19
|
createDialogAnswers,
|
|
@@ -479,6 +483,25 @@ function videoGapWarnings(state) {
|
|
|
479
483
|
];
|
|
480
484
|
}
|
|
481
485
|
|
|
486
|
+
// The endpoint check in config.mjs covers the address we discover on; the
|
|
487
|
+
// browser answers with the websocket to use, so that address is checked too.
|
|
488
|
+
export function assertDebuggerSocketUrl(websocketUrl, config) {
|
|
489
|
+
if (config?.allowRemoteEndpoints) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
let hostname;
|
|
493
|
+
try {
|
|
494
|
+
hostname = new URL(websocketUrl).hostname;
|
|
495
|
+
} catch {
|
|
496
|
+
hostname = null;
|
|
497
|
+
}
|
|
498
|
+
if (!isLoopbackHost(hostname)) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
"The browser's debugger websocket is not on this machine; set MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1 to allow it",
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
482
505
|
export class CdpSession {
|
|
483
506
|
constructor(target, options = {}) {
|
|
484
507
|
this.id = newSessionId();
|
|
@@ -527,6 +550,7 @@ export class CdpSession {
|
|
|
527
550
|
);
|
|
528
551
|
}
|
|
529
552
|
|
|
553
|
+
assertDebuggerSocketUrl(this.target.webSocketDebuggerUrl, this.config);
|
|
530
554
|
this.websocket = new WebSocket(this.target.webSocketDebuggerUrl);
|
|
531
555
|
|
|
532
556
|
await new Promise((resolve, reject) => {
|
|
@@ -2462,6 +2486,7 @@ export class CdpSessionManager {
|
|
|
2462
2486
|
this.lastAttemptedBaseUrl = baseUrl;
|
|
2463
2487
|
const url = new URL(pathname, `${baseUrl}/`);
|
|
2464
2488
|
const response = await fetch(url, {
|
|
2489
|
+
redirect: "error",
|
|
2465
2490
|
signal: AbortSignal.timeout(this.fetchTimeoutMs),
|
|
2466
2491
|
});
|
|
2467
2492
|
return parseJson(response);
|
|
@@ -2535,6 +2560,7 @@ export class CdpSessionManager {
|
|
|
2535
2560
|
throw new Error("Browser endpoint does not expose a debugger websocket");
|
|
2536
2561
|
}
|
|
2537
2562
|
|
|
2563
|
+
assertDebuggerSocketUrl(info.webSocketDebuggerUrl, this.config);
|
|
2538
2564
|
const websocket = new WebSocket(info.webSocketDebuggerUrl);
|
|
2539
2565
|
|
|
2540
2566
|
return new Promise((resolve, reject) => {
|
|
@@ -2674,6 +2700,7 @@ export class CdpSessionManager {
|
|
|
2674
2700
|
async createTab(url = "about:blank") {
|
|
2675
2701
|
const targetUrl =
|
|
2676
2702
|
typeof url === "string" && url.trim() ? url.trim() : "about:blank";
|
|
2703
|
+
assertPageUrlAllowed(targetUrl, this.config);
|
|
2677
2704
|
const result = await this.sendBrowserCommand("Target.createTarget", {
|
|
2678
2705
|
url: targetUrl,
|
|
2679
2706
|
});
|
|
@@ -2829,6 +2856,7 @@ export class CdpSessionManager {
|
|
|
2829
2856
|
}
|
|
2830
2857
|
|
|
2831
2858
|
async navigate(sessionId, url, options) {
|
|
2859
|
+
assertPageUrlAllowed(url, this.config);
|
|
2832
2860
|
return this.getSession(sessionId).navigate(url, options);
|
|
2833
2861
|
}
|
|
2834
2862
|
|
package/src/config.mjs
CHANGED
|
@@ -14,6 +14,39 @@ export function isLoopbackHost(value) {
|
|
|
14
14
|
return ["127.0.0.1", "localhost", "::1", "[::1]"].includes(value);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
// A file:// page shows any local file the browser can read, which would get
|
|
18
|
+
// around upload_file's folder limit, so pages are not opened from file://
|
|
19
|
+
// unless MCP_BROWSER_ALLOW_FILE_URLS=1, including inside view-source: and
|
|
20
|
+
// Firefox's jar: (files in a local archive). Like
|
|
21
|
+
// Playwright MCP's default, this keeps an agent from wandering off; it is
|
|
22
|
+
// not a sandbox.
|
|
23
|
+
const WRAPPING_PROTOCOLS = new Set(["view-source:", "jar:"]);
|
|
24
|
+
|
|
25
|
+
export function assertPageUrlAllowed(url, config) {
|
|
26
|
+
if (config?.allowFileUrls || typeof url !== "string") {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let current = url;
|
|
30
|
+
for (let depth = 0; depth < 4; depth += 1) {
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
parsed = new URL(current);
|
|
34
|
+
} catch {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (WRAPPING_PROTOCOLS.has(parsed.protocol)) {
|
|
38
|
+
current = parsed.href.slice(parsed.protocol.length);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (parsed.protocol === "file:") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
"file:// URLs are not opened unless MCP_BROWSER_ALLOW_FILE_URLS=1",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
17
50
|
function allowsRemoteEndpoints(env = process.env) {
|
|
18
51
|
return (
|
|
19
52
|
isTruthyFlag(env.MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS) ||
|
|
@@ -126,6 +159,7 @@ export function loadConfig(env = process.env) {
|
|
|
126
159
|
enableUnsafeLaunchArgs: isTruthyFlag(
|
|
127
160
|
env.MCP_BROWSER_ENABLE_UNSAFE_LAUNCH_ARGS,
|
|
128
161
|
),
|
|
162
|
+
allowFileUrls: isTruthyFlag(env.MCP_BROWSER_ALLOW_FILE_URLS),
|
|
129
163
|
eventBufferSize: parsePositiveInteger(
|
|
130
164
|
env.MCP_BROWSER_EVENT_BUFFER_SIZE,
|
|
131
165
|
DEFAULT_EVENT_BUFFER_SIZE,
|
package/src/doctor.mjs
CHANGED
|
@@ -19,9 +19,14 @@ function reportEndpoints(config) {
|
|
|
19
19
|
return config.cdpBaseUrl;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// Only for an explicit doctor --url: one request, no redirects followed, and
|
|
23
|
+
// a bounded wait, so a slow or redirecting site cannot hold the report up.
|
|
24
|
+
async function probeUrl(url, timeoutMs) {
|
|
23
25
|
try {
|
|
24
|
-
const response = await fetch(url
|
|
26
|
+
const response = await fetch(url, {
|
|
27
|
+
redirect: "manual",
|
|
28
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
29
|
+
});
|
|
25
30
|
return {
|
|
26
31
|
reachable: true,
|
|
27
32
|
status: response.status,
|
|
@@ -39,6 +44,7 @@ async function probeUrl(url) {
|
|
|
39
44
|
export async function collectDoctorReport({
|
|
40
45
|
env = process.env,
|
|
41
46
|
url = null,
|
|
47
|
+
urlTimeoutMs = 5_000,
|
|
42
48
|
} = {}) {
|
|
43
49
|
const config = loadConfig(env);
|
|
44
50
|
const installedBrowsers = await detectInstalledBrowsers(env);
|
|
@@ -46,7 +52,7 @@ export async function collectDoctorReport({
|
|
|
46
52
|
|
|
47
53
|
try {
|
|
48
54
|
const browserStatus = await adapter.getBrowserStatus();
|
|
49
|
-
const targetUrl = url ? await probeUrl(url) : null;
|
|
55
|
+
const targetUrl = url ? await probeUrl(url, urlTimeoutMs) : null;
|
|
50
56
|
|
|
51
57
|
return {
|
|
52
58
|
platform: process.platform,
|
|
@@ -5,7 +5,10 @@ import {
|
|
|
5
5
|
summarizeNetworkRequests,
|
|
6
6
|
} from "./session-events.mjs";
|
|
7
7
|
import { newSessionId } from "./session-ids.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
assertPageUrlAllowed,
|
|
10
|
+
DEFAULT_FIREFOX_BIDI_WS_URL,
|
|
11
|
+
} from "./config.mjs";
|
|
9
12
|
import {
|
|
10
13
|
answerDialog,
|
|
11
14
|
createDialogAnswers,
|
|
@@ -878,9 +881,10 @@ export class FirefoxBidiSessionManager {
|
|
|
878
881
|
}
|
|
879
882
|
|
|
880
883
|
async createTab(url = "about:blank") {
|
|
881
|
-
await this.ensureConnected();
|
|
882
884
|
const targetUrl =
|
|
883
885
|
typeof url === "string" && url.trim() ? url.trim() : "about:blank";
|
|
886
|
+
assertPageUrlAllowed(targetUrl, this.config);
|
|
887
|
+
await this.ensureConnected();
|
|
884
888
|
const result = await this.send("browsingContext.create", {
|
|
885
889
|
type: "tab",
|
|
886
890
|
});
|
|
@@ -1166,6 +1170,7 @@ export class FirefoxBidiSessionManager {
|
|
|
1166
1170
|
}
|
|
1167
1171
|
|
|
1168
1172
|
async navigate(sessionId, url, options = {}) {
|
|
1173
|
+
assertPageUrlAllowed(url, this.config);
|
|
1169
1174
|
const session = this.getSession(sessionId);
|
|
1170
1175
|
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
|
1171
1176
|
const initiatedAt = new Date().toISOString();
|
package/src/tcp-relay.mjs
CHANGED
|
@@ -105,6 +105,16 @@ export function resolveRelayOptions(
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
const targetHost = normalizeHost(
|
|
109
|
+
options["target-host"],
|
|
110
|
+
DEFAULT_RELAY_TARGET_HOST,
|
|
111
|
+
);
|
|
112
|
+
if (!isLoopbackHost(targetHost) && !allowsRemoteEndpoints(env)) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
"--target-host must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
108
118
|
return {
|
|
109
119
|
listenHost,
|
|
110
120
|
listenPort: parseRelayPort(
|
|
@@ -112,10 +122,7 @@ export function resolveRelayOptions(
|
|
|
112
122
|
DEFAULT_RELAY_LISTEN_PORT,
|
|
113
123
|
"--listen-port",
|
|
114
124
|
),
|
|
115
|
-
targetHost
|
|
116
|
-
options["target-host"],
|
|
117
|
-
DEFAULT_RELAY_TARGET_HOST,
|
|
118
|
-
),
|
|
125
|
+
targetHost,
|
|
119
126
|
targetPort: parseRelayPort(
|
|
120
127
|
options["target-port"],
|
|
121
128
|
DEFAULT_RELAY_TARGET_PORT,
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
requestedBrowserFamily,
|
|
3
3
|
shouldCreateBrowserTab,
|
|
4
4
|
} from "../browser-requests.mjs";
|
|
5
|
+
import { CHANGE_NAVIGATION_MS } from "../action-changes.mjs";
|
|
5
6
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "../package-info.mjs";
|
|
6
7
|
import {
|
|
7
8
|
emptyObjectSchema,
|
|
@@ -235,6 +236,7 @@ async function attachTab(server, { targetId, url }) {
|
|
|
235
236
|
targetId ?? (await tabForUrl(server, url)),
|
|
236
237
|
);
|
|
237
238
|
server.lastAttachedSessionId = session.sessionId;
|
|
239
|
+
await waitForTabDocument(server, session);
|
|
238
240
|
const settled = await settleAfterAttach(server, session.sessionId);
|
|
239
241
|
try {
|
|
240
242
|
const snapshot = await server.browserAdapter.snapshotPage(
|
|
@@ -252,6 +254,59 @@ async function attachTab(server, { targetId, url }) {
|
|
|
252
254
|
}
|
|
253
255
|
}
|
|
254
256
|
|
|
257
|
+
const DOCUMENT_POLL_MS = 50;
|
|
258
|
+
// What a page state request can fail with while the document is replaced.
|
|
259
|
+
const DOCUMENT_REPLACED = /context|navigat|destroyed/i;
|
|
260
|
+
|
|
261
|
+
function withinMs(promise, ms) {
|
|
262
|
+
let timer;
|
|
263
|
+
const timeout = new Promise((resolve, reject) => {
|
|
264
|
+
timer = setTimeout(
|
|
265
|
+
() => reject(new Error("The page state did not arrive in time")),
|
|
266
|
+
Math.max(0, ms),
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// A tab opened a moment ago can still show its first, blank document while
|
|
273
|
+
// its page loads. That load began before the attach, so the settle below
|
|
274
|
+
// cannot see it and would find the blank page quiet, and the snapshot would
|
|
275
|
+
// be empty. So first wait, for at most the navigation limit, until the
|
|
276
|
+
// document has left about:blank for the tab's URL and is past loading. Any
|
|
277
|
+
// error other than the document being replaced ends the wait; the snapshot
|
|
278
|
+
// that follows reports it.
|
|
279
|
+
async function waitForTabDocument(server, session) {
|
|
280
|
+
const expectsPage =
|
|
281
|
+
typeof session.url === "string" &&
|
|
282
|
+
session.url !== "" &&
|
|
283
|
+
session.url !== "about:blank";
|
|
284
|
+
const deadline =
|
|
285
|
+
Date.now() +
|
|
286
|
+
(server.stepRunner?.changeTracker?.navigationMs ?? CHANGE_NAVIGATION_MS);
|
|
287
|
+
for (;;) {
|
|
288
|
+
let state = null;
|
|
289
|
+
try {
|
|
290
|
+
state = await withinMs(
|
|
291
|
+
server.browserAdapter.getPageState(session.sessionId),
|
|
292
|
+
deadline - Date.now(),
|
|
293
|
+
);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (!DOCUMENT_REPLACED.test(error?.message ?? "")) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const blank = expectsPage && state?.url === "about:blank";
|
|
300
|
+
if (
|
|
301
|
+
(state && !blank && state.readyState !== "loading") ||
|
|
302
|
+
Date.now() >= deadline
|
|
303
|
+
) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
await new Promise((resolve) => setTimeout(resolve, DOCUMENT_POLL_MS));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
255
310
|
// Waits for the page to settle before the snapshot, as a batch does before a
|
|
256
311
|
// step that reads the page: a page still loading or rendering is listed once
|
|
257
312
|
// its DOM has been quiet for a moment, for at most 2 s (10 s while it
|