/** * `claudemesh install` / `uninstall` — manage Claude Code MCP registration. * * install: * 1. Preflight: bun is on PATH, this package's MCP entry is on disk. * 2. Read ~/.claude.json (or empty object if absent). * 3. Add/update `mcpServers.claudemesh` with the resolved entry path. * 4. Write back with 0600 perms. * 5. Verify via read-back, print success. * * uninstall: * 1. Read ~/.claude.json (bail if missing). * 2. Delete `mcpServers.claudemesh` if present. * 3. Write back. * * Both are idempotent — re-running install is a no-op if the entry is * already correct, and uninstall is a no-op if no entry exists. */ import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, } from "node:fs"; import { homedir, platform } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; import { loadConfig } from "../state/config"; const MCP_NAME = "claudemesh"; const CLAUDE_CONFIG = join(homedir(), ".claude.json"); const CLAUDE_SETTINGS = join(homedir(), ".claude", "settings.json"); const HOOK_COMMAND_STOP = "claudemesh hook idle"; const HOOK_COMMAND_USER_PROMPT = "claudemesh hook working"; const HOOK_MARKER = "claudemesh hook "; type McpEntry = { command: string; args?: string[]; env?: Record; }; interface HookCommand { type: "command"; command: string; } interface HookMatcher { matcher?: string; hooks: HookCommand[]; } type HooksConfig = Record; function readClaudeConfig(): Record { if (!existsSync(CLAUDE_CONFIG)) return {}; const text = readFileSync(CLAUDE_CONFIG, "utf-8").trim(); if (!text) return {}; try { return JSON.parse(text) as Record; } catch (e) { throw new Error( `failed to parse ${CLAUDE_CONFIG}: ${e instanceof Error ? e.message : String(e)}`, ); } } /** * Create a timestamped backup of ~/.claude.json before any write. */ function backupClaudeConfig(): void { if (!existsSync(CLAUDE_CONFIG)) return; const backupDir = join(dirname(CLAUDE_CONFIG), ".claude", "backups"); mkdirSync(backupDir, { recursive: true }); const ts = Date.now(); const dest = join(backupDir, `.claude.json.pre-claudemesh.${ts}`); copyFileSync(CLAUDE_CONFIG, dest); } /** * Atomic read-merge-write: re-reads ~/.claude.json at write time and * patches ONLY the `claudemesh` MCP entry. Never touches other keys. * Returns the action taken ("added" | "updated" | "unchanged"). */ function patchMcpServer(entry: McpEntry): "added" | "updated" | "unchanged" { backupClaudeConfig(); const cfg = readClaudeConfig(); const servers = ((cfg.mcpServers as Record) ?? {}); if (!cfg.mcpServers) cfg.mcpServers = servers; const existing = servers[MCP_NAME]; let action: "added" | "updated" | "unchanged"; if (!existing) { servers[MCP_NAME] = entry; action = "added"; } else if (entriesEqual(existing, entry)) { return "unchanged"; } else { servers[MCP_NAME] = entry; action = "updated"; } flushClaudeConfig(cfg); return action; } /** * Atomic read-merge-write: re-reads ~/.claude.json at write time and * removes ONLY the `claudemesh` MCP entry. Never touches other keys. * Returns true if an entry was removed. */ function removeMcpServer(): boolean { if (!existsSync(CLAUDE_CONFIG)) return false; backupClaudeConfig(); const cfg = readClaudeConfig(); const servers = cfg.mcpServers as Record | undefined; if (!servers || !(MCP_NAME in servers)) return false; delete servers[MCP_NAME]; cfg.mcpServers = servers; flushClaudeConfig(cfg); return true; } /** Low-level write — callers must backup + merge first. */ function flushClaudeConfig(obj: Record): void { mkdirSync(dirname(CLAUDE_CONFIG), { recursive: true }); writeFileSync( CLAUDE_CONFIG, JSON.stringify(obj, null, 2) + "\n", "utf-8", ); try { chmodSync(CLAUDE_CONFIG, 0o600); } catch { /* windows has no chmod */ } } /** Check `bun` is on PATH — OS-agnostic, node:child_process. */ function bunAvailable(): boolean { const res = platform() === "win32" ? spawnSync("where", ["bun"]) : spawnSync("sh", ["-c", "command -v bun"]); return res.status === 0; } /** Absolute path to this CLI's entry file. */ function resolveEntry(): string { const here = fileURLToPath(import.meta.url); // When bundled (dist/index.js), this file IS the entry → return self. // When running from source (src/index.ts via bun), walk up to the // dir + resolve index.ts. if (here.endsWith("/dist/index.js") || here.endsWith("\\dist\\index.js")) { return here; } return resolve(dirname(here), "..", "index.ts"); } /** * Build the MCP server entry for Claude Code's config. * * Two modes: * - Installed globally (npm i -g claudemesh-cli): use `claudemesh` * as the command, relies on it being on PATH. * - Local dev (bun apps/cli/src/index.ts): use `bun `. */ function buildMcpEntry(entryPath: string): McpEntry { const isBundled = entryPath.endsWith("/dist/index.js") || entryPath.endsWith("\\dist\\index.js"); if (isBundled) { return { command: "claudemesh", args: ["mcp"], }; } return { command: "bun", args: [entryPath, "mcp"], }; } function entriesEqual(a: McpEntry, b: McpEntry): boolean { return ( a.command === b.command && JSON.stringify(a.args ?? []) === JSON.stringify(b.args ?? []) ); } function readClaudeSettings(): Record { if (!existsSync(CLAUDE_SETTINGS)) return {}; const text = readFileSync(CLAUDE_SETTINGS, "utf-8").trim(); if (!text) return {}; try { return JSON.parse(text) as Record; } catch (e) { throw new Error( `failed to parse ${CLAUDE_SETTINGS}: ${e instanceof Error ? e.message : String(e)}`, ); } } function writeClaudeSettings(obj: Record): void { mkdirSync(dirname(CLAUDE_SETTINGS), { recursive: true }); writeFileSync( CLAUDE_SETTINGS, JSON.stringify(obj, null, 2) + "\n", "utf-8", ); } /** * All claudemesh MCP tool names, prefixed for allowedTools. * These let Claude Code use claudemesh tools without --dangerously-skip-permissions. */ const CLAUDEMESH_TOOLS = [ "mcp__claudemesh__cancel_scheduled", "mcp__claudemesh__check_messages", "mcp__claudemesh__claim_task", "mcp__claudemesh__complete_task", "mcp__claudemesh__create_stream", "mcp__claudemesh__create_task", "mcp__claudemesh__delete_file", "mcp__claudemesh__file_status", "mcp__claudemesh__forget", "mcp__claudemesh__get_context", "mcp__claudemesh__get_file", "mcp__claudemesh__get_state", "mcp__claudemesh__grant_file_access", "mcp__claudemesh__graph_execute", "mcp__claudemesh__graph_query", "mcp__claudemesh__join_group", "mcp__claudemesh__leave_group", "mcp__claudemesh__list_collections", "mcp__claudemesh__list_contexts", "mcp__claudemesh__list_files", "mcp__claudemesh__list_peers", "mcp__claudemesh__list_scheduled", "mcp__claudemesh__list_state", "mcp__claudemesh__list_streams", "mcp__claudemesh__list_tasks", "mcp__claudemesh__mesh_execute", "mcp__claudemesh__mesh_info", "mcp__claudemesh__mesh_query", "mcp__claudemesh__mesh_schema", "mcp__claudemesh__message_status", "mcp__claudemesh__ping_mesh", "mcp__claudemesh__publish", "mcp__claudemesh__recall", "mcp__claudemesh__remember", "mcp__claudemesh__schedule_reminder", "mcp__claudemesh__send_message", "mcp__claudemesh__set_state", "mcp__claudemesh__set_status", "mcp__claudemesh__set_summary", "mcp__claudemesh__share_context", "mcp__claudemesh__share_file", "mcp__claudemesh__subscribe", "mcp__claudemesh__vector_delete", "mcp__claudemesh__vector_search", "mcp__claudemesh__vector_store", ]; /** * Pre-approve all claudemesh MCP tools in allowedTools. * Merges into any existing list — never overwrites other entries. * Returns which tools were added vs already present. */ function installAllowedTools(): { added: string[]; unchanged: number } { const settings = readClaudeSettings(); const existing = new Set((settings.allowedTools as string[] | undefined) ?? []); const toAdd = CLAUDEMESH_TOOLS.filter((t) => !existing.has(t)); if (toAdd.length > 0) { settings.allowedTools = [...Array.from(existing), ...toAdd]; writeClaudeSettings(settings); } return { added: toAdd, unchanged: CLAUDEMESH_TOOLS.length - toAdd.length }; } /** * Remove claudemesh tools from allowedTools. * Leaves all other entries intact. Returns count removed. */ function uninstallAllowedTools(): number { if (!existsSync(CLAUDE_SETTINGS)) return 0; const settings = readClaudeSettings(); const existing = (settings.allowedTools as string[] | undefined) ?? []; const toolSet = new Set(CLAUDEMESH_TOOLS); const kept = existing.filter((t) => !toolSet.has(t)); const removed = existing.length - kept.length; if (removed > 0) { settings.allowedTools = kept; writeClaudeSettings(settings); } return removed; } /** * Add a Stop + UserPromptSubmit hook entry to ~/.claude/settings.json, * idempotent on the command string. Returns counts for reporting. */ function installHooks(): { added: number; unchanged: number } { const settings = readClaudeSettings(); const hooks = ((settings.hooks ??= {}) as HooksConfig) ?? {}; let added = 0; let unchanged = 0; const ensure = (event: string, command: string): void => { const list = (hooks[event] ??= []); const alreadyPresent = list.some((entry) => (entry.hooks ?? []).some((h) => h.command === command), ); if (alreadyPresent) { unchanged += 1; return; } list.push({ hooks: [{ type: "command", command }] }); added += 1; }; ensure("Stop", HOOK_COMMAND_STOP); ensure("UserPromptSubmit", HOOK_COMMAND_USER_PROMPT); settings.hooks = hooks; writeClaudeSettings(settings); return { added, unchanged }; } /** * Remove every hook entry whose command contains "claudemesh hook " * from ~/.claude/settings.json. Idempotent. Returns removed count. */ function uninstallHooks(): number { if (!existsSync(CLAUDE_SETTINGS)) return 0; const settings = readClaudeSettings(); const hooks = settings.hooks as HooksConfig | undefined; if (!hooks) return 0; let removed = 0; for (const event of Object.keys(hooks)) { const kept: HookMatcher[] = []; for (const entry of hooks[event] ?? []) { const filtered = (entry.hooks ?? []).filter( (h) => !(h.command ?? "").includes(HOOK_MARKER), ); removed += (entry.hooks ?? []).length - filtered.length; if (filtered.length > 0) kept.push({ ...entry, hooks: filtered }); } if (kept.length === 0) delete hooks[event]; else hooks[event] = kept; } settings.hooks = hooks; writeClaudeSettings(settings); return removed; } export function runInstall(args: string[] = []): void { const skipHooks = args.includes("--no-hooks"); console.log("claudemesh install"); console.log("------------------"); const entry = resolveEntry(); const isBundled = entry.endsWith("/dist/index.js") || entry.endsWith("\\dist\\index.js"); // Dev mode (running from src/) requires bun on PATH; bundled mode // (npm install -g) just uses node + the claudemesh bin shim. if (!isBundled && !bunAvailable()) { console.error( "✗ `bun` is not on PATH. Install Bun first: https://bun.com", ); process.exit(1); } if (!existsSync(entry)) { console.error(`✗ MCP entry not found at ${entry}`); process.exit(1); } const desired = buildMcpEntry(entry); const action = patchMcpServer(desired); // Read-back verification. const verify = readClaudeConfig(); const verifyServers = (verify.mcpServers ?? {}) as Record; const stored = verifyServers[MCP_NAME]; if (!stored || !entriesEqual(stored, desired)) { console.error( `✗ post-write verification failed — ${CLAUDE_CONFIG} may be corrupt`, ); process.exit(1); } // ANSI color helpers — stick to 8-color set so terminals without // truecolor still render. Fall back to plain if NO_COLOR or dumb TERM. const useColor = !process.env.NO_COLOR && process.env.TERM !== "dumb" && process.stdout.isTTY; const bold = (s: string) => (useColor ? `\x1b[1m${s}\x1b[22m` : s); const yellow = (s: string) => (useColor ? `\x1b[33m${s}\x1b[39m` : s); const dim = (s: string) => (useColor ? `\x1b[2m${s}\x1b[22m` : s); console.log(`✓ MCP server "${MCP_NAME}" ${action}`); console.log(dim(` config: ${CLAUDE_CONFIG}`)); console.log( dim( ` command: ${desired.command}${desired.args?.length ? " " + desired.args.join(" ") : ""}`, ), ); // allowedTools — pre-approve claudemesh MCP tools so peers don't need // --dangerously-skip-permissions just to call mesh tools. try { const { added, unchanged } = installAllowedTools(); if (added.length > 0) { console.log( `✓ allowedTools: ${added.length} claudemesh tools pre-approved${unchanged > 0 ? `, ${unchanged} already present` : ""}`, ); console.log(dim(` This lets claudemesh tools run without --dangerously-skip-permissions.`)); console.log(dim(` Your existing allowedTools entries were preserved.`)); } else { console.log(`✓ allowedTools: all ${unchanged} claudemesh tools already pre-approved`); } console.log(dim(` config: ${CLAUDE_SETTINGS}`)); } catch (e) { console.error( `⚠ allowedTools update failed: ${e instanceof Error ? e.message : String(e)}`, ); } // Hooks — status accuracy (Stop/UserPromptSubmit → POST /hook/set-status). if (!skipHooks) { try { const { added, unchanged } = installHooks(); if (added > 0) { console.log( `✓ Hooks registered (Stop + UserPromptSubmit) → ${added} added, ${unchanged} already present`, ); } else { console.log(`✓ Hooks already registered (${unchanged} present)`); } console.log(dim(` config: ${CLAUDE_SETTINGS}`)); } catch (e) { console.error( `⚠ hook registration failed: ${e instanceof Error ? e.message : String(e)}`, ); console.error( " (MCP is still installed — hooks just skip. Retry with --no-hooks to suppress.)", ); } } else { console.log(dim("· Hooks skipped (--no-hooks)")); } // Check if user has any meshes joined — nudge them if not. let hasMeshes = false; try { const meshConfig = loadConfig(); hasMeshes = meshConfig.meshes.length > 0; } catch { // Config missing or corrupt — treat as no meshes. } console.log(""); console.log(yellow(bold("⚠ RESTART CLAUDE CODE")) + yellow(" for MCP tools to appear.")); if (!hasMeshes) { console.log(""); console.log(yellow("No meshes joined.") + " To connect with peers:"); console.log( ` ${bold("claudemesh join ")}` + dim(" — join an existing mesh"), ); console.log( ` ${dim("Create one at")} ${bold("https://claudemesh.com/dashboard")}`, ); } else { console.log(""); console.log( `Next: ${bold("claudemesh join https://claudemesh.com/join/")}`, ); } console.log(""); console.log( yellow("⚠ For real-time push messages from peers, launch with:"), ); console.log( ` ${bold("claudemesh launch")}` + dim(" (or: claude --dangerously-load-development-channels server:claudemesh)"), ); console.log( dim(" Plain `claude` still works — messages are then pull-only via check_messages."), ); } export function runUninstall(): void { console.log("claudemesh uninstall"); console.log("--------------------"); // MCP entry — only removes claudemesh, never touches other servers. if (removeMcpServer()) { console.log(`✓ MCP server "${MCP_NAME}" removed`); } else { console.log(`· MCP server "${MCP_NAME}" not present`); } // allowedTools try { const removed = uninstallAllowedTools(); if (removed > 0) { console.log(`✓ allowedTools: ${removed} claudemesh tools removed`); } else { console.log("· No claudemesh allowedTools to remove"); } } catch (e) { console.error( `⚠ allowedTools removal failed: ${e instanceof Error ? e.message : String(e)}`, ); } // Hooks try { const removed = uninstallHooks(); if (removed > 0) { console.log(`✓ Hooks removed (${removed} entries)`); } else { console.log("· No claudemesh hooks to remove"); } } catch (e) { console.error( `⚠ hook removal failed: ${e instanceof Error ? e.message : String(e)}`, ); } console.log(""); console.log("Restart Claude Code to drop the MCP connection + hooks."); }