mcp-browser-dev-tools 0.0.1-beta.8 → 0.0.1-beta.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-browser-dev-tools",
3
- "version": "0.0.1-beta.8",
3
+ "version": "0.0.1-beta.9",
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",
package/src/cli.mjs CHANGED
@@ -154,9 +154,7 @@ export function parseFirefoxBidiServerInfo(contents) {
154
154
  parsed.ws_host ?? parsed.host ?? parsed.hostname ?? "127.0.0.1",
155
155
  );
156
156
  const protocol =
157
- parsed.protocol === "https" || parsed.protocol === "wss"
158
- ? "https:"
159
- : "http:";
157
+ parsed.protocol === "https" || parsed.protocol === "wss" ? "wss:" : "ws:";
160
158
  const endpoint = new URL(`${protocol}//${host}`);
161
159
  endpoint.port = String(port);
162
160
  endpoint.pathname = normalizeFirefoxDoctorPath(
@@ -239,10 +239,6 @@ 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
242
  function isHttpProtocol(protocol) {
247
243
  return protocol === "http:" || protocol === "https:";
248
244
  }
@@ -251,20 +247,12 @@ function isRootPathname(pathname) {
251
247
  return pathname === "" || pathname === "/";
252
248
  }
253
249
 
254
- function isAbortError(error) {
255
- return (
256
- error?.name === "AbortError" ||
257
- error?.name === "TimeoutError" ||
258
- error?.code === "ABORT_ERR"
259
- );
260
- }
261
-
262
- function toHttpEndpointUrl(endpointUrl) {
250
+ function toWebSocketEndpointUrl(endpointUrl) {
263
251
  const url = new URL(endpointUrl);
264
- if (url.protocol === "ws:") {
265
- url.protocol = "http:";
266
- } else if (url.protocol === "wss:") {
267
- url.protocol = "https:";
252
+ if (url.protocol === "http:") {
253
+ url.protocol = "ws:";
254
+ } else if (url.protocol === "https:") {
255
+ url.protocol = "wss:";
268
256
  }
269
257
 
270
258
  return url;
@@ -349,7 +337,6 @@ export class FirefoxBidiSessionManager {
349
337
  this.sessions = new Map();
350
338
  this.websocketFactory =
351
339
  options.websocketFactory ?? ((url) => new WebSocket(url));
352
- this.fetchImpl = options.fetchImpl ?? fetch;
353
340
  this.connectionTimeoutMs = options.connectionTimeoutMs ?? 2_000;
354
341
  this.pending = new Map();
355
342
  this.nextMessageId = 1;
@@ -358,7 +345,6 @@ export class FirefoxBidiSessionManager {
358
345
  this.capabilities = null;
359
346
  this.connectPromise = null;
360
347
  this.resolvedWebSocketUrl = null;
361
- this.deleteSessionUrl = null;
362
348
  }
363
349
 
364
350
  async ensureConnected() {
@@ -384,7 +370,6 @@ export class FirefoxBidiSessionManager {
384
370
  async openConnection() {
385
371
  const endpoint = await this.resolveConnectionEndpoint();
386
372
  this.resolvedWebSocketUrl = endpoint.webSocketUrl;
387
- this.deleteSessionUrl = endpoint.deleteSessionUrl ?? null;
388
373
  this.websocket = this.websocketFactory(endpoint.webSocketUrl);
389
374
 
390
375
  await new Promise((resolve, reject) => {
@@ -470,78 +455,32 @@ export class FirefoxBidiSessionManager {
470
455
 
471
456
  async resolveConnectionEndpoint() {
472
457
  const configuredUrl = new URL(this.config.firefoxBidiWsUrl);
473
- if (
474
- isHttpProtocol(configuredUrl.protocol) ||
475
- (isWebSocketProtocol(configuredUrl.protocol) &&
476
- isRootPathname(configuredUrl.pathname))
477
- ) {
478
- return this.createWebDriverSession(configuredUrl);
479
- }
480
-
481
- return {
482
- webSocketUrl: configuredUrl.toString(),
483
- requiresSessionNew: true,
484
- sessionId: null,
485
- capabilities: null,
486
- deleteSessionUrl: null,
487
- };
488
- }
489
-
490
- async createWebDriverSession(endpointUrl) {
491
- const sessionUrl = joinUrlPath(toHttpEndpointUrl(endpointUrl), "/session");
492
- let response;
493
- try {
494
- response = await this.fetchImpl(sessionUrl, {
495
- method: "POST",
496
- headers: {
497
- "content-type": "application/json",
498
- },
499
- body: JSON.stringify({
500
- capabilities: {
501
- alwaysMatch: {
502
- webSocketUrl: true,
503
- },
504
- },
505
- }),
506
- signal: AbortSignal.timeout(this.connectionTimeoutMs),
507
- });
508
- } catch (error) {
509
- if (isAbortError(error)) {
510
- throw new Error(
511
- `Timed out creating Firefox WebDriver session at ${sessionUrl}`,
512
- {
513
- cause: error,
514
- },
515
- );
516
- }
517
458
 
518
- throw error;
459
+ if (isHttpProtocol(configuredUrl.protocol)) {
460
+ return {
461
+ webSocketUrl: joinUrlPath(
462
+ toWebSocketEndpointUrl(configuredUrl),
463
+ "/session",
464
+ ).toString(),
465
+ requiresSessionNew: true,
466
+ sessionId: null,
467
+ capabilities: null,
468
+ };
519
469
  }
520
470
 
521
- if (!response.ok) {
522
- throw new Error(
523
- `Failed to create Firefox WebDriver session at ${sessionUrl}: ${response.status} ${response.statusText}`,
524
- );
525
- }
526
-
527
- const payload = await response.json();
528
- const value = payload?.value ?? payload;
529
- const sessionId = value?.sessionId ?? null;
530
- const capabilities = value?.capabilities ?? {};
531
- const webSocketUrl = capabilities?.webSocketUrl ?? null;
532
-
533
- if (!sessionId || !webSocketUrl) {
534
- throw new Error(
535
- `Firefox WebDriver session at ${sessionUrl} did not return a webSocketUrl`,
536
- );
471
+ if (isRootPathname(configuredUrl.pathname)) {
472
+ configuredUrl.pathname = "/session";
537
473
  }
538
474
 
539
475
  return {
540
- webSocketUrl,
541
- requiresSessionNew: false,
542
- sessionId,
543
- capabilities,
544
- deleteSessionUrl: joinUrlPath(sessionUrl, sessionId).toString(),
476
+ webSocketUrl: configuredUrl.toString(),
477
+ requiresSessionNew: configuredUrl.pathname === "/session",
478
+ sessionId:
479
+ configuredUrl.pathname === "/session"
480
+ ? null
481
+ : configuredUrl.pathname.split("/").at(-1) ||
482
+ configuredUrl.toString(),
483
+ capabilities: null,
545
484
  };
546
485
  }
547
486
 
@@ -571,14 +510,6 @@ export class FirefoxBidiSessionManager {
571
510
  return;
572
511
  }
573
512
 
574
- if (this.deleteSessionUrl) {
575
- await this.fetchImpl(this.deleteSessionUrl, {
576
- method: "DELETE",
577
- signal: AbortSignal.timeout(2_000),
578
- }).catch(() => {});
579
- return;
580
- }
581
-
582
513
  if (this.websocket?.readyState === WebSocket.OPEN) {
583
514
  await this.send("session.end", {}).catch(() => {});
584
515
  }
@@ -1119,6 +1050,5 @@ export class FirefoxBidiSessionManager {
1119
1050
  this.browserSessionId = null;
1120
1051
  this.capabilities = null;
1121
1052
  this.resolvedWebSocketUrl = null;
1122
- this.deleteSessionUrl = null;
1123
1053
  }
1124
1054
  }