mcp-browser-dev-tools 0.5.5 → 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 CHANGED
@@ -187,9 +187,9 @@ Every setting, per-client examples, and how the server reuses or launches browse
187
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.
188
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
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
- - `navigate` opens any URL the browser can, `file://` included. 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`.
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
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.5`.
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
193
 
194
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
195
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mcp-browser-dev-tools",
3
3
  "mcpName": "io.github.Hinaser/mcp-browser-dev-tools",
4
- "version": "0.5.5",
4
+ "version": "0.5.7",
5
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",
@@ -17,7 +17,7 @@ import {
17
17
  buildBrowserLaunchArgs,
18
18
  findBrowserExecutable,
19
19
  } from "./browser-launcher.mjs";
20
- import { isLoopbackHost } from "./config.mjs";
20
+ import { assertPageUrlAllowed, isLoopbackHost } from "./config.mjs";
21
21
  import { collectDoctorReport } from "./doctor.mjs";
22
22
 
23
23
  const FIREFOX_BIDI_SERVER_FILENAME = "WebDriverBiDiServer.json";
@@ -41,15 +41,6 @@ function sleep(ms) {
41
41
  });
42
42
  }
43
43
 
44
- function isHttpLikeUrl(value) {
45
- try {
46
- const url = new URL(value);
47
- return url.protocol === "http:" || url.protocol === "https:";
48
- } catch {
49
- return false;
50
- }
51
- }
52
-
53
44
  function normalizeFirefoxDoctorHost(value) {
54
45
  const host = typeof value === "string" ? value.trim() : "";
55
46
  if (!host || host === "0.0.0.0" || host === "::" || host === "[::]") {
@@ -593,6 +584,7 @@ export async function launchBrowser(
593
584
  const family = resolveLaunchFamily(config.browserFamily, browserFamily);
594
585
  const targetUrl = normalizeStringValue(url) ?? "about:blank";
595
586
  assertLaunchUrl(targetUrl);
587
+ assertPageUrlAllowed(targetUrl, config);
596
588
  const resolvedPort =
597
589
  normalizeIntegerValue(port, "port", { minimum: 1 }) ??
598
590
  resolveDefaultPort(config, family);
@@ -720,10 +712,10 @@ export async function launchBrowser(
720
712
  doctorEnv.CDP_BASE_URL = result.endpoint;
721
713
  }
722
714
 
723
- let report = await collectDoctorReportFn({
724
- env: doctorEnv,
725
- url: isHttpLikeUrl(targetUrl) ? targetUrl : null,
726
- });
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 });
727
719
 
728
720
  while (
729
721
  !report.browserStatus.available &&
@@ -737,10 +729,7 @@ export async function launchBrowser(
737
729
  });
738
730
  }
739
731
 
740
- report = await collectDoctorReportFn({
741
- env: doctorEnv,
742
- url: isHttpLikeUrl(targetUrl) ? targetUrl : null,
743
- });
732
+ report = await collectDoctorReportFn({ env: doctorEnv });
744
733
  }
745
734
 
746
735
  result.endpoint =
@@ -9,7 +9,11 @@ import {
9
9
  InFlightRequests,
10
10
  summarizeNetworkRequests,
11
11
  } from "./session-events.mjs";
12
- import { DEFAULT_CDP_BASE_URL, isLoopbackHost } from "./config.mjs";
12
+ import {
13
+ assertPageUrlAllowed,
14
+ DEFAULT_CDP_BASE_URL,
15
+ isLoopbackHost,
16
+ } from "./config.mjs";
13
17
  import {
14
18
  answerDialog,
15
19
  createDialogAnswers,
@@ -2696,6 +2700,7 @@ export class CdpSessionManager {
2696
2700
  async createTab(url = "about:blank") {
2697
2701
  const targetUrl =
2698
2702
  typeof url === "string" && url.trim() ? url.trim() : "about:blank";
2703
+ assertPageUrlAllowed(targetUrl, this.config);
2699
2704
  const result = await this.sendBrowserCommand("Target.createTarget", {
2700
2705
  url: targetUrl,
2701
2706
  });
@@ -2851,6 +2856,7 @@ export class CdpSessionManager {
2851
2856
  }
2852
2857
 
2853
2858
  async navigate(sessionId, url, options) {
2859
+ assertPageUrlAllowed(url, this.config);
2854
2860
  return this.getSession(sessionId).navigate(url, options);
2855
2861
  }
2856
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
- async function probeUrl(url) {
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 { DEFAULT_FIREFOX_BIDI_WS_URL } from "./config.mjs";
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();
@@ -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