@@ -11,44 +11,46 @@ 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 ,
5355 } ) ;
5456 return browser ;
@@ -68,7 +70,8 @@ interface McpLaunchOptions {
6870 height : number ;
6971 } ;
7072 args ?: string [ ] ;
71- }
73+ devtools : boolean ;
74+ } ;
7275
7376export async function launch ( options : McpLaunchOptions ) : Promise < Browser > {
7477 const { channel, executablePath, customDevTools, headless, isolated} = options ;
@@ -101,6 +104,9 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
101104 args . push ( '--screen-info={3840x2160}' ) ;
102105 }
103106 let puppeteerChannel : ChromeReleaseChannel | undefined ;
107+ if ( options . devtools ) {
108+ args . push ( '--auto-open-devtools-for-tabs' ) ;
109+ }
104110 if ( ! executablePath ) {
105111 puppeteerChannel =
106112 channel && channel !== 'stable'
@@ -110,8 +116,8 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
110116
111117 try {
112118 const browser = await puppeteer . launch ( {
113- ...connectOptions ,
114119 channel : puppeteerChannel ,
120+ targetFilter : makeTargetFilter ( options . devtools ) ,
115121 executablePath,
116122 defaultViewport : null ,
117123 userDataDir,
0 commit comments