@mwillbanks/ast-mcp 0.2.2 → 0.4.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 (62) hide show
  1. package/CONTRIBUTING.md +13 -0
  2. package/README.md +18 -16
  3. package/dist/ast-mcp.js +209 -147
  4. package/dist/ast-mcp.js.map +70 -50
  5. package/package.json +21 -15
  6. package/src/ast-bro/capability.ts +20 -10
  7. package/src/ast-bro/client.ts +12 -6
  8. package/src/ast-bro/result.ts +79 -0
  9. package/src/ast-bro/tools.json +29 -4
  10. package/src/cli.ts +10 -7
  11. package/src/config-cli.ts +166 -19
  12. package/src/config-edit.ts +236 -0
  13. package/src/config-migrate.ts +206 -0
  14. package/src/config-registry.ts +329 -0
  15. package/src/config-schema-common.ts +88 -0
  16. package/src/config-v2-schema.ts +109 -0
  17. package/src/config.ts +523 -170
  18. package/src/helpers/ast-bro.ts +1 -7
  19. package/src/helpers/config.ts +16 -3
  20. package/src/helpers/installer.ts +8 -0
  21. package/src/helpers/mcp-schema.ts +98 -4
  22. package/src/host-smoke.ts +182 -0
  23. package/src/http.ts +8 -3
  24. package/src/installer-transport.ts +6 -4
  25. package/src/installer.ts +242 -54
  26. package/src/patch/engine.ts +575 -280
  27. package/src/patch/languages.ts +9 -0
  28. package/src/patch/strategy/aiderBlock.ts +110 -0
  29. package/src/patch/strategy/ast.ts +170 -0
  30. package/src/patch/strategy.ts +61 -0
  31. package/src/runtime/approval.ts +438 -0
  32. package/src/runtime/config-write.ts +327 -0
  33. package/src/runtime/dependencies.ts +14 -78
  34. package/src/runtime/document-inspection.ts +78 -0
  35. package/src/runtime/file-capabilities.ts +250 -0
  36. package/src/runtime/file-delete.ts +110 -37
  37. package/src/runtime/file-read.ts +134 -15
  38. package/src/runtime/file-rename.ts +5 -2
  39. package/src/runtime/format.ts +90 -30
  40. package/src/runtime/git-worktrees.ts +179 -0
  41. package/src/runtime/path-policy.ts +643 -0
  42. package/src/runtime/path-utils.ts +80 -0
  43. package/src/runtime/paths.ts +171 -47
  44. package/src/server.ts +2 -1
  45. package/src/tools/ast-bro.ts +294 -75
  46. package/src/tools/configuration.ts +668 -0
  47. package/src/tools/configured.ts +49 -21
  48. package/src/tools/files.ts +169 -62
  49. package/src/tools/index.ts +2 -0
  50. package/src/tools/lifecycle.ts +82 -38
  51. package/templates/AGENTS.md +17 -15
  52. package/templates/skills/ast-mcp/SKILL.md +57 -20
  53. package/templates/skills/ast-mcp/evals/evals.json +319 -9
  54. package/templates/skills/ast-mcp/evals/score.ts +80 -0
  55. package/templates/skills/ast-mcp/evals/semantic.ts +3 -1
  56. package/templates/skills/ast-mcp/references/installation.md +12 -12
  57. package/templates/skills/ast-mcp/references/patch-state-machine.md +15 -14
  58. package/templates/skills/ast-mcp/references/tool-catalog.md +18 -8
  59. package/templates/skills/ast-mcp/scripts/binary-resolution.ts +112 -0
  60. package/templates/skills/ast-mcp/scripts/check-install.ts +165 -51
  61. package/templates/skills/ast-mcp/scripts/managed-hook.ts +34 -2
  62. package/src/ast-bro/rewrite-capability.ts +0 -9
