@mwillbanks/ast-mcp 0.1.3 → 0.2.1

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 +59 -20
  2. package/dist/ast-mcp.js +232 -130
  3. package/dist/ast-mcp.js.map +107 -27
  4. package/package.json +2 -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 +70 -95
  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 +604 -247
  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 +48 -29
  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 +217 -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 +9 -8
  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 +323 -131
package/package.json CHANGED
@@ -12,6 +12,7 @@
12
12
  "@modelcontextprotocol/server": "^2.0.0-beta.5",
13
13
  "diff-match-patch-ts": "^2.0.0",
14
14
  "dprint": "0.55.2",
15
+ "unbash": "^4.0.3",
15
16
  "zod": "^4.4.3"
16
17
  },
17
18
  "description": "Zero-trust MCP file writes, AST edits, ast-bro intelligence, hooks, and agent skills",
@@ -85,5 +86,5 @@
85
86
  "dprint"
86
87
  ],
87
88
  "type": "module",
88
- "version": "0.1.3"
89
+ "version": "0.2.1"
89
90
  }
@@ -1,5 +1,5 @@
1
1
  import path from "node:path";
2
-
2
+ import { captureProcess, successfulProcessOutput } from "../helpers/process";
3
3
  import { AST_BRO_BINARY } from "./client";
4
4
 
5
5
  type AstBroMapPayload = {
@@ -16,13 +16,8 @@ export async function astCapable(
16
16
  stderr: "pipe",
17
17
  stdout: "pipe",
18
18
  });
19
- const [exitCode, stdout, stderr] = await Promise.all([
20
- processHandle.exited,
21
- new Response(processHandle.stdout).text(),
22
- new Response(processHandle.stderr).text(),
23
- ]);
24
- const failure = stderr.trim() || `ast-bro map exited ${exitCode}`;
25
- if (exitCode !== 0) throw new Error(failure);
19
+ const result = await captureProcess(processHandle);
20
+ const stdout = successfulProcessOutput("ast-bro map", result);
26
21
  const payload = JSON.parse(stdout) as AstBroMapPayload;
