* Helper method to update a server's configuration in the appropriate settings file * @param serverName The name of the server to update * @param configUpdate The configuration updates to apply * @param source Whether to update the global or project config
( serverName: string, configUpdate: Record<string, any>, source: "global" | "project" = "global", )
| 2005 | * @param source Whether to update the global or project config |
| 2006 | */ |
| 2007 | private async updateServerConfig( |
| 2008 | serverName: string, |
| 2009 | configUpdate: Record<string, any>, |
| 2010 | source: "global" | "project" = "global", |
| 2011 | ): Promise<void> { |
| 2012 | // Determine which config file to update |
| 2013 | let configPath: string |
| 2014 | if (source === "project") { |
| 2015 | const projectMcpPath = await this.getProjectMcpPath() |
| 2016 | if (!projectMcpPath) { |
| 2017 | throw new Error("Project MCP configuration file not found") |
| 2018 | } |
| 2019 | configPath = projectMcpPath |
| 2020 | } else { |
| 2021 | configPath = await this.getMcpSettingsFilePath() |
| 2022 | } |
| 2023 | |
| 2024 | // Ensure the settings file exists and is accessible |
| 2025 | try { |
| 2026 | await fs.access(configPath) |
| 2027 | } catch (error) { |
| 2028 | console.error("Settings file not accessible:", error) |
| 2029 | throw new Error("Settings file not accessible") |
| 2030 | } |
| 2031 | |
| 2032 | // Read and parse the config file |
| 2033 | const content = await fs.readFile(configPath, "utf-8") |
| 2034 | const config = JSON.parse(content) |
| 2035 | |
| 2036 | // Validate the config structure |
| 2037 | if (!config || typeof config !== "object") { |
| 2038 | throw new Error("Invalid config structure") |
| 2039 | } |
| 2040 | |
| 2041 | if (!config.mcpServers || typeof config.mcpServers !== "object") { |
| 2042 | config.mcpServers = {} |
| 2043 | } |
| 2044 | |
| 2045 | if (!config.mcpServers[serverName]) { |
| 2046 | config.mcpServers[serverName] = {} |
| 2047 | } |
| 2048 | |
| 2049 | // Create a new server config object to ensure clean structure |
| 2050 | const serverConfig = { |
| 2051 | ...config.mcpServers[serverName], |
| 2052 | ...configUpdate, |
| 2053 | } |
| 2054 | |
| 2055 | // Ensure required fields exist |
| 2056 | if (!serverConfig.alwaysAllow) { |
| 2057 | serverConfig.alwaysAllow = [] |
| 2058 | } |
| 2059 | |
| 2060 | config.mcpServers[serverName] = serverConfig |
| 2061 | |
| 2062 | // Write the entire config back |
| 2063 | const updatedConfig = { |
| 2064 | mcpServers: config.mcpServers, |
no test coverage detected