@@ -33,6 +33,7 @@ import * as snapshotTools from './tools/snapshot.js';
3333import path from 'node:path' ;
3434import fs from 'node:fs' ;
3535import assert from 'node:assert' ;
36+ import { Mutex } from './Mutex.js' ;
3637
3738export const cliOptions = {
3839 browserUrl : {
@@ -169,6 +170,8 @@ Avoid sharing sensitive or personal information that you do want to share with M
169170 ) ;
170171} ;
171172
173+ const toolMutex = new Mutex ( ) ;
174+
172175function registerTool ( tool : ToolDefinition ) : void {
173176 server . registerTool (
174177 tool . name ,
@@ -178,35 +181,39 @@ function registerTool(tool: ToolDefinition): void {
178181 annotations : tool . annotations ,
179182 } ,
180183 async ( params ) : Promise < CallToolResult > => {
181- logger ( `${ tool . name } request: ${ JSON . stringify ( params , null , ' ' ) } ` ) ;
182- const context = await getContext ( ) ;
183- const response = new McpResponse ( ) ;
184- await tool . handler (
185- {
186- params,
187- } ,
188- response ,
189- context ,
190- ) ;
184+ const guard = await toolMutex . acquire ( ) ;
191185 try {
192- const content = await response . handle ( tool . name , context ) ;
193-
194- return {
195- content ,
196- } ;
197- } catch ( error ) {
198- const errorText =
199- error instanceof Error ? error . message : String ( error ) ;
200-
201- return {
202- content : [
203- {
204- type : 'text' ,
205- text : errorText ,
206- } ,
207- ] ,
208- isError : true ,
209- } ;
186+ logger ( `${ tool . name } request: ${ JSON . stringify ( params , null , ' ' ) } ` ) ;
187+ const context = await getContext ( ) ;
188+ const response = new McpResponse ( ) ;
189+ await tool . handler (
190+ {
191+ params,
192+ } ,
193+ response ,
194+ context ,
195+ ) ;
196+ try {
197+ const content = await response . handle ( tool . name , context ) ;
198+ return {
199+ content ,
200+ } ;
201+ } catch ( error ) {
202+ const errorText =
203+ error instanceof Error ? error . message : String ( error ) ;
204+
205+ return {
206+ content : [
207+ {
208+ type : 'text' ,
209+ text : errorText ,
210+ } ,
211+ ] ,
212+ isError : true ,
213+ } ;
214+ }
215+ } finally {
216+ guard. dispose ( ) ;
210217 }
211218 } ,
212219 ) ;
0 commit comments