@mwillbanks/ast-mcp 0.2.2 → 0.3.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 (55) hide show
  1. package/CONTRIBUTING.md +13 -0
  2. package/README.md +18 -16
  3. package/dist/ast-mcp.js +183 -137
  4. package/dist/ast-mcp.js.map +58 -42
  5. package/package.json +17 -11
  6. package/src/ast-bro/capability.ts +20 -10
  7. package/src/ast-bro/tools.json +22 -2
  8. package/src/cli.ts +10 -7
  9. package/src/config-cli.ts +166 -19
  10. package/src/config-migrate.ts +223 -0
  11. package/src/config-registry.ts +321 -0
  12. package/src/config-schema-common.ts +81 -0
  13. package/src/config-v2-schema.ts +97 -0
  14. package/src/config.ts +346 -148
  15. package/src/helpers/ast-bro.ts +1 -7
  16. package/src/helpers/config.ts +7 -0
  17. package/src/helpers/installer.ts +8 -0
  18. package/src/helpers/mcp-schema.ts +98 -4
  19. package/src/host-smoke.ts +182 -0
  20. package/src/http.ts +8 -3
  21. package/src/installer-transport.ts +6 -4
  22. package/src/installer.ts +192 -51
  23. package/src/patch/engine.ts +555 -279
  24. package/src/patch/languages.ts +9 -0
  25. package/src/patch/strategy/aiderBlock.ts +110 -0
  26. package/src/patch/strategy/ast.ts +170 -0
  27. package/src/patch/strategy.ts +61 -0
  28. package/src/runtime/approval.ts +438 -0
  29. package/src/runtime/dependencies.ts +1 -1
  30. package/src/runtime/document-inspection.ts +78 -0
  31. package/src/runtime/file-capabilities.ts +250 -0
  32. package/src/runtime/file-delete.ts +110 -37
  33. package/src/runtime/file-read.ts +142 -14
  34. package/src/runtime/file-rename.ts +5 -2
  35. package/src/runtime/format.ts +65 -27
  36. package/src/runtime/path-policy.ts +557 -0
  37. package/src/runtime/path-utils.ts +40 -0
  38. package/src/runtime/paths.ts +142 -44
  39. package/src/server.ts +2 -1
  40. package/src/tools/ast-bro.ts +196 -75
  41. package/src/tools/configuration.ts +450 -0
  42. package/src/tools/configured.ts +49 -21
  43. package/src/tools/files.ts +168 -61
  44. package/src/tools/index.ts +2 -0
  45. package/src/tools/lifecycle.ts +82 -38
  46. package/templates/AGENTS.md +13 -15
  47. package/templates/skills/ast-mcp/SKILL.md +28 -20
  48. package/templates/skills/ast-mcp/evals/evals.json +221 -9
  49. package/templates/skills/ast-mcp/evals/score.ts +80 -0
  50. package/templates/skills/ast-mcp/evals/semantic.ts +3 -1
  51. package/templates/skills/ast-mcp/references/installation.md +12 -12
  52. package/templates/skills/ast-mcp/references/patch-state-machine.md +15 -14
  53. package/templates/skills/ast-mcp/references/tool-catalog.md +13 -7
  54. package/templates/skills/ast-mcp/scripts/check-install.ts +8 -17
  55. package/src/ast-bro/rewrite-capability.ts +0 -9
