claude.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * Claude Code target. Writes:
  3. *
  4. * - MCP server entry to `~/.claude.json` (global = user scope, loads
  5. * in every project) or `./.mcp.json` (local = project scope, the
  6. * file Claude Code actually reads for a single project). See the
  7. * scope table at https://code.claude.com/docs/en/mcp.
  8. * - Permissions to `~/.claude/settings.json` (global) or
  9. * `./.claude/settings.json` (local), gated on `autoAllow`.
  10. * - Instructions to `~/.claude/CLAUDE.md` (global) or
  11. * `./.claude/CLAUDE.md` (local).
  12. *
  13. * Earlier versions wrote the local MCP entry to `./.claude.json` — a
  14. * file Claude Code never reads — so the server silently never loaded
  15. * until the user manually renamed it to `.mcp.json` (issue #207). We
  16. * now write `./.mcp.json` and migrate any stale `./.claude.json` entry
  17. * out of the way on install and uninstall.
  18. */
  19. import * as fs from 'fs';
  20. import * as path from 'path';
  21. import * as os from 'os';
  22. import {
  23. AgentTarget,
  24. DetectionResult,
  25. InstallOptions,
  26. Location,
  27. WriteResult,
  28. } from './types';
  29. import {
  30. getCodeGraphPermissions,
  31. getMcpServerConfig,
  32. jsonDeepEqual,
  33. readJsonFile,
  34. removeMarkedSection,
  35. writeJsonFile,
  36. } from './shared';
  37. import {
  38. CODEGRAPH_SECTION_END,
  39. CODEGRAPH_SECTION_START,
  40. } from '../instructions-template';
  41. function configDir(loc: Location): string {
  42. return loc === 'global'
  43. ? path.join(os.homedir(), '.claude')
  44. : path.join(process.cwd(), '.claude');
  45. }
  46. function mcpJsonPath(loc: Location): string {
  47. // global → ~/.claude.json (user scope: visible in every project).
  48. // local → ./.mcp.json (project scope: the ONLY project-level MCP
  49. // file Claude Code reads — NOT ./.claude.json, which it ignores).
  50. return loc === 'global'
  51. ? path.join(os.homedir(), '.claude.json')
  52. : path.join(process.cwd(), '.mcp.json');
  53. }
  54. /**
  55. * Where pre-#207 installers wrote the local MCP entry. Claude Code
  56. * never reads a project-level `./.claude.json`, so we migrate the
  57. * codegraph entry out of it on install and strip it on uninstall.
  58. * Only the project-local path is legacy — global `~/.claude.json` is
  59. * the correct user-scope location and is left untouched.
  60. */
  61. function legacyLocalMcpPath(): string {
  62. return path.join(process.cwd(), '.claude.json');
  63. }
  64. function settingsJsonPath(loc: Location): string {
  65. return path.join(configDir(loc), 'settings.json');
  66. }
  67. function instructionsPath(loc: Location): string {
  68. return path.join(configDir(loc), 'CLAUDE.md');
  69. }
  70. class ClaudeCodeTarget implements AgentTarget {
  71. readonly id = 'claude' as const;
  72. readonly displayName = 'Claude Code';
  73. readonly docsUrl = 'https://docs.claude.com/en/docs/claude-code';
  74. supportsLocation(_loc: Location): boolean {
  75. return true;
  76. }
  77. detect(loc: Location): DetectionResult {
  78. const mcpPath = mcpJsonPath(loc);
  79. const config = readJsonFile(mcpPath);
  80. const alreadyConfigured = !!config.mcpServers?.codegraph;
  81. // For "installed" we infer from the existence of either the dir
  82. // (global) or the project marker file (local). Cheap and avoids
  83. // shelling out to `claude --version`.
  84. const installed = loc === 'global'
  85. ? fs.existsSync(configDir(loc)) || fs.existsSync(mcpPath)
  86. : fs.existsSync(mcpPath) || fs.existsSync(configDir(loc));
  87. return { installed, alreadyConfigured, configPath: mcpPath };
  88. }
  89. install(loc: Location, opts: InstallOptions): WriteResult {
  90. const files: WriteResult['files'] = [];
  91. // 1. MCP server entry
  92. files.push(writeMcpEntry(loc));
  93. // 1b. Migrate away any stale ./.claude.json left by a pre-#207
  94. // local install, so the project isn't left with two competing
  95. // (one dead) MCP configs.
  96. if (loc === 'local') {
  97. const migrated = cleanupLegacyLocalMcp();
  98. if (migrated) files.push(migrated);
  99. }
  100. // 2. Permissions (only when autoAllow)
  101. if (opts.autoAllow) {
  102. files.push(writePermissionsEntry(loc));
  103. }
  104. // 2b. Strip stale auto-sync hooks left by a pre-0.8 install. Those
  105. // versions wrote `codegraph mark-dirty` / `sync-if-dirty` hooks to
  106. // settings.json; both subcommands are gone from the CLI, so the
  107. // Stop hook now fails every turn with "unknown command
  108. // 'sync-if-dirty'". Cleaning up on install makes an upgrade
  109. // self-healing. Only surfaced when something was actually removed.
  110. const hookCleanup = cleanupLegacyHooks(loc);
  111. if (hookCleanup.action === 'removed') files.push(hookCleanup);
  112. // 3. CLAUDE.md instructions — no longer written. The codegraph
  113. // usage guidance now ships solely in the MCP server's `initialize`
  114. // response (see `mcp/server-instructions.ts`), which Claude Code
  115. // surfaces in the system prompt automatically. Writing it into
  116. // CLAUDE.md as well meant the agent read the same playbook twice
  117. // every turn (issue #529). Strip any block a previous install left
  118. // behind so an upgrade self-heals — same idiom as the hook cleanup.
  119. const instrCleanup = removeInstructionsEntry(loc);
  120. if (instrCleanup.action === 'removed') files.push(instrCleanup);
  121. return { files };
  122. }
  123. uninstall(loc: Location): WriteResult {
  124. const files: WriteResult['files'] = [];
  125. // 1. MCP server entry
  126. const mcpPath = mcpJsonPath(loc);
  127. const config = readJsonFile(mcpPath);
  128. if (config.mcpServers?.codegraph) {
  129. delete config.mcpServers.codegraph;
  130. if (Object.keys(config.mcpServers).length === 0) {
  131. delete config.mcpServers;
  132. }
  133. writeJsonFile(mcpPath, config);
  134. files.push({ path: mcpPath, action: 'removed' });
  135. } else {
  136. files.push({ path: mcpPath, action: 'not-found' });
  137. }
  138. // 1b. Also strip the codegraph entry from a legacy ./.claude.json
  139. // so uninstall fully reverses a pre-#207 local install.
  140. if (loc === 'local') {
  141. const migrated = cleanupLegacyLocalMcp();
  142. if (migrated) files.push(migrated);
  143. }
  144. // 2. Permissions
  145. const settingsPath = settingsJsonPath(loc);
  146. const settings = readJsonFile(settingsPath);
  147. if (Array.isArray(settings.permissions?.allow)) {
  148. const before = settings.permissions.allow.length;
  149. settings.permissions.allow = settings.permissions.allow.filter(
  150. (p: string) => !p.startsWith('mcp__codegraph__'),
  151. );
  152. if (settings.permissions.allow.length !== before) {
  153. if (settings.permissions.allow.length === 0) {
  154. delete settings.permissions.allow;
  155. }
  156. if (Object.keys(settings.permissions).length === 0) {
  157. delete settings.permissions;
  158. }
  159. writeJsonFile(settingsPath, settings);
  160. files.push({ path: settingsPath, action: 'removed' });
  161. } else {
  162. files.push({ path: settingsPath, action: 'not-found' });
  163. }
  164. } else {
  165. files.push({ path: settingsPath, action: 'not-found' });
  166. }
  167. // 2b. Strip any stale auto-sync hooks a pre-0.8 install left in
  168. // settings.json. The hook-cleanup step was lost when the installer
  169. // moved to the per-target architecture; restoring it here means
  170. // uninstall — and the npm `preuninstall` hook that drives it — fully
  171. // reverses a legacy install.
  172. const hookCleanup = cleanupLegacyHooks(loc);
  173. if (hookCleanup.action === 'removed') files.push(hookCleanup);
  174. // 3. Instructions — strip the legacy CodeGraph block if present.
  175. files.push(removeInstructionsEntry(loc));
  176. return { files };
  177. }
  178. printConfig(loc: Location): string {
  179. const target = mcpJsonPath(loc);
  180. const snippet = JSON.stringify({ mcpServers: { codegraph: getMcpServerConfig() } }, null, 2);
  181. return `# Add to ${target}\n\n${snippet}\n`;
  182. }
  183. describePaths(loc: Location): string[] {
  184. return [mcpJsonPath(loc), settingsJsonPath(loc), instructionsPath(loc)];
  185. }
  186. }
  187. /**
  188. * Per-file write helpers, exported so the legacy `config-writer.ts`
  189. * shim can call only the named operation (writeMcpConfig writes ONLY
  190. * the MCP entry, etc.) instead of `claudeTarget.install()` which
  191. * writes all three files. Without this split the shims silently
  192. * cause side effects callers don't expect.
  193. */
  194. export function writeMcpEntry(loc: Location): WriteResult['files'][number] {
  195. const file = mcpJsonPath(loc);
  196. const existing = readJsonFile(file);
  197. const before = existing.mcpServers?.codegraph;
  198. const after = getMcpServerConfig();
  199. if (jsonDeepEqual(before, after)) {
  200. // Already exactly what we'd write — preserve byte-identical file.
  201. return { path: file, action: 'unchanged' };
  202. }
  203. // 'created' here means: the file itself did not exist before this
  204. // write. A pre-existing MCP JSON file (`~/.claude.json` globally,
  205. // `./.mcp.json` locally) containing other MCP servers (no
  206. // `codegraph` key) is 'updated', not 'created' — we're adding an
  207. // entry to a file that was already there. Codex uses a different
  208. // idiom (empty-content => 'created') because its config.toml is
  209. // ours alone to manage.
  210. const action: 'created' | 'updated' = before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created');
  211. if (!existing.mcpServers) existing.mcpServers = {};
  212. existing.mcpServers.codegraph = after;
  213. writeJsonFile(file, existing);
  214. return { path: file, action };
  215. }
  216. /**
  217. * Strip the codegraph entry from a legacy project-local
  218. * `./.claude.json` (written by pre-#207 installers, which Claude Code
  219. * never read). Surgical: only our `codegraph` key is removed; sibling
  220. * MCP servers and any unrelated keys are preserved, and the file is
  221. * deleted only when removal leaves it completely empty. Returns the
  222. * file action for reporting, or `null` when there's nothing to migrate.
  223. */
  224. function cleanupLegacyLocalMcp(): WriteResult['files'][number] | null {
  225. const file = legacyLocalMcpPath();
  226. if (!fs.existsSync(file)) return null;
  227. const config = readJsonFile(file);
  228. if (!config.mcpServers?.codegraph) return null;
  229. delete config.mcpServers.codegraph;
  230. if (Object.keys(config.mcpServers).length === 0) delete config.mcpServers;
  231. if (Object.keys(config).length === 0) {
  232. try { fs.unlinkSync(file); } catch { /* ignore */ }
  233. } else {
  234. writeJsonFile(file, config);
  235. }
  236. return { path: file, action: 'removed' };
  237. }
  238. /**
  239. * True when a Claude Code hook `command` is one of the auto-sync hooks
  240. * a pre-0.8 install wrote. Those installers added
  241. * `PostToolUse(Edit|Write) → codegraph mark-dirty` and
  242. * `Stop → codegraph sync-if-dirty` (local builds used the
  243. * `npx @colbymchenry/codegraph …` form, which still contains the
  244. * `codegraph <subcommand>` substring). Both subcommands were later
  245. * removed from the CLI, so the Stop hook fails every turn with
  246. * "unknown command 'sync-if-dirty'". Matching on the codegraph-scoped
  247. * subcommand keeps unrelated user hooks (e.g. GitKraken's
  248. * `gk ai hook run`) untouched.
  249. */
  250. function isLegacyCodegraphHookCommand(command: unknown): boolean {
  251. if (typeof command !== 'string') return false;
  252. return (
  253. command.includes('codegraph mark-dirty') ||
  254. command.includes('codegraph sync-if-dirty')
  255. );
  256. }
  257. /**
  258. * Remove stale codegraph auto-sync hooks from Claude `settings.json`.
  259. *
  260. * Surgical at the individual-command level: only entries matching
  261. * `isLegacyCodegraphHookCommand` are dropped, so a sibling hook sharing
  262. * a matcher group (or the Stop event) with ours survives. We prune a
  263. * matcher group only once its `hooks` array is empty, an event only
  264. * once it has no groups left, and `hooks` itself only once every event
  265. * is gone — and none of that runs unless we actually removed a
  266. * codegraph command, so a settings.json with no legacy hooks is left
  267. * byte-for-byte untouched and reported `unchanged`.
  268. *
  269. * Exported so it can be unit-tested directly and reused by both
  270. * `install` (an upgrade self-heals) and `uninstall`.
  271. */
  272. export function cleanupLegacyHooks(loc: Location): WriteResult['files'][number] {
  273. const file = settingsJsonPath(loc);
  274. if (!fs.existsSync(file)) return { path: file, action: 'not-found' };
  275. const settings = readJsonFile(file);
  276. const hooks = settings.hooks;
  277. if (!hooks || typeof hooks !== 'object' || Array.isArray(hooks)) {
  278. return { path: file, action: 'unchanged' };
  279. }
  280. // Pass 1: drop the legacy command(s) from inside every matcher group.
  281. let removedAny = false;
  282. for (const event of Object.keys(hooks)) {
  283. const groups = hooks[event];
  284. if (!Array.isArray(groups)) continue;
  285. for (const group of groups) {
  286. if (!group || !Array.isArray(group.hooks)) continue;
  287. const before = group.hooks.length;
  288. group.hooks = group.hooks.filter(
  289. (h: any) => !isLegacyCodegraphHookCommand(h?.command),
  290. );
  291. if (group.hooks.length !== before) removedAny = true;
  292. }
  293. }
  294. if (!removedAny) return { path: file, action: 'unchanged' };
  295. // Pass 2: prune empty matcher groups, then events with no groups
  296. // left, then an empty top-level `hooks`. Guarded by `removedAny` so
  297. // we never restructure a settings.json that had no codegraph hooks.
  298. for (const event of Object.keys(hooks)) {
  299. const groups = hooks[event];
  300. if (!Array.isArray(groups)) continue;
  301. hooks[event] = groups.filter(
  302. (g: any) => !(g && Array.isArray(g.hooks) && g.hooks.length === 0),
  303. );
  304. if (hooks[event].length === 0) delete hooks[event];
  305. }
  306. if (Object.keys(hooks).length === 0) delete settings.hooks;
  307. writeJsonFile(file, settings);
  308. return { path: file, action: 'removed' };
  309. }
  310. export function writePermissionsEntry(loc: Location): WriteResult['files'][number] {
  311. const file = settingsJsonPath(loc);
  312. const settings = readJsonFile(file);
  313. const created = !fs.existsSync(file);
  314. if (!settings.permissions) settings.permissions = {};
  315. if (!Array.isArray(settings.permissions.allow)) settings.permissions.allow = [];
  316. const want = getCodeGraphPermissions();
  317. const before = [...settings.permissions.allow];
  318. for (const perm of want) {
  319. if (!settings.permissions.allow.includes(perm)) {
  320. settings.permissions.allow.push(perm);
  321. }
  322. }
  323. if (jsonDeepEqual(before, settings.permissions.allow) && !created) {
  324. return { path: file, action: 'unchanged' };
  325. }
  326. writeJsonFile(file, settings);
  327. return { path: file, action: created ? 'created' : 'updated' };
  328. }
  329. /**
  330. * Strip the marker-delimited CodeGraph block from CLAUDE.md if a prior
  331. * install wrote one. Codegraph no longer maintains an instructions file
  332. * (issue #529) — the MCP server's `initialize` instructions are the
  333. * single source of truth — so both install (self-heal on upgrade) and
  334. * uninstall call this. `removeMarkedSection` returns `not-found`/`kept`
  335. * when there's nothing to strip; the install caller drops those from
  336. * the report so a fresh install stays quiet.
  337. */
  338. export function removeInstructionsEntry(loc: Location): WriteResult['files'][number] {
  339. const file = instructionsPath(loc);
  340. const action = removeMarkedSection(file, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END);
  341. return { path: file, action };
  342. }
  343. export const claudeTarget: AgentTarget = new ClaudeCodeTarget();