@@ -0,0 +1,80 @@
1
+ import { realpathSync } from "node:fs";
2
+ import { realpath } from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ export function pathWithin(root: string, target: string): boolean {
6
+ const relative = path.relative(root, target);
7
+ return (
8
+ relative === "" ||
9
+ (!relative.startsWith("..") && !path.isAbsolute(relative))
10
+ );
11
+ }
12
+
13
+ export function canonicalizePathSync(targetPath: string): string {
14
+ let existing = path.resolve(targetPath);
15
+ const missing: string[] = [];
16
+ while (true) {
17
+ try {
18
+ return path.join(realpathSync(existing), ...missing);
19
+ } catch (error) {
20
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
21
+ return path.join(existing, ...missing);
22
+ }
23
+ const parent = path.dirname(existing);
24
+ if (parent === existing) return path.join(existing, ...missing);
25
+ missing.unshift(path.basename(existing));
26
+ existing = parent;
27
+ }
28
+ }
29
+ }
30
+
31
+ export function containingRoot(
32
+ roots: string[],
33
+ target: string,
34
+ ): string | undefined {
35
+ const canonicalTarget = canonicalizePathSync(target);
36
+ return roots
37
+ .map((root) => ({
38
+ canonical: canonicalizePathSync(root),
39
+ root,
40
+ }))
41
+ .filter(({ canonical }) => pathWithin(canonical, canonicalTarget))
42
+ .sort((left, right) => right.canonical.length - left.canonical.length)[0]
43
+ ?.canonical;
44
+ }
45
+
46
+ export function relativeRootFromPwd(roots: string[]): string | undefined {
47
+ const pwd = process.env.PWD;
48
+ if (!pwd) return undefined;
49
+ return containingRoot(roots, path.resolve(pwd));
50
+ }
51
+
52
+ export function effectiveWorkspaceRoot(
53
+ projectRoot: string,
54
+ roots: string[],
55
+ ): string {
56
+ const project = path.resolve(projectRoot);
57
+ const resolved = roots.map((root) => path.resolve(root));
58
+ return (
59
+ resolved.find((root) => pathWithin(project, root)) ??
60
+ resolved.find((root) => pathWithin(root, project)) ??
61
+ resolved[0] ??
62
+ project
63
+ );
64
+ }
65
+
66
+ export async function canonicalizePath(targetPath: string): Promise<string> {
67
+ let existing = path.resolve(targetPath);
68
+ const missing: string[] = [];
69
+ while (true) {
70
+ try {
71
+ return path.join(await realpath(existing), ...missing);
72
+ } catch (error) {
73
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
74
+ const parent = path.dirname(existing);
75
+ if (parent === existing) throw error;
76
+ missing.unshift(path.basename(existing));
77
+ existing = parent;
78
+ }
79
+ }
80
+ }
@@ -2,29 +2,45 @@ import { lstat, realpath } from "node:fs/promises";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
  import { currentConfig } from "../config";
5
+ import {
6
+ assertPolicy,
7
+ evaluatePolicy,
8
+ type PathOperation,
9
+ } from "./path-policy";
10
+ import {
11
+ canonicalizePath,
12
+ canonicalizePathSync,
13
+ containingRoot,
14
+ effectiveWorkspaceRoot,
15
+ pathWithin,
16
+ relativeRootFromPwd,
17
+ } from "./path-utils";
18
+
19
+ async function realpathOrSelf(filePath: string): Promise<string> {
20
+ try {
21
+ return await realpath(filePath);
22
+ } catch {
23
+ return filePath;
24
+ }
25
+ }
5
26
 
6
27
  async function configuredRoots(): Promise<string[]> {
7
28
  return (await currentConfig()).workspace.roots;
8
29
  }
9
30
  function within(root: string, target: string): boolean {
10
- const relative = path.relative(root, target);
11
- return (
12
- relative === "" ||
13
- (!relative.startsWith("..") && !path.isAbsolute(relative))
14
- );
31
+ return pathWithin(root, target);
15
32
  }
