|
| 1 | +import { readJSON } from "./fs.js"; |
| 2 | +import { resolve, dirname } from "node:path"; |
| 3 | +import { existsSync } from "node:fs"; |
| 4 | +import { genaiscriptDebug } from "./debug.js"; |
| 5 | + |
| 6 | +const dbg = genaiscriptDebug("mcp:config"); |
| 7 | + |
| 8 | +/** |
| 9 | + * Claude MCP configuration file format |
| 10 | + */ |
| 11 | +interface ClaudeMcpConfig { |
| 12 | + servers?: Record<string, ClaudeMcpServerConfig>; |
| 13 | + mcpServers?: Record<string, ClaudeMcpServerConfig>; |
| 14 | +} |
| 15 | + |
| 16 | +interface ClaudeMcpServerConfig { |
| 17 | + type?: "stdio"; |
| 18 | + command: string; |
| 19 | + args?: string[]; |
| 20 | + env?: Record<string, string>; |
| 21 | + envFile?: string; |
| 22 | + cwd?: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Interpolates Claude environment variables in a string |
| 27 | + * Supports ${workspaceFolder}, ${env:VARIABLE_NAME}, ${VARIABLE_NAME} (for capitalized env vars), etc. |
| 28 | + */ |
| 29 | +function interpolateClaudeVariables( |
| 30 | + value: string, |
| 31 | + workspaceFolder: string, |
| 32 | + env: Record<string, string> = process.env, |
| 33 | +): string { |
| 34 | + return value |
| 35 | + .replace(/\$\{workspaceFolder\}/g, workspaceFolder) |
| 36 | + .replace(/\$\{env:([^}]+)\}/g, (_, varName) => env[varName] || "") |
| 37 | + .replace(/\$\{([A-Z_][A-Z0-9_]*)\}/g, (_, varName) => env[varName] || ""); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Recursively interpolates Claude variables in an object |
| 42 | + */ |
| 43 | +function interpolateObjectValues( |
| 44 | + obj: any, |
| 45 | + workspaceFolder: string, |
| 46 | + env: Record<string, string> = process.env, |
| 47 | +): any { |
| 48 | + if (typeof obj === "string") { |
| 49 | + return interpolateClaudeVariables(obj, workspaceFolder, env); |
| 50 | + } |
| 51 | + if (Array.isArray(obj)) { |
| 52 | + return obj.map((item) => interpolateObjectValues(item, workspaceFolder, env)); |
| 53 | + } |
| 54 | + if (obj && typeof obj === "object") { |
| 55 | + const result: any = {}; |
| 56 | + for (const [key, value] of Object.entries(obj)) { |
| 57 | + result[key] = interpolateObjectValues(value, workspaceFolder, env); |
| 58 | + } |
| 59 | + return result; |
| 60 | + } |
| 61 | + return obj; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Loads and parses a Claude MCP configuration file |
| 66 | + * @param configPath Path to the MCP configuration file |
| 67 | + * @param workspaceFolder Workspace folder for variable interpolation (defaults to config file directory) |
| 68 | + * @returns Parsed MCP server configurations |
| 69 | + */ |
| 70 | +export async function loadClaudeMcpConfig( |
| 71 | + configPath: string, |
| 72 | + workspaceFolder?: string, |
| 73 | +): Promise<Record<string, any>> { |
| 74 | + const resolvedPath = resolve(configPath); |
| 75 | + |
| 76 | + dbg(`Loading MCP configuration from: ${resolvedPath}`); |
| 77 | + |
| 78 | + if (!existsSync(resolvedPath)) { |
| 79 | + throw new Error(`MCP configuration file not found: ${resolvedPath}`); |
| 80 | + } |
| 81 | + |
| 82 | + let config: ClaudeMcpConfig; |
| 83 | + try { |
| 84 | + config = await readJSON(resolvedPath); |
| 85 | + dbg(`Successfully parsed MCP configuration file`); |
| 86 | + } catch (error) { |
| 87 | + dbg(`Failed to parse MCP configuration file: ${error.message}`); |
| 88 | + throw new Error(`Failed to parse MCP configuration file: ${error.message}`); |
| 89 | + } |
| 90 | + |
| 91 | + // Support both "servers" and "mcpServers" key names |
| 92 | + const serversConfig = config.servers || config.mcpServers; |
| 93 | + if (!serversConfig || typeof serversConfig !== "object") { |
| 94 | + throw new Error( |
| 95 | + "Invalid MCP configuration: missing or invalid 'servers' or 'mcpServers' object", |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Use config file directory as workspace folder if not provided |
| 100 | + const wsFolder = workspaceFolder || dirname(resolvedPath); |
| 101 | + dbg(`Using workspace folder: ${wsFolder}`); |
| 102 | + |
| 103 | + // Convert Claude format to GenAIScript format |
| 104 | + const mcpServers: Record<string, any> = {}; |
| 105 | + |
| 106 | + for (const [serverId, serverConfig] of Object.entries(serversConfig)) { |
| 107 | + dbg(`Processing server: ${serverId}`); |
| 108 | + |
| 109 | + // Interpolate variables in the server configuration |
| 110 | + const interpolatedConfig = interpolateObjectValues(serverConfig, wsFolder); |
| 111 | + |
| 112 | + dbg(`Interpolated config for ${serverId}:`, interpolatedConfig); |
| 113 | + |
| 114 | + // Convert to GenAIScript McpServerConfig format |
| 115 | + const genaiscriptConfig = { |
| 116 | + command: interpolatedConfig.command, |
| 117 | + args: interpolatedConfig.args || [], |
| 118 | + env: interpolatedConfig.env, |
| 119 | + cwd: interpolatedConfig.cwd, |
| 120 | + }; |
| 121 | + |
| 122 | + mcpServers[serverId] = genaiscriptConfig; |
| 123 | + } |
| 124 | + |
| 125 | + dbg(`Loaded ${Object.keys(mcpServers).length} MCP servers:`, Object.keys(mcpServers)); |
| 126 | + |
| 127 | + return mcpServers; |
| 128 | +} |
0 commit comments