Validate + normalize a single server config object from disk.
(raw: unknown)
| 79 | |
| 80 | /** Validate + normalize a single server config object from disk. */ |
| 81 | function normalizeServerConfig(raw: unknown): McpServerConfig | undefined { |
| 82 | if (!isPlainObject(raw)) return undefined |
| 83 | const type = typeof raw.type === "string" ? raw.type : "stdio" |
| 84 | |
| 85 | if (type === "stdio") { |
| 86 | if (typeof raw.command !== "string" || !raw.command.trim()) return undefined |
| 87 | const config: McpStdioServerConfig = { type: "stdio", command: expandEnv(raw.command) } |
| 88 | if (Array.isArray(raw.args)) { |
| 89 | config.args = raw.args.map((a) => expandEnv(String(a))) |
| 90 | } |
| 91 | if (isPlainObject(raw.env)) { |
| 92 | const env: Record<string, string> = {} |
| 93 | for (const [k, v] of Object.entries(raw.env)) { |
| 94 | if (typeof v === "string") env[k] = expandEnv(v) |
| 95 | } |
| 96 | config.env = env |
| 97 | } |
| 98 | if (typeof raw.cwd === "string") config.cwd = expandEnv(raw.cwd) |
| 99 | return config |
| 100 | } |
| 101 | |
| 102 | if (type === "http" || type === "sse") { |
| 103 | if (typeof raw.url !== "string" || !raw.url.trim()) return undefined |
| 104 | const config: McpHttpServerConfig | McpSseServerConfig = { |
| 105 | type, |
| 106 | url: expandEnv(raw.url), |
| 107 | } as McpHttpServerConfig | McpSseServerConfig |
| 108 | if (isPlainObject(raw.headers)) { |
| 109 | const headers: Record<string, string> = {} |
| 110 | for (const [k, v] of Object.entries(raw.headers)) { |
| 111 | if (typeof v === "string") headers[k] = expandEnv(v) |
| 112 | } |
| 113 | ;(config as McpHttpServerConfig).headers = headers |
| 114 | } |
| 115 | const oauth = normalizeOAuth(raw.oauth) |
| 116 | if (oauth !== undefined) (config as McpHttpServerConfig).oauth = oauth |
| 117 | return config |
| 118 | } |
| 119 | |
| 120 | return undefined |
| 121 | } |
| 122 | |
| 123 | /** Validate + normalize a whole `mcpServers` record. */ |
| 124 | function normalizeServers( |
no test coverage detected