Implementation:1mcp app Agent setupServer
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, MCP_Protocol |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete orchestration function that initializes the full 1MCP server stack—config, presets, transports, loading mode selection, server manager, template indexing, and async loading—returning a unified setup result.
Description
The setupServer function is the central wiring point for 1MCP runtime startup. It initializes the ConfigManager and ConfigChangeHandler singletons, resolves global context, loads transport config (static servers only), initializes the preset system, creates transports, and then branches between `setupServerAsync` (non-blocking) and `setupServerSync` (blocking) based on the async loading feature flag and startup policy. After setup, it loads declared server configs and rebuilds the template index. The result includes the server manager, loading manager, loading promise, async/lazy orchestrators, and instruction aggregator.
Usage
Import this function when you need to:
- Bootstrap the 1MCP runtime at server startup
- Choose between async and sync loading modes based on configuration
- Get the ServerSetupResult containing all runtime components for the HTTP server
- Initialize the preset system alongside server setup
Code Reference
Source Location
- Repository: 1MCP Agent
- File: src/server.ts (L58-307)
Signature
export interface ServerSetupResult {
serverManager: ServerManager;
loadingManager: McpLoadingManager;
loadingPromise: Promise<void>;
asyncOrchestrator?: AsyncLoadingOrchestrator;
lazyLoadingOrchestrator?: LazyLoadingOrchestrator;
instructionAggregator: InstructionAggregator;
}
async function setupServer(
configFilePath?: string,
context?: ContextData,
backendLoadingPolicy?: BackendLoadingPolicy,
startupPolicy?: BackendStartupPolicy,
): Promise<ServerSetupResult>;
Import
import { setupServer } from '@src/server.js';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| configFilePath | string | No | Path to config file |
| context | ContextData | No | Session context; falls back to global context manager |
| backendLoadingPolicy | BackendLoadingPolicy | No | Loading policy; defaults to DEFAULT_BACKEND_LOADING_POLICY |
| startupPolicy | BackendStartupPolicy | No | 'configured' or 'cooperative-activation' |
Outputs
| Name | Type | Description |
|---|---|---|
| serverManager | ServerManager | Central server manager facade |
| loadingManager | McpLoadingManager | Async loading manager |
| loadingPromise | Promise<void> | Resolves when all MCP servers finish loading |
| asyncOrchestrator | undefined | Only in async mode |
Usage Examples
Basic Server Setup
import { setupServer } from '@src/server.js';
const result = await setupServer('/path/to/config.json');
// Use serverManager for HTTP server
const { serverManager, loadingManager, loadingPromise } = result;
// Start HTTP server with serverManager
// In async mode, HTTP starts immediately; loadingPromise resolves later
await loadingPromise; // Optional: wait for all backends
With Cooperative Activation
const result = await setupServer(
configPath,
undefined,
undefined,
'cooperative-activation', // Forces async mode even if flag is off
);