Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1mcp app Agent McpLoadingManager

From Leeroopedia
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');

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment