fix(core): remove isolated MCP registries upon subagent completion

McpClient's registeredRegistries grows indefinitely during long sessions because isolated subagent registries were never removed. This commit introduces a cleanup mechanism invoked in LocalAgentExecutor's finally block to release these dead registries, preventing a memory leak.
This commit is contained in:
Akhilesh Kumar
2026-03-16 17:22:58 +00:00
parent e76d684210
commit cdf848d5c7
3 changed files with 27 additions and 0 deletions
@@ -625,6 +625,15 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
}
} finally {
this.config.userHintService.offUserHint(hintListener);
const globalMcpManager = this.context.config.getMcpClientManager();
if (globalMcpManager) {
globalMcpManager.removeRegistries({
toolRegistry: this.toolRegistry,
promptRegistry: this.promptRegistry,
resourceRegistry: this.resourceRegistry,
});
}
}
// === UNIFIED RECOVERY BLOCK ===
@@ -164,6 +164,16 @@ export class McpClientManager {
return this.clients.get(serverName);
}
removeRegistries(registries: {
toolRegistry: ToolRegistry;
promptRegistry: PromptRegistry;
resourceRegistry: ResourceRegistry;
}): void {
for (const client of this.clients.values()) {
client.removeRegistries(registries);
}
}
/**
* For all the MCP servers associated with this extension:
*
+8
View File
@@ -266,6 +266,14 @@ export class McpClient implements McpProgressReporter {
}
}
/**
* Unregisters registries so this client will no longer update them when it receives
* list_changed notifications from the server.
*/
removeRegistries(registries: RegistrySet): void {
this.registeredRegistries.delete(registries);
}
/**
* Disconnects from the MCP server.
*/