mcp-browser-dev-tools 0.0.1-beta.5 → 0.0.1-beta.6

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
@@ -225,7 +225,7 @@ mbdt relay --wsl
225
225
 
226
226
  - `MCP_BROWSER_FAMILY` defaults to `chromium`; set `firefox` for Firefox BiDi
227
227
  - `CDP_BASE_URL` defaults to `http://127.0.0.1:9222`
228
- - `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`
228
+ - `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`; when pointed at the root Firefox remote debugging port, the broker first requests `webSocketUrl` from `POST /session` and then connects to the returned BiDi session socket
229
229
  - `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
230
230
  - `MCP_BROWSER_ENABLE_EVAL=1` enables `evaluate_js`
231
231
  - `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` allows non-loopback CDP or BiDi endpoints
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-browser-dev-tools",
3
- "version": "0.0.1-beta.5",
3
+ "version": "0.0.1-beta.6",
4
4
  "description": "Local MCP server for Chromium DevTools Protocol and Firefox WebDriver BiDi",
5
5
  "author": "Hinaser <rindo.hinase@gmail.com>",
6
6
  "type": "module",
@@ -239,6 +239,37 @@ function normalizeRequestedDepth(depth) {
239
239
  return Number.isInteger(depth) && depth > 0 ? depth : 2;
240
240
  }
241
241
 
242
+ function isWebSocketProtocol(protocol) {
243
+ return protocol === "ws:" || protocol === "wss:";
244
+ }
245
+
246
+ function isHttpProtocol(protocol) {
247
+ return protocol === "http:" || protocol === "https:";
248
+ }
249
+
250
+ function isRootPathname(pathname) {
251
+ return pathname === "" || pathname === "/";
252
+ }
253
+
254
+ function toHttpEndpointUrl(endpointUrl) {
255
+ const url = new URL(endpointUrl);
256
+ if (url.protocol === "ws:") {
257
+ url.protocol = "http:";
258
+ } else if (url.protocol === "wss:") {
259
+ url.protocol = "https:";
260
+ }
261
+
262
+ return url;
263
+ }
264
+
265
+ function joinUrlPath(baseUrl, pathname) {
266
+ const url = new URL(baseUrl);
267
+ const basePath = url.pathname.replace(/\/+$/, "");
268
+ const nextPath = pathname.replace(/^\/+/, "");
269
+ url.pathname = `${basePath}/${nextPath}`.replace(/\/{2,}/g, "/");
270
+ return url;
271
+ }
272
+
242
273
  class FirefoxBidiTabSession {
243
274
  constructor(target, options = {}) {
244
275
  this.id = crypto.randomUUID();
@@ -310,12 +341,14 @@ export class FirefoxBidiSessionManager {
310
341
  this.sessions = new Map();
311
342
  this.websocketFactory =
312
343
  options.websocketFactory ?? ((url) => new WebSocket(url));
344
+ this.fetchImpl = options.fetchImpl ?? fetch;
313
345
  this.pending = new Map();
314
346
  this.nextMessageId = 1;
315
347
  this.websocket = null;
316
348
  this.browserSessionId = null;
317
349
  this.capabilities = null;
318
350
  this.connectPromise = null;
351
+ this.resolvedWebSocketUrl = null;
319
352
  }
320
353
 
321
354
  async ensureConnected() {
@@ -339,7 +372,9 @@ export class FirefoxBidiSessionManager {
339
372
  }
340
373
 
341
374
  async openConnection() {
342
- this.websocket = this.websocketFactory(this.config.firefoxBidiWsUrl);
375
+ const endpoint = await this.resolveConnectionEndpoint();
376
+ this.resolvedWebSocketUrl = endpoint.webSocketUrl;
377
+ this.websocket = this.websocketFactory(endpoint.webSocketUrl);
343
378
 
344
379
  await new Promise((resolve, reject) => {
345
380
  let settled = false;
@@ -387,14 +422,80 @@ export class FirefoxBidiSessionManager {
387
422
  });
388
423
  });
389
424
 
390
- const result = await this.send("session.new", {
391
- capabilities: {
392
- alwaysMatch: {},
425
+ if (endpoint.requiresSessionNew) {
426
+ const result = await this.send("session.new", {
427
+ capabilities: {
428
+ alwaysMatch: {},
429
+ },
430
+ });
431
+
432
+ this.browserSessionId = result.sessionId;
433
+ this.capabilities = result.capabilities ?? {};
434
+ return;
435
+ }
436
+
437
+ this.browserSessionId = endpoint.sessionId;
438
+ this.capabilities = endpoint.capabilities ?? {};
439
+ }
440
+
441
+ async resolveConnectionEndpoint() {
442
+ const configuredUrl = new URL(this.config.firefoxBidiWsUrl);
443
+ if (
444
+ isHttpProtocol(configuredUrl.protocol) ||
445
+ (isWebSocketProtocol(configuredUrl.protocol) &&
446
+ isRootPathname(configuredUrl.pathname))
447
+ ) {
448
+ return this.createWebDriverSession(configuredUrl);
449
+ }
450
+
451
+ return {
452
+ webSocketUrl: configuredUrl.toString(),
453
+ requiresSessionNew: true,
454
+ sessionId: null,
455
+ capabilities: null,
456
+ };
457
+ }
458
+
459
+ async createWebDriverSession(endpointUrl) {
460
+ const sessionUrl = joinUrlPath(toHttpEndpointUrl(endpointUrl), "/session");
461
+ const response = await this.fetchImpl(sessionUrl, {
462
+ method: "POST",
463
+ headers: {
464
+ "content-type": "application/json",
393
465
  },
466
+ body: JSON.stringify({
467
+ capabilities: {
468
+ alwaysMatch: {
469
+ webSocketUrl: true,
470
+ },
471
+ },
472
+ }),
394
473
  });
395
474
 
396
- this.browserSessionId = result.sessionId;
397
- this.capabilities = result.capabilities ?? {};
475
+ if (!response.ok) {
476
+ throw new Error(
477
+ `Failed to create Firefox WebDriver session at ${sessionUrl}: ${response.status} ${response.statusText}`,
478
+ );
479
+ }
480
+
481
+ const payload = await response.json();
482
+ const value = payload?.value ?? payload;
483
+ const sessionId = value?.sessionId ?? null;
484
+ const capabilities = value?.capabilities ?? {};
485
+ const webSocketUrl = capabilities?.webSocketUrl ?? null;
486
+
487
+ if (!sessionId || !webSocketUrl) {
488
+ throw new Error(
489
+ `Firefox WebDriver session at ${sessionUrl} did not return a webSocketUrl`,
490
+ );
491
+ }
492
+
493
+ return {
494
+ webSocketUrl,
495
+ requiresSessionNew: false,
496
+ sessionId,
497
+ capabilities,
498
+ };
398
499
  }
399
500
 
400
501
  rejectPending(error) {
@@ -475,7 +576,7 @@ export class FirefoxBidiSessionManager {
475
576
  browser: this.capabilities?.browserName || "firefox",
476
577
  browserVersion: this.capabilities?.browserVersion ?? null,
477
578
  protocol: "webdriver-bidi",
478
- webSocketUrl: this.config.firefoxBidiWsUrl,
579
+ webSocketUrl: this.resolvedWebSocketUrl ?? this.config.firefoxBidiWsUrl,
479
580
  };
480
581
  } catch (error) {
481
582
  return {