/**
* Loads MCP server config from mcp-config.json or mcp-config.yaml.
* Config: enabledTools, defaults, webhookUrl, rateLimitPerMinute, assumeRole.
*/
import { readFileSync, existsSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
export interface MCPConfig {
enabledTools?: string[];
defaults?: Record<string, unknown>;
webhookUrl?: string;
webhookSecret?: string;
rateLimitPerMinute?: number;
assumeRole?: {
roleArn: string;
externalId?: string;
sessionName?: string;
};
}
let cached: MCPConfig | null = null;
function findConfigPath(): string | null {
const dir = path.dirname(fileURLToPath(import.meta.url));
const candidates = [
path.join(process.cwd(), "mcp-config.json"),
path.join(dir, "..", "..", "mcp-config.json"),
];
for (const p of candidates) {
if (existsSync(p)) return p;
}
return null;
}
export function loadConfig(): MCPConfig {
if (cached) return cached;
const configPath = findConfigPath();
if (!configPath) {
cached = {};
return cached;
}
try {
const raw = readFileSync(configPath, "utf-8");
cached = JSON.parse(raw) as MCPConfig;
} catch {
cached = {};
}
return cached;
}