Implementation:1mcp app Agent McpLoadingManager
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Reliability |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete event-driven manager for asynchronous MCP server initialization with bounded parallel execution, exponential backoff retry, OAuth-aware error handling, and background retry for failed servers.
Description
The McpLoadingManager class coordinates the non-blocking initialization of MCP backends. It uses a ParallelExecutor with a configurable `maxConcurrentLoads` limit to avoid overwhelming the system. Each server goes through a retry loop with exponential backoff (`retryDelayMs * 2^(retryCount-1)`) up to `maxRetries`. OAuth-required errors are detected and the server enters an `AwaitingOAuth` state rather than retrying. Non-retryable errors (`NonRetryableClientConnectionError`) stop the retry loop immediately. After the initial loading wave, a background retry timer periodically attempts to reconnect failed servers. Per-server abort controllers allow cancellation during hot-reload without race conditions.
Usage
Import this class when you need to:
- Start async loading of MCP servers without blocking HTTP server startup
- Load a single server at runtime for config hot-reload additions
- Unload a server and cancel any in-flight loading operation
- Track per-server loading state (pending, loading, ready, failed, awaiting-oauth, cancelled)
- Listen for loading events (LoadingStarted, ServerLoaded, ServerFailed, LoadingComplete)
Code Reference
Source Location
- Repository: 1MCP Agent
- File: src/core/loading/mcpLoadingManager.ts (L90-832)
Signature
export class McpLoadingManager extends EventEmitter {
public static get current(): McpLoadingManager;
constructor(clientManager: ClientManager, config?: Partial<BackendLoadingPolicy>);
public async startAsyncLoading(transports: Record<string, AuthProviderTransport>): Promise<OutboundConnections>;
public waitForInitialLoading(): Promise<void>;
public async loadServer(name: string, config: MCPServerParams): Promise<void>;
public async unloadServer(name: string): Promise<void>;
public cancelServerLoading(serverName: string): void;
public cancelAllLoading(): void;
public getCancellableServers(): string[];
public getSummary(): LoadingSummary;
public isServerReady(name: string): boolean;
public getReadyServers(): string[];
public getFailedServers(): string[];
public getStateTracker(): LoadingStateTracker;
public shutdown(): void;
}
Import
import { McpLoadingManager, McpLoadingEvent } from '@src/core/loading/mcpLoadingManager.js';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| clientManager | ClientManager | Yes | Client manager for creating connections |
| config | Partial<BackendLoadingPolicy> | No | Override defaults (maxConcurrentLoads, maxRetries, retryDelayMs) |
| transports | Record<string, AuthProviderTransport> | Yes | Transports to load |
Outputs
| Name | Type | Description |
|---|---|---|
| startAsyncLoading | Promise<OutboundConnections> | Non-blocking; loading happens in background |
| waitForInitialLoading | Promise<void> | Resolves when all initial attempts settle |
| McpLoadingEvent | enum | ServerLoaded, ServerFailed, LoadingComplete, OAuthRequired, etc. |
Usage Examples
Async Loading with Events
import { McpLoadingManager, McpLoadingEvent } from '@src/core/loading/mcpLoadingManager.js';
const loadingManager = new McpLoadingManager(clientManager);
loadingManager.on(McpLoadingEvent.ServerLoaded, (name, result) => {
console.log(`Server ${name} loaded in ${result.duration}ms`);
});
loadingManager.on(McpLoadingEvent.ServerFailed, (name, result) => {
console.error(`Server ${name} failed: ${result.error?.message}`);
});
// Start non-blocking loading
const clients = await loadingManager.startAsyncLoading(transports);
// HTTP server can start immediately here
// Later, optionally wait for loading to complete:
await loadingManager.waitForInitialLoading();
Hot-Reload Single Server
// Add a server at runtime
await loadingManager.loadServer('new-server', serverConfig);
// Remove a server (cancels any in-flight loading)
await loadingManager.unloadServer('old-server');