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 ClientManager

From Leeroopedia
Knowledge Sources
Domains MCP_Protocol, Networking
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete singleton that manages MCP client lifecycle—creation, connection, disconnection, and capability tracking—with OAuth flow handling, stdio backend supervision, and session-loss recovery.

Description

The ClientManager class creates and manages outbound connections to MCP backend servers. In sync mode, `createClients` connects all transports before returning. In async mode, `initializeClientsAsync` creates storage without connecting, deferring connection to the loading manager. The manager handles OAuth-required errors by surfacing authorization URLs, supervises stdio backends (crash detection, restart), and recovers from session-loss errors by recreating the transport and reconnecting. It delegates to ClientFactory, ConnectionHandler, OAuthFlowHandler, and TransportRecreator collaborators.

Usage

Import this class when you need to:

  • Create MCP clients from transports at server startup (sync or async mode)
  • Connect a single server at runtime for hot-reload additions
  • Remove a client and close its transport during hot-reload removal
  • Handle OAuth-required errors with authorization URL extraction
  • Set a backend availability handler for capability refresh notifications

Code Reference

Source Location

  • Repository: 1MCP Agent
  • File: src/sdk/legacy/client/runtime/clientManager.ts (L80-705, re-exported via src/core/client/clientManager.ts)

Signature

export class ClientManager extends EventEmitter {
  private static instance: ClientManager;

  public static getOrCreateInstance(): ClientManager;
  public static get current(): ClientManager;
  public static resetInstance(): void;

  public async createClients(transports: Record<string, AuthProviderTransport>): Promise<OutboundConnections>;
  public initializeClientsAsync(transports: Record<string, AuthProviderTransport>): OutboundConnections;
  public async createSingleClient(name: string, transport: AuthProviderTransport, signal?: AbortSignal): Promise<void>;
  public async removeClient(name: string): Promise<void>;

  public getClients(): OutboundConnections;
  public getTransport(name: string): AuthProviderTransport | undefined;

  public setInstructionAggregator(aggregator: InstructionAggregator): void;
  public setBackendAvailabilityHandler(handler: (name: string, snapshot?: BackendSupervisionSnapshot) => void | Promise<void>): void;
}

Import

import { ClientManager } from '@src/core/client/clientManager.js';

I/O Contract

Inputs

Name Type Required Description
transports Record<string, AuthProviderTransport> Yes Transport instances to connect
name string Yes Server name for single-client operations
signal AbortSignal No Cancellation signal for connection attempt

Outputs

Name Type Description
createClients Promise<OutboundConnections> Map of connected clients (sync mode)
initializeClientsAsync OutboundConnections Empty storage for async loading
getClients OutboundConnections Current connected clients

Usage Examples

Sync Mode Client Creation

import { ClientManager } from '@src/core/client/clientManager.js';

const clientManager = ClientManager.getOrCreateInstance();
clientManager.setInstructionAggregator(instructionAggregator);

// Connect all transports synchronously
const clients = await clientManager.createClients(transports);
// clients: Map<string, OutboundConnection>

Async Mode Initialization

const clientManager = ClientManager.getOrCreateInstance();

// Create storage without connecting (async loading handles connection)
const clients = clientManager.initializeClientsAsync(transports);
// clients is empty Map; loading manager will fill it via createSingleClient

Related Pages

Implements Principle

Page Connections

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