File tree Expand file tree Collapse file tree
packages/opencode/src/mcp Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -266,6 +266,13 @@ export namespace MCP {
266266 status : s . status ,
267267 }
268268 }
269+ // Close existing client if present to prevent memory leaks
270+ const existingClient = s . clients [ name ]
271+ if ( existingClient ) {
272+ await existingClient . close ( ) . catch ( ( error ) => {
273+ log . error ( "Failed to close existing MCP client" , { name, error } )
274+ } )
275+ }
269276 s . clients [ name ] = result . mcpClient
270277 s . status [ name ] = result . status
271278
@@ -523,6 +530,13 @@ export namespace MCP {
523530 const s = await state ( )
524531 s . status [ name ] = result . status
525532 if ( result . mcpClient ) {
533+ // Close existing client if present to prevent memory leaks
534+ const existingClient = s . clients [ name ]
535+ if ( existingClient ) {
536+ await existingClient . close ( ) . catch ( ( error ) => {
537+ log . error ( "Failed to close existing MCP client" , { name, error } )
538+ } )
539+ }
526540 s . clients [ name ] = result . mcpClient
527541 }
528542 }
You can’t perform that action at this time.
0 commit comments