@mwillbanks/ast-mcp 0.1.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 (61) hide show
  1. package/CONTRIBUTING.md +60 -0
  2. package/LICENSE +21 -0
  3. package/README.md +109 -0
  4. package/dist/ast-mcp-hook.js +7 -0
  5. package/dist/ast-mcp-hook.js.map +11 -0
  6. package/dist/ast-mcp-install.js +22 -0
  7. package/dist/ast-mcp-install.js.map +12 -0
  8. package/dist/ast-mcp.js +115 -0
  9. package/dist/ast-mcp.js.map +89 -0
  10. package/dprint.json +37 -0
  11. package/logo.svg +1 -0
  12. package/package.json +89 -0
  13. package/src/ast-bro/capability.ts +21 -0
  14. package/src/ast-bro/client.ts +47 -0
  15. package/src/ast-bro/result.ts +42 -0
  16. package/src/ast-bro/rewrite-capability.ts +9 -0
  17. package/src/ast-bro/tools.json +603 -0
  18. package/src/hook.ts +79 -0
  19. package/src/http-entry.ts +3 -0
  20. package/src/http.ts +138 -0
  21. package/src/index.ts +8 -0
  22. package/src/installer.ts +501 -0
  23. package/src/lifecycle.ts +10 -0
  24. package/src/managed-hook.ts +4 -0
  25. package/src/patch/aider.ts +224 -0
  26. package/src/patch/engine.ts +446 -0
  27. package/src/patch/languages.ts +36 -0
  28. package/src/runtime/atomic.ts +26 -0
  29. package/src/runtime/attributes.ts +70 -0
  30. package/src/runtime/file-delete.ts +96 -0
  31. package/src/runtime/file-read.ts +178 -0
  32. package/src/runtime/format.ts +56 -0
  33. package/src/runtime/hash.ts +11 -0
  34. package/src/runtime/locks.ts +87 -0
  35. package/src/runtime/paths.ts +73 -0
  36. package/src/runtime/process-input.ts +25 -0
  37. package/src/runtime/signals.ts +47 -0
  38. package/src/server.ts +13 -0
  39. package/src/stdio.ts +154 -0
  40. package/src/tools/ast-bro.ts +172 -0
  41. package/src/tools/files.ts +173 -0
  42. package/src/tools/index.ts +11 -0
  43. package/src/tools/lifecycle.ts +124 -0
  44. package/templates/AGENTS.md +56 -0
  45. package/templates/skills/ast-mcp/SKILL.md +96 -0
  46. package/templates/skills/ast-mcp/agents/openai.yaml +4 -0
  47. package/templates/skills/ast-mcp/evals/batch.evals.json +274 -0
  48. package/templates/skills/ast-mcp/evals/evals.json +1209 -0
  49. package/templates/skills/ast-mcp/evals/file-hash.evals.json +50 -0
  50. package/templates/skills/ast-mcp/evals/measure.ts +18 -0
  51. package/templates/skills/ast-mcp/evals/results.md +17 -0
  52. package/templates/skills/ast-mcp/evals/score.ts +417 -0
  53. package/templates/skills/ast-mcp/evals/semantic.ts +332 -0
  54. package/templates/skills/ast-mcp/evals/trigger-queries.json +22 -0
  55. package/templates/skills/ast-mcp/evals/validate.ts +99 -0
  56. package/templates/skills/ast-mcp/references/installation.md +40 -0
  57. package/templates/skills/ast-mcp/references/managed-hook.ts +3 -0
  58. package/templates/skills/ast-mcp/references/patch-state-machine.md +39 -0
  59. package/templates/skills/ast-mcp/references/tool-catalog.md +46 -0
  60. package/templates/skills/ast-mcp/scripts/check-install.ts +269 -0
  61. package/templates/skills/ast-mcp/scripts/managed-hook.ts +22 -0
