mcp-browser-dev-tools 0.0.1-beta.5 → 0.0.1-beta.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
@@ -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.7",
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,15 @@ 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;
352
+ this.deleteSessionUrl = null;
319
353
  }
320
354
 
321
355
  async ensureConnected() {
@@ -339,7 +373,10 @@ export class FirefoxBidiSessionManager {
339
373
  }
340
374
 
341
375
  async openConnection() {
342
- this.websocket = this.websocketFactory(this.config.firefoxBidiWsUrl);
376
+ const endpoint = await this.resolveConnectionEndpoint();
377
+ this.resolvedWebSocketUrl = endpoint.webSocketUrl;
378
+ this.deleteSessionUrl = endpoint.deleteSessionUrl ?? null;
379
+ this.websocket = this.websocketFactory(endpoint.webSocketUrl);
343
380
 
344
381
  await new Promise((resolve, reject) => {
345
382
  let settled = false;
@@ -387,14 +424,121 @@ export class FirefoxBidiSessionManager {
387
424
  });
388
425
  });
389
426
 
390
- const result = await this.send("session.new", {
391
- capabilities: {
392
- alwaysMatch: {},
427
+ if (endpoint.requiresSessionNew) {
428
+ const result = await this.send("session.new", {
429
+ capabilities: {
430
+ alwaysMatch: {},
431
+ },
432
+ });
433
+
434
+ this.browserSessionId = result.sessionId;
435
+ this.capabilities = result.capabilities ?? {};
436
+ return;
437
+ }
438
+
439
+ this.browserSessionId = endpoint.sessionId;
440
+ this.capabilities = endpoint.capabilities ?? {};
441
+ }
442
+
443
+ async resolveConnectionEndpoint() {
444
+ const configuredUrl = new URL(this.config.firefoxBidiWsUrl);
445
+ if (
446
+ isHttpProtocol(configuredUrl.protocol) ||
447
+ (isWebSocketProtocol(configuredUrl.protocol) &&
448
+ isRootPathname(configuredUrl.pathname))
449
+ ) {
450
+ return this.createWebDriverSession(configuredUrl);
451
+ }
452
+
453
+ return {
454
+ webSocketUrl: configuredUrl.toString(),
455
+ requiresSessionNew: true,
456
+ sessionId: null,
457
+ capabilities: null,
458
+ deleteSessionUrl: null,
459
+ };
460
+ }
461
+
462
+ async createWebDriverSession(endpointUrl) {
463
+ const sessionUrl = joinUrlPath(toHttpEndpointUrl(endpointUrl), "/session");
464
+ const response = await this.fetchImpl(sessionUrl, {
465
+ method: "POST",
466
+ headers: {
467
+ "content-type": "application/json",
393
468
  },
469
+ body: JSON.stringify({
470
+ capabilities: {
471
+ alwaysMatch: {
472
+ webSocketUrl: true,
473
+ },
474
+ },
475
+ }),
476
+ });
477
+
478
+ if (!response.ok) {
479
+ throw new Error(
480
+ `Failed to create Firefox WebDriver session at ${sessionUrl}: ${response.status} ${response.statusText}`,
481
+ );
482
+ }
483
+
484
+ const payload = await response.json();
485
+ const value = payload?.value ?? payload;
486
+ const sessionId = value?.sessionId ?? null;
487
+ const capabilities = value?.capabilities ?? {};
488
+ const webSocketUrl = capabilities?.webSocketUrl ?? null;
489
+
490
+ if (!sessionId || !webSocketUrl) {
491
+ throw new Error(
492
+ `Firefox WebDriver session at ${sessionUrl} did not return a webSocketUrl`,
493
+ );
494
+ }
495
+
496
+ return {
497
+ webSocketUrl,
498
+ requiresSessionNew: false,
499
+ sessionId,
500
+ capabilities,
501
+ deleteSessionUrl: joinUrlPath(sessionUrl, sessionId).toString(),
502
+ };
503
+ }
504
+
505
+ async waitForWebSocketClose(websocket, timeoutMs = 250) {
506
+ if (!websocket || websocket.readyState === WebSocket.CLOSED) {
507
+ return;
508
+ }
509
+
510
+ await new Promise((resolve) => {
511
+ let settled = false;
512
+ const done = () => {
513
+ if (settled) {
514
+ return;
515
+ }
516
+ settled = true;
517
+ resolve();
518
+ };
519
+
520
+ websocket.addEventListener("close", done, { once: true });
521
+ const timer = setTimeout(done, timeoutMs);
522
+ timer.unref?.();
394
523
  });
524
+ }
525
+
526
+ async closeBrowserSession() {
527
+ if (!this.browserSessionId) {
528
+ return;
529
+ }
530
+
531
+ if (this.deleteSessionUrl) {
532
+ await this.fetchImpl(this.deleteSessionUrl, {
533
+ method: "DELETE",
534
+ signal: AbortSignal.timeout(2_000),
535
+ }).catch(() => {});
536
+ return;
537
+ }
395
538
 
396
- this.browserSessionId = result.sessionId;
397
- this.capabilities = result.capabilities ?? {};
539
+ if (this.websocket?.readyState === WebSocket.OPEN) {
540
+ await this.send("session.end", {}).catch(() => {});
541
+ }
398
542
  }
399
543
 
400
544
  rejectPending(error) {
@@ -475,7 +619,7 @@ export class FirefoxBidiSessionManager {
475
619
  browser: this.capabilities?.browserName || "firefox",
476
620
  browserVersion: this.capabilities?.browserVersion ?? null,
477
621
  protocol: "webdriver-bidi",
478
- webSocketUrl: this.config.firefoxBidiWsUrl,
622
+ webSocketUrl: this.resolvedWebSocketUrl ?? this.config.firefoxBidiWsUrl,
479
623
  };
480
624
  } catch (error) {
481
625
  return {
@@ -918,10 +1062,20 @@ export class FirefoxBidiSessionManager {
918
1062
  ),
919
1063
  );
920
1064
 
921
- if (this.websocket && this.websocket.readyState < WebSocket.CLOSING) {
922
- this.websocket.close();
1065
+ const websocket = this.websocket;
1066
+
1067
+ await this.closeBrowserSession();
1068
+
1069
+ if (websocket && websocket.readyState < WebSocket.CLOSING) {
1070
+ websocket.close();
1071
+ await this.waitForWebSocketClose(websocket);
923
1072
  }
924
1073
 
925
1074
  this.sessions.clear();
1075
+ this.websocket = null;
1076
+ this.browserSessionId = null;
1077
+ this.capabilities = null;
1078
+ this.resolvedWebSocketUrl = null;
1079
+ this.deleteSessionUrl = null;
926
1080
  }
927
1081
  }