| 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": {...} } } } |
| 4 | const fs = require('fs'); |
| 5 | const path = require('path'); |
| 6 | const { SKILLS_ROOT } = require('./skill-fs'); |
| 7 | const { buildServerConfig } = require('./mcp-registry'); |
| 8 | |
| 9 | function mcpPath(agentId) { |
| 10 | return path.join(SKILLS_ROOT, agentId, 'mcp.json'); |
| 11 | } |
| 12 | |
| 13 | function 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 | |
| 20 | function 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 | |
| 26 | function listAgentServers(agentId) { |
| 27 | const cfg = readMcp(agentId); |
| 28 | return Object.keys(cfg.mcpServers || {}); |
| 29 | } |
| 30 | |
| 31 | function 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 | |
| 40 | function removeServer(agentId, name) { |
| 41 | const cfg = readMcp(agentId); |
| 42 | if (cfg.mcpServers) delete cfg.mcpServers[name]; |
| 43 | writeMcp(agentId, cfg); |
| 44 | } |
| 45 | |
| 46 | module.exports = { mcpPath, readMcp, writeMcp, listAgentServers, addServer, removeServer }; |