@mwillbanks/ast-mcp 0.1.2 → 0.2.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 (58) hide show
  1. package/README.md +71 -22
  2. package/dist/ast-mcp.js +301 -101
  3. package/dist/ast-mcp.js.map +107 -27
  4. package/package.json +3 -1
  5. package/src/ast-bro/capability.ts +3 -8
  6. package/src/ast-bro/client.ts +18 -2
  7. package/src/ast-bro/result.ts +5 -12
  8. package/src/cli.ts +112 -13
  9. package/src/config-cli.ts +54 -0
  10. package/src/config.ts +886 -0
  11. package/src/helpers/aider.ts +252 -0
  12. package/src/helpers/ast-bro.ts +64 -0
  13. package/src/helpers/config.ts +149 -0
  14. package/src/helpers/hook.ts +60 -0
  15. package/src/helpers/installer.ts +249 -0
  16. package/src/helpers/json.ts +19 -0
  17. package/src/helpers/mcp-schema.ts +31 -0
  18. package/src/helpers/process.ts +31 -0
  19. package/src/helpers/string.ts +97 -0
  20. package/src/hook.ts +135 -40
  21. package/src/http-entry.ts +1 -1
  22. package/src/http.ts +118 -103
  23. package/src/installer-transport.ts +190 -0
  24. package/src/installer.ts +596 -235
  25. package/src/mcp-cli.ts +104 -0
  26. package/src/patch/aider.ts +6 -199
  27. package/src/patch/engine.ts +271 -197
  28. package/src/runtime/dependencies.ts +161 -8
  29. package/src/runtime/file-delete.ts +13 -9
  30. package/src/runtime/file-read.ts +91 -57
  31. package/src/runtime/file-rename.ts +158 -0
  32. package/src/runtime/format.ts +57 -34
  33. package/src/runtime/paths.ts +30 -16
  34. package/src/runtime/policy.ts +21 -0
  35. package/src/runtime/process-input.ts +14 -4
  36. package/src/service.ts +219 -0
  37. package/src/shell-policy.ts +331 -0
  38. package/src/stdio.ts +47 -35
  39. package/src/tools/ast-bro.ts +12 -6
  40. package/src/tools/configured.ts +28 -0
  41. package/src/tools/files.ts +24 -27
  42. package/src/tools/index.ts +5 -3
  43. package/src/tools/lifecycle.ts +69 -55
  44. package/templates/skills/ast-mcp/SKILL.md +7 -6
  45. package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
  46. package/templates/skills/ast-mcp/evals/evals.json +1 -0
  47. package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
  48. package/templates/skills/ast-mcp/evals/score.ts +304 -267
  49. package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
  50. package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
  51. package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
  52. package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
  53. package/templates/skills/ast-mcp/evals/source.ts +164 -0
  54. package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
  55. package/templates/skills/ast-mcp/references/installation.md +24 -11
  56. package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
  57. package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
  58. package/templates/skills/ast-mcp/scripts/check-install.ts +312 -110
@@ -5,23 +5,91 @@ import path from "node:path";
5
5
 
6
6
  type Scope = "local" | "global";
7
7
  type Target = "codex" | "claude" | "copilot";
8
+ type Transport = "stdio" | "http";
8
9
 
