@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
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",
@@ -48,6 +49,7 @@
48
49
  "license": "MIT",
49
50
  "name": "@mwillbanks/ast-mcp",
50
51
  "packageManager": "bun@1.3.14",
52
+ "preferUnplugged": true,
51
53
  "publishConfig": {
52
54
  "access": "public"
53
55
  },
@@ -84,5 +86,5 @@
84
86
  "dprint"
85
87
  ],
86
88
  "type": "module",
87
- "version": "0.1.2"
89
+ "version": "0.2.0"
88
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,7 +1,106 @@
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>;
6
+ stderr?(text: string): void;
7
+ stdout?(text: string): void;
8
+ }
9
+
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`;
11
+
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`;
13
+
14
+ const commandHelp: Record<string, string> = {
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}`,
21
+ };
22
+
23
+ export function getCliHelp(): string;
24
+ export function getCliHelp(command: keyof typeof commandHelp): string;
25
+ export function getCliHelp(command: string): string | undefined;
26
+ export function getCliHelp(command?: string) {
27
+ return command && Object.hasOwn(commandHelp, command)
28
+ ? commandHelp[command]
29
+ : command
30
+ ? undefined
31
+ : rootHelp;
32
+ }
33
+
34
+ function writeHelp(handlers: CliHandlers, command?: string) {
35
+ const output =
36
+ command === undefined ? getCliHelp() : (getCliHelp(command) ?? rootHelp);
37
+ if (handlers.stdout) handlers.stdout(output);
38
+ else process.stdout.write(output);
39
+ }
40
+
41
+ function usageError(handlers: CliHandlers, message: string, command?: string) {
42
+ const help =
43
+ command === undefined ? getCliHelp() : (getCliHelp(command) ?? rootHelp);
44
+ const output = `ast-mcp: ${message}\n\n${help}`;
45
+ if (handlers.stderr) handlers.stderr(output);
46
+ else process.stderr.write(output);
47
+ return 1;
48
+ }
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
+ }
5
104
  }
6
105
 
7
106
  export async function runCli(
@@ -9,18 +108,18 @@ export async function runCli(
9
108
  handlers: CliHandlers,
10
109
  ): Promise<number | undefined> {
11
110
  const [command, ...rest] = args;
12
- if (command === "mcp") {
13
- await handlers.mcp();
14
- return;
15
- }
16
- if (command === "hook") {
17
- return handlers.hook();
111
+ if (!command || command === "--help" || command === "-h") {
112
+ writeHelp(handlers);
113
+ return 0;
18
114
  }
19
- if (["install", "update", "uninstall"].includes(command ?? "")) {
20
- await handlers.installer([command as string, ...rest]);
21
- return;
115
+ if (command === "help") return runHelpCommand(rest, handlers);
116
+ if (!Object.hasOwn(commandHelp, command))
117
+ return usageError(handlers, `Unknown command: ${command}`);
118
+ if (rest.includes("--help") || rest.includes("-h")) {
119
+ writeHelp(handlers, command);
120
+ return 0;
22
121
  }
23
- throw new Error(
24
- "Usage: ast-mcp <install|update|uninstall|hook|mcp> [options]",
25
- );
122
+ if (command === "mcp") return runMcpCommand(rest, handlers);
123
+ if (command === "hook") return runHookCommand(rest, handlers);
124
+ return runConfiguredCommand(command, rest, handlers);
26
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
+ }