mcp-browser-dev-tools 0.0.2-beta.0 → 0.0.3

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.
@@ -1,5 +1,9 @@
1
1
  import { encodeMessage, MessageBuffer } from "./json-rpc-stdio.mjs";
2
2
  import { createLogger } from "./logger.mjs";
3
+ import {
4
+ launchBrowser as launchLocalBrowser,
5
+ supportedLaunchFamilies,
6
+ } from "./browser-launch-service.mjs";
3
7
  import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
4
8
 
5
9
  const SERVER_NAME = PACKAGE_NAME;
@@ -211,6 +215,87 @@ function waitForInputSchema() {
211
215
  };
212
216
  }
213
217
 
218
+ function compareSessionsSchema(properties, required) {
219
+ return {
220
+ type: "object",
221
+ properties: {
222
+ sessionIdA: {
223
+ type: "string",
224
+ },
225
+ sessionIdB: {
226
+ type: "string",
227
+ },
228
+ ...properties,
229
+ },
230
+ required: ["sessionIdA", "sessionIdB", ...required],
231
+ additionalProperties: false,
232
+ };
233
+ }
234
+
235
+ function storageInputSchema() {
236
+ return {
237
+ type: "object",
238
+ properties: {
239
+ sessionId: {
240
+ type: "string",
241
+ },
242
+ area: {
243
+ type: "string",
244
+ enum: ["all", "localStorage", "sessionStorage"],
245
+ },
246
+ },
247
+ required: ["sessionId"],
248
+ additionalProperties: false,
249
+ };
250
+ }
251
+
252
+ function captureDebugReportInputSchema(browserFamily) {
253
+ return {
254
+ type: "object",
255
+ properties: {
256
+ sessionId: {
257
+ type: "string",
258
+ },
259
+ consoleLimit: {
260
+ type: "integer",
261
+ minimum: 1,
262
+ },
263
+ networkLimit: {
264
+ type: "integer",
265
+ minimum: 1,
266
+ },
267
+ includeScreenshot: {
268
+ type: "boolean",
269
+ },
270
+ screenshotFormat: {
271
+ type: "string",
272
+ enum: screenshotFormatsFor(browserFamily),
273
+ },
274
+ },
275
+ required: ["sessionId"],
276
+ additionalProperties: false,
277
+ };
278
+ }
279
+
280
+ function restoreSessionSnapshotInputSchema() {
281
+ return {
282
+ type: "object",
283
+ properties: {
284
+ sessionId: {
285
+ type: "string",
286
+ },
287
+ snapshot: {
288
+ type: "string",
289
+ },
290
+ clearStorage: {
291
+ type: "boolean",
292
+ },
293
+ },
294
+ required: ["sessionId", "snapshot"],
295
+ additionalProperties: false,
296
+ };
297
+ }
298
+
214
299
  function newTabInputSchema(browserFamily) {
215
300
  const properties = {
216
301
  url: {
@@ -235,10 +320,178 @@ function newTabInputSchema(browserFamily) {
235
320
  };
236
321
  }
237
322
 
323
+ function unsafeArgsProperty() {
324
+ return {
325
+ type: "array",
326
+ items: {
327
+ type: "string",
328
+ },
329
+ };
330
+ }
331
+
332
+ function launchBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
333
+ const properties = {
334
+ url: {
335
+ type: "string",
336
+ },
337
+ browserFamily: {
338
+ type: "string",
339
+ enum: supportedLaunchFamilies(browserFamily),
340
+ },
341
+ port: {
342
+ type: "integer",
343
+ minimum: 1,
344
+ },
345
+ address: {
346
+ type: "string",
347
+ },
348
+ userDataDir: {
349
+ type: "string",
350
+ },
351
+ waitMs: {
352
+ type: "integer",
353
+ minimum: 0,
354
+ },
355
+ skipDoctor: {
356
+ type: "boolean",
357
+ },
358
+ };
359
+ if (enableUnsafeLaunchArgs) {
360
+ properties.unsafeArgs = unsafeArgsProperty();
361
+ }
362
+ const required = browserFamily === "auto" ? ["browserFamily"] : [];
363
+
364
+ return {
365
+ type: "object",
366
+ properties,
367
+ required,
368
+ additionalProperties: false,
369
+ };
370
+ }
371
+
372
+ function ensureBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
373
+ const properties = {
374
+ browserFamily: {
375
+ type: "string",
376
+ enum: supportedLaunchFamilies(browserFamily),
377
+ },
378
+ url: {
379
+ type: "string",
380
+ },
381
+ createTab: {
382
+ type: "boolean",
383
+ },
384
+ launchIfMissing: {
385
+ type: "boolean",
386
+ },
387
+ port: {
388
+ type: "integer",
389
+ minimum: 1,
390
+ },
391
+ address: {
392
+ type: "string",
393
+ },
394
+ userDataDir: {
395
+ type: "string",
396
+ },
397
+ waitMs: {
398
+ type: "integer",
399
+ minimum: 0,
400
+ },
401
+ skipDoctor: {
402
+ type: "boolean",
403
+ },
404
+ };
405
+ if (enableUnsafeLaunchArgs) {
406
+ properties.unsafeArgs = unsafeArgsProperty();
407
+ }
408
+
409
+ return {
410
+ type: "object",
411
+ properties,
412
+ required: browserFamily === "auto" ? ["browserFamily"] : [],
413
+ additionalProperties: false,
414
+ };
415
+ }
416
+
417
+ function requestedBrowserFamily(configuredFamily, requestedFamily) {
418
+ if (requestedFamily) {
419
+ return requestedFamily;
420
+ }
421
+
422
+ return configuredFamily === "auto" ? null : configuredFamily;
423
+ }
424
+
425
+ function isRequestedBrowserAvailable(
426
+ status,
427
+ configuredFamily,
428
+ requestedFamily,
429
+ ) {
430
+ if (configuredFamily !== "auto" || !requestedFamily) {
431
+ return Boolean(status?.available);
432
+ }
433
+
434
+ return Boolean(status?.browsers?.[requestedFamily]?.available);
435
+ }
436
+
437
+ function shouldCreateBrowserTab(args) {
438
+ if (args.createTab !== undefined) {
439
+ return args.createTab;
440
+ }
441
+
442
+ return typeof args.url === "string" && args.url.trim() !== "";
443
+ }
444
+
445
+ function sameValue(left, right) {
446
+ return JSON.stringify(left) === JSON.stringify(right);
447
+ }
448
+
449
+ function compareField(left, right) {
450
+ return {
451
+ equal: sameValue(left, right),
452
+ a: left,
453
+ b: right,
454
+ };
455
+ }
456
+
457
+ function summarizeComparablePageState(page = {}) {
458
+ return {
459
+ browserFamily: page.browserFamily ?? null,
460
+ url: page.url ?? null,
461
+ title: page.title ?? null,
462
+ readyState: page.readyState ?? null,
463
+ visibilityState: page.visibilityState ?? null,
464
+ viewport: page.viewport ?? null,
465
+ scroll: page.scroll ?? null,
466
+ };
467
+ }
468
+
469
+ function summarizeComparableElement(result = {}) {
470
+ if (!result.found || !result.node) {
471
+ return {
472
+ found: false,
473
+ selector: result.selector ?? null,
474
+ error: result.error ?? null,
475
+ };
476
+ }
477
+
478
+ return {
479
+ found: true,
480
+ selector: result.selector ?? null,
481
+ tagName: result.node.tagName ?? null,
482
+ accessibleName: result.node.accessibleName ?? null,
483
+ role: result.node.role ?? null,
484
+ visible: result.node.visible ?? null,
485
+ disabled: result.node.disabled ?? null,
486
+ textContent: result.node.textContent ?? null,
487
+ };
488
+ }
489
+
238
490
  export class McpBrowserDevToolsServer {
239
491
  constructor({
240
492
  config,
241
493
  browserAdapter,
494
+ launchBrowser = launchLocalBrowser,
242
495
  input = process.stdin,
243
496
  output = process.stdout,
244
497
  errorOutput = process.stderr,
@@ -250,6 +503,7 @@ export class McpBrowserDevToolsServer {
250
503
  }) {
251
504
  this.config = config;
252
505
  this.browserAdapter = browserAdapter;
506
+ this.launchBrowser = launchBrowser;
253
507
  this.input = input;
254
508
  this.output = output;
255
509
  this.errorOutput = errorOutput;
@@ -290,6 +544,105 @@ export class McpBrowserDevToolsServer {
290
544
  }),
291
545
  },
292
546
  ],
