fix(mcp): close existing client before reassignment to prevent leaks #8253
+14
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix memory leak in MCP client management where existing clients are orphaned when replaced.
Fixes #8257
Relates to #7261
Relates to #3013
Problem
In
MCP.add()and the OAuthfinishAuth()flow, when a new MCP client is created and assigned tos.clients[name], any existing client at that key is silently overwritten:The old client is never closed, leaving:
This happens when:
Solution
Check for and close existing clients before reassignment in both locations:
Changes
packages/opencode/src/mcp/index.ts: Added client cleanup inadd()functionpackages/opencode/src/mcp/index.ts: Added client cleanup infinishAuth()flowTesting