|
| 1 | +import { readFile, writeFile, mkdir } from 'fs/promises'; |
| 2 | +import { dirname } from 'path'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Base interface that all MCP config formats must implement. |
| 6 | + * This ensures type safety when accessing the servers collection. |
| 7 | + */ |
| 8 | +export interface MCPConfig { |
| 9 | + [serversKey: string]: Record<string, unknown>; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Abstract base class for managing MCP server configurations across different platforms. |
| 14 | + * |
| 15 | + * This implementation preserves all unknown properties in the config file to avoid data loss |
| 16 | + * when modifying only the server configuration. |
| 17 | + * |
| 18 | + * @template T - The specific config type for the platform (must extend MCPConfig) |
| 19 | + * @template S - The server configuration type for the platform |
| 20 | + */ |
| 21 | +export abstract class ConfigManager<T extends MCPConfig, S = unknown> { |
| 22 | + protected readonly serverName = 'argocd-mcp-stdio'; |
| 23 | + |
| 24 | + protected abstract configPath: string; |
| 25 | + protected abstract getDefaultConfig(): T; |
| 26 | + protected abstract getServersKey(): Extract<keyof T, string>; |
| 27 | + protected abstract createServerConfig(baseUrl: string, apiToken: string): S; |
| 28 | + |
| 29 | + /** |
| 30 | + * ReadConfig preserves all existing properties in the config file. |
| 31 | + * @returns config casted to type T |
| 32 | + */ |
| 33 | + async readConfig(): Promise<T> { |
| 34 | + try { |
| 35 | + const content = await readFile(this.configPath, 'utf-8'); |
| 36 | + |
| 37 | + // Parse as unknown first to ensure we preserve all properties |
| 38 | + const parsed = JSON.parse(content) as unknown; |
| 39 | + |
| 40 | + // Ensure parsed is an object, not array |
| 41 | + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { |
| 42 | + throw new Error(`Config file ${this.configPath} contains invalid data: expected an object`); |
| 43 | + } |
| 44 | + |
| 45 | + return parsed as T; |
| 46 | + } catch (error) { |
| 47 | + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { |
| 48 | + return this.getDefaultConfig(); |
| 49 | + } |
| 50 | + |
| 51 | + if (error instanceof SyntaxError) { |
| 52 | + throw new Error(`Invalid JSON in config file ${this.configPath}: ${error.message}`); |
| 53 | + } |
| 54 | + |
| 55 | + throw error; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + async writeConfig(config: T): Promise<void> { |
| 60 | + const dir = dirname(this.configPath); |
| 61 | + try { |
| 62 | + await mkdir(dir, { recursive: true }); |
| 63 | + await writeFile(this.configPath, JSON.stringify(config, null, 2), 'utf-8'); |
| 64 | + } catch (error) { |
| 65 | + throw new Error(`Failed to write config to ${this.configPath}: ${(error as Error).message}`); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Enable the server configuration. |
| 71 | + * @param baseUrl - Optional ArgoCD base URL |
| 72 | + * @param apiToken - Optional ArgoCD API token |
| 73 | + * @returns true if the server was already enabled, false if it was newly enabled |
| 74 | + */ |
| 75 | + async enable(baseUrl?: string, apiToken?: string): Promise<boolean> { |
| 76 | + // MCP servers do not have access to local env, it must be set in config |
| 77 | + // If flag was not set, fallback to env |
| 78 | + if (!baseUrl) { |
| 79 | + baseUrl = process.env.ARGOCD_BASE_URL; |
| 80 | + if (!baseUrl) { |
| 81 | + throw new Error( |
| 82 | + 'Argocd baseurl not provided and not in env, please provide it with the --url flag' |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + // Validate url |
| 87 | + new URL(baseUrl); |
| 88 | + |
| 89 | + if (!apiToken) { |
| 90 | + apiToken = process.env.ARGOCD_API_TOKEN; |
| 91 | + if (!apiToken) { |
| 92 | + throw new Error( |
| 93 | + 'Argocd token not provided and not in env, please provide it with the --token flag' |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const config = await this.readConfig(); |
| 99 | + const serversKey = this.getServersKey(); |
| 100 | + |
| 101 | + // Ensure servers object exists |
| 102 | + const obj = config[serversKey]; |
| 103 | + if (!obj || typeof obj !== 'object' || Array.isArray(obj)) { |
| 104 | + (config[serversKey] as Record<string, S>) = {}; |
| 105 | + } |
| 106 | + |
| 107 | + const servers = config[serversKey] as Record<string, S>; |
| 108 | + const wasEnabled = this.serverName in servers; |
| 109 | + const serverConfig = this.createServerConfig(baseUrl, apiToken); |
| 110 | + servers[this.serverName] = serverConfig; |
| 111 | + await this.writeConfig(config); |
| 112 | + return wasEnabled; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Disable the server configuration. |
| 117 | + * @returns true if the server was enabled and has been disabled, false if it was not enabled |
| 118 | + */ |
| 119 | + async disable(): Promise<boolean> { |
| 120 | + const config = await this.readConfig(); |
| 121 | + const serversKey = this.getServersKey(); |
| 122 | + |
| 123 | + const obj = config[serversKey]; |
| 124 | + if (!obj || typeof obj !== 'object' || Array.isArray(obj)) { |
| 125 | + // Nothing to disable if servers key doesn't exist |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + const servers = config[serversKey] as Record<string, S>; |
| 130 | + const wasEnabled = this.serverName in servers; |
| 131 | + |
| 132 | + if (wasEnabled) { |
| 133 | + delete servers[this.serverName]; |
| 134 | + await this.writeConfig(config); |
| 135 | + } |
| 136 | + |
| 137 | + return wasEnabled; |
| 138 | + } |
| 139 | + |
| 140 | + getConfigPath(): string { |
| 141 | + return this.configPath; |
| 142 | + } |
| 143 | +} |
0 commit comments