import { existsSync, readFileSync } from "node:fs"; import type { McpServerEntry } from "../types.ts"; function loadJson(path: string): Record { if (!existsSync(path)) return {}; try { return JSON.parse(readFileSync(path, "utf-8")) as Record; } catch { return {}; } } export function readMcpServersFromConfig(path: string): McpServerEntry[] { const config = loadJson(path); const servers = config.mcpServers; if (!servers || typeof servers !== "object") return []; const entries: McpServerEntry[] = []; for (const [name, value] of Object.entries(servers as Record)) { if (!value || typeof value !== "object") continue; const server = value as Record; const command = typeof server.command === "string" ? server.command : undefined; const url = typeof server.url === "string" ? server.url : undefined; const lifecycle = typeof server.lifecycle === "string" ? server.lifecycle : undefined; const mode = url ? "http" : "stdio"; entries.push({ name, mode, lifecycle, command, url }); } return entries; }