9
- function parse(args: string[]) {
10
- let scope: Scope = "local";
11
- let target: Target = "codex";
12
- let root = process.cwd();
13
- for (let index = 0; index < args.length; index += 1) {
14
- if (args[index] === "--scope") scope = args[++index] as Scope;
15
- else if (args[index] === "--target") target = args[++index] as Target;
16
- else if (args[index] === "--root") root = path.resolve(args[++index]);
17
- else throw new Error(`Unknown argument: ${args[index]}`);
10
+ type CheckOptions = {
11
+ host: string;
12
+ port: number;
13
+ root: string;
14
+ scope: Scope;
15
+ service: boolean;
16
+ target: Target;
17
+ transport: Transport;
18
+ url: string;
19
+ };
20
+ type MutableCheckOptions = Omit<CheckOptions, "url">;
21
+ function applyOption(
22
+ options: MutableCheckOptions,
23
+ args: string[],
24
+ index: number,
25
+ ) {
26
+ const option = args[index];
27
+ const value = args[index + 1];
28
+ switch (option) {
29
+ case "--scope":
30
+ options.scope = value as Scope;
31
+ return index + 1;
32
+ case "--target":
33
+ options.target = value as Target;
34
+ return index + 1;
35
+ case "--root":
36
+ options.root = path.resolve(value);
37
+ return index + 1;
38
+ case "--transport":
39
+ options.transport = value as Transport;
40
+ return index + 1;
41
+ case "--host":
42
+ options.host = value;
43
+ return index + 1;
44
+ case "--port":
45
+ options.port = Number(value);
46
+ return index + 1;
47
+ case "--service":
48
+ options.service = true;
49
+ return index;
50
+ default:
51
+ throw new Error(`Unknown argument: ${option}`);
18
52
  }
53
+ }
54
+ function validateOptions(options: MutableCheckOptions) {
55
+ if (
56
+ !["local", "global"].includes(options.scope) ||
57
+ !["codex", "claude", "copilot"].includes(options.target) ||
58
+ !["stdio", "http"].includes(options.transport)
59
+ )
60
+ throw new Error("Invalid --scope, --target, or --transport");
19
61
  if (
20
- !["local", "global"].includes(scope) ||
21
- !["codex", "claude", "copilot"].includes(target)
62
+ !Number.isInteger(options.port) ||
63
+ options.port < 1 ||
64
+ options.port > 65_535
22
65
  )
23
- throw new Error("Invalid --scope or --target");
24
- return { root, scope, target };
66
+ throw new Error("Invalid --port");
67
+ if (options.service && options.transport !== "http")
68
+ throw new Error("--service requires --transport http");
69
+ }
70
+ function clientHost(host: string) {
71
+ if (host === "0.0.0.0") return "127.0.0.1";
72
+ if (host === "::") return "::1";
73
+ return host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
74
+ }
75
+ function endpointUrl(host: string, port: number) {
76
+ const client = clientHost(host);
77
+ return `http://${client.includes(":") ? `[${client}]` : client}:${port}/mcp`;
78
+ }
79
+ function parse(args: string[]): CheckOptions {
80
+ const options: MutableCheckOptions = {
81
+ host: "127.0.0.1",
82
+ port: 3768,
83
+ root: process.cwd(),
84
+ scope: "local",
85
+ service: false,
86
+ target: "codex",
87
+ transport: "stdio",
88
+ };
89
+ for (let index = 0; index < args.length; index += 1)
90
+ index = applyOption(options, args, index);
91
+ validateOptions(options);
92
+ return { ...options, url: endpointUrl(options.host, options.port) };
25
93
  }
26
94
 
27
95
  const instructionsBegin = "<!-- ast-mcp:begin -->";
@@ -114,37 +182,90 @@ async function hookCurrent(
114
182
  return false;
115
183
  }
116
184
 
185
+ type McpEntry = {
186
+ args?: unknown[];
187
+ command?: unknown;
188
+ env?: Record<string, unknown>;
189
+ tools?: unknown;
190
+ type?: unknown;
191
+ url?: unknown;
192
+ };
193
+ function httpJsonMcpCurrent(
194
+ entry: McpEntry | undefined,
195
+ type: "local" | "stdio" | undefined,
196
+ url: string | undefined,
197
+ ) {
198
+ return (
199
+ entry?.type === "http" &&
200
+ entry.url === url &&
201
+ (type !== "local" || Array.isArray(entry.tools))
202
+ );
203
+ }
204
+ async function stdioCommandCurrent(entry: McpEntry | undefined) {
205
+ return (
206
+ entry?.command === "bun" &&
207
+ Array.isArray(entry.args) &&
208
+ entry.args[1] === "mcp" &&
209
+ (await astMcpEntry(entry.args[0]))
210
+ );
211
+ }
212
+
213
+ function entryTypeCurrent(
214
+ entry: McpEntry,
215
+ type: "local" | "stdio" | undefined,
216
+ ) {
217
+ if (!type) return true;
218
+ if (entry.type !== type) return false;
219
+ return type !== "local" || Array.isArray(entry.tools);
220
+ }
221
+
222
+ function entryRootCurrent(entry: McpEntry, root: string | undefined) {
223
+ return root
224
+ ? entry.env?.AST_MCP_PROJECT_ROOT === root
225
+ : Object.keys(entry.env ?? {}).length === 0;
226
+ }
227
+
228
+ function stdioMetadataCurrent(
229
+ entry: McpEntry,
230
+ root: string | undefined,
231
+ type: "local" | "stdio" | undefined,
232
+ ) {
233
+ return entryTypeCurrent(entry, type) && entryRootCurrent(entry, root);
234
+ }
235
+
236
+ async function stdioJsonMcpCurrent(
237
+ entry: McpEntry | undefined,
238
+ root: string | undefined,
239
+ type: "local" | "stdio" | undefined,
240
+ ) {
241
+ return (
242
+ (await stdioCommandCurrent(entry)) &&
243
+ stdioMetadataCurrent(entry as McpEntry, root, type)
244
+ );
245
+ }
117
246
  async function jsonMcpCurrent(
118
247
  file: string,
119
248
  section: "mcpServers" | "servers",
120
249
  root?: string,
121
250
  type?: "local" | "stdio",
251
+ transport: Transport = "stdio",
252
+ url?: string,
122
253
  ) {
123
254
  const value = JSON.parse(await readFile(file, "utf8").catch(() => "{}"));
124
- const entry = value[section]?.["ast-mcp"];
125
- if (
126
- entry?.command !== "bun" ||
127
- !Array.isArray(entry.args) ||
128
- !(await astMcpEntry(entry.args[0])) ||
129
- entry.args[1] !== "mcp"
130
- )
131
- return false;
132
- if (type && entry.type !== type) return false;
133
- if (
134
- root &&
135
- (entry.env?.AST_MCP_ROOTS !== root ||
136
- entry.env?.AST_MCP_ALLOW_EXTERNAL_ROOTS !== "1")
137
- )
138
- return false;
139
- if (!root && Object.keys(entry.env ?? {}).length !== 0) return false;
140
- if (type === "local" && !Array.isArray(entry.tools)) return false;
141
- return true;
255
+ const entry: McpEntry | undefined = value[section]?.["ast-mcp"];
256
+ return transport === "http"
257
+ ? httpJsonMcpCurrent(entry, type, url)
258
+ : stdioJsonMcpCurrent(entry, root, type);
142
259
  }
143
260
 
144
- async function codexMcpCurrent(file: string, root?: string) {
145
- const content = await readFile(file, "utf8").catch(() => "");
146
- const block =
147
- content.match(/# ast-mcp:begin\n([\s\S]*?)# ast-mcp:end/)?.[1] ?? "";
261
+ function codexHttpMcpCurrent(block: string, url: string | undefined) {
262
+ return (
263
+ block.includes("[mcp_servers.ast-mcp]") &&
264
+ block.includes(`url = ${JSON.stringify(url)}`) &&
265
+ !block.includes("command =")
266
+ );
267
+ }
268
+ async function codexStdioMcpCurrent(block: string, root: string | undefined) {
148
269
  const args = block.match(/args = \[(".*"), "mcp"\]/);
149
270
  if (
150
271
  !block.includes("[mcp_servers.ast-mcp]") ||
@@ -154,130 +275,211 @@ async function codexMcpCurrent(file: string, root?: string) {
154
275
  )
155
276
  return false;
156
277
  return root
157
- ? block.includes(`AST_MCP_ROOTS = ${JSON.stringify(root)}`) &&
158
- block.includes('AST_MCP_ALLOW_EXTERNAL_ROOTS = "1"')
159
- : !block.includes("AST_MCP_ROOTS");
278
+ ? block.includes(`AST_MCP_PROJECT_ROOT = ${JSON.stringify(root)}`)
279
+ : !block.includes("AST_MCP_PROJECT_ROOT");
160
280
  }
161
- export async function checkInstall(
162
- args = process.argv.slice(2),
163
- home = os.homedir(),
281
+ async function codexMcpCurrent(
282
+ file: string,
283
+ root?: string,
284
+ transport: Transport = "stdio",
285
+ url?: string,
164
286
  ) {
165
- const options = parse(args);
166
- const global = options.scope === "global";
167
- const checks: Record<string, boolean> = {};
168
- if (options.target === "codex") {
169
- const base = global
170
- ? path.join(home, ".codex")
171
- : path.join(options.root, ".codex");
172
- checks.mcp = await codexMcpCurrent(
287
+ const content = await readFile(file, "utf8").catch(() => "");
288
+ const block =
289
+ content.match(/# ast-mcp:begin\n([\s\S]*?)# ast-mcp:end/)?.[1] ?? "";
290
+ return transport === "http"
291
+ ? codexHttpMcpCurrent(block, url)
292
+ : codexStdioMcpCurrent(block, root);
293
+ }
294
+
295
+ type InstallChecks = Record<string, boolean>;
296
+ async function codexChecks(
297
+ options: CheckOptions,
298
+ home: string,
299
+ global: boolean,
300
+ ): Promise<InstallChecks> {
301
+ const base = global
302
+ ? path.join(home, ".codex")
303
+ : path.join(options.root, ".codex");
304
+ const [mcp, hook, skill, instructions] = await Promise.all([
305
+ codexMcpCurrent(
173
306
  path.join(base, "config.toml"),
174
307
  global ? undefined : options.root,
175
- );
176
- checks.hook = await hookCurrent(
308
+ options.transport,
309
+ options.url,
310
+ ),
311
+ hookCurrent(
177
312
  path.join(base, "hooks.json"),
178
313
  "PreToolUse",
179
314
  path.join(base, "hooks/ast-mcp.ts"),
180
315
  global ? path.join(base, "hooks/ast-mcp.ts") : ".codex/hooks/ast-mcp.ts",
181
- );
182
- checks.skill = await skillCurrent(
183
- path.join(base, "skills/ast-mcp/SKILL.md"),
184
- );
185
- checks.instructions = await instructionsCurrent(
316
+ ),
317
+ skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
318
+ instructionsCurrent(
186
319
  global
187
320
  ? path.join(base, "AGENTS.md")
188
321
  : path.join(options.root, "AGENTS.md"),
189
- );
190
- } else if (options.target === "claude") {
191
- const base = global
192
- ? path.join(home, ".claude")
193
- : path.join(options.root, ".claude");
194
- checks.mcp = await jsonMcpCurrent(
322
+ ),
323
+ ]);
324
+ return { hook, instructions, mcp, skill };
325
+ }
326
+ async function claudeChecks(
327
+ options: CheckOptions,
328
+ home: string,
329
+ global: boolean,
330
+ ): Promise<InstallChecks> {
331
+ const base = global
332
+ ? path.join(home, ".claude")
333
+ : path.join(options.root, ".claude");
334
+ const [mcp, hook, skill, instructions] = await Promise.all([
335
+ jsonMcpCurrent(
195
336
  global
196
337
  ? path.join(home, ".claude.json")
197
338
  : path.join(options.root, ".mcp.json"),
198
339
  "mcpServers",
199
340
  global ? undefined : options.root,
200
- );
201
- checks.hook = await hookCurrent(
341
+ undefined,
342
+ options.transport,
343
+ options.url,
344
+ ),
345
+ hookCurrent(
202
346
  path.join(base, "settings.json"),
203
347
  "PreToolUse",
204
348
  path.join(base, "hooks/ast-mcp.ts"),
205
349
  global
206
350
  ? path.join(base, "hooks/ast-mcp.ts")
207
351
  : `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
208
- );
209
- checks.skill = await skillCurrent(
210
- path.join(base, "skills/ast-mcp/SKILL.md"),
211
- );
212
- checks.instructions = await instructionsCurrent(
352
+ ),
353
+ skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
354
+ instructionsCurrent(
213
355
  global
214
356
  ? path.join(base, "CLAUDE.md")
215
357
  : path.join(options.root, "AGENTS.md"),
216
- );
217
- } else {
218
- const base = global
219
- ? path.join(home, ".copilot")
220
- : path.join(options.root, ".github");
221
- checks.mcp = await jsonMcpCurrent(
358
+ ),
359
+ ]);
360
+ return { hook, instructions, mcp, skill };
361
+ }
362
+ async function copilotChecks(
363
+ options: CheckOptions,
364
+ home: string,
365
+ global: boolean,
366
+ ): Promise<InstallChecks> {
367
+ const base = global
368
+ ? path.join(home, ".copilot")
369
+ : path.join(options.root, ".github");
370
+ const [mcp, hook, skill, instructions] = await Promise.all([
371
+ jsonMcpCurrent(
222
372
  global
223
373
  ? path.join(base, "mcp-config.json")
224
374
  : path.join(options.root, ".github/mcp.json"),
225
375
  "mcpServers",
226
376
  global ? undefined : options.root,
227
377
  "local",
228
- );
229
- checks.hook = await hookCurrent(
378
+ options.transport,
379
+ options.url,
380
+ ),
381
+ hookCurrent(
230
382
  path.join(base, "hooks/ast-mcp.json"),
231
383
  "preToolUse",
232
384
  path.join(base, "hooks/ast-mcp.ts"),
233
385
  global ? path.join(base, "hooks/ast-mcp.ts") : ".github/hooks/ast-mcp.ts",
234
- );
235
- checks.skill = await skillCurrent(
236
- path.join(base, "skills/ast-mcp/SKILL.md"),
237
- );
238
- checks.instructions = await instructionsCurrent(
386
+ ),
387
+ skillCurrent(path.join(base, "skills/ast-mcp/SKILL.md")),
388
+ instructionsCurrent(
239
389
  global
240
390
  ? path.join(base, "copilot-instructions.md")
241
391
  : path.join(options.root, "AGENTS.md"),
392
+ ),
393
+ ]);
394
+ const checks: InstallChecks = { hook, instructions, mcp, skill };
395
+ if (!global)
396
+ checks.vscode = await jsonMcpCurrent(
397
+ path.join(options.root, ".vscode/mcp.json"),
398
+ "servers",
399
+ options.root,
400
+ "stdio",
401
+ options.transport,
402
+ options.url,
242
403
  );
243
- if (!global)
244
- checks.vscode = await jsonMcpCurrent(
245
- path.join(options.root, ".vscode/mcp.json"),
246
- "servers",
247
- options.root,
248
- "stdio",
249
- );
250
- }
404
+ return checks;
405
+ }
406
+ function targetChecks(options: CheckOptions, home: string, global: boolean) {
407
+ if (options.target === "codex") return codexChecks(options, home, global);
408
+ if (options.target === "claude") return claudeChecks(options, home, global);
409
+ return copilotChecks(options, home, global);
410
+ }
411
+ function serviceFile(options: CheckOptions, home: string) {
412
+ const digest = new Bun.CryptoHasher("sha256")
413
+ .update(path.resolve(options.root))
414
+ .digest("hex")
415
+ .slice(0, 12);
416
+ const id = options.scope === "global" ? "ast-mcp" : `ast-mcp-${digest}`;
417
+ return process.platform === "darwin"
418
+ ? path.join(home, "Library/LaunchAgents", `com.mwillbanks.${id}.plist`)
419
+ : path.join(home, ".config/systemd/user", `${id}.service`);
420
+ }
421
+ async function serviceCurrent(options: CheckOptions, home: string) {
422
+ if (process.platform !== "darwin" && process.platform !== "linux")
423
+ return false;
424
+ const content = await readFile(serviceFile(options, home), "utf8").catch(
425
+ () => "",
426
+ );
427
+ return (
428
+ content.includes("--transport") &&
429
+ content.includes("http") &&
430
+ content.includes(options.host) &&
431
+ content.includes(String(options.port)) &&
432
+ (options.scope === "global" || content.includes(path.resolve(options.root)))
433
+ );
434
+ }
435
+ function installOperation(checks: InstallChecks) {
436
+ if (Object.values(checks).every(Boolean)) return "none" as const;
437
+ return Object.values(checks).some(Boolean)
438
+ ? ("update" as const)
439
+ : ("install" as const);
440
+ }
441
+ function commandSuffix(options: CheckOptions, global: boolean) {
442
+ 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" : ""}`;
443
+ }
444
+ function installCommands(options: CheckOptions, global: boolean) {
445
+ const suffix = commandSuffix(options, global);
446
+ return {
447
+ installCommand: global
448
+ ? `bun add --global --trust @ast-bro/cli dprint @mwillbanks/ast-mcp && ast-mcp install ${suffix}`
449
+ : `bun add --dev @mwillbanks/ast-mcp && bun pm trust @ast-bro/cli dprint && ./node_modules/.bin/ast-mcp install ${suffix}`,
450
+ uninstallCommand: global
451
+ ? `ast-mcp uninstall ${suffix}`
452
+ : `./node_modules/.bin/ast-mcp uninstall ${suffix}`,
453
+ updateCommand: global
454
+ ? `ast-mcp update ${suffix}`
455
+ : `./node_modules/.bin/ast-mcp update ${suffix}`,
456
+ };
457
+ }
458
+ export async function checkInstall(
459
+ args = process.argv.slice(2),
460
+ home = os.homedir(),
461
+ ) {
462
+ const options = parse(args);
463
+ const global = options.scope === "global";
464
+ const checks = await targetChecks(options, home, global);
465
+ if (options.service) checks.service = await serviceCurrent(options, home);
251
466
  const installed = Object.values(checks).every(Boolean);
252
- const operation = installed
253
- ? "none"
254
- : Object.values(checks).some(Boolean)
255
- ? "update"
256
- : "install";
257
- const suffix = `--scope ${options.scope} --target ${options.target}${global ? "" : ` --root ${JSON.stringify(options.root)}`}`;
258
- const installCommand = global
259
- ? `bun add --global --trust @ast-bro/cli dprint @mwillbanks/ast-mcp && ast-mcp install ${suffix}`
260
- : `bun add --dev @mwillbanks/ast-mcp && bun pm trust @ast-bro/cli dprint && ./node_modules/.bin/ast-mcp install ${suffix}`;
261
- const updateCommand = global
262
- ? `ast-mcp update ${suffix}`
263
- : `./node_modules/.bin/ast-mcp update ${suffix}`;
264
- const uninstallCommand = global
265
- ? `ast-mcp uninstall ${suffix}`
266
- : `./node_modules/.bin/ast-mcp uninstall ${suffix}`;
467
+ const operation = installOperation(checks);
468
+ const commands = installCommands(options, global);
267
469
  return {
268
470
  checks,
269
- installCommand,
471
+ installCommand: commands.installCommand,
270
472
  installed,
271
473
  needsUpdate: operation === "update",
272
474
  operation,
273
475
  recommendedCommand:
274
476
  operation === "update"
275
- ? updateCommand
477
+ ? commands.updateCommand
276
478
  : operation === "install"
277
- ? installCommand
479
+ ? commands.installCommand
278
480
  : undefined,
279
- uninstallCommand,
280
- updateCommand,
481
+ uninstallCommand: commands.uninstallCommand,
482
+ updateCommand: commands.updateCommand,
281
483
  ...options,
282
484
  };
283
485
  }