@mwillbanks/ast-mcp 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CONTRIBUTING.md +13 -0
  2. package/README.md +18 -16
  3. package/dist/ast-mcp.js +209 -147
  4. package/dist/ast-mcp.js.map +70 -50
  5. package/package.json +21 -15
  6. package/src/ast-bro/capability.ts +20 -10
  7. package/src/ast-bro/client.ts +12 -6
  8. package/src/ast-bro/result.ts +79 -0
  9. package/src/ast-bro/tools.json +29 -4
  10. package/src/cli.ts +10 -7
  11. package/src/config-cli.ts +166 -19
  12. package/src/config-edit.ts +236 -0
  13. package/src/config-migrate.ts +206 -0
  14. package/src/config-registry.ts +329 -0
  15. package/src/config-schema-common.ts +88 -0
  16. package/src/config-v2-schema.ts +109 -0
  17. package/src/config.ts +523 -170
  18. package/src/helpers/ast-bro.ts +1 -7
  19. package/src/helpers/config.ts +16 -3
  20. package/src/helpers/installer.ts +8 -0
  21. package/src/helpers/mcp-schema.ts +98 -4
  22. package/src/host-smoke.ts +182 -0
  23. package/src/http.ts +8 -3
  24. package/src/installer-transport.ts +6 -4
  25. package/src/installer.ts +242 -54
  26. package/src/patch/engine.ts +575 -280
  27. package/src/patch/languages.ts +9 -0
  28. package/src/patch/strategy/aiderBlock.ts +110 -0
  29. package/src/patch/strategy/ast.ts +170 -0
  30. package/src/patch/strategy.ts +61 -0
  31. package/src/runtime/approval.ts +438 -0
  32. package/src/runtime/config-write.ts +327 -0
  33. package/src/runtime/dependencies.ts +14 -78
  34. package/src/runtime/document-inspection.ts +78 -0
  35. package/src/runtime/file-capabilities.ts +250 -0
  36. package/src/runtime/file-delete.ts +110 -37
  37. package/src/runtime/file-read.ts +134 -15
  38. package/src/runtime/file-rename.ts +5 -2
  39. package/src/runtime/format.ts +90 -30
  40. package/src/runtime/git-worktrees.ts +179 -0
  41. package/src/runtime/path-policy.ts +643 -0
  42. package/src/runtime/path-utils.ts +80 -0
  43. package/src/runtime/paths.ts +171 -47
  44. package/src/server.ts +2 -1
  45. package/src/tools/ast-bro.ts +294 -75
  46. package/src/tools/configuration.ts +668 -0
  47. package/src/tools/configured.ts +49 -21
  48. package/src/tools/files.ts +169 -62
  49. package/src/tools/index.ts +2 -0
  50. package/src/tools/lifecycle.ts +82 -38
  51. package/templates/AGENTS.md +17 -15
  52. package/templates/skills/ast-mcp/SKILL.md +57 -20
  53. package/templates/skills/ast-mcp/evals/evals.json +319 -9
  54. package/templates/skills/ast-mcp/evals/score.ts +80 -0
  55. package/templates/skills/ast-mcp/evals/semantic.ts +3 -1
  56. package/templates/skills/ast-mcp/references/installation.md +12 -12
  57. package/templates/skills/ast-mcp/references/patch-state-machine.md +15 -14
  58. package/templates/skills/ast-mcp/references/tool-catalog.md +18 -8
  59. package/templates/skills/ast-mcp/scripts/binary-resolution.ts +112 -0
  60. package/templates/skills/ast-mcp/scripts/check-install.ts +165 -51
  61. package/templates/skills/ast-mcp/scripts/managed-hook.ts +34 -2
  62. package/src/ast-bro/rewrite-capability.ts +0 -9
@@ -2,6 +2,15 @@
2
2
  import { readFile } from "node:fs/promises";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
+ import {
6
+ directoryBinaryCandidates,
7
+ executableCandidate,
8
+ executableNames,
9
+ globalBinDirectories,
10
+ isExecutable,
11
+ resolveGlobalBinaryAlias,
12
+ } from "./binary-resolution";
13
+ import { managedAstMcpHookEntry } from "./managed-hook";
5
14
 