16
33
 
17
34
  async function workspaceRoots(): Promise<string[]> {
18
35
  const config = await currentConfig();
19
36
  const roots = await Promise.all(
20
- (await configuredRoots()).map(async (root) =>
21
- realpath(root).catch(() => root),
22
- ),
37
+ (await configuredRoots()).map(realpathOrSelf),
23
38
  );
24
39
  const trustedRoots = await Promise.all(
25
- config.trustedRoots.map(async (root) => realpath(root).catch(() => root)),
40
+ config.trustedRoots.map(realpathOrSelf),
26
41
  );
27
42
  if (
43
+ config.version === 1 &&
28
44
  !config.safety.allowExternalRoots &&
29
45
  roots.some(
30
46
  (root) => !trustedRoots.some((trustedRoot) => within(trustedRoot, root)),
@@ -33,28 +49,72 @@ async function workspaceRoots(): Promise<string[]> {
33
49
  throw new Error(
34
50
  "Configured workspace roots contain a root outside the trusted project boundary; set safety.allow_external_roots=true or AST_MCP_ALLOW_EXTERNAL_ROOTS=1 to opt in",
35
51
  );
36
- return roots;
52
+ const primary = effectiveWorkspaceRoot(
53
+ await realpathOrSelf(config.projectRoot),
54
+ roots,
55
+ );
56
+ return [primary, ...roots.filter((root) => root !== primary)];
37
57
  }
38
58
 
39
59
  async function fileOperationRoots(): Promise<string[]> {
60
+ const config = await currentConfig();
40
61
  const roots = await workspaceRoots();
41
- if (!(await currentConfig()).safety.allowTempDirectory) return roots;
42
- const temporaryRoot = await realpath(os.tmpdir()).catch(() => os.tmpdir());
43
- return roots.includes(temporaryRoot) ? roots : [...roots, temporaryRoot];
62
+ const policyRoots = (config.paths ?? []).map((rule) => rule.path);
63
+ const combined = [...new Set([...roots, ...policyRoots])];
64
+ if (!config.safety.allowTempDirectory) return combined;
65
+ const temporaryRoot = await realpathOrSelf(os.tmpdir());
66
+ return combined.includes(temporaryRoot)
67
+ ? combined
68
+ : [...combined, temporaryRoot];
44
69
  }
45
70
 
46
71
  export async function primaryRoot(): Promise<string> {
47
72
  return (await workspaceRoots())[0] as string;
48
73
  }
49
74
 
50
- export async function configuredRootForPath(
75
+ async function configuredRootForPath(
51
76
  filePath: string,
52
77
  ): Promise<string | undefined> {
53
- return (await fileOperationRoots()).find((candidate) =>
54
- within(candidate, filePath),
78
+ const canonicalTarget = canonicalizePathSync(filePath);
79
+ return (await fileOperationRoots()).find(
80
+ (candidate) =>
81
+ pathWithin(candidate, filePath) ||
82
+ pathWithin(canonicalizePathSync(candidate), canonicalTarget),
55
83
  );
56
84
  }
57
85
 
86
+ export async function intelligenceRoot(
87
+ requestPaths: string[] = [],
88
+ ): Promise<string> {
89
+ const roots = await workspaceRoots();
90
+ const matched = [
91
+ ...new Set(
92
+ requestPaths
93
+ .filter((item) => path.isAbsolute(item))
94
+ .map((item) => containingRoot(roots, path.resolve(item)))
95
+ .filter((item): item is string => Boolean(item)),
96
+ ),
97
+ ];
98
+ if (matched.length === 1) return matched[0] as string;
99
+ return relativeRootFromPwd(roots) ?? (roots[0] as string);
100
+ }
101
+
102
+ export async function referenceRootForPath(
103
+ filePath: string,
104
+ ): Promise<string | undefined> {
105
+ const candidates = await Promise.all(
106
+ (await fileOperationRoots()).map(async (candidate) =>
107
+ (await lstat(candidate).catch(() => undefined))?.isDirectory()
108
+ ? candidate
109
+ : undefined,
110
+ ),
111
+ );
112
+ return candidates
113
+ .filter((candidate): candidate is string => Boolean(candidate))
114
+ .filter((candidate) => within(candidate, filePath))
115
+ .sort((left, right) => right.length - left.length)[0];
116
+ }
117
+
58
118
  export async function rootForPath(filePath: string): Promise<string> {
59
119
  const root = await configuredRootForPath(filePath);
60
120
  if (root) return root;
@@ -65,34 +125,42 @@ export async function rootForPath(filePath: string): Promise<string> {
65
125
  );
66
126
  }
67
127
 
128
+ export async function assertSingleProjectRoot(
129
+ filePaths: string[],
130
+ ): Promise<void> {
131
+ const selectedRoots = new Set(
132
+ await Promise.all(filePaths.map((filePath) => rootForPath(filePath))),
133
+ );
134
+ if (selectedRoots.size > 1)
135
+ throw Object.assign(
136
+ new Error(
137
+ "AST requests may not span project roots; issue one root-specific call per project",
138
+ ),
139
+ {
140
+ code: "cross_root_ast_request",
141
+ retryable: true,
142
+ suggestedNextCall:
143
+ "Split the request by project root; independent read-only calls may run in parallel",
144
+ },
145
+ );
146
+ }
147
+
68
148
  export async function pathsShareRoot(
69
149
  first: string,
70
150
  second: string,
71
151
  ): Promise<boolean> {
72
- if ((await currentConfig()).safety.allowAnyPath) return true;
152
+ const config = await currentConfig();
153
+ if (config.version === 2 || config.safety.allowAnyPath) return true;
73
154
  return (await rootForPath(first)) === (await rootForPath(second));
74
155
  }
75
156
  async function canonicalCandidate(filePath: string, root: string) {
76
157
  const absolute = path.isAbsolute(filePath)
77
158
  ? filePath
78
159
  : path.resolve(root, filePath);
79
- let existing = path.dirname(absolute);
80
- const missing: string[] = [];
81
- while (true) {
82
- try {
83
- return path.join(
84
- await realpath(existing),
85
- ...missing,
86
- path.basename(absolute),
87
- );
88
- } catch (error) {
89
- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
90
- const parent = path.dirname(existing);
91
- if (parent === existing) throw error;
92
- missing.unshift(path.basename(existing));
93
- existing = parent;
94
- }
95
- }
160
+ return path.join(
161
+ await canonicalizePath(path.dirname(absolute)),
162
+ path.basename(absolute),
163
+ );
96
164
  }
97
165
 
98
166
  function assertWithinBoundary(
@@ -105,45 +173,101 @@ function assertWithinBoundary(
105
173
  throw new Error(message);
106
174
  }
107
175
 
176
+ function enforcePolicy(
177
+ config: Awaited<ReturnType<typeof currentConfig>>,
178
+ decision: ReturnType<typeof evaluatePolicy>,
179
+ ): void {
180
+ if (config.version === 2 || config.paths?.length)
181
+ assertPolicy(decision, config.generation);
182
+ }
183
+
184
+ async function resolvedSymlinkTarget(args: {
185
+ allowAnyPath: boolean;
186
+ boundary: string;
187
+ config: Awaited<ReturnType<typeof currentConfig>>;
188
+ filePath: string;
189
+ linkDecision: ReturnType<typeof evaluatePolicy>;
190
+ operation: PathOperation;
191
+ resolved: string;
192
+ roots: string[];
193
+ }): Promise<string> {
194
+ if (!args.linkDecision.symlinks)
195
+ throw new Error(
196
+ `Symbolic-link targets are not permitted by the winning path rule: ${args.filePath}`,
197
+ );
198
+ const target = await realpath(args.resolved);
199
+ assertWithinBoundary(
200
+ target,
201
+ args.roots,
202
+ args.allowAnyPath,
203
+ `Symbolic-link target is outside ${args.boundary}: ${args.filePath}`,
204
+ );
205
+ const targetDecision = evaluatePolicy(args.config, target, args.operation);
206
+ enforcePolicy(args.config, targetDecision);
207
+ if (!targetDecision.symlinks)
208
+ throw new Error(
209
+ `Resolved symbolic-link target is not permitted by the winning path rule: ${target}`,
210
+ );
211
+ return target;
212
+ }
213
+
108
214
  async function resolvePath(
109
215
  filePath: string,
110
216
  roots: string[],
111
217
  allowAnyPath: boolean,
112
218
  boundary: string,
219
+ operation: PathOperation,
113
220
  ): Promise<string> {
114
- const resolved = await canonicalCandidate(filePath, roots[0] as string);
221
+ const config = await currentConfig();
222
+ const base =
223
+ path.isAbsolute(filePath) || !relativeRootFromPwd(roots)
224
+ ? (roots[0] as string)
225
+ : (relativeRootFromPwd(roots) as string);
226
+ const resolved = await canonicalCandidate(filePath, base);
115
227
  assertWithinBoundary(
116
228
  resolved,
117
229
  roots,
118
230
  allowAnyPath,
119
231
  `Path is outside ${boundary}: ${filePath}`,
120
232
  );
233
+ const linkDecision = evaluatePolicy(config, resolved, operation);
234
+ enforcePolicy(config, linkDecision);
121
235
  const metadata = await lstat(resolved).catch(() => undefined);
122
236
  if (!metadata?.isSymbolicLink()) return resolved;
123
- if (!(await currentConfig()).safety.followSymlinks)
124
- throw new Error(`Symbolic-link targets are not permitted: ${filePath}`);
125
- const target = await realpath(resolved);
126
- assertWithinBoundary(
127
- target,
128
- roots,
237
+ return resolvedSymlinkTarget({
129
238
  allowAnyPath,
130
- `Symbolic-link target is outside ${boundary}: ${filePath}`,
131
- );
132
- return target;
239
+ boundary,
240
+ config,
241
+ filePath,
242
+ linkDecision,
243
+ operation,
244
+ resolved,
245
+ roots,
246
+ });
133
247
  }
134
248
 
135
- export async function resolveWritablePath(filePath: string): Promise<string> {
249
+ export async function resolveWritablePath(
250
+ filePath: string,
251
+ operation: PathOperation = "write",
252
+ ): Promise<string> {
136
253
  const config = await currentConfig();
137
254
  return resolvePath(
138
255
  filePath,
139
256
  await fileOperationRoots(),
140
257
  config.safety.allowAnyPath,
141
- "configured file-operation roots; use the OS temporary directory, configure workspace.roots, or explicitly set safety.allow_any_path=true",
258
+ "configured file-operation roots and paths rules",
259
+ operation,
142
260
  );
143
261
  }
144
262
 
145
263
  export async function resolveWorkspacePath(filePath: string): Promise<string> {
146
- return resolvePath(filePath, await workspaceRoots(), false, "AST_MCP_ROOTS");
264
+ return resolvePath(
265
+ filePath,
266
+ await fileOperationRoots(),
267
+ false,
268
+ "configured workspace and paths roots",
269
+ "read",
270
+ );
147
271
  }
148
272
  export async function rootsForDisplay(): Promise<string[]> {
149
273
  return configuredRoots();
package/src/server.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import { McpServer } from "@modelcontextprotocol/server";
2
+ import packageJson from "../package.json" with { type: "json" };
2
3
  import tools from "./tools";
3
4
 
4
5
  export function createServer() {
5
6
  const server = new McpServer({
6
7
  name: "ast-mcp",
7
- version: "1.0.0",
8
+ version: packageJson.version,
8
9
  });
9
10
 
10
11
  tools(server);