UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

58 lines • 2.45 kB
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { getAgentDir } from "../../../../config.js"; import { McpConfigValidationError } from "./config.js"; import { getServerEndpointValidationError, validateConfig, } from "./config-schema.js"; export function getGlobalMcpConfigPath(agentDir = getAgentDir()) { return join(agentDir, "mcp.json"); } export function addGlobalMcpServer(name, server) { const path = getGlobalMcpConfigPath(); const config = readGlobalMcpConfig(path); const next = { ...config, mcpServers: { ...(config.mcpServers ?? {}), [name]: stripDefaults(server) } }; writeValidatedConfig(path, next); return path; } export function setGlobalMcpServerEnabled(name, enabled) { return updateMcpServerConfig(getGlobalMcpConfigPath(), name, { enabled }); } /** Update only a resolved global/project source, retaining placeholders and unrelated fields. */ export function updateMcpServerConfig(path, name, patch) { const config = readGlobalMcpConfig(path); const existing = config.mcpServers?.[name]; if (existing === undefined) return false; const next = { ...config, mcpServers: { ...(config.mcpServers ?? {}), [name]: { ...existing, ...patch } }, }; writeValidatedConfig(path, next); return true; } function readGlobalMcpConfig(path) { if (!existsSync(path)) return {}; const raw = JSON.parse(readFileSync(path, "utf8")); assertValidConfig(raw); return raw; } function writeValidatedConfig(path, config) { assertValidConfig(config); mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 }); } function assertValidConfig(config) { if (!validateConfig.Check(config)) { const error = Array.from(validateConfig.Errors(config))[0]; throw new McpConfigValidationError(`Invalid MCP config: ${error?.message ?? "unknown validation error"}`); } const endpointError = getServerEndpointValidationError(config); if (endpointError) throw new McpConfigValidationError(`Invalid MCP config at ${endpointError}`); } function stripDefaults(server) { if (server.type === "http") return { type: "http", url: server.url ?? "" }; return { type: "stdio", command: server.command ?? "", args: server.args }; } //# sourceMappingURL=config-edit.js.map