@@ -45,6 +45,7 @@ export class ActorsMcpServer {
4545 public readonly tools : Map < string , ToolEntry > ;
4646 private options : ActorsMcpServerOptions ;
4747 private toolsChangedHandler : ToolsChangedHandler | undefined ;
48+ private sigintHandler : ( ( ) => Promise < void > ) | undefined ;
4849
4950 constructor ( options : ActorsMcpServerOptions = { } , setupSigintHandler = true ) {
5051 this . options = {
@@ -319,12 +320,13 @@ export class ActorsMcpServer {
319320 this . server . onerror = ( error ) => {
320321 console . error ( '[MCP Error]' , error ) ; // eslint-disable-line no-console
321322 } ;
322- // Allow disabling of the SIGINT handler to prevent max listeners warning
323323 if ( setupSIGINTHandler ) {
324- process . on ( 'SIGINT' , async ( ) => {
324+ const handler = async ( ) => {
325325 await this . server . close ( ) ;
326326 process . exit ( 0 ) ;
327- } ) ;
327+ } ;
328+ process . once ( 'SIGINT' , handler ) ;
329+ this . sigintHandler = handler ; // Store the actual handler
328330 }
329331 }
330332
@@ -497,6 +499,18 @@ export class ActorsMcpServer {
497499 }
498500
499501 async close ( ) : Promise < void > {
502+ // 1. Remove SIGINT handler
503+ if ( this . sigintHandler ) {
504+ process . removeListener ( 'SIGINT' , this . sigintHandler ) ;
505+ this . sigintHandler = undefined ;
506+ }
507+ // // 2. Clear all tools
508+ this . tools . clear ( ) ;
509+ // 3. Unregister tools changed handler
510+ if ( this . toolsChangedHandler ) {
511+ this . unregisterToolsChangedHandler ( ) ;
512+ }
513+ // 4. Close server (which should also remove its event handlers)
500514 await this . server . close ( ) ;
501515 }
502516}
0 commit comments