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
15 changes: 13 additions & 2 deletions src/services/mcp/McpHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,25 @@ export class McpHub {
private isProgrammaticUpdate: boolean = false
private flagResetTimer?: NodeJS.Timeout
private sanitizedNameRegistry: Map<string, string> = new Map()
private initializationPromise: Promise<void>

constructor(provider: ClineProvider) {
this.providerRef = new WeakRef(provider)
this.watchMcpSettingsFile()
this.watchProjectMcpFile().catch(console.error)
this.setupWorkspaceFoldersWatcher()
this.initializeGlobalMcpServers()
this.initializeProjectMcpServers()
this.initializationPromise = Promise.all([
this.initializeGlobalMcpServers(),
this.initializeProjectMcpServers(),
]).then(() => {})
}

/**
* Waits until all MCP servers have finished their initial connection attempts.
* Each server individually handles its own timeout, so this will not block indefinitely.
*/
async waitUntilReady(): Promise<void> {
await this.initializationPromise
}
/**
* Registers a client (e.g., ClineProvider) using this hub.
Expand Down
5 changes: 4 additions & 1 deletion src/services/mcp/McpServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export class McpServerManager {
try {
// Double-check instance in case it was created while we were waiting
if (!this.instance) {
this.instance = new McpHub(provider)
const hub = new McpHub(provider)
// Wait for all MCP servers to finish connecting (or timing out)
await hub.waitUntilReady()
this.instance = hub
// Store a unique identifier in global state to track the primary instance
await context.globalState.update(this.GLOBAL_STATE_KEY, Date.now().toString())
}
Expand Down
Loading