6
15
  type Scope = "local" | "global";
7
16
  type Target = "codex" | "claude" | "copilot";
@@ -94,15 +103,75 @@ function parse(args: string[]): CheckOptions {
94
103
 
95
104
  const instructionsBegin = "<!-- ast-mcp:begin -->";
96
105
  const instructionsEnd = "<!-- ast-mcp:end -->";
106
+ const astBroVersion = "4.2.0";
97
107
 
98
- async function astMcpEntry(entry: unknown): Promise<boolean> {
99
- if (entry === "./node_modules/.bin/ast-mcp") return true;
100
- if (typeof entry !== "string" || path.basename(entry) !== "ast-mcp")
108
+ async function astMcpEntry(
109
+ entry: unknown,
110
+ root?: string,
111
+ home = os.homedir(),
112
+ ): Promise<boolean> {
113
+ if (typeof entry !== "string") return false;
114
+ const normalized = entry.replaceAll("\\", "/");
115
+ if (root) return normalized === "./node_modules/.bin/ast-mcp";
116
+ if (
117
+ ![
118
+ "ast-mcp",
119
+ "ast-mcp.bat",
120
+ "ast-mcp.cmd",
121
+ "ast-mcp.com",
122
+ "ast-mcp.exe",
123
+ ].includes(path.basename(normalized).toLowerCase())
124
+ )
101
125
  return false;
102
- const bin = path.dirname(entry);
126
+ const directory = path.dirname(path.resolve(entry));
127
+ const recognized = globalBinDirectories(
128
+ "ast-mcp",
129
+ process.platform,
130
+ home,
131
+ ).some((candidate) => path.resolve(candidate) === directory);
132
+ return recognized && isExecutable(entry);
133
+ }
134
+
135
+ async function astBroVersionCurrent(binary: string) {
136
+ if (!isExecutable(binary)) return false;
137
+ const result = Bun.spawnSync([binary, "--version"], {
138
+ stderr: "ignore",
139
+ stdout: "pipe",
140
+ });
103
141
  return (
104
- path.basename(bin) === "bin" && path.basename(path.dirname(bin)) === ".bun"
142
+ result.exitCode === 0 &&
143
+ result.stdout.toString().trim() === `ast-bro ${astBroVersion}`
144
+ );
145
+ }
146
+
147
+ async function astBroCurrent(options: CheckOptions, home: string) {
148
+ if (process.env.AST_BRO_BINARY)
149
+ return astBroVersionCurrent(process.env.AST_BRO_BINARY);
150
+ const localBinDirectory = path.join(options.root, "node_modules/.bin");
151
+ if (options.scope === "local") {
152
+ const projectBinary = executableCandidate(
153
+ directoryBinaryCandidates(
154
+ [localBinDirectory],
155
+ executableNames("ast-bro", process.platform),
156
+ ),
157
+ process.platform,
158
+ );
159
+ return projectBinary ? astBroVersionCurrent(projectBinary) : false;
160
+ }
161
+ const bundled = path.resolve(
162
+ import.meta.dir,
163
+ "../../../../node_modules/.bin/ast-bro",
105
164
  );
165
+ if (isExecutable(bundled)) return astBroVersionCurrent(bundled);
166
+ const globalBinary = resolveGlobalBinaryAlias("ast-bro", {
167
+ globalBinDirectories: globalBinDirectories(
168
+ "ast-bro",
169
+ process.platform,
170
+ home,
171
+ ),
172
+ platform: process.platform,
173
+ });
174
+ return globalBinary ? astBroVersionCurrent(globalBinary) : false;
106
175
  }
107
176
 
108
177
  async function expectedReference(
@@ -146,8 +215,8 @@ async function skillCurrent(file: string) {
146
215
  async function hookCurrent(
147
216
  configFile: string,
148
217
  event: "PreToolUse" | "preToolUse",
149
- _scriptFile: string,
150
- _commandPath?: string,
218
+ root: string | undefined,
219
+ home: string,
151
220
  ) {
152
221
  const config = JSON.parse(
153
222
  await readFile(configFile, "utf8").catch(() => "{}"),
@@ -161,15 +230,21 @@ async function hookCurrent(
161
230
  : entries.flatMap((item: { hooks?: Array<{ command?: unknown }> }) =>
162
231
  (item.hooks ?? []).map((child) => child.command),
163
232
  );
164
- for (const command of commands) {
165
- if (typeof command !== "string") continue;
166
- const match = command.match(/^(.+) hook$/);
167
- if (!match) continue;
168
- try {
169
- if (await astMcpEntry(JSON.parse(match[1]))) return true;
170
- } catch {}
171
- }
172
- return false;
233
+ const managed = commands
234
+ .map((command) => ({
235
+ command,
236
+ entry: managedAstMcpHookEntry(command),
237
+ }))
238
+ .filter(
239
+ (item): item is { command: string; entry: string } =>
240
+ typeof item.command === "string" && typeof item.entry === "string",
241
+ );
242
+ if (managed.length !== 1) return false;
243
+ const [{ command, entry }] = managed;
244
+ return (
245
+ command === `${JSON.stringify(entry)} hook` &&
246
+ (await astMcpEntry(entry, root, home))
247
+ );
173
248
  }
174
249
 
175
250
  type McpEntry = {
@@ -191,9 +266,13 @@ function httpJsonMcpCurrent(
191
266
  (type !== "local" || Array.isArray(entry.tools))
192
267
  );
193
268
  }
194
- async function stdioCommandCurrent(entry: McpEntry | undefined) {
269
+ async function stdioCommandCurrent(
270
+ entry: McpEntry | undefined,
271
+ root?: string,
272
+ home = os.homedir(),
273
+ ) {
195
274
  return (
196
- (await astMcpEntry(entry?.command)) &&
275
+ (await astMcpEntry(entry?.command, root, home)) &&
197
276
  Array.isArray(entry?.args) &&
198
277
  entry.args.length === 1 &&
199
278
  entry.args[0] === "mcp"
@@ -209,10 +288,8 @@ function entryTypeCurrent(
209
288
  return type !== "local" || Array.isArray(entry.tools);
210
289
  }
211
290
 
212
- function entryRootCurrent(entry: McpEntry, root: string | undefined) {
213
- return root
214
- ? entry.env?.AST_MCP_PROJECT_ROOT === root
215
- : Object.keys(entry.env ?? {}).length === 0;
291
+ function entryRootCurrent(entry: McpEntry, _root: string | undefined) {
292
+ return Object.keys(entry.env ?? {}).length === 0;
216
293
  }
217
294
 
218
295
  function stdioMetadataCurrent(
@@ -227,9 +304,10 @@ async function stdioJsonMcpCurrent(
227
304
  entry: McpEntry | undefined,
228
305
  root: string | undefined,
229
306
  type: "local" | "stdio" | undefined,
307
+ home: string,
230
308
  ) {
231
309
  return (
232
- (await stdioCommandCurrent(entry)) &&
310
+ (await stdioCommandCurrent(entry, root, home)) &&
233
311
  stdioMetadataCurrent(entry as McpEntry, root, type)
234
312
  );
235
313
  }
@@ -240,12 +318,13 @@ async function jsonMcpCurrent(
240
318
  type?: "local" | "stdio",
241
319
  transport: Transport = "stdio",
242
320
  url?: string,
321
+ home = os.homedir(),
243
322
  ) {
244
323
  const value = JSON.parse(await readFile(file, "utf8").catch(() => "{}"));
245
324
  const entry: McpEntry | undefined = value[section]?.["ast-mcp"];
246
325
  return transport === "http"
247
326
  ? httpJsonMcpCurrent(entry, type, url)
248
- : stdioJsonMcpCurrent(entry, root, type);
327
+ : stdioJsonMcpCurrent(entry, root, type, home);
249
328
  }
250
329
 
251
330
  function codexHttpMcpCurrent(block: string, url: string | undefined) {
@@ -255,31 +334,39 @@ function codexHttpMcpCurrent(block: string, url: string | undefined) {
255
334
  !block.includes("command =")
256
335
  );
257
336
  }
258
- async function codexStdioMcpCurrent(block: string, root: string | undefined) {
337
+ async function codexStdioMcpCurrent(
338
+ block: string,
339
+ root: string | undefined,
340
+ home: string,
341
+ ) {
259
342
  const command = block.match(/command = (".*")/);
260
343
  if (
261
344
  !block.includes("[mcp_servers.ast-mcp]") ||
262
345
  !block.includes('args = ["mcp"]') ||
263
346
  !command ||
264
- !(await astMcpEntry(JSON.parse(command[1])))
347
+ !(await astMcpEntry(JSON.parse(command[1]), root, home))
265
348
  )
266
349
  return false;
267
- return root
268
- ? block.includes(`AST_MCP_PROJECT_ROOT = ${JSON.stringify(root)}`)
269
- : !block.includes("AST_MCP_PROJECT_ROOT");
350
+ return !block.includes("AST_MCP_PROJECT_ROOT") && !block.includes("env =");
270
351
  }
271
352
  async function codexMcpCurrent(
272
353
  file: string,
273
354
  root?: string,
274
355
  transport: Transport = "stdio",
275
356
  url?: string,
357
+ home = os.homedir(),
276
358
  ) {
277
359
  const content = await readFile(file, "utf8").catch(() => "");
360
+ if (
361
+ content.match(/# ast-mcp:begin/g)?.length !== 1 ||
362
+ content.match(/# ast-mcp:end/g)?.length !== 1
363
+ )
364
+ return false;
278
365
  const block =
279
366
  content.match(/# ast-mcp:begin\n([\s\S]*?)# ast-mcp:end/)?.[1] ?? "";
280
367
  return transport === "http"
281
368
  ? codexHttpMcpCurrent(block, url)
282
- : codexStdioMcpCurrent(block, root);
369
+ : codexStdioMcpCurrent(block, root, home);
283
370
  }
284
371
 
285
372
  type InstallChecks = Record<string, boolean>;
@@ -297,12 +384,13 @@ async function codexChecks(
297
384
  global ? undefined : options.root,
298
385
  options.transport,
299
386
  options.url,
387
+ home,
300
388
  ),
301
389
  hookCurrent(
302
390
  path.join(base, "hooks.json"),
303
391
  "PreToolUse",
304
- path.join(base, "hooks/ast-mcp.ts"),
305
- global ? path.join(base, "hooks/ast-mcp.ts") : ".codex/hooks/ast-mcp.ts",
392
+ global ? undefined : options.root,
393
+ home,
306
394
  ),
307
395
  skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
308
396
  instructionsCurrent(
@@ -331,14 +419,13 @@ async function claudeChecks(
331
419
  undefined,
332
420
  options.transport,
333
421
  options.url,
422
+ home,
334
423
  ),
335
424
  hookCurrent(
336
425
  path.join(base, "settings.json"),
337
426
  "PreToolUse",
338
- path.join(base, "hooks/ast-mcp.ts"),
339
- global
340
- ? path.join(base, "hooks/ast-mcp.ts")
341
- : `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
427
+ global ? undefined : options.root,
428
+ home,
342
429
  ),
343
430
  skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
344
431
  instructionsCurrent(
@@ -367,12 +454,13 @@ async function copilotChecks(
367
454
  "local",
368
455
  options.transport,
369
456
  options.url,
457
+ home,
370
458
  ),
371
459
  hookCurrent(
372
460
  path.join(base, "hooks/ast-mcp.json"),
373
461
  "preToolUse",
374
- path.join(base, "hooks/ast-mcp.ts"),
375
- global ? path.join(base, "hooks/ast-mcp.ts") : ".github/hooks/ast-mcp.ts",
462
+ global ? undefined : options.root,
463
+ home,
376
464
  ),
377
465
  skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
378
466
  instructionsCurrent(
@@ -390,6 +478,7 @@ async function copilotChecks(
390
478
  "stdio",
391
479
  options.transport,
392
480
  options.url,
481
+ home,
393
482
  );
394
483
  return checks;
395
484
  }
@@ -414,7 +503,21 @@ async function serviceCurrent(options: CheckOptions, home: string) {
414
503
  const content = await readFile(serviceFile(options, home), "utf8").catch(
415
504
  () => "",
416
505
  );
506
+ const commandEntries = [
507
+ ...content.matchAll(
508
+ /(?:<string>|ExecStart=")([^"<\n]*ast-mcp)(?=<\/string>|")/g,
509
+ ),
510
+ ].map((match) => match[1]);
511
+ const commandCurrent =
512
+ options.scope === "local"
513
+ ? commandEntries.includes("./node_modules/.bin/ast-mcp")
514
+ : (
515
+ await Promise.all(
516
+ commandEntries.map((entry) => astMcpEntry(entry, undefined, home)),
517
+ )
518
+ ).some(Boolean);
417
519
  return (
520
+ commandCurrent &&
418
521
  content.includes("--transport") &&
419
522
  content.includes("http") &&
420
523
  content.includes(options.host) &&
@@ -424,25 +527,32 @@ async function serviceCurrent(options: CheckOptions, home: string) {
424
527
  }
425
528
  function installOperation(checks: InstallChecks) {
426
529
  if (Object.values(checks).every(Boolean)) return "none" as const;
427
- return Object.values(checks).some(Boolean)
530
+ const managedSurfaces = Object.entries(checks)
531
+ .filter(([name]) => name !== "astBro")
532
+ .map(([, current]) => current);
533
+ return managedSurfaces.some(Boolean)
428
534
  ? ("update" as const)
429
535
  : ("install" as const);
430
536
  }
431
- function commandSuffix(options: CheckOptions, global: boolean) {
432
- return `--scope ${options.scope} --target ${options.target}${global ? "" : ` --root ${JSON.stringify(options.root)}`}${options.transport === "http" ? ` --transport http --host ${JSON.stringify(options.host)} --port ${options.port}` : ""}${options.service ? " --service" : ""}`;
537
+ function commandSuffix(options: CheckOptions, _global: boolean) {
538
+ return `--scope ${options.scope} --target ${options.target}${options.transport === "http" ? ` --transport http --host ${JSON.stringify(options.host)} --port ${options.port}` : ""}${options.service ? " --service" : ""}`;
433
539
  }
434
540
  function installCommands(options: CheckOptions, global: boolean) {
435
541
  const suffix = commandSuffix(options, global);
542
+ const updateCommand = global
543
+ ? `ast-mcp update ${suffix}`
544
+ : `./node_modules/.bin/ast-mcp update ${suffix}`;
436
545
  return {
437
546
  installCommand: global
438
- ? `bun add --global --trust @ast-bro/cli dprint @mwillbanks/ast-mcp && ast-mcp install ${suffix}`
439
- : `bun add --dev @mwillbanks/ast-mcp && bun pm trust @ast-bro/cli dprint && ./node_modules/.bin/ast-mcp install ${suffix}`,
547
+ ? `bun add --global --trust @ast-bro/cli@${astBroVersion} dprint @mwillbanks/ast-mcp && ast-mcp install ${suffix}`
548
+ : `bun add --dev @mwillbanks/ast-mcp @ast-bro/cli@${astBroVersion} && bun pm trust @ast-bro/cli dprint && ./node_modules/.bin/ast-mcp install ${suffix}`,
549
+ repairCommand: global
550
+ ? `bun add --global --trust @ast-bro/cli@${astBroVersion} && ${updateCommand}`
551
+ : `bun add --dev @ast-bro/cli@${astBroVersion} && bun pm trust @ast-bro/cli && ${updateCommand}`,
440
552
  uninstallCommand: global
441
553
  ? `ast-mcp uninstall ${suffix}`
442
554
  : `./node_modules/.bin/ast-mcp uninstall ${suffix}`,
443
- updateCommand: global
444
- ? `ast-mcp update ${suffix}`
445
- : `./node_modules/.bin/ast-mcp update ${suffix}`,
555
+ updateCommand,
446
556
  };
447
557
  }
448
558
  export async function checkInstall(
@@ -452,6 +562,7 @@ export async function checkInstall(
452
562
  const options = parse(args);
453
563
  const global = options.scope === "global";
454
564
  const checks = await targetChecks(options, home, global);
565
+ checks.astBro = await astBroCurrent(options, home);
455
566
  if (options.service) checks.service = await serviceCurrent(options, home);
456
567
  const installed = Object.values(checks).every(Boolean);
457
568
  const operation = installOperation(checks);
@@ -463,11 +574,14 @@ export async function checkInstall(
463
574
  needsUpdate: operation === "update",
464
575
  operation,
465
576
  recommendedCommand:
466
- operation === "update"
467
- ? commands.updateCommand
468
- : operation === "install"
469
- ? commands.installCommand
470
- : undefined,
577
+ !checks.astBro && operation !== "install"
578
+ ? commands.repairCommand
579
+ : operation === "update"
580
+ ? commands.updateCommand
581
+ : operation === "install"
582
+ ? commands.installCommand
583
+ : undefined,
584
+ repairCommand: commands.repairCommand,
471
585
  uninstallCommand: commands.uninstallCommand,
472
586
  updateCommand: commands.updateCommand,
473
587
  ...options,
@@ -1,5 +1,37 @@
1
+ import path from "node:path";
2
+
1
3
  export type HookEvent = "PreToolUse" | "preToolUse";
2
4
 
5
+ export function managedAstMcpHookEntry(command: unknown) {
6
+ if (typeof command !== "string") return undefined;
7
+ const normalized = command.trim();
8
+ if (!normalized.endsWith(" hook")) return undefined;
9
+ let invocation = normalized.slice(0, -" hook".length).trim();
10
+ if (invocation.startsWith("bun ")) {
11
+ invocation = invocation.slice(4).trim();
12
+ if (invocation.startsWith("run ")) invocation = invocation.slice(4).trim();
13
+ }
14
+ try {
15
+ invocation = JSON.parse(invocation);
16
+ } catch {}
17
+ if (typeof invocation !== "string") return undefined;
18
+ return [
19
+ "ast-mcp",
20
+ "ast-mcp.bat",
21
+ "ast-mcp.cmd",
22
+ "ast-mcp.com",
23
+ "ast-mcp.exe",
24
+ "ast-mcp.js",
25
+ "ast-mcp.ts",
26
+ ].includes(path.basename(invocation.replaceAll("\\", "/")).toLowerCase())
27
+ ? invocation
28
+ : undefined;
29
+ }
30
+
31
+ function commandMatches(actual: unknown, expected: string) {
32
+ return actual === expected || managedAstMcpHookEntry(actual) !== undefined;
33
+ }
34
+
3
35
  export function isManagedHook(
4
36
  item: unknown,
5
37
  event: HookEvent,
@@ -8,14 +40,14 @@ export function isManagedHook(
8
40
  if (!item || typeof item !== "object") return false;
9
41
  const value = item as Record<string, unknown>;
10
42
  if (event === "preToolUse")
11
- return value.command === command && value.type === "command";
43
+ return commandMatches(value.command, command) && value.type === "command";
12
44
  return (
13
45
  Array.isArray(value.hooks) &&
14
46
  value.hooks.some(
15
47
  (child) =>
16
48
  child &&
17
49
  typeof child === "object" &&
18
- (child as Record<string, unknown>).command === command &&
50
+ commandMatches((child as Record<string, unknown>).command, command) &&
19
51
  (child as Record<string, unknown>).type === "command",
20
52
  )
21
53
  );
@@ -1,9 +0,0 @@
1
- import { astCapable } from "./capability";
2
-
3
- export async function astRewritable(
4
- filePath: string,
5
- language?: string,
6
- ): Promise<boolean> {
7
- if (language === "markdown") return false;
8
- return astCapable(filePath, language);
9
- }