|
| 1 | +/** |
| 2 | + * Browser-safe MCPClient that excludes Node.js-specific functionality |
| 3 | + */ |
| 4 | + |
| 5 | +import { createConnectorFromConfig } from './config-browser.js' |
| 6 | +import { logger } from './logging.js' |
| 7 | +import { MCPSession } from './session.js' |
| 8 | + |
| 9 | +export class MCPClient { |
| 10 | + private config: Record<string, any> = {} |
| 11 | + private sessions: Record<string, MCPSession> = {} |
| 12 | + public activeSessions: string[] = [] |
| 13 | + |
| 14 | + constructor(config?: Record<string, any>) { |
| 15 | + if (config) { |
| 16 | + this.config = config |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public static fromDict(cfg: Record<string, any>): MCPClient { |
| 21 | + return new MCPClient(cfg) |
| 22 | + } |
| 23 | + |
| 24 | + public addServer(name: string, serverConfig: Record<string, any>): void { |
| 25 | + this.config.mcpServers = this.config.mcpServers || {} |
| 26 | + this.config.mcpServers[name] = serverConfig |
| 27 | + } |
| 28 | + |
| 29 | + public removeServer(name: string): void { |
| 30 | + if (this.config.mcpServers?.[name]) { |
| 31 | + delete this.config.mcpServers[name] |
| 32 | + this.activeSessions = this.activeSessions.filter(n => n !== name) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public getServerNames(): string[] { |
| 37 | + return Object.keys(this.config.mcpServers ?? {}) |
| 38 | + } |
| 39 | + |
| 40 | + public getServerConfig(name: string): Record<string, any> { |
| 41 | + return this.config.mcpServers?.[name] |
| 42 | + } |
| 43 | + |
| 44 | + public getConfig(): Record<string, any> { |
| 45 | + return this.config ?? {} |
| 46 | + } |
| 47 | + |
| 48 | + public async createSession( |
| 49 | + serverName: string, |
| 50 | + autoInitialize = true, |
| 51 | + ): Promise<MCPSession> { |
| 52 | + const servers = this.config.mcpServers ?? {} |
| 53 | + |
| 54 | + if (Object.keys(servers).length === 0) { |
| 55 | + logger.warn('No MCP servers defined in config') |
| 56 | + } |
| 57 | + |
| 58 | + if (!servers[serverName]) { |
| 59 | + throw new Error(`Server '${serverName}' not found in config`) |
| 60 | + } |
| 61 | + |
| 62 | + const connector = createConnectorFromConfig(servers[serverName]) |
| 63 | + const session = new MCPSession(connector) |
| 64 | + if (autoInitialize) { |
| 65 | + await session.initialize() |
| 66 | + } |
| 67 | + |
| 68 | + this.sessions[serverName] = session |
| 69 | + if (!this.activeSessions.includes(serverName)) { |
| 70 | + this.activeSessions.push(serverName) |
| 71 | + } |
| 72 | + return session |
| 73 | + } |
| 74 | + |
| 75 | + public async createAllSessions( |
| 76 | + autoInitialize = true, |
| 77 | + ): Promise<Record<string, MCPSession>> { |
| 78 | + const servers = this.config.mcpServers ?? {} |
| 79 | + |
| 80 | + if (Object.keys(servers).length === 0) { |
| 81 | + logger.warn('No MCP servers defined in config') |
| 82 | + } |
| 83 | + |
| 84 | + for (const name of Object.keys(servers)) { |
| 85 | + await this.createSession(name, autoInitialize) |
| 86 | + } |
| 87 | + |
| 88 | + return this.sessions |
| 89 | + } |
| 90 | + |
| 91 | + public getSession(serverName: string): MCPSession | null { |
| 92 | + const session = this.sessions[serverName] |
| 93 | + if (!session) { |
| 94 | + return null |
| 95 | + } |
| 96 | + return session |
| 97 | + } |
| 98 | + |
| 99 | + public getAllActiveSessions(): Record<string, MCPSession> { |
| 100 | + return Object.fromEntries( |
| 101 | + this.activeSessions.map(n => [n, this.sessions[n]]), |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + public async closeSession(serverName: string): Promise<void> { |
| 106 | + const session = this.sessions[serverName] |
| 107 | + if (!session) { |
| 108 | + logger.warn(`No session exists for server ${serverName}, nothing to close`) |
| 109 | + return |
| 110 | + } |
| 111 | + try { |
| 112 | + logger.debug(`Closing session for server ${serverName}`) |
| 113 | + await session.disconnect() |
| 114 | + } |
| 115 | + catch (e) { |
| 116 | + logger.error(`Error closing session for server '${serverName}': ${e}`) |
| 117 | + } |
| 118 | + finally { |
| 119 | + delete this.sessions[serverName] |
| 120 | + this.activeSessions = this.activeSessions.filter(n => n !== serverName) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public async closeAllSessions(): Promise<void> { |
| 125 | + const serverNames = Object.keys(this.sessions) |
| 126 | + const errors: string[] = [] |
| 127 | + for (const serverName of serverNames) { |
| 128 | + try { |
| 129 | + logger.debug(`Closing session for server ${serverName}`) |
| 130 | + await this.closeSession(serverName) |
| 131 | + } |
| 132 | + catch (e: any) { |
| 133 | + const errorMsg = `Failed to close session for server '${serverName}': ${e}` |
| 134 | + logger.error(errorMsg) |
| 135 | + errors.push(errorMsg) |
| 136 | + } |
| 137 | + } |
| 138 | + if (errors.length) { |
| 139 | + logger.error(`Encountered ${errors.length} errors while closing sessions`) |
| 140 | + } |
| 141 | + else { |
| 142 | + logger.debug('All sessions closed successfully') |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments