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 ConfigManager

From Leeroopedia
Knowledge Sources
Domains Configuration, Software_Architecture
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete singleton configuration manager for loading, validating, and hot-reloading MCP server configuration with Zod schema validation, environment variable substitution, and template processing.

Description

The ConfigManager class extends EventEmitter and provides the single entry point for all configuration access in 1MCP. It loads raw config from disk via a ConfigLoader, validates it through Zod schemas (`mcpServerConfigSchema`, `transportConfigSchema`), separates static and template server definitions, and emits typed change events when the config file changes on disk. It supports environment variable substitution in server definitions, runtime scope environment loading, and Handlebars template rendering for template servers. A `runtimeAdmission` guard ensures config reloads are deferred during runtime drain operations.

Usage

Import this class when you need to:

  • Initialize configuration at server startup (`ConfigManager.getInstance(configFilePath).initialize()`)
  • Get transport config for creating MCP client transports (`configManager.getTransportConfig()`)
  • Load declared server configs for template indexing (`configManager.loadDeclaredServerConfigs()`)
  • Listen for configuration changes via `CONFIG_EVENTS.CONFIG_CHANGED`, `SERVER_ADDED`, `SERVER_REMOVED`
  • Access runtime instruction configuration for instruction aggregation

Code Reference

Source Location

  • Repository: 1MCP Agent
  • File: src/config/configManager.ts (L37-657)

Signature

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

  public static getInstance(configFilePath?: string): ConfigManager;

  public async initialize(): Promise<void>;
  public async stop(): Promise<void>;

  public getTransportConfig(options?: { includeDisabled?: boolean }): Record<string, MCPServerParams>;

  public loadDeclaredServerConfigs(): {
    staticServers: Record<string, MCPServerParams>;
    templateServers: Record<string, MCPServerParams>;
    errors: string[];
  };

  public async loadConfigWithTemplates(context?: ContextData): Promise<{
    staticServers: Record<string, MCPServerParams>;
    templateServers: Record<string, MCPServerParams>;
    errors: string[];
  }>;

  public getRuntimeInstructionConfiguration(): {
    instructionTemplates?: MCPServerConfiguration['instructionTemplates'];
    publishedInstructionTemplates?: MCPServerConfiguration['publishedInstructionTemplates'];
    activeInstructionTemplate?: string;
    configuredTargets: { mcpServers: MCPServerConfiguration['mcpServers']; mcpTemplates: NonNullable<MCPServerConfiguration['mcpTemplates']> };
  };

  public async reloadConfig(): Promise<void>;
  public isReloadEnabled(): boolean;
  public getAvailableTags(): string[];
  public getAppConfig(): ApplicationConfig;
}

Import

import { ConfigManager, CONFIG_EVENTS, ConfigChangeType } from '@src/config/configManager.js';

I/O Contract

Inputs

Name Type Required Description
configFilePath string No Path to config file; falls back to global config path
context ContextData No Session context for template processing
options.includeDisabled boolean No Include disabled servers in transport config

Outputs

Name Type Description
getInstance ConfigManager Singleton instance with loaded config
getTransportConfig Record<string, MCPServerParams> Enabled server definitions keyed by name
loadDeclaredServerConfigs { staticServers, templateServers, errors } All declared servers separated by type
CONFIG_CHANGED event ConfigChange[] Array of detected changes on hot-reload

Usage Examples

Basic Initialization

import { ConfigManager } from '@src/config/configManager.js';

// Initialize at startup
const configManager = ConfigManager.getInstance('/path/to/config.json');
await configManager.initialize();

// Get transport config for creating transports
const transportConfig = configManager.getTransportConfig();
// transportConfig: { serverName: { type: 'stdio', command: 'node', ... }, ... }

Listening for Config Changes

import { CONFIG_EVENTS, ConfigChangeType } from '@src/config/configManager.js';

const configManager = ConfigManager.getInstance();

configManager.on(CONFIG_EVENTS.CONFIG_CHANGED, (changes) => {
  for (const change of changes) {
    if (change.type === ConfigChangeType.ADDED) {
      console.log(`Server added: ${change.serverName}`);
    } else if (change.type === ConfigChangeType.REMOVED) {
      console.log(`Server removed: ${change.serverName}`);
    }
  }
});

Loading Declared Server Configs

const configManager = ConfigManager.getInstance();
const { staticServers, templateServers, errors } = configManager.loadDeclaredServerConfigs();

if (errors.length > 0) {
  console.warn('Config validation errors:', errors);
}
// staticServers: { name: MCPServerParams } — always-running servers
// templateServers: { name: MCPServerParams } — per-client instantiation templates

Related Pages

Implements Principle

Page Connections

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