@@ -19,36 +19,39 @@ import puppeteer from 'puppeteer-core';
1919
2020let browser : Browser | undefined ;
2121
22- const ignoredPrefixes = new Set ( [
23- 'chrome://' ,
24- 'chrome-extension ://' ,
25- 'chrome-untrusted ://' ,
26- 'devtools ://',
27- ] ) ;
22+ function makeTargetFilter ( devtools : boolean ) {
23+ const ignoredPrefixes = new Set ( [
24+ 'chrome://' ,
25+ 'chrome-extension ://' ,
26+ 'chrome-untrusted ://',
27+ ] ) ;
2828
29- function targetFilter ( target : Target ) : boolean {
30- if ( target . url ( ) === 'chrome://newtab/' ) {
31- return true ;
29+ if ( ! devtools ) {
30+ ignoredPrefixes . add ( 'devtools://' ) ;
3231 }
33- for ( const prefix of ignoredPrefixes ) {
34- if ( target . url ( ) . startsWith ( prefix ) ) {
35- return false ;
32+ return function targetFilter ( target : Target ) : boolean {
33+ if ( target . url ( ) === 'chrome://newtab/' ) {
34+ return true ;
3635 }
37- }
38- return true ;
36+ for ( const prefix of ignoredPrefixes ) {
37+ if ( target . url ( ) . startsWith ( prefix ) ) {
38+ return false ;
39+ }
40+ }
41+ return true ;
42+ } ;
3943}
4044
41- const connectOptions : ConnectOptions = {
42- targetFilter,
43- } ;
44-
45- export async function ensureBrowserConnected ( browserURL : string ) {
45+ export async function ensureBrowserConnected ( options : {
46+ browserURL : string ;
47+ devtools : boolean ;
48+ } ) {
4649 if ( browser ?. connected ) {
4750 return browser ;
4851 }
4952 browser = await puppeteer . connect ( {
50- ... connectOptions ,
51- browserURL,
53+ targetFilter : makeTargetFilter ( options . devtools ) ,
54+ browserURL : options . browserURL ,
5255 defaultViewport : null ,
5356 } ) ;
5457 return browser ;
@@ -68,7 +71,8 @@ interface McpLaunchOptions {
6871 height : number ;
6972 } ;
7073 args ?: string [ ] ;
71- }
74+ devtools : boolean ;
75+ } ;
7276
7377export async function launch ( options : McpLaunchOptions ) : Promise < Browser > {
7478 const { channel, executablePath, customDevTools, headless, isolated} = options ;
@@ -101,6 +105,10 @@ 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+ }
111+ let puppeterChannel : ChromeReleaseChannel | undefined ;
104112 if ( ! executablePath ) {
105113 puppeteerChannel =
106114 channel && channel !== 'stable'
@@ -110,8 +118,8 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
110118
111119 try {
112120 const browser = await puppeteer . launch ( {
113- ...connectOptions ,
114121 channel : puppeteerChannel ,
122+ targetFilter : makeTargetFilter ( options . devtools ) ,
115123 executablePath,
116124 defaultViewport : null ,
117125 userDataDir,
0 commit comments