@@ -31,9 +31,9 @@ await checkForUpdates(
3131 'Run `npm install -g chrome-devtools-mcp@latest` and `chrome-devtools start` to update and restart the daemon.' ,
3232) ;
3333
34- async function start ( args : string [ ] ) {
34+ async function start ( args : string [ ] , sessionId : string ) {
3535 const combinedArgs = [ ...args , ...defaultArgs ] ;
36- await startDaemon ( combinedArgs ) ;
36+ await startDaemon ( combinedArgs , sessionId ) ;
3737 logDisclaimers ( parseArguments ( VERSION , combinedArgs ) ) ;
3838}
3939
@@ -78,6 +78,12 @@ const y = yargs(hideBin(process.argv))
7878 .usage(
7979 `Run 'chrome-devtools < command > --help' for help on the specific command.`,
8080 )
81+ .option('sessionId', {
82+ type : 'string' ,
83+ description : 'Session ID for daemon scoping' ,
84+ default : '' ,
85+ hidden : true ,
86+ } )
8187 .demandCommand()
8288 .version(VERSION)
8389 .strict()
@@ -96,8 +102,8 @@ y.command(
96102 )
97103 . strict ( ) ,
98104 async argv => {
99- if ( isDaemonRunning ( ) ) {
100- await stopDaemon ( ) ;
105+ if ( isDaemonRunning ( argv . sessionId ) ) {
106+ await stopDaemon ( argv . sessionId ) ;
101107 }
102108 // Defaults but we do not want to affect the yargs conflict resolution.
103109 if ( argv . isolated === undefined && argv . userDataDir === undefined ) {
@@ -107,46 +113,60 @@ y.command(
107113 argv . headless = true ;
108114 }
109115 const args = serializeArgs ( cliOptions , argv ) ;
110- await start ( args ) ;
116+ await start ( args , argv . sessionId ) ;
111117 process . exit ( 0 ) ;
112118 } ,
113119) . strict ( ) ; // Re-enable strict validation for other commands; this is applied to the yargs instance itself
114120
115- y . command ( 'status' , 'Checks if chrome-devtools-mcp is running' , async ( ) => {
116- if ( isDaemonRunning ( ) ) {
117- console . log ( 'chrome-devtools-mcp daemon is running.' ) ;
118- const response = await sendCommand ( {
119- method : 'status' ,
120- } ) ;
121- if ( response . success ) {
122- const data = JSON . parse ( response . result ) as {
123- pid : number | null ;
124- socketPath: string ;
125- startDate: string ;
126- version: string ;
127- args: string [ ] ;
128- } ;
129- console . log (
130- `pid=${ data . pid } socket=${ data . socketPath } start-date=${ data . startDate } version=${ data . version } ` ,
121+ y . command (
122+ 'status' ,
123+ 'Checks if chrome-devtools-mcp is running' ,
124+ y => y ,
125+ async argv => {
126+ if ( isDaemonRunning ( argv . sessionId ) ) {
127+ console . log ( 'chrome-devtools-mcp daemon is running.' ) ;
128+ const response = await sendCommand (
129+ {
130+ method : 'status' ,
131+ } ,
132+ argv . sessionId ,
131133 ) ;
132- console . log ( `args=${ JSON . stringify ( data . args ) } ` ) ;
134+ if ( response . success ) {
135+ const data = JSON . parse ( response . result ) as {
136+ pid : number | null ;
137+ socketPath: string ;
138+ startDate: string ;
139+ version: string ;
140+ args: string [ ] ;
141+ } ;
142+ console . log (
143+ `pid=${ data . pid } socket=${ data . socketPath } start-date=${ data . startDate } version=${ data . version } ` ,
144+ ) ;
145+ console . log ( `args=${ JSON . stringify ( data . args ) } ` ) ;
146+ } else {
147+ console . error ( 'Error:' , response . error ) ;
148+ process . exit ( 1 ) ;
149+ }
133150 } else {
134- console . error ( 'Error:' , response . error ) ;
135- process . exit ( 1 ) ;
151+ console . log ( 'chrome-devtools-mcp daemon is not running.' ) ;
136152 }
137- } else {
138- console . log ( 'chrome-devtools-mcp daemon is not running.' ) ;
139- }
140- process . exit ( 0 ) ;
141- } ) ;
153+ process . exit ( 0 ) ;
154+ } ,
155+ ) ;
142156
143- y . command ( 'stop' , 'Stop chrome-devtools-mcp if any' , async ( ) => {
144- if ( ! isDaemonRunning ( ) ) {
157+ y . command (
158+ 'stop' ,
159+ 'Stop chrome-devtools-mcp if any' ,
160+ y => y ,
161+ async argv => {
162+ const sessionId = argv . sessionId as string ;
163+ if ( ! isDaemonRunning ( sessionId ) ) {
164+ process . exit ( 0 ) ;
165+ }
166+ await stopDaemon ( sessionId ) ;
145167 process . exit ( 0 ) ;
146- }
147- await stopDaemon ( ) ;
148- process . exit ( 0 ) ;
149- } ) ;
168+ } ,
169+ ) ;
150170
151171for ( const [ commandName , commandDef ] of Object . entries ( commands ) ) {
152172 const args = commandDef . args ;
@@ -213,9 +233,10 @@ for (const [commandName, commandDef] of Object.entries(commands)) {
213233 }
214234 } ,
215235 async argv => {
236+ const sessionId = argv . sessionId as string ;
216237 try {
217- if ( ! isDaemonRunning ( ) ) {
218- await start ( [ ] ) ;
238+ if ( ! isDaemonRunning ( sessionId ) ) {
239+ await start ( [ ] , sessionId ) ;
219240 }
220241
221242 const commandArgs : Record < string , unknown > = { } ;
@@ -225,11 +246,14 @@ for (const [commandName, commandDef] of Object.entries(commands)) {
225246 }
226247 }
227248
228- const response = await sendCommand ( {
229- method : 'invoke_tool' ,
230- tool : commandName ,
231- args : commandArgs ,
232- } ) ;
249+ const response = await sendCommand (
250+ {
251+ method : 'invoke_tool' ,
252+ tool : commandName ,
253+ args : commandArgs ,
254+ } ,
255+ sessionId ,
256+ ) ;
233257
234258 if ( response . success ) {
235259 console . log (
0 commit comments