Skip to main content
Glama
ConnectionManager.ts2.79 kB
import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; import { McpServerConnectionConfig } from "../../domain/types.js"; import { IConnectionManager } from "../../interfaces/IConnectionManager.js"; /** * ConnectionManager handles connections to multiple MCP child servers * Implements dependency injection pattern for better testability */ export class ConnectionManager implements IConnectionManager { private connections: Map<string, Client> = new Map(); /** * Create a new connection to an MCP server * @param config - Server configuration * @returns The connected Client instance * @throws Error if connection fails */ async createConnection(config: McpServerConnectionConfig): Promise<Client> { if (this.connections.has(config.name)) { console.error(`Connection '${config.name}' already exists`); return this.connections.get(config.name)!; } try { console.error(`Connecting to MCP server: ${config.name}`); const transport = new StdioClientTransport({ command: config.command, args: config.args, env: config.env, stderr: "pipe", }); const client = new Client( { name: `${config.name}-client`, version: "1.0.0", }, { capabilities: {}, } ); await client.connect(transport); // Store connection this.connections.set(config.name, client); console.error(`✓ Connected to ${config.name}`); return client; } catch (error) { this.handleConnectionError(config.name, error as Error); throw error; } } /** * Get an existing connection by server name * @param serverName - Name of the server * @returns The Client instance or undefined if not found */ getConnection(serverName: string): Client | undefined { return this.connections.get(serverName); } /** * Get all active connections * @returns Map of all connections (serverName -> Client) */ getAllConnections(): Map<string, Client> { return new Map(this.connections); } /** * Get the number of active connections * @returns Number of active connections */ getConnectionCount(): number { return this.connections.size; } /** * Handle connection errors consistently * @param serverName - Name of the server * @param error - Error that occurred */ private handleConnectionError(serverName: string, error: Error): void { const errorMessage = error.message || String(error); console.error(`✗ Failed to connect to ${serverName}:`, error); throw new Error(`Failed to connect to ${serverName}: ${errorMessage}`); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/eliavamar/mcp-of-mcps'

If you have feedback or need assistance with the MCP directory API, please join our Discord server