@@ -0,0 +1,269 @@
1
+ #!/usr/bin/env bun
2
+ import { readFile } from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { isManagedHook } from "./managed-hook";
6
+
7
+ type Scope = "local" | "global";
8
+ type Target = "codex" | "claude" | "copilot";
9
+
10
+ function parse(args: string[]) {
11
+ let scope: Scope = "local";
12
+ let target: Target = "codex";
13
+ let root = process.cwd();
14
+ for (let index = 0; index < args.length; index += 1) {
15
+ if (args[index] === "--scope") scope = args[++index] as Scope;
16
+ else if (args[index] === "--target") target = args[++index] as Target;
17
+ else if (args[index] === "--root") root = path.resolve(args[++index]);
18
+ else throw new Error(`Unknown argument: ${args[index]}`);
19
+ }
20
+ if (
21
+ !["local", "global"].includes(scope) ||
22
+ !["codex", "claude", "copilot"].includes(target)
23
+ )
24
+ throw new Error("Invalid --scope or --target");
25
+ return { root, scope, target };
26
+ }
27
+
28
+ const instructionsBegin = "<!-- ast-mcp:begin -->";
29
+ const instructionsEnd = "<!-- ast-mcp:end -->";
30
+
31
+ async function astMcpEntry(entry: unknown): Promise<boolean> {
32
+ if (typeof entry !== "string" || path.basename(entry) !== "index.ts")
33
+ return false;
34
+ let folder = path.dirname(path.resolve(entry));
35
+ while (true) {
36
+ try {
37
+ const manifest = JSON.parse(
38
+ await readFile(path.join(folder, "package.json"), "utf8"),
39
+ );
40
+ return (
41
+ manifest.name === "@mwillbanks/ast-mcp" &&
42
+ path.resolve(entry) === path.join(folder, "src/index.ts")
43
+ );
44
+ } catch {}
45
+ const parent = path.dirname(folder);
46
+ if (parent === folder) return false;
47
+ folder = parent;
48
+ }
49
+ }
50
+
51
+ async function expectedReference(
52
+ name: "agents-guidance.md" | "hook.ts" | "skill-template.md",
53
+ ) {
54
+ const bundled = path.resolve(import.meta.dir, "../references", name);
55
+ const source =
56
+ name === "agents-guidance.md"
57
+ ? path.resolve(import.meta.dir, "../../../AGENTS.md")
58
+ : name === "hook.ts"
59
+ ? path.resolve(import.meta.dir, "../../../../src/hook.ts")
60
+ : path.resolve(import.meta.dir, "../SKILL.md");
61
+ return (
62
+ await readFile(bundled, "utf8").catch(() => readFile(source, "utf8"))
63
+ ).trim();
64
+ }
65
+
66
+ async function managedInstructions(file: string) {
67
+ const content = await readFile(file, "utf8").catch(() => "");
68
+ const begin = content.indexOf(instructionsBegin);
69
+ const end = content.indexOf(
70
+ instructionsEnd,
71
+ begin + instructionsBegin.length,
72
+ );
73
+ if (begin < 0 || end < 0) return undefined;
74
+ return content.slice(begin + instructionsBegin.length, end).trim();
75
+ }
76
+
77
+ async function instructionsCurrent(file: string) {
78
+ return (
79
+ (await managedInstructions(file)) ===
80
+ (await expectedReference("agents-guidance.md"))
81
+ );
82
+ }
83
+
84
+ async function skillCurrent(file: string) {
85
+ const installed = await readFile(file, "utf8").catch(() => "");
86
+ return installed.trim() === (await expectedReference("skill-template.md"));
87
+ }
88
+
89
+ async function hookCurrent(
90
+ configFile: string,
91
+ event: "PreToolUse" | "preToolUse",
92
+ scriptFile: string,
93
+ commandPath = scriptFile,
94
+ ) {
95
+ const config = JSON.parse(
96
+ await readFile(configFile, "utf8").catch(() => "{}"),
97
+ );
98
+ const script = await readFile(scriptFile, "utf8").catch(() => "");
99
+ const command = `bun ${JSON.stringify(commandPath)}`;
100
+ const entries = Array.isArray(config.hooks?.[event])
101
+ ? config.hooks[event]
102
+ : [];
103
+ return (
104
+ entries.some((item: unknown) => isManagedHook(item, event, command)) &&
105
+ script.trim() === (await expectedReference("hook.ts"))
106
+ );
107
+ }
108
+
109
+ async function jsonMcpCurrent(
110
+ file: string,
111
+ section: "mcpServers" | "servers",
112
+ root?: string,
113
+ type?: "local" | "stdio",
114
+ ) {
115
+ const value = JSON.parse(await readFile(file, "utf8").catch(() => "{}"));
116
+ const entry = value[section]?.["ast-mcp"];
117
+ if (
118
+ entry?.command !== "bun" ||
119
+ !Array.isArray(entry.args) ||
120
+ entry.args[0] !== "--hot" ||
121
+ !(await astMcpEntry(entry.args[1]))
122
+ )
123
+ return false;
124
+ if (type && entry.type !== type) return false;
125
+ if (
126
+ root &&
127
+ (entry.env?.AST_MCP_ROOTS !== root ||
128
+ entry.env?.AST_MCP_ALLOW_EXTERNAL_ROOTS !== "1")
129
+ )
130
+ return false;
131
+ if (!root && Object.keys(entry.env ?? {}).length !== 0) return false;
132
+ if (type === "local" && !Array.isArray(entry.tools)) return false;
133
+ return true;
134
+ }
135
+
136
+ async function codexMcpCurrent(file: string, root?: string) {
137
+ const content = await readFile(file, "utf8").catch(() => "");
138
+ const block =
139
+ content.match(/# ast-mcp:begin\n([\s\S]*?)# ast-mcp:end/)?.[1] ?? "";
140
+ return (
141
+ block.includes("[mcp_servers.ast-mcp]") &&
142
+ block.includes('command = "bun"') &&
143
+ block.includes('args = ["--hot",') &&
144
+ block.includes("/src/index.ts") &&
145
+ (root
146
+ ? block.includes(`AST_MCP_ROOTS = ${JSON.stringify(root)}`) &&
147
+ block.includes('AST_MCP_ALLOW_EXTERNAL_ROOTS = "1"')
148
+ : !block.includes("AST_MCP_ROOTS"))
149
+ );
150
+ }
151
+ export async function checkInstall(
152
+ args = process.argv.slice(2),
153
+ home = os.homedir(),
154
+ ) {
155
+ const options = parse(args);
156
+ const global = options.scope === "global";
157
+ const checks: Record<string, boolean> = {};
158
+ if (options.target === "codex") {
159
+ const base = global
160
+ ? path.join(home, ".codex")
161
+ : path.join(options.root, ".codex");
162
+ checks.mcp = await codexMcpCurrent(
163
+ path.join(base, "config.toml"),
164
+ global ? undefined : options.root,
165
+ );
166
+ checks.hook = await hookCurrent(
167
+ path.join(base, "hooks.json"),
168
+ "PreToolUse",
169
+ path.join(base, "hooks/ast-mcp.ts"),
170
+ global ? path.join(base, "hooks/ast-mcp.ts") : ".codex/hooks/ast-mcp.ts",
171
+ );
172
+ checks.skill = await skillCurrent(
173
+ path.join(base, "skills/ast-mcp/SKILL.md"),
174
+ );
175
+ checks.instructions = await instructionsCurrent(
176
+ global
177
+ ? path.join(base, "AGENTS.md")
178
+ : path.join(options.root, "AGENTS.md"),
179
+ );
180
+ } else if (options.target === "claude") {
181
+ const base = global
182
+ ? path.join(home, ".claude")
183
+ : path.join(options.root, ".claude");
184
+ checks.mcp = await jsonMcpCurrent(
185
+ global
186
+ ? path.join(home, ".claude.json")
187
+ : path.join(options.root, ".mcp.json"),
188
+ "mcpServers",
189
+ global ? undefined : options.root,
190
+ );
191
+ checks.hook = await hookCurrent(
192
+ path.join(base, "settings.json"),
193
+ "PreToolUse",
194
+ path.join(base, "hooks/ast-mcp.ts"),
195
+ global
196
+ ? path.join(base, "hooks/ast-mcp.ts")
197
+ : `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
198
+ );
199
+ checks.skill = await skillCurrent(
200
+ path.join(base, "skills/ast-mcp/SKILL.md"),
201
+ );
202
+ checks.instructions = await instructionsCurrent(
203
+ global
204
+ ? path.join(base, "CLAUDE.md")
205
+ : path.join(options.root, "AGENTS.md"),
206
+ );
207
+ } else {
208
+ const base = global
209
+ ? path.join(home, ".copilot")
210
+ : path.join(options.root, ".github");
211
+ checks.mcp = await jsonMcpCurrent(
212
+ global
213
+ ? path.join(base, "mcp-config.json")
214
+ : path.join(options.root, ".github/mcp.json"),
215
+ "mcpServers",
216
+ global ? undefined : options.root,
217
+ "local",
218
+ );
219
+ checks.hook = await hookCurrent(
220
+ path.join(base, "hooks/ast-mcp.json"),
221
+ "preToolUse",
222
+ path.join(base, "hooks/ast-mcp.ts"),
223
+ global ? path.join(base, "hooks/ast-mcp.ts") : ".github/hooks/ast-mcp.ts",
224
+ );
225
+ checks.skill = await skillCurrent(
226
+ path.join(base, "skills/ast-mcp/SKILL.md"),
227
+ );
228
+ checks.instructions = await instructionsCurrent(
229
+ global
230
+ ? path.join(base, "copilot-instructions.md")
231
+ : path.join(options.root, "AGENTS.md"),
232
+ );
233
+ if (!global)
234
+ checks.vscode = await jsonMcpCurrent(
235
+ path.join(options.root, ".vscode/mcp.json"),
236
+ "servers",
237
+ options.root,
238
+ "stdio",
239
+ );
240
+ }
241
+ const installed = Object.values(checks).every(Boolean);
242
+ const operation = installed
243
+ ? "none"
244
+ : Object.values(checks).some(Boolean)
245
+ ? "update"
246
+ : "install";
247
+ const suffix = `--scope ${options.scope} --target ${options.target}${global ? "" : ` --root ${JSON.stringify(options.root)}`}`;
248
+ const installCommand = `bunx --package @mwillbanks/ast-mcp ast-mcp-install install ${suffix}`;
249
+ const updateCommand = `bunx --package @mwillbanks/ast-mcp ast-mcp-install update ${suffix}`;
250
+ return {
251
+ checks,
252
+ installCommand,
253
+ installed,
254
+ needsUpdate: operation === "update",
255
+ operation,
256
+ recommendedCommand:
257
+ operation === "update"
258
+ ? updateCommand
259
+ : operation === "install"
260
+ ? installCommand
261
+ : undefined,
262
+ updateCommand,
263
+ ...options,
264
+ };
265
+ }
266
+ export async function runCheckInstallCli(args = process.argv.slice(2)) {
267
+ process.stdout.write(`${JSON.stringify(await checkInstall(args))}\n`);
268
+ }
269
+ if (import.meta.main) await runCheckInstallCli();
@@ -0,0 +1,22 @@
1
+ export type HookEvent = "PreToolUse" | "preToolUse";
2
+
3
+ export function isManagedHook(
4
+ item: unknown,
5
+ event: HookEvent,
6
+ command: string,
7
+ ): boolean {
8
+ if (!item || typeof item !== "object") return false;
9
+ const value = item as Record<string, unknown>;
10
+ if (event === "preToolUse")
11
+ return value.command === command && value.type === "command";
12
+ return (
13
+ Array.isArray(value.hooks) &&
14
+ value.hooks.some(
15
+ (child) =>
16
+ child &&
17
+ typeof child === "object" &&
18
+ (child as Record<string, unknown>).command === command &&
19
+ (child as Record<string, unknown>).type === "command",
20
+ )
21
+ );
22
+ }