package/src/installer.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
1
2
  import {
2
3
  cp,
3
4
  lstat,
@@ -9,7 +10,8 @@ import {
9
10
  } from "node:fs/promises";
10
11
  import os from "node:os";
11
12
  import path from "node:path";
12
- import { globalConfigPath, resolveConfig } from "./config";
13
+ import { clearConfigCache, globalConfigPath, resolveConfig } from "./config";
14
+ import { migrateConfigSource, writeMigratedConfig } from "./config-migrate";
13
15
  import {
14
16
  codexTransport,
15
17
  installerTargetPaths,
@@ -36,9 +38,25 @@ import {
36
38
 
37
39
  const packageRoot = path.resolve(import.meta.dir, "..");
38
40
  const cliEntry = path.join(packageRoot, "dist/ast-mcp.js");
39
- const localCliEntry = "./node_modules/.bin/ast-mcp";
40
- function cliEntryFor(root: string | undefined, home: string) {
41
- return root ? localCliEntry : path.join(home, ".bun/bin/ast-mcp");
41
+ const installerRuntime = new AsyncLocalStorage<{ cliEntry: string }>();
42
+ function configuredCliEntry(
43
+ options: InstallOptions,
44
+ root: string,
45
+ home: string,
46
+ ) {
47
+ return options.cliEntry
48
+ ? path.resolve(options.cliEntry)
49
+ : options.scope === "local"
50
+ ? path.join(root, "node_modules/.bin/ast-mcp")
51
+ : path.join(home, ".bun/bin/ast-mcp");
52
+ }
53
+ function cliEntryFor(root: string | undefined, _home: string) {
54
+ const entry = installerRuntime.getStore()?.cliEntry ?? cliEntry;
55
+ if (!root) return entry;
56
+ const relative = path.relative(root, entry);
57
+ if (relative && !relative.startsWith("..") && !path.isAbsolute(relative))
58
+ return `./${relative.split(path.sep).join("/")}`;
59
+ return entry;
42
60
  }
43
61
  const hookEntry = path.join(packageRoot, "src/hook.ts");
44
62
  const targets = ["codex", "claude", "copilot"] as const;
@@ -59,13 +77,9 @@ async function save(file: string, value: unknown) {
59
77
  }
60
78
  async function installerAstBroBinary(options: InstallOptions) {
61
79
  const root = path.resolve(options.root);
62
- const env =
63
- options.scope === "local"
64
- ? { ...process.env, AST_MCP_PROJECT_ROOT: root }
65
- : process.env;
66
80
  const config = await resolveConfig({
67
81
  cwd: root,
68
- env,
82
+ env: installerConfigEnvironment(options),
69
83
  home: options.home,
70
84
  });
71
85
  return (
@@ -80,11 +94,7 @@ function definition(
80
94
  endpoint?: HttpEndpoint,
81
95
  ) {
82
96
  if (transport === "http") return { type: "http", url: endpoint?.url };
83
- return {
84
- args: ["mcp"],
85
- command: cliEntryFor(root, home),
86
- env: root ? { AST_MCP_PROJECT_ROOT: root } : {},
87
- };
97
+ return { args: ["mcp"], command: cliEntryFor(root, home) };
88
98
  }
89
99
  async function codexMcp(
90
100
  file: string,
@@ -97,13 +107,10 @@ async function codexMcp(
97
107
  const clean = old
98
108
  .replace(/# ast-mcp:begin[\s\S]*?# ast-mcp:end\n?/g, "")
99
109
  .trimEnd();
100
- const environment = root
101
- ? `env = { AST_MCP_PROJECT_ROOT = ${JSON.stringify(root)} }\n`
102
- : "";
103
110
  const block =
104
111
  transport === "http"
105
112
  ? `# ast-mcp:begin\n[mcp_servers.ast-mcp]\nurl = ${JSON.stringify(endpoint?.url)}\n# ast-mcp:end`
106
- : `# ast-mcp:begin\n[mcp_servers.ast-mcp]\ncommand = ${JSON.stringify(cliEntryFor(root, home))}\nargs = ["mcp"]\n${environment}# ast-mcp:end`;
113
+ : `# ast-mcp:begin\n[mcp_servers.ast-mcp]\ncommand = ${JSON.stringify(cliEntryFor(root, home))}\nargs = ["mcp"]\n# ast-mcp:end`;
107
114
  await mkdir(path.dirname(file), { recursive: true });
108
115
  await writeFile(file, `${clean ? `${clean}\n\n` : ""}${block}\n`);
109
116
  }
@@ -304,6 +311,8 @@ async function hasLocalInstallation(root: string) {
304
311
 
305
312
  export interface InstallOptions {
306
313
  astBroBinary?: string;
314
+ cliEntry?: string;
315
+ deprecatedRoot?: boolean;
307
316
  home?: string;
308
317
  host?: string;
309
318
  platform?: NodeJS.Platform;
@@ -606,6 +615,103 @@ function installerConfigEnvironment(options: InstallOptions) {
606
615
  : { ...process.env, APPDATA: undefined, XDG_CONFIG_HOME: undefined };
607
616
  }
608
617
 
618
+ function installerConfigPath(
619
+ options: InstallOptions,
620
+ root: string,
621
+ home: string,
622
+ ) {
623
+ return options.scope === "local"
624
+ ? path.join(root, "ast-mcp.toml")
625
+ : globalConfigPath({ env: installerConfigEnvironment(options), home });
626
+ }
627
+
628
+ function installerConfigSeed(local: boolean) {
629
+ const common = [
630
+ "version = 2",
631
+ "",
632
+ "[files.read]",
633
+ 'modes = ["ast", "text"]',
634
+ "",
635
+ "[files.patch]",
636
+ 'strategies = ["ast", "aider_block"]',
637
+ 'aider_matchers = ["exact", "whitespace", "relative-indentation", "diff-match-patch"]',
638
+ "",
639
+ "[formatting]",
640
+ "enabled = true",
641
+ 'fallback = "preserve"',
642
+ "",
643
+ "[safety]",
644
+ "require_hash = true",
645
+ "",
646
+ "[safety.hook]",
647
+ "enabled = true",
648
+ "allow_tools = []",
649
+ "block_tools = []",
650
+ "",
651
+ "[http]",
652
+ 'host = "127.0.0.1"',
653
+ "port = 3768",
654
+ "session_timeout_ms = 1800000",
655
+ "session_sweep_interval_ms = 60000",
656
+ ];
657
+ if (!local) return `${common.join("\n")}\n`;
658
+ return `${[
659
+ "version = 2",
660
+ "",
661
+ "[workspace]",
662
+ 'roots = ["."]',
663
+ "",
664
+ ...common.slice(2),
665
+ "",
666
+ "[[paths]]",
667
+ 'id = "workspace"',
668
+ 'path = "."',
669
+ 'policies = { read = "allow", write = "allow", delete = "deny" }',
670
+ "follow_symlinks = false",
671
+ 'includes = ["**/*"]',
672
+ 'excludes = [".git/**"]',
673
+ ].join("\n")}\n`;
674
+ }
675
+
676
+ async function ensureInstallerConfig(
677
+ options: InstallOptions,
678
+ root: string,
679
+ home: string,
680
+ ) {
681
+ const file = installerConfigPath(options, root, home);
682
+ const existing = await readFile(file, "utf8").catch((error) => {
683
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
684
+ throw error;
685
+ });
686
+ clearConfigCache();
687
+ await resolveConfig({
688
+ cwd: options.scope === "local" ? root : home,
689
+ env: installerConfigEnvironment(options),
690
+ home,
691
+ });
692
+ if (existing === undefined) {
693
+ await mkdir(path.dirname(file), { recursive: true });
694
+ await writeFile(file, installerConfigSeed(options.scope === "local"), {
695
+ flag: "wx",
696
+ });
697
+ } else {
698
+ try {
699
+ const migration = migrateConfigSource(existing, file);
700
+ if (migration.changed) await writeMigratedConfig(file, migration.source);
701
+ } catch (error) {
702
+ throw new Error(
703
+ `invalid configuration: ${error instanceof Error ? error.message : String(error)}`,
704
+ );
705
+ }
706
+ }
707
+ clearConfigCache();
708
+ await resolveConfig({
709
+ cwd: options.scope === "local" ? root : home,
710
+ env: installerConfigEnvironment(options),
711
+ home,
712
+ });
713
+ }
714
+
609
715
  async function reconcileEndpoint(
610
716
  options: InstallOptions,
611
717
  transport: McpTransport,
@@ -642,18 +748,13 @@ function reconcilePaths(
642
748
  global: boolean,
643
749
  root: string,
644
750
  home: string,
645
- transport: McpTransport,
751
+ _transport: McpTransport,
646
752
  endpoint?: HttpEndpoint,
647
753
  ) {
648
754
  const paths = options.targets.flatMap((target) =>
649
755
  targetPaths(target, global, root, home),
650
756
  );
651
- if (transport === "http" && hasExplicitEndpoint(options))
652
- paths.push(
653
- options.scope === "local"
654
- ? path.join(root, "ast-mcp.toml")
655
- : globalConfigPath({ env: installerConfigEnvironment(options), home }),
656
- );
757
+ paths.push(installerConfigPath(options, root, home));
657
758
  if (options.service !== undefined)
658
759
  paths.push(
659
760
  createServicePlan(
@@ -685,26 +786,44 @@ async function reconcile(
685
786
  options: InstallOptions,
686
787
  operation: "install" | "update",
687
788
  ) {
688
- assertAstBroAvailable(await installerAstBroBinary(options));
689
789
  const root = path.resolve(options.root);
690
790
  const home = options.home ?? os.homedir();
691
791
  const global = options.scope === "global";
692
- const transport = await selectedTransport(options, operation);
693
- validateReconcileOptions(options, transport);
694
- const endpoint = await reconcileEndpoint(options, transport, root, home);
695
- const paths = reconcilePaths(
696
- options,
697
- global,
698
- root,
699
- home,
700
- transport,
701
- endpoint,
792
+ return installerRuntime.run(
793
+ { cliEntry: configuredCliEntry(options, root, home) },
794
+ async () => {
795
+ const transport = await selectedTransport(options, operation);
796
+ validateReconcileOptions(options, transport);
797
+ const paths = reconcilePaths(
798
+ options,
799
+ global,
800
+ root,
801
+ home,
802
+ transport,
803
+ undefined,
804
+ );
805
+ const before = await snapshot(paths);
806
+ if (transport === "http" && options.service === true) {
807
+ const endpoint = await resolveInstallerEndpoint({
808
+ env: installerConfigEnvironment(options),
809
+ home,
810
+ host: options.host,
811
+ persist: false,
812
+ port: options.port,
813
+ root,
814
+ scope: options.scope,
815
+ });
816
+ await preflightService(serviceConfiguration(options, endpoint));
817
+ }
818
+ await ensureInstallerConfig(options, root, home);
819
+ assertAstBroAvailable(await installerAstBroBinary(options));
820
+ const endpoint = await reconcileEndpoint(options, transport, root, home);
821
+ for (const target of options.targets)
822
+ await installTarget(target, global, root, home, transport, endpoint);
823
+ await reconcileService(options, endpoint);
824
+ return changedFiles(before, await snapshot(paths));
825
+ },
702
826
  );
703
- const before = await snapshot(paths);
704
- for (const target of options.targets)
705
- await installTarget(target, global, root, home, transport, endpoint);
706
- await reconcileService(options, endpoint);
707
- return changedFiles(before, await snapshot(paths));
708
827
  }
709
828
 
710
829
  export async function install(options: InstallOptions) {
@@ -817,15 +936,20 @@ async function removeUnusedService(options: InstallOptions, root: string) {
817
936
  export async function uninstall(options: InstallOptions) {
818
937
  const root = path.resolve(options.root);
819
938
  const home = options.home ?? os.homedir();
820
- const global = options.scope === "global";
821
- const paths = options.targets.flatMap((target) =>
822
- targetPaths(target, global, root, home),
939
+ return installerRuntime.run(
940
+ { cliEntry: configuredCliEntry(options, root, home) },
941
+ async () => {
942
+ const global = options.scope === "global";
943
+ const paths = options.targets.flatMap((target) =>
944
+ targetPaths(target, global, root, home),
945
+ );
946
+ const before = await snapshot(paths);
947
+ for (const target of options.targets)
948
+ await uninstallTarget(target, global, root, home);
949
+ await removeUnusedService(options, root);
950
+ return changedFiles(before, await snapshot(paths));
951
+ },
823
952
  );
824
- const before = await snapshot(paths);
825
- for (const target of options.targets)
826
- await uninstallTarget(target, global, root, home);
827
- await removeUnusedService(options, root);
828
- return changedFiles(before, await snapshot(paths));
829
953
  }
830
954
 
831
955
  export async function runInstallerCli(
@@ -835,7 +959,14 @@ export async function runInstallerCli(
835
959
  args,
836
960
  process.cwd(),
837
961
  );
838
- const options: InstallOptions = parsedOptions;
962
+ const options: InstallOptions = {
963
+ ...parsedOptions,
964
+ cliEntry: process.argv[1],
965
+ };
966
+ if (options.deprecatedRoot)
967
+ process.stderr.write(
968
+ "ast-mcp: --root is deprecated; run this command from the project root.\n",
969
+ );
839
970
  const operationHandlers = { install, uninstall, update };
840
971
  const changed = await operationHandlers[operation](options);
841
972
  const root = path.resolve(options.root);
@@ -867,7 +998,17 @@ export async function runInstallerCli(
867
998
  : undefined;
868
999
  const manualStart =
869
1000
  endpoint && options.service !== true
870
- ? `${global ? "" : `cd ${JSON.stringify(root)} && `}${JSON.stringify(cliEntryFor(global ? undefined : root, options.home ?? os.homedir()))} mcp --transport http --host ${JSON.stringify(endpoint.host)} --port ${endpoint.port}`
1001
+ ? installerRuntime.run(
1002
+ {
1003
+ cliEntry: configuredCliEntry(
1004
+ options,
1005
+ root,
1006
+ options.home ?? os.homedir(),
1007
+ ),
1008
+ },
1009
+ () =>
1010
+ `${global ? "" : `cd ${JSON.stringify(root)} && `}${JSON.stringify(cliEntryFor(global ? undefined : root, options.home ?? os.homedir()))} mcp --transport http --host ${JSON.stringify(endpoint.host)} --port ${endpoint.port}`,
1011
+ )
871
1012
  : undefined;
872
1013
  const result = {
873
1014
  changed,