@@ -467,32 +467,49 @@ async function createMCPProxyDoNotUseDirectly(
467467 }
468468
469469 // Fall back to client for connections without indexed tools
470- const client = await createClient ( ) ;
471- return await client . listTools ( ) . then ( ( result ) => {
472- client . close ( ) . catch ( console . error ) ;
473- return result ;
474- } ) ;
470+ let client : Awaited < ReturnType < typeof createClient > > | undefined ;
471+ try {
472+ client = await createClient ( ) ;
473+ return await client . listTools ( ) ;
474+ } finally {
475+ client ?. close ( ) . catch ( console . error ) ;
476+ }
475477 } ;
476478
477479 // List resources from downstream connection
478480 const listResources = async ( ) : Promise < ListResourcesResult > => {
479- const client = await createClient ( ) ;
480- return await client . listResources ( ) ;
481+ let client : Awaited < ReturnType < typeof createClient > > | undefined ;
482+ try {
483+ client = await createClient ( ) ;
484+ return await client . listResources ( ) ;
485+ } finally {
486+ client ?. close ( ) . catch ( console . error ) ;
487+ }
481488 } ;
482489
483490 // Read a specific resource from downstream connection
484491 const readResource = async (
485492 params : ReadResourceRequest [ "params" ] ,
486493 ) : Promise < ReadResourceResult > => {
487- const client = await createClient ( ) ;
488- return await client . readResource ( params ) ;
494+ let client : Awaited < ReturnType < typeof createClient > > | undefined ;
495+ try {
496+ client = await createClient ( ) ;
497+ return await client . readResource ( params ) ;
498+ } finally {
499+ client ?. close ( ) . catch ( console . error ) ;
500+ }
489501 } ;
490502
491503 // List resource templates from downstream connection
492504 const listResourceTemplates =
493505 async ( ) : Promise < ListResourceTemplatesResult > => {
494- const client = await createClient ( ) ;
495- return await client . listResourceTemplates ( ) ;
506+ let client : Awaited < ReturnType < typeof createClient > > | undefined ;
507+ try {
508+ client = await createClient ( ) ;
509+ return await client . listResourceTemplates ( ) ;
510+ } finally {
511+ client ?. close ( ) . catch ( console . error ) ;
512+ }
496513 } ;
497514
498515 // List prompts from downstream connection
@@ -513,8 +530,13 @@ async function createMCPProxyDoNotUseDirectly(
513530 const getPrompt = async (
514531 params : GetPromptRequest [ "params" ] ,
515532 ) : Promise < GetPromptResult > => {
516- const client = await createClient ( ) ;
517- return await client . getPrompt ( params ) ;
533+ let client : Awaited < ReturnType < typeof createClient > > | undefined ;
534+ try {
535+ client = await createClient ( ) ;
536+ return await client . getPrompt ( params ) ;
537+ } finally {
538+ client ?. close ( ) . catch ( console . error ) ;
539+ }
518540 } ;
519541
520542 // Call tool using fetch directly for streaming support
0 commit comments