|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as os from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { |
| 5 | + SERVER_NAME, |
| 6 | + getBunPath, |
| 7 | + getMcpServerPath, |
| 8 | + getPackageRoot, |
| 9 | + readJsonFile, |
| 10 | + writeJsonFile, |
| 11 | +} from './utils.js'; |
| 12 | + |
| 13 | +export const IRONCLAW_CONFIG_SCHEMA_VERSION = 1; |
| 14 | + |
| 15 | +export type IronclawStdioTransport = { |
| 16 | + transport: 'stdio'; |
| 17 | + command: string; |
| 18 | + args: string[]; |
| 19 | + env: Record<string, string>; |
| 20 | +}; |
| 21 | + |
| 22 | +export type IronclawMcpServer = { |
| 23 | + name: string; |
| 24 | + url: string; |
| 25 | + transport: IronclawStdioTransport; |
| 26 | + headers: Record<string, string>; |
| 27 | + enabled: boolean; |
| 28 | + description?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +export type IronclawMcpConfig = { |
| 32 | + schema_version: number; |
| 33 | + servers: IronclawMcpServer[]; |
| 34 | +}; |
| 35 | + |
| 36 | +type MergeAction = 'created' | 'updated' | 'skipped'; |
| 37 | +type SkillAction = 'created' | 'updated' | 'skipped'; |
| 38 | + |
| 39 | +function getIronclawBaseDir(): string { |
| 40 | + return path.join(os.homedir(), '.ironclaw'); |
| 41 | +} |
| 42 | + |
| 43 | +export function getIronclawMcpConfigPath(): string { |
| 44 | + return path.join(getIronclawBaseDir(), 'mcp-servers.json'); |
| 45 | +} |
| 46 | + |
| 47 | +export function getIronclawSkillsDir(): string { |
| 48 | + return path.join(getIronclawBaseDir(), 'skills'); |
| 49 | +} |
| 50 | + |
| 51 | +export function getIronclawSkillInstallPath(skillsDir = getIronclawSkillsDir()): string { |
| 52 | + return path.join(skillsDir, SERVER_NAME, 'SKILL.md'); |
| 53 | +} |
| 54 | + |
| 55 | +function getBundledSkillPath(): string { |
| 56 | + return path.join(getPackageRoot(), 'SKILL.md'); |
| 57 | +} |
| 58 | + |
| 59 | +function normalizeIronclawMcpConfig(existing: any): IronclawMcpConfig { |
| 60 | + return { |
| 61 | + schema_version: |
| 62 | + typeof existing?.schema_version === 'number' |
| 63 | + ? existing.schema_version |
| 64 | + : IRONCLAW_CONFIG_SCHEMA_VERSION, |
| 65 | + servers: Array.isArray(existing?.servers) ? [...existing.servers] : [], |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function generateIronclawServerEntry(customServerPath?: string): IronclawMcpServer { |
| 70 | + return { |
| 71 | + name: SERVER_NAME, |
| 72 | + url: '', |
| 73 | + transport: { |
| 74 | + transport: 'stdio', |
| 75 | + command: getBunPath(), |
| 76 | + args: ['run', customServerPath || getMcpServerPath()], |
| 77 | + env: { |
| 78 | + AELFSCAN_API_BASE_URL: 'https://aelfscan.io', |
| 79 | + }, |
| 80 | + }, |
| 81 | + headers: {}, |
| 82 | + enabled: true, |
| 83 | + description: 'AelfScan explorer MCP server for IronClaw', |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +export function mergeIronclawMcpConfig( |
| 88 | + existing: any, |
| 89 | + entry: IronclawMcpServer, |
| 90 | + force = false, |
| 91 | +): { config: IronclawMcpConfig; action: MergeAction } { |
| 92 | + const config = normalizeIronclawMcpConfig(existing); |
| 93 | + const index = config.servers.findIndex(server => server?.name === entry.name); |
| 94 | + |
| 95 | + if (index >= 0 && !force) { |
| 96 | + return { config, action: 'skipped' }; |
| 97 | + } |
| 98 | + |
| 99 | + if (index >= 0) { |
| 100 | + const servers = [...config.servers]; |
| 101 | + servers[index] = entry; |
| 102 | + return { |
| 103 | + config: { ...config, schema_version: IRONCLAW_CONFIG_SCHEMA_VERSION, servers }, |
| 104 | + action: 'updated', |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + config: { |
| 110 | + ...config, |
| 111 | + schema_version: IRONCLAW_CONFIG_SCHEMA_VERSION, |
| 112 | + servers: [...config.servers, entry], |
| 113 | + }, |
| 114 | + action: 'created', |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +export function removeIronclawMcpConfig( |
| 119 | + existing: any, |
| 120 | + serverName = SERVER_NAME, |
| 121 | +): { config: IronclawMcpConfig; removed: boolean } { |
| 122 | + const config = normalizeIronclawMcpConfig(existing); |
| 123 | + const servers = config.servers.filter(server => server?.name !== serverName); |
| 124 | + |
| 125 | + return { |
| 126 | + config: { |
| 127 | + ...config, |
| 128 | + schema_version: IRONCLAW_CONFIG_SCHEMA_VERSION, |
| 129 | + servers, |
| 130 | + }, |
| 131 | + removed: servers.length !== config.servers.length, |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +function installIronclawSkill( |
| 136 | + skillsDir = getIronclawSkillsDir(), |
| 137 | + force = false, |
| 138 | +): { action: SkillAction; skillPath: string } { |
| 139 | + const sourcePath = getBundledSkillPath(); |
| 140 | + const targetPath = getIronclawSkillInstallPath(skillsDir); |
| 141 | + const targetDir = path.dirname(targetPath); |
| 142 | + const existedBefore = fs.existsSync(targetPath); |
| 143 | + |
| 144 | + if (!fs.existsSync(sourcePath)) { |
| 145 | + throw new Error(`Bundled SKILL.md not found at ${sourcePath}`); |
| 146 | + } |
| 147 | + |
| 148 | + if (existedBefore && !force) { |
| 149 | + return { action: 'skipped', skillPath: targetPath }; |
| 150 | + } |
| 151 | + |
| 152 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 153 | + fs.copyFileSync(sourcePath, targetPath); |
| 154 | + |
| 155 | + return { |
| 156 | + action: existedBefore ? 'updated' : 'created', |
| 157 | + skillPath: targetPath, |
| 158 | + }; |
| 159 | +} |
| 160 | + |
| 161 | +export function assertSafeIronclawSkillDir(skillsDir: string, skillDir: string): void { |
| 162 | + const resolvedSkillsDir = path.resolve(skillsDir); |
| 163 | + const resolvedSkillDir = path.resolve(skillDir); |
| 164 | + const expectedSkillDir = path.dirname(getIronclawSkillInstallPath(resolvedSkillsDir)); |
| 165 | + const relative = path.relative(resolvedSkillsDir, resolvedSkillDir); |
| 166 | + |
| 167 | + if ( |
| 168 | + resolvedSkillDir !== expectedSkillDir || |
| 169 | + path.isAbsolute(relative) || |
| 170 | + relative.startsWith('..') || |
| 171 | + path.basename(resolvedSkillDir) !== SERVER_NAME |
| 172 | + ) { |
| 173 | + throw new Error(`Refusing to remove unexpected IronClaw skill path: ${resolvedSkillDir}`); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +function removeIronclawSkill( |
| 178 | + skillsDir = getIronclawSkillsDir(), |
| 179 | +): { skillRemoved: boolean; skillPath: string } { |
| 180 | + const skillPath = getIronclawSkillInstallPath(skillsDir); |
| 181 | + const skillDir = path.dirname(skillPath); |
| 182 | + const exists = fs.existsSync(skillDir); |
| 183 | + |
| 184 | + if (exists) { |
| 185 | + assertSafeIronclawSkillDir(skillsDir, skillDir); |
| 186 | + fs.rmSync(skillDir, { recursive: true, force: true }); |
| 187 | + } |
| 188 | + |
| 189 | + return { skillRemoved: exists, skillPath }; |
| 190 | +} |
| 191 | + |
| 192 | +export function setupIronclaw(opts: { |
| 193 | + mcpConfigPath?: string; |
| 194 | + skillsDir?: string; |
| 195 | + serverPath?: string; |
| 196 | + force?: boolean; |
| 197 | +}) { |
| 198 | + const mcpConfigPath = opts.mcpConfigPath || getIronclawMcpConfigPath(); |
| 199 | + const existing = readJsonFile(mcpConfigPath); |
| 200 | + const entry = generateIronclawServerEntry(opts.serverPath); |
| 201 | + const { config, action } = mergeIronclawMcpConfig(existing, entry, opts.force); |
| 202 | + const skill = installIronclawSkill(opts.skillsDir, opts.force); |
| 203 | + |
| 204 | + if (action !== 'skipped') { |
| 205 | + writeJsonFile(mcpConfigPath, config); |
| 206 | + } |
| 207 | + |
| 208 | + console.log(`[${action.toUpperCase()}] IronClaw MCP config → ${mcpConfigPath}`); |
| 209 | + console.log(`[${skill.action.toUpperCase()}] Trusted skill → ${skill.skillPath}`); |
| 210 | + |
| 211 | + return { |
| 212 | + mcpAction: action, |
| 213 | + skillAction: skill.action, |
| 214 | + mcpConfigPath, |
| 215 | + skillPath: skill.skillPath, |
| 216 | + }; |
| 217 | +} |
| 218 | + |
| 219 | +export function uninstallIronclaw(opts: { |
| 220 | + mcpConfigPath?: string; |
| 221 | + skillsDir?: string; |
| 222 | +}) { |
| 223 | + const mcpConfigPath = opts.mcpConfigPath || getIronclawMcpConfigPath(); |
| 224 | + const existing = readJsonFile(mcpConfigPath); |
| 225 | + const { config, removed } = removeIronclawMcpConfig(existing); |
| 226 | + const skill = removeIronclawSkill(opts.skillsDir); |
| 227 | + |
| 228 | + if (removed) { |
| 229 | + writeJsonFile(mcpConfigPath, config); |
| 230 | + } |
| 231 | + |
| 232 | + if (!removed) { |
| 233 | + console.log(`[SKIP] "${SERVER_NAME}" not found in ${mcpConfigPath}`); |
| 234 | + } else { |
| 235 | + console.log(`[REMOVED] "${SERVER_NAME}" from ${mcpConfigPath}`); |
| 236 | + } |
| 237 | + |
| 238 | + if (!skill.skillRemoved) { |
| 239 | + console.log(`[SKIP] Trusted skill not found at ${skill.skillPath}`); |
| 240 | + } else { |
| 241 | + console.log(`[REMOVED] Trusted skill → ${skill.skillPath}`); |
| 242 | + } |
| 243 | + |
| 244 | + return { |
| 245 | + mcpRemoved: removed, |
| 246 | + skillRemoved: skill.skillRemoved, |
| 247 | + mcpConfigPath, |
| 248 | + skillPath: skill.skillPath, |
| 249 | + }; |
| 250 | +} |
0 commit comments