Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
McpServerManager.getInstance(this.context, this)
.then((hub) => {
this.mcpHub = hub
this.mcpHub.registerClient()
})
.catch((error) => {
this.outputChannel.appendLine(`Failed to initialize MCP Hub: ${error}`)
Expand Down Expand Up @@ -221,7 +222,7 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements

this.workspaceTracker?.dispose()
this.workspaceTracker = undefined
this.mcpHub?.dispose()
await this.mcpHub?.unregisterClient()
this.mcpHub = undefined
this.customModesManager?.dispose()
this.outputChannel.appendLine("Disposed all disposables")
Expand Down
28 changes: 28 additions & 0 deletions src/services/mcp/McpHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class McpHub {
private isDisposed: boolean = false
connections: McpConnection[] = []
isConnecting: boolean = false
private refCount: number = 0 // Reference counter for active clients

constructor(provider: ClineProvider) {
this.providerRef = new WeakRef(provider)
Expand All @@ -118,6 +119,27 @@ export class McpHub {
this.initializeGlobalMcpServers()
this.initializeProjectMcpServers()
}
/**
* Registers a client (e.g., ClineProvider) using this hub.
* Increments the reference count.
*/
public registerClient(): void {
this.refCount++
console.log(`McpHub: Client registered. Ref count: ${this.refCount}`)
}

/**
* Unregisters a client. Decrements the reference count.
* If the count reaches zero, disposes the hub.
*/
public async unregisterClient(): Promise<void> {
this.refCount--
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In unregisterClient(), consider adding a safeguard to prevent the reference count from dropping below zero before proceeding to dispose. This helps avoid potential underflow if unregisterClient() is called extra times.

Suggested change
this.refCount--
if (this.refCount > 0) this.refCount--

console.log(`McpHub: Client unregistered. Ref count: ${this.refCount}`)
if (this.refCount <= 0) {
console.log("McpHub: Last client unregistered. Disposing hub.")
await this.dispose()
}
}

/**
* Validates and normalizes server configuration
Expand Down Expand Up @@ -1247,6 +1269,12 @@ export class McpHub {
}

async dispose(): Promise<void> {
// Prevent multiple disposals
if (this.isDisposed) {
console.log("McpHub: Already disposed.")
return
}
console.log("McpHub: Disposing...")
this.isDisposed = true
this.removeAllFileWatchers()
for (const connection of this.connections) {
Expand Down