@@ -11,45 +11,48 @@ import path from 'node:path';
1111import type {
1212 Browser ,
1313 ChromeReleaseChannel ,
14- ConnectOptions ,
1514 LaunchOptions ,
1615 Target ,
1716} from 'puppeteer-core' ;
1817import puppeteer from 'puppeteer-core' ;
1918
2019let browser : Browser | undefined ;
2120
22- const ignoredPrefixes = new Set ( [
23- 'chrome://' ,
24- 'chrome-extension ://' ,
25- 'chrome-untrusted ://' ,
26- 'devtools ://',
27- ] ) ;
21+ function makeTargetFilter ( devtools : boolean ) {
22+ const ignoredPrefixes = new Set ( [
23+ 'chrome://' ,
24+ 'chrome-extension ://' ,
25+ 'chrome-untrusted ://',
26+ ] ) ;
2827
29- function targetFilter ( target : Target ) : boolean {
30- if ( target . url ( ) === 'chrome://newtab/' ) {
31- return true ;
28+ if ( ! devtools ) {
29+ ignoredPrefixes . add ( 'devtools://' ) ;
3230 }
33- for ( const prefix of ignoredPrefixes ) {
34- if ( target . url ( ) . startsWith ( prefix ) ) {
35- return false ;
31+ return function targetFilter ( target : Target ) : boolean {
32+ if ( target . url ( ) === 'chrome://newtab/' ) {
33+ return true ;
3634 }
37- }
38- return true ;
35+ for ( const prefix of ignoredPrefixes ) {
36+ if ( target . url ( ) . startsWith ( prefix ) ) {
37+ return false ;
38+ }
39+ }
40+ return true ;
41+ } ;
3942}
4043
41- const connectOptions : ConnectOptions = {
42- targetFilter,
43- } ;
44-
45- export async function ensureBrowserConnected ( browserURL : string ) {
44+ export async function ensureBrowserConnected ( options : {
45+ browserURL : string ;
46+ devtools : boolean ;
47+ } ) {
4648 if ( browser ?. connected ) {
4749 return browser ;
4850 }
4951 browser = await puppeteer . connect ( {
50- ... connectOptions ,
51- browserURL,
52+ targetFilter : makeTargetFilter ( options . devtools ) ,
53+ browserURL : options . browserURL ,
5254 defaultViewport : null ,
55+ handleDevToolsAsPage : options . devtools ,
5356 } ) ;
5457 return browser ;
5558}
@@ -68,6 +71,7 @@ interface McpLaunchOptions {
6871 height : number ;
6972 } ;
7073 args ?: string [ ] ;
74+ devtools : boolean ;
7175}
7276
7377export async function launch ( options : McpLaunchOptions ) : Promise < Browser > {
@@ -101,6 +105,9 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
101105 args . push ( '--screen-info={3840x2160}' ) ;
102106 }
103107 let puppeteerChannel : ChromeReleaseChannel | undefined ;
108+ if ( options . devtools ) {
109+ args . push ( '--auto-open-devtools-for-tabs' ) ;
110+ }
104111 if ( ! executablePath ) {
105112 puppeteerChannel =
106113 channel && channel !== 'stable'
@@ -110,15 +117,16 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
110117
111118 try {
112119 const browser = await puppeteer . launch ( {
113- ...connectOptions ,
114120 channel : puppeteerChannel ,
121+ targetFilter : makeTargetFilter ( options . devtools ) ,
115122 executablePath,
116123 defaultViewport : null ,
117124 userDataDir,
118125 pipe : true ,
119126 headless,
120127 args,
121128 acceptInsecureCerts : options . acceptInsecureCerts ,
129+ handleDevToolsAsPage : options . devtools ,
122130 } ) ;
123131 if ( options . logFile ) {
124132 // FIXME: we are probably subscribing too late to catch startup logs. We
0 commit comments