Elffuss BraidHub

kikocis/clonagent public

main · 1.5 KiBRawPermalink
1// Read/write per-agent MCP config (mcp.json) under ~/.claude/skills/<id>/mcp.json
2// Format matches Claude Code's expected mcp.json:
3// { "mcpServers": { "<name>": { "command": "...", "args": [...], "env": {...} } } }
4const fs = require('fs');
5const path = require('path');
6const { SKILLS_ROOT } = require('./skill-fs');
7const { buildServerConfig } = require('./mcp-registry');
8
9function mcpPath(agentId) {
10 return path.join(SKILLS_ROOT, agentId, 'mcp.json');
11}
12
13function readMcp(agentId) {
14 const p = mcpPath(agentId);
15 if (!fs.existsSync(p)) return { mcpServers: {} };
16 try { return JSON.parse(fs.readFileSync(p, 'utf8')); }
17 catch { return { mcpServers: {} }; }
18}
19
20function writeMcp(agentId, mcp) {
21 const skillDir = path.join(SKILLS_ROOT, agentId);
22 if (!fs.existsSync(skillDir)) throw new Error(`skill ${agentId} does not exist`);
23 fs.writeFileSync(mcpPath(agentId), JSON.stringify(mcp, null, 2));
24}
25
26function listAgentServers(agentId) {
27 const cfg = readMcp(agentId);
28 return Object.keys(cfg.mcpServers || {});
29}
30
31function addServer(agentId, registryName, values) {
32 const built = buildServerConfig(registryName, values);
33 const cfg = readMcp(agentId);
34 cfg.mcpServers = cfg.mcpServers || {};
35 cfg.mcpServers[registryName] = built;
36 writeMcp(agentId, cfg);
37 return cfg.mcpServers[registryName];
38}
39
40function removeServer(agentId, name) {
41 const cfg = readMcp(agentId);
42 if (cfg.mcpServers) delete cfg.mcpServers[name];
43 writeMcp(agentId, cfg);
44}
45
46module.exports = { mcpPath, readMcp, writeMcp, listAgentServers, addServer, removeServer };