Implementation:1mcp app Agent ServerManager
| Knowledge Sources | |
|---|---|
| Domains | MCP_Protocol, Software_Architecture |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete singleton facade that coordinates MCP server connection lifecycle, template server management, capability routing, and instruction aggregation behind a unified interface.
Description
The ServerManager class acts as a facade over multiple specialized managers: ConnectionManager (transport lifecycle), TemplateServerManager (per-client template instantiation), MCPServerLifecycleManager (start/stop/restart), TemplateConfigurationManager (configuration reprocessing), and ServerRegistry (backend listing). It maintains outbound connections to MCP backends, routes capability queries (tools, resources, prompts), manages logical template contexts per session, and supports instruction aggregation from all connected servers. Schema boundary initialization and shutdown are tied to the singleton's lifecycle.
Usage
Import this class when you need to:
- Create the central server manager at startup with connected clients and transports
- Route MCP requests (tools/call, resources/read, prompts/get) to the correct backend
- Manage template server instances per client session with context-aware configuration
- Trigger hot-reload operations (loadMcpServer, unloadMcpServer) for config changes
- Aggregate instructions from all connected backends for the runtime instructions feature
- Register cleanup callbacks for owned subprocess settlement before shutdown
Code Reference
Source Location
- Repository: 1MCP Agent
- File: src/sdk/legacy/server/runtime/serverManager.ts (L60-707, re-exported via src/core/server/serverManager.ts)
Signature
export class ServerManager {
private static instance: ServerManager | undefined;
public static getOrCreateInstance(
config: { name: string; version: string },
capabilities: { capabilities: Record<string, unknown> },
outboundConns: OutboundConnections,
transports: Record<string, Transport>,
): ServerManager;
public static get current(): ServerManager;
public getTemplateServerManager(): TemplateServerManager;
public getClients(): OutboundConnections;
public getInboundConnections(): Map<string, InboundConnection>;
public getInstructionAggregator(): InstructionAggregator | undefined;
public setInstructionAggregator(aggregator: InstructionAggregator): void;
public setLazyLoadingOrchestrator(orchestrator: LazyLoadingOrchestrator): void;
public loadMcpServer(name: string, config: MCPServerParams): Promise<void>;
public unloadMcpServer(name: string): Promise<void>;
public updateServerMetadata(name: string, config: MCPServerParams): Promise<void>;
public isMcpServerRunning(name: string): boolean;
public recordMcpServerReady(name: string): void;
public syncMcpServerLifecycleFromConnectedClients(): void;
public notifyBackendCapabilityListsChanged(): void;
public registerOwnedCleanup(callback: () => Promise<void>): () => void;
public registerCleanup(callback: () => Promise<void>): () => void;
}
Import
import { ServerManager } from '@src/core/server/serverManager.js';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | { name: string; version: string } | Yes | MCP server identity |
| capabilities | { capabilities: Record<string, unknown> } | Yes | Server capability record |
| outboundConns | OutboundConnections | Yes | Connected MCP clients |
| transports | Record<string, Transport> | Yes | Transport instances |
Outputs
| Name | Type | Description |
|---|---|---|
| getOrCreateInstance | ServerManager | Singleton with initialized subsystems |
| getClients | OutboundConnections | Map of connected backends |
| loadMcpServer | Promise<void> | Server loaded via canonical pipeline |
Usage Examples
Creating at Startup
import { ServerManager } from '@src/core/server/serverManager.js';
import { MCP_SERVER_NAME, MCP_SERVER_VERSION, MCP_SERVER_CAPABILITIES } from '@src/constants.js';
const serverManager = ServerManager.getOrCreateInstance(
{ name: MCP_SERVER_NAME, version: MCP_SERVER_VERSION },
{ capabilities: MCP_SERVER_CAPABILITIES },
clients,
transports,
);
serverManager.setInstructionAggregator(instructionAggregator);
Hot-Reload via Facade
const serverManager = ServerManager.current;
// Add a new server (from config hot-reload)
await serverManager.loadMcpServer('new-server', serverConfig);
// Remove a server
await serverManager.unloadMcpServer('old-server');
// Check if running
const running = serverManager.isMcpServerRunning('my-server');