* Helper method to read a server's configuration from the appropriate settings file * @param serverName The name of the server to read * @param source Whether to read from the global or project config * @returns The validated server configuration
( serverName: string, source: "global" | "project" = "global", )
| 1954 | * @returns The validated server configuration |
| 1955 | */ |
| 1956 | private async readServerConfigFromFile( |
| 1957 | serverName: string, |
| 1958 | source: "global" | "project" = "global", |
| 1959 | ): Promise<z.infer<typeof ServerConfigSchema>> { |
| 1960 | // Determine which config file to read |
| 1961 | let configPath: string |
| 1962 | if (source === "project") { |
| 1963 | const projectMcpPath = await this.getProjectMcpPath() |
| 1964 | if (!projectMcpPath) { |
| 1965 | throw new Error("Project MCP configuration file not found") |
| 1966 | } |
| 1967 | configPath = projectMcpPath |
| 1968 | } else { |
| 1969 | configPath = await this.getMcpSettingsFilePath() |
| 1970 | } |
| 1971 | |
| 1972 | // Ensure the settings file exists and is accessible |
| 1973 | try { |
| 1974 | await fs.access(configPath) |
| 1975 | } catch (error) { |
| 1976 | console.error("Settings file not accessible:", error) |
| 1977 | throw new Error("Settings file not accessible") |
| 1978 | } |
| 1979 | |
| 1980 | // Read and parse the config file |
| 1981 | const content = await fs.readFile(configPath, "utf-8") |
| 1982 | const config = JSON.parse(content) |
| 1983 | |
| 1984 | // Validate the config structure |
| 1985 | if (!config || typeof config !== "object") { |
| 1986 | throw new Error("Invalid config structure") |
| 1987 | } |
| 1988 | |
| 1989 | if (!config.mcpServers || typeof config.mcpServers !== "object") { |
| 1990 | throw new Error("No mcpServers section in config") |
| 1991 | } |
| 1992 | |
| 1993 | if (!config.mcpServers[serverName]) { |
| 1994 | throw new Error(`Server ${serverName} not found in config`) |
| 1995 | } |
| 1996 | |
| 1997 | // Validate and return the server config |
| 1998 | return this.validateServerConfig(config.mcpServers[serverName], serverName) |
| 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * Helper method to update a server's configuration in the appropriate settings file |
no test coverage detected