547
+ [
548
+ "launch_browser",
549
+ {
550
+ definition: {
551
+ name: "launch_browser",
552
+ description:
553
+ "Launch a local debug-enabled browser process that matches the current broker configuration and return launch details plus an optional doctor report.",
554
+ inputSchema: launchBrowserInputSchema(
555
+ this.config.browserFamily,
556
+ this.config.enableUnsafeLaunchArgs,
557
+ ),
558
+ },
559
+ handler: async (args) =>
560
+ this.launchBrowser({
561
+ config: this.config,
562
+ browserFamily: args.browserFamily,
563
+ url: args.url,
564
+ port: args.port,
565
+ address: args.address,
566
+ userDataDir: args.userDataDir,
567
+ unsafeArgs: args.unsafeArgs,
568
+ waitMs: args.waitMs,
569
+ skipDoctor: args.skipDoctor,
570
+ }),
571
+ },
572
+ ],
573
+ [
574
+ "ensure_browser",
575
+ {
576
+ definition: {
577
+ name: "ensure_browser",
578
+ description:
579
+ "Ensure a compatible browser is reachable through the current broker. If needed, launch one locally and optionally open a tab for the requested URL.",
580
+ inputSchema: ensureBrowserInputSchema(
581
+ this.config.browserFamily,
582
+ this.config.enableUnsafeLaunchArgs,
583
+ ),
584
+ },
585
+ handler: async (args) => {
586
+ const browserFamily = requestedBrowserFamily(
587
+ this.config.browserFamily,
588
+ args.browserFamily,
589
+ );
590
+ const currentStatus = await this.browserAdapter.getBrowserStatus();
591
+ const available = isRequestedBrowserAvailable(
592
+ currentStatus,
593
+ this.config.browserFamily,
594
+ browserFamily,
595
+ );
596
+ let launch = null;
597
+ let status = currentStatus;
598
+
599
+ if (!available) {
600
+ if (args.launchIfMissing === false) {
601
+ return {
602
+ browserFamily,
603
+ available: false,
604
+ launched: false,
605
+ status,
606
+ tab: null,
607
+ };
608
+ }
609
+
610
+ launch = await this.launchBrowser({
611
+ config: this.config,
612
+ browserFamily,
613
+ url: args.url,
614
+ port: args.port,
615
+ address: args.address,
616
+ userDataDir: args.userDataDir,
617
+ unsafeArgs: args.unsafeArgs,
618
+ waitMs: args.waitMs,
619
+ skipDoctor: args.skipDoctor,
620
+ });
621
+ status = await this.browserAdapter.getBrowserStatus();
622
+ }
623
+
624
+ let tab = null;
625
+ if (shouldCreateBrowserTab(args)) {
626
+ tab = await this.browserAdapter.createTab(args.url, {
627
+ browserFamily,
628
+ });
629
+ }
630
+
631
+ return {
632
+ browserFamily,
633
+ available: isRequestedBrowserAvailable(
634
+ status,
635
+ this.config.browserFamily,
636
+ browserFamily,
637
+ ),
638
+ launched: Boolean(launch),
639
+ launch,
640
+ status,
641
+ tab,
642
+ };
643
+ },
644
+ },
645
+ ],
293
646
  [
294
647
  "list_sessions",
295
648
  {
@@ -399,6 +752,111 @@ export class McpBrowserDevToolsServer {
399
752
  this.browserAdapter.getPageState(args.sessionId),
400
753
  },
401
754
  ],
755
+ [
756
+ "compare_page_state",
757
+ {
758
+ definition: {
759
+ name: "compare_page_state",
760
+ description:
761
+ "Compare bounded page state across two attached sessions, useful for cross-browser checks in auto mode.",
762
+ inputSchema: compareSessionsSchema({}, []),
763
+ },
764
+ handler: async (args) => {
765
+ const [pageA, pageB] = await Promise.all([
766
+ this.browserAdapter.getPageState(args.sessionIdA),
767
+ this.browserAdapter.getPageState(args.sessionIdB),
768
+ ]);
769
+ const left = summarizeComparablePageState(pageA);
770
+ const right = summarizeComparablePageState(pageB);
771
+ const fields = {
772
+ browserFamily: compareField(
773
+ left.browserFamily,
774
+ right.browserFamily,
775
+ ),
776
+ url: compareField(left.url, right.url),
777
+ title: compareField(left.title, right.title),
778
+ readyState: compareField(left.readyState, right.readyState),
779
+ visibilityState: compareField(
780
+ left.visibilityState,
781
+ right.visibilityState,
782
+ ),
783
+ viewport: compareField(left.viewport, right.viewport),
784
+ scroll: compareField(left.scroll, right.scroll),
785
+ };
786
+
787
+ return {
788
+ matches: Object.values(fields).every((field) => field.equal),
789
+ a: left,
790
+ b: right,
791
+ fields,
792
+ };
793
+ },
794
+ },
795
+ ],
796
+ [
797
+ "compare_selector",
798
+ {
799
+ definition: {
800
+ name: "compare_selector",
801
+ description:
802
+ "Compare a selector across two attached sessions using a bounded element summary.",
803
+ inputSchema: compareSessionsSchema(
804
+ {
805
+ selector: {
806
+ type: "string",
807
+ },
808
+ },
809
+ ["selector"],
810
+ ),
811
+ },
812
+ handler: async (args) => {
813
+ const [elementA, elementB] = await Promise.all([
814
+ this.browserAdapter.inspectElement(
815
+ args.sessionIdA,
816
+ args.selector,
817
+ ),
818
+ this.browserAdapter.inspectElement(
819
+ args.sessionIdB,
820
+ args.selector,
821
+ ),
822
+ ]);
823
+ const left = summarizeComparableElement(elementA);
824
+ const right = summarizeComparableElement(elementB);
825
+ const fields = {
826
+ found: compareField(left.found, right.found),
827
+ tagName: compareField(
828
+ left.tagName ?? null,
829
+ right.tagName ?? null,
830
+ ),
831
+ accessibleName: compareField(
832
+ left.accessibleName ?? null,
833
+ right.accessibleName ?? null,
834
+ ),
835
+ role: compareField(left.role ?? null, right.role ?? null),
836
+ visible: compareField(
837
+ left.visible ?? null,
838
+ right.visible ?? null,
839
+ ),
840
+ disabled: compareField(
841
+ left.disabled ?? null,
842
+ right.disabled ?? null,
843
+ ),
844
+ textContent: compareField(
845
+ left.textContent ?? null,
846
+ right.textContent ?? null,
847
+ ),
848
+ };
849
+
850
+ return {
851
+ selector: args.selector,
852
+ matches: Object.values(fields).every((field) => field.equal),
853
+ a: left,
854
+ b: right,
855
+ fields,
856
+ };
857
+ },
858
+ },
859
+ ],
402
860
  [
403
861
  "wait_for",
404
862
  {
@@ -436,6 +894,126 @@ export class McpBrowserDevToolsServer {
436
894
  },
437
895
  },
438
896
  ],
897
+ [
898
+ "get_cookies",
899
+ {
900
+ definition: {
901
+ name: "get_cookies",
902
+ description:
903
+ "Read bounded page-visible cookies for the attached session.",
904
+ inputSchema: sessionSchema({}, []),
905
+ },
906
+ handler: async (args) =>
907
+ this.browserAdapter.getCookies(args.sessionId),
908
+ },
909
+ ],
910
+ [
911
+ "get_storage",
912
+ {
913
+ definition: {
914
+ name: "get_storage",
915
+ description:
916
+ "Read bounded localStorage and sessionStorage entries for the attached session.",
917
+ inputSchema: storageInputSchema(),
918
+ },
919
+ handler: async (args) => {
920
+ const result = await this.browserAdapter.getStorage(args.sessionId);
921
+ if (!args.area || args.area === "all") {
922
+ return result;
923
+ }
924
+
925
+ return {
926
+ browserFamily: result.browserFamily,
927
+ storage: {
928
+ [args.area]: result.storage?.[args.area] ?? null,
929
+ },
930
+ };
931
+ },
932
+ },
933
+ ],
934
+ [
935
+ "capture_debug_report",
936
+ {
937
+ definition: {
938
+ name: "capture_debug_report",
939
+ description:
940
+ "Capture a bounded debug bundle with page state, cookies/storage summary, recent console, recent network, and an optional screenshot.",
941
+ inputSchema: captureDebugReportInputSchema(
942
+ this.config.browserFamily,
943
+ ),
944
+ },
945
+ handler: async (args) =>
946
+ this.browserAdapter.captureDebugReport(args.sessionId, {
947
+ consoleLimit: args.consoleLimit,
948
+ networkLimit: args.networkLimit,
949
+ includeScreenshot: args.includeScreenshot,
950
+ screenshotFormat: args.screenshotFormat,
951
+ }),
952
+ },
953
+ ],
954
+ [
955
+ "capture_session_snapshot",
956
+ {
957
+ definition: {
958
+ name: "capture_session_snapshot",
959
+ description:
960
+ "Capture a bounded session snapshot with page state, cookies, and web storage for later bounded restore on the same origin.",
961
+ inputSchema: sessionSchema({}, []),
962
+ },
963
+ handler: async (args) =>
964
+ this.browserAdapter.captureSessionSnapshot(args.sessionId),
965
+ },
966
+ ],
967
+ [
968
+ "restore_session_snapshot",
969
+ {
970
+ definition: {
971
+ name: "restore_session_snapshot",
972
+ description:
973
+ "Restore a bounded session snapshot into the currently attached page context. Restores only page-visible cookies plus localStorage/sessionStorage on the current origin.",
974
+ inputSchema: restoreSessionSnapshotInputSchema(),
975
+ },
976
+ handler: async (args) => {
977
+ let snapshot;
978
+ try {
979
+ snapshot = JSON.parse(args.snapshot);
980
+ } catch {
981
+ throw new Error(
982
+ "restore_session_snapshot snapshot must be valid JSON",
983
+ );
984
+ }
985
+
986
+ if (!snapshot || typeof snapshot !== "object") {
987
+ throw new Error(
988
+ "restore_session_snapshot snapshot must decode to an object",
989
+ );
990
+ }
991
+
992
+ return this.browserAdapter.restoreSessionSnapshot(
993
+ args.sessionId,
994
+ snapshot,
995
+ {
996
+ clearStorage: args.clearStorage,
997
+ },
998
+ );
999
+ },
1000
+ },
1001
+ ],
1002
+ [
1003
+ "get_har",
1004
+ {
1005
+ definition: {
1006
+ name: "get_har",
1007
+ description:
1008
+ "Export a bounded HAR-like summary from buffered network activity for an attached session.",
1009
+ inputSchema: sessionWithLimitSchema(),
1010
+ },
1011
+ handler: async (args) =>
1012
+ this.browserAdapter.getHar(args.sessionId, {
1013
+ limit: args.limit ?? 50,
1014
+ }),
1015
+ },
1016
+ ],
439
1017
  [
440
1018
  "navigate",
441
1019
  {