@@ -12,21 +12,25 @@ import {McpContext} from '../src/McpContext.js';
1212import { McpResponse } from '../src/McpResponse.js' ;
1313import { stableIdSymbol } from '../src/PageCollector.js' ;
1414
15- let browser : Browser | undefined ;
15+ const browsers = new Map < string , Browser > ( ) ;
1616
1717export async function withBrowser (
1818 cb : ( response : McpResponse , context : McpContext ) => Promise < void > ,
19- options : { debug ?: boolean ; autoOpenDevToos ?: boolean ; force ?: boolean } = { } ,
19+ options : { debug ?: boolean ; autoOpenDevTools ?: boolean } = { } ,
2020) {
21- const { debug = false } = options ;
22- if ( ! browser || options . force ) {
23- browser = await puppeteer . launch ( {
24- executablePath : process . env . PUPPETEER_EXECUTABLE_PATH ,
25- headless : ! debug ,
26- defaultViewport : null ,
27- devtools : options . autoOpenDevToos ?? false ,
28- handleDevToolsAsPage : true ,
29- } ) ;
21+ const launchOptions = {
22+ executablePath : process . env . PUPPETEER_EXECUTABLE_PATH ,
23+ headless : ! options . debug ,
24+ defaultViewport : null ,
25+ devtools : options . autoOpenDevTools ?? false ,
26+ handleDevToolsAsPage : true ,
27+ } ;
28+ const key = JSON . stringify ( launchOptions ) ;
29+
30+ let browser = browsers . get ( key ) ;
31+ if ( ! browser ) {
32+ browser = await puppeteer . launch ( launchOptions ) ;
33+ browsers . set ( key , browser ) ;
3034 }
3135 const newPage = await browser . newPage ( ) ;
3236 // Close other pages.
0 commit comments