27
22
  const file = (payload.files ?? []).find(
28
23
  (candidate) => candidate.path === filePath,
@@ -1,6 +1,8 @@
1
1
  import { Client } from "@modelcontextprotocol/client";
2
2
  import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
3
- import { AST_BRO_BINARY } from "../runtime/dependencies";
3
+ import { indexedAstBroCommand } from "../helpers/ast-bro";
4
+ import { captureProcess, successfulProcessOutput } from "../helpers/process";
5
+ import { configuredAstBroBinary } from "../runtime/dependencies";
4
6
 
5
7
  export { AST_BRO_BINARY } from "../runtime/dependencies";
6
8
  export const AST_BRO_TOOLS = [
@@ -29,10 +31,24 @@ export async function callAstBro(
29
31
  args: Record<string, unknown>,
30
32
  root: string,
31
33
  ) {
34
+ if (
35
+ toolName === "search" ||
36
+ toolName === "find_related" ||
37
+ toolName === "index"
38
+ ) {
39
+ const commandArgs = indexedAstBroCommand(toolName, args, root);
40
+ const processHandle = Bun.spawn(
41
+ [await configuredAstBroBinary(), ...commandArgs],
42
+ { cwd: root, stderr: "pipe", stdout: "pipe" },
43
+ );
44
+ const result = await captureProcess(processHandle);
45
+ const stdout = successfulProcessOutput(`ast-bro ${commandArgs[0]}`, result);
46
+ return { content: [{ text: stdout, type: "text" as const }] };
47
+ }
32
48
  const client = new Client({ name: "ast-mcp", version: "1.0.0" });
33
49
  const transport = new StdioClientTransport({
34
50
  args: ["mcp"],
35
- command: AST_BRO_BINARY,
51
+ command: await configuredAstBroBinary(),
36
52
  cwd: root,
37
53
  });
38
54
  try {
@@ -4,18 +4,11 @@ export interface AstBroResult {
4
4
  }
5
5
 
6
6
  // biome-ignore lint/suspicious/noExplicitAny: Upstream JSON schemas are intentionally dynamic at this boundary.
7
- export function parseAstBroJson(result: AstBroResult): Record<string, any> {
8
- const text = result.content
9
- .filter((item) => item.type === "text" && typeof item.text === "string")
10
- .map((item) => item.text)
11
- .join("\n");
12
- if (result.isError) throw new Error(text || "ast-bro MCP call failed");
13
- if (!text) throw new Error("ast-bro MCP returned no JSON text");
14
- try {
15
- return JSON.parse(text);
16
- } catch (error) {
17
- throw new Error(`Invalid ast-bro MCP JSON: ${text}`, { cause: error });
18
- }
7
+ type AstBroJson = Record<string, any>;
8
+
9
+ import { parseTextResultJson } from "../helpers/json";
10
+ export function parseAstBroJson(result: AstBroResult): AstBroJson {
11
+ return parseTextResultJson(result);
19
12
  }
20
13
 
21
14
  export function astBroMatchFiles(result: AstBroResult): string[] {
package/src/cli.ts CHANGED
@@ -1,72 +1,23 @@
1
1
  export interface CliHandlers {
2
+ config(args: string[]): Promise<void>;
2
3
  hook(): Promise<number>;
3
4
  installer(args: string[]): Promise<void>;
4
- mcp(): Promise<void>;
5
+ mcp(args: string[]): Promise<void>;
5
6
  stderr?(text: string): void;
6
7
  stdout?(text: string): void;
7
8
  }
8
9
 
9
- const rootHelp = `ast-mcp - AST-aware Model Context Protocol server
10
+ const rootHelp = `ast-mcp - AST-aware Model Context Protocol server\n\nUsage:\n ast-mcp <command> [options]\n\nCommands:\n config Validate or display layered ast-mcp.toml configuration\n install Configure ast-mcp for one or more supported hosts\n update Refresh an existing ast-mcp configuration\n uninstall Remove ast-mcp-managed configuration\n mcp Start the stdio or Streamable HTTP MCP server\n hook Run the pre-tool-use filesystem guard\n help Display help for a command\n\nOptions:\n -h, --help Display help\n\nRun "ast-mcp help <command>" for command-specific help.\n`;
10
11
 
11
- Usage:
12
- ast-mcp <command> [options]
13
-
14
- Commands:
15
- install Configure ast-mcp for one or more supported hosts
16
- update Refresh an existing ast-mcp configuration
17
- uninstall Remove ast-mcp-managed configuration
18
- mcp Start the stdio MCP server
19
- hook Run the pre-tool-use filesystem guard
20
- help Display help for a command
21
-
22
- Options:
23
- -h, --help Display help
24
-
25
- Run "ast-mcp help <command>" for command-specific help.
26
- `;
27
-
28
- const installerOptions = `Options:
29
- -s, --scope <scope> Installation scope: local or global (default: local)
30
- -t, --target <target> Host: codex, claude, copilot, or all (default: all)
31
- -r, --root <path> Project root for local scope (default: current directory)
32
- -h, --help Display help
33
- `;
12
+ const installerOptions = `Options:\n -s, --scope <scope> Installation scope: local or global (default: local)\n -t, --target <target> Host: codex, claude, copilot, or all (default: all)\n -r, --root <path> Project root for local scope (default: current directory)\n --transport <mode> MCP transport: stdio or http (default: stdio)\n --host <address> HTTP bind host (default: layered configuration)\n --port <number> HTTP bind port (default: layered configuration)\n --service Install and start a managed user HTTP service\n --no-service Stop and remove a managed user HTTP service\n -h, --help Display help\n`;
34
13
 
35
14
  const commandHelp: Record<string, string> = {
36
- hook: `Usage:
37
- ast-mcp hook
38
-
39
- Run the pre-tool-use filesystem guard. Hook input is read from stdin.
40
-
41
- Options:
42
- -h, --help Display help
43
- `,
44
- install: `Usage:
45
- ast-mcp install [options]
46
-
47
- Configure ast-mcp for supported hosts.
48
-
49
- ${installerOptions}`,
50
- mcp: `Usage:
51
- ast-mcp mcp
52
-
53
- Start the stdio MCP server. Protocol messages are read from stdin and written to stdout.
54
-
55
- Options:
56
- -h, --help Display help
57
- `,
58
- uninstall: `Usage:
59
- ast-mcp uninstall [options]
60
-
61
- Remove ast-mcp-managed configuration while preserving shared host files.
62
-
63
- ${installerOptions}`,
64
- update: `Usage:
65
- ast-mcp update [options]
66
-
67
- Refresh managed configuration and installed skill content.
68
-
69
- ${installerOptions}`,
15
+ config: `Usage:\n ast-mcp config <validate|show> [options]\n\nValidate configuration or display the resolved values, sources, and provenance.\n\nOptions:\n -r, --root <path> Project root to resolve (default: current directory)\n -h, --help Display help\n`,
16
+ hook: `Usage:\n ast-mcp hook\n\nRun the pre-tool-use filesystem guard. Hook input is read from stdin.\n\nOptions:\n -h, --help Display help\n`,
17
+ install: `Usage:\n ast-mcp install [options]\n\nConfigure ast-mcp for supported hosts.\n\n${installerOptions}`,
18
+ mcp: `Usage:\n ast-mcp mcp [options]\n\nStart the MCP server. Stdio is the default; HTTP listens on the layered configuration unless flags override it.\n\nOptions:\n --transport <mode> MCP transport: stdio or http (default: stdio)\n --host <address> HTTP bind host\n --port <number> HTTP bind port\n -h, --help Display help\n`,
19
+ uninstall: `Usage:\n ast-mcp uninstall [options]\n\nRemove ast-mcp-managed configuration while preserving shared host files.\n\n${installerOptions}`,
20
+ update: `Usage:\n ast-mcp update [options]\n\nRefresh managed configuration and installed skill content.\n\n${installerOptions}`,
70
21
  };
71
22
 
72
23
  export function getCliHelp(): string;
@@ -96,6 +47,62 @@ function usageError(handlers: CliHandlers, message: string, command?: string) {
96
47
  return 1;
97
48
  }
98
49
 
50
+ function runHelpCommand(rest: string[], handlers: CliHandlers) {
51
+ const [topic, ...extra] = rest;
52
+ if (extra.length > 0)
53
+ return usageError(handlers, `Unexpected argument: ${extra[0]}`);
54
+ if (topic && !Object.hasOwn(commandHelp, topic))
55
+ return usageError(handlers, `Unknown command: ${topic}`);
56
+ writeHelp(handlers, topic);
57
+ return 0;
58
+ }
59
+
60
+ async function runMcpCommand(rest: string[], handlers: CliHandlers) {
61
+ try {
62
+ await handlers.mcp(rest);
63
+ return undefined;
64
+ } catch (error) {
65
+ if (error instanceof Error && error.name === "McpUsageError")
66
+ return usageError(handlers, error.message, "mcp");
67
+ throw error;
68
+ }
69
+ }
70
+
71
+ function runHookCommand(rest: string[], handlers: CliHandlers) {
72
+ if (rest.length > 0)
73
+ return usageError(
74
+ handlers,
75
+ `Unexpected argument for hook: ${rest[0]}`,
76
+ "hook",
77
+ );
78
+ return handlers.hook();
79
+ }
80
+
81
+ function isCliUsageError(error: unknown): error is Error {
82
+ if (!(error instanceof Error)) return false;
83
+ return [
84
+ "InstallerUsageError",
85
+ "ConfigurationUsageError",
86
+ "McpUsageError",
87
+ ].includes(error.name);
88
+ }
89
+
90
+ async function runConfiguredCommand(
91
+ command: string,
92
+ rest: string[],
93
+ handlers: CliHandlers,
94
+ ) {
95
+ try {
96
+ if (command === "config") await handlers.config(rest);
97
+ else await handlers.installer([command, ...rest]);
98
+ return undefined;
99
+ } catch (error) {
100
+ if (isCliUsageError(error))
101
+ return usageError(handlers, error.message, command);
102
+ throw error;
103
+ }
104
+ }
105
+
99
106
  export async function runCli(
100
107
  args: string[],
101
108
  handlers: CliHandlers,
@@ -105,46 +112,14 @@ export async function runCli(
105
112
  writeHelp(handlers);
106
113
  return 0;
107
114
  }
108
- if (command === "help") {
109
- const [topic, ...extra] = rest;
110
- if (extra.length > 0)
111
- return usageError(handlers, `Unexpected argument: ${extra[0]}`);
112
- if (topic && !Object.hasOwn(commandHelp, topic))
113
- return usageError(handlers, `Unknown command: ${topic}`);
114
- writeHelp(handlers, topic);
115
- return 0;
116
- }
115
+ if (command === "help") return runHelpCommand(rest, handlers);
117
116
  if (!Object.hasOwn(commandHelp, command))
118
117
  return usageError(handlers, `Unknown command: ${command}`);
119
118
  if (rest.includes("--help") || rest.includes("-h")) {
120
119
  writeHelp(handlers, command);
121
120
  return 0;
122
121
  }
123
- if (command === "mcp") {
124
- if (rest.length > 0)
125
- return usageError(
126
- handlers,
127
- `Unexpected argument for mcp: ${rest[0]}`,
128
- command,
129
- );
130
- await handlers.mcp();
131
- return;
132
- }
133
- if (command === "hook") {
134
- if (rest.length > 0)
135
- return usageError(
136
- handlers,
137
- `Unexpected argument for hook: ${rest[0]}`,
138
- command,
139
- );
140
- return handlers.hook();
141
- }
142
- try {
143
- await handlers.installer([command, ...rest]);
144
- return;
145
- } catch (error) {
146
- if (error instanceof Error && error.name === "InstallerUsageError")
147
- return usageError(handlers, error.message, command);
148
- throw error;
149
- }
122
+ if (command === "mcp") return runMcpCommand(rest, handlers);
123
+ if (command === "hook") return runHookCommand(rest, handlers);
124
+ return runConfiguredCommand(command, rest, handlers);
150
125
  }
@@ -0,0 +1,54 @@
1
+ import path from "node:path";
2
+ import { resolveConfig } from "./config";
3
+
4
+ class ConfigurationUsageError extends Error {
5
+ constructor(message: string) {
6
+ super(message);
7
+ this.name = "ConfigurationUsageError";
8
+ }
9
+ }
10
+
11
+ const stdout = process.stdout;
12
+
13
+ function valueAfter(tokens: string[], index: number) {
14
+ const value = tokens[index + 1];
15
+ if (!value || value.startsWith("-"))
16
+ throw new ConfigurationUsageError(
17
+ `Missing value for ${tokens[index] ?? "option"}`,
18
+ );
19
+ return value;
20
+ }
21
+
22
+ export async function runConfigCli(
23
+ args: string[],
24
+ write: (text: string) => void = stdout.write.bind(stdout),
25
+ ) {
26
+ const tokens = args.flatMap((token) => {
27
+ const match = /^--root=(.*)$/.exec(token);
28
+ return match ? ["--root", match[1] as string] : [token];
29
+ });
30
+ const operation = tokens.shift();
31
+ if (operation !== "validate" && operation !== "show")
32
+ throw new ConfigurationUsageError(
33
+ `Expected config validate or config show; received "${operation ?? ""}"`,
34
+ );
35
+
36
+ let root = process.cwd();
37
+ for (let index = 0; index < tokens.length; index += 1) {
38
+ if (tokens[index] !== "--root" && tokens[index] !== "-r")
39
+ throw new ConfigurationUsageError(`Unknown option: ${tokens[index]}`);
40
+ root = valueAfter(tokens, index);
41
+ index += 1;
42
+ }
43
+
44
+ const config = await resolveConfig({ cwd: path.resolve(root) });
45
+ const output =
46
+ operation === "show"
47
+ ? config
48
+ : {
49
+ projectRoot: config.projectRoot,
50
+ sources: config.sources,
51
+ valid: true,
52
+ };
53
+ write(`${JSON.stringify(output, null, 2)}\n`);
54
+ }