| | import { logger } from '@librechat/data-schemas'; |
| | import { MCPConnectionFactory } from '~/mcp/MCPConnectionFactory'; |
| | import { MCPConnection } from './connection'; |
| | import type * as t from './types'; |
| |
|
| | |
| | |
| | |
| | |
| | export class ConnectionsRepository { |
| | protected readonly serverConfigs: Record<string, t.MCPOptions>; |
| | protected connections: Map<string, MCPConnection> = new Map(); |
| | protected oauthOpts: t.OAuthConnectionOptions | undefined; |
| |
|
| | constructor(serverConfigs: t.MCPServers, oauthOpts?: t.OAuthConnectionOptions) { |
| | this.serverConfigs = serverConfigs; |
| | this.oauthOpts = oauthOpts; |
| | } |
| |
|
| | |
| | has(serverName: string): boolean { |
| | return !!this.serverConfigs[serverName]; |
| | } |
| |
|
| | |
| | async get(serverName: string): Promise<MCPConnection> { |
| | const existingConnection = this.connections.get(serverName); |
| | if (existingConnection && (await existingConnection.isConnected())) return existingConnection; |
| | else await this.disconnect(serverName); |
| |
|
| | const connection = await MCPConnectionFactory.create( |
| | { |
| | serverName, |
| | serverConfig: this.getServerConfig(serverName), |
| | }, |
| | this.oauthOpts, |
| | ); |
| |
|
| | this.connections.set(serverName, connection); |
| | return connection; |
| | } |
| |
|
| | |
| | async getMany(serverNames: string[]): Promise<Map<string, MCPConnection>> { |
| | const connectionPromises = serverNames.map(async (name) => [name, await this.get(name)]); |
| | const connections = await Promise.all(connectionPromises); |
| | return new Map(connections as [string, MCPConnection][]); |
| | } |
| |
|
| | |
| | async getLoaded(): Promise<Map<string, MCPConnection>> { |
| | return this.getMany(Array.from(this.connections.keys())); |
| | } |
| |
|
| | |
| | async getAll(): Promise<Map<string, MCPConnection>> { |
| | return this.getMany(Object.keys(this.serverConfigs)); |
| | } |
| |
|
| | |
| | disconnect(serverName: string): Promise<void> { |
| | const connection = this.connections.get(serverName); |
| | if (!connection) return Promise.resolve(); |
| | this.connections.delete(serverName); |
| | return connection.disconnect().catch((err) => { |
| | logger.error(`${this.prefix(serverName)} Error disconnecting`, err); |
| | }); |
| | } |
| |
|
| | |
| | disconnectAll(): Promise<void>[] { |
| | const serverNames = Array.from(this.connections.keys()); |
| | return serverNames.map((serverName) => this.disconnect(serverName)); |
| | } |
| |
|
| | |
| | protected getServerConfig(serverName: string): t.MCPOptions { |
| | const serverConfig = this.serverConfigs[serverName]; |
| | if (serverConfig) return serverConfig; |
| | throw new Error(`${this.prefix(serverName)} Server not found in configuration`); |
| | } |
| |
|
| | |
| | protected prefix(serverName: string): string { |
| | return `[MCP][${serverName}]`; |
| | } |
| | } |
| |
|