| 69 | // ============================================================================ |
| 70 | |
| 71 | class McpManagerImpl implements McpManager { |
| 72 | private connections = new Map<string, MCPServerConnection>() |
| 73 | private toolsCache = new Map<string, CoreTool[]>() |
| 74 | private listeners = new Map<string, Set<EventHandler>>() |
| 75 | private deps: McpClientDependencies |
| 76 | private connectFn: |
| 77 | | (( |
| 78 | name: string, |
| 79 | config: ScopedMcpServerConfig, |
| 80 | ) => Promise<MCPServerConnection>) |
| 81 | | null = null |
| 82 | |
| 83 | constructor(deps: McpClientDependencies) { |
| 84 | this.deps = deps |
| 85 | } |
| 86 | |
| 87 | /** Set the connect function — the host provides this with all transport logic */ |
| 88 | setConnectFn( |
| 89 | fn: ( |
| 90 | name: string, |
| 91 | config: ScopedMcpServerConfig, |
| 92 | ) => Promise<MCPServerConnection>, |
| 93 | ): void { |
| 94 | this.connectFn = fn |
| 95 | } |
| 96 | |
| 97 | async connect( |
| 98 | name: string, |
| 99 | config: McpServerConfig, |
| 100 | ): Promise<MCPServerConnection> { |
| 101 | if (!this.connectFn) { |
| 102 | throw new Error( |
| 103 | 'McpManager: connectFn not set. Call setConnectFn() first.', |
| 104 | ) |
| 105 | } |
| 106 | |
| 107 | const scopedConfig: ScopedMcpServerConfig = { ...config, scope: 'dynamic' } |
| 108 | |
| 109 | try { |
| 110 | const connection = await this.connectFn(name, scopedConfig) |
| 111 | this.connections.set(name, connection) |
| 112 | |
| 113 | if (connection.type === 'connected') { |
| 114 | this.emit('connected', name) |
| 115 | // Fetch tools for this server |
| 116 | await this.refreshTools(name, connection) |
| 117 | } else if (connection.type === 'needs-auth') { |
| 118 | this.emit('authRequired', name) |
| 119 | } |
| 120 | |
| 121 | return connection |
| 122 | } catch (err) { |
| 123 | const error = err instanceof Error ? err : new Error(String(err)) |
| 124 | this.emit('error', name, error) |
| 125 | throw err |
| 126 | } |
| 127 | } |
| 128 |
nothing calls this directly
no outgoing calls
no test coverage detected