@mwillbanks/ast-mcp 0.2.0 → 0.2.2

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.
package/package.json CHANGED
@@ -86,5 +86,5 @@
86
86
  "dprint"
87
87
  ],
88
88
  "type": "module",
89
- "version": "0.2.0"
89
+ "version": "0.2.2"
90
90
  }
package/src/config.ts CHANGED
@@ -81,7 +81,9 @@ const fileSchema = z
81
81
  .optional(),
82
82
  safety: z
83
83
  .object({
84
+ allow_any_path: z.boolean().optional(),
84
85
  allow_external_roots: z.boolean().optional(),
86
+ allow_temp_directory: z.boolean().optional(),
85
87
  follow_symlinks: z.boolean().optional(),
86
88
  hook: hookSchema.optional(),
87
89
  require_hash: z.boolean().optional(),
@@ -120,7 +122,9 @@ export interface ResolvedConfig {
120
122
  projectRoot: string;
121
123
  provenance: Record<string, "default" | "global" | "project" | "environment">;
122
124
  safety: {
125
+ allowAnyPath: boolean;
123
126
  allowExternalRoots: boolean;
127
+ allowTempDirectory: boolean;
124
128
  followSymlinks: boolean;
125
129
  hook: { allowTools: string[]; blockTools: string[]; enabled: boolean };
126
130
  requireHash: boolean;
@@ -170,7 +174,9 @@ interface InternalConfig {
170
174
  session_sweep_interval_ms?: number;
171
175
  };
172
176
  safety?: {
177
+ allow_any_path?: boolean;
173
178
  allow_external_roots?: boolean;
179
+ allow_temp_directory?: boolean;
174
180
  follow_symlinks?: boolean;
175
181
  hook?: {
176
182
  allow_tools?: string[];
@@ -457,13 +463,29 @@ function environmentHttp(env: NodeJS.ProcessEnv, usage: EnvironmentUsage) {
457
463
  }
458
464
 
459
465
  function environmentSafety(env: NodeJS.ProcessEnv, usage: EnvironmentUsage) {
460
- const allowExternal = usage.use(
466
+ const allowAnyPath = usage.use(
467
+ "AST_MCP_ALLOW_ANY_PATH",
468
+ booleanEnvironment(env, "AST_MCP_ALLOW_ANY_PATH"),
469
+ );
470
+ const allowExternalRoots = usage.use(
461
471
  "AST_MCP_ALLOW_EXTERNAL_ROOTS",
462
472
  booleanEnvironment(env, "AST_MCP_ALLOW_EXTERNAL_ROOTS"),
463
473
  );
464
- return allowExternal === undefined
465
- ? undefined
466
- : { allow_external_roots: allowExternal };
474
+ const allowTempDirectory = usage.use(
475
+ "AST_MCP_ALLOW_TEMP_DIRECTORY",
476
+ booleanEnvironment(env, "AST_MCP_ALLOW_TEMP_DIRECTORY"),
477
+ );
478
+ if (
479
+ allowAnyPath === undefined &&
480
+ allowExternalRoots === undefined &&
481
+ allowTempDirectory === undefined
482
+ )
483
+ return undefined;
484
+ return {
485
+ allow_any_path: allowAnyPath,
486
+ allow_external_roots: allowExternalRoots,
487
+ allow_temp_directory: allowTempDirectory,
488
+ };
467
489
  }
468
490
 
469
491
  function environmentLayer(
@@ -491,7 +513,9 @@ function environmentLayer(
491
513
  const leaves = [
492
514
  "version",
493
515
  "workspace.roots",
516
+ "safety.allow_any_path",
494
517
  "safety.allow_external_roots",
518
+ "safety.allow_temp_directory",
495
519
  "safety.follow_symlinks",
496
520
  "safety.hook.allow_tools",
497
521
  "safety.hook.block_tools",
@@ -615,7 +639,9 @@ function defaultInternalConfig(candidates: string[]): InternalConfig {
615
639
  session_timeout_ms: 30 * 60 * 1000,
616
640
  },
617
641
  safety: {
642
+ allow_any_path: false,
618
643
  allow_external_roots: false,
644
+ allow_temp_directory: true,
619
645
  follow_symlinks: false,
620
646
  hook: { allow_tools: [], block_tools: [], enabled: true },
621
647
  require_hash: true,
@@ -679,7 +705,9 @@ function resolvedHook(value: InternalConfig["safety"]) {
679
705
 
680
706
  function resolvedSafety(value: InternalConfig): ResolvedConfig["safety"] {
681
707
  return {
708
+ allowAnyPath: withDefault(value.safety?.allow_any_path, false),
682
709
  allowExternalRoots: withDefault(value.safety?.allow_external_roots, false),
710
+ allowTempDirectory: withDefault(value.safety?.allow_temp_directory, true),
683
711
  followSymlinks: withDefault(value.safety?.follow_symlinks, false),
684
712
  hook: resolvedHook(value.safety),
685
713
  requireHash: withDefault(value.safety?.require_hash, true),
@@ -139,11 +139,13 @@ function isKeyedBatchPath(
139
139
  childKey: string,
140
140
  child: unknown,
141
141
  ): boolean {
142
+ const isBatchEntry =
143
+ parentKey === "files" ||
144
+ (parentKey === undefined && !keyedBatchExclusions.has(childKey));
142
145
  return (
143
- parentKey === undefined &&
146
+ isBatchEntry &&
144
147
  Boolean(child) &&
145
148
  typeof child === "object" &&
146
- !Array.isArray(child) &&
147
- !keyedBatchExclusions.has(childKey)
149
+ !Array.isArray(child)
148
150
  );
149
151
  }
@@ -18,6 +18,13 @@ export function boundedRecord<T extends z.ZodType>(schema: T, label: string) {
18
18
  }, label);
19
19
  }
20
20
 
21
+ export function boundedFileBatch<T extends z.ZodType>(
22
+ schema: T,
23
+ label: string,
24
+ ) {
25
+ return z.object({ files: boundedRecord(schema, label) }).strict();
26
+ }
27
+
21
28
  export function toolFailure(error: unknown) {
22
29
  return {
23
30
  content: [
package/src/installer.ts CHANGED
@@ -36,6 +36,10 @@ import {
36
36
 
37
37
  const packageRoot = path.resolve(import.meta.dir, "..");
38
38
  const cliEntry = path.join(packageRoot, "dist/ast-mcp.js");
39
+ const localCliEntry = "./node_modules/.bin/ast-mcp";
40
+ function cliEntryFor(root: string | undefined, home: string) {
41
+ return root ? localCliEntry : path.join(home, ".bun/bin/ast-mcp");
42
+ }
39
43
  const hookEntry = path.join(packageRoot, "src/hook.ts");
40
44
  const targets = ["codex", "claude", "copilot"] as const;
41
45
  type Target = (typeof targets)[number];
@@ -71,19 +75,21 @@ async function installerAstBroBinary(options: InstallOptions) {
71
75
 
72
76
  function definition(
73
77
  root: string | undefined,
78
+ home: string,
74
79
  transport: McpTransport,
75
80
  endpoint?: HttpEndpoint,
76
81
  ) {
77
82
  if (transport === "http") return { type: "http", url: endpoint?.url };
78
83
  return {
79
- args: [cliEntry, "mcp"],
80
- command: "bun",
84
+ args: ["mcp"],
85
+ command: cliEntryFor(root, home),
81
86
  env: root ? { AST_MCP_PROJECT_ROOT: root } : {},
82
87
  };
83
88
  }
84
89
  async function codexMcp(
85
90
  file: string,
86
91
  root: string | undefined,
92
+ home: string,
87
93
  transport: McpTransport,
88
94
  endpoint?: HttpEndpoint,
89
95
  ) {
@@ -97,19 +103,20 @@ async function codexMcp(
97
103
  const block =
98
104
  transport === "http"
99
105
  ? `# ast-mcp:begin\n[mcp_servers.ast-mcp]\nurl = ${JSON.stringify(endpoint?.url)}\n# ast-mcp:end`
100
- : `# ast-mcp:begin\n[mcp_servers.ast-mcp]\ncommand = "bun"\nargs = [${JSON.stringify(cliEntry)}, "mcp"]\n${environment}# ast-mcp:end`;
106
+ : `# ast-mcp:begin\n[mcp_servers.ast-mcp]\ncommand = ${JSON.stringify(cliEntryFor(root, home))}\nargs = ["mcp"]\n${environment}# ast-mcp:end`;
101
107
  await mkdir(path.dirname(file), { recursive: true });
102
108
  await writeFile(file, `${clean ? `${clean}\n\n` : ""}${block}\n`);
103
109
  }
104
110
  async function jsonMcp(
105
111
  file: string,
106
112
  root: string | undefined,
113
+ home: string,
107
114
  copilot: boolean,
108
115
  transport: McpTransport,
109
116
  endpoint?: HttpEndpoint,
110
117
  ) {
111
118
  const value = await json(file);
112
- const entry = definition(root, transport, endpoint);
119
+ const entry = definition(root, home, transport, endpoint);
113
120
  value.mcpServers = {
114
121
  ...(value.mcpServers ?? {}),
115
122
  "ast-mcp": copilot
@@ -123,11 +130,12 @@ async function jsonMcp(
123
130
  async function vscodeMcp(
124
131
  file: string,
125
132
  root: string,
133
+ home: string,
126
134
  transport: McpTransport,
127
135
  endpoint?: HttpEndpoint,
128
136
  ) {
129
137
  const value = await json(file);
130
- const entry = definition(root, transport, endpoint);
138
+ const entry = definition(root, home, transport, endpoint);
131
139
  value.servers = {
132
140
  ...(value.servers ?? {}),
133
141
  "ast-mcp": transport === "stdio" ? { type: "stdio", ...entry } : entry,
@@ -139,16 +147,18 @@ async function hook(
139
147
  file: string,
140
148
  event: "PreToolUse" | "preToolUse",
141
149
  scriptFile: string,
142
- _commandPath?: string,
150
+ commandPath: string,
143
151
  ) {
144
152
  await rm(scriptFile, { force: true });
145
153
  const value = await json(file);
146
154
  if (event === "preToolUse") value.version ??= 1;
147
155
  value.hooks ??= {};
148
156
  const prior = Array.isArray(value.hooks[event]) ? value.hooks[event] : [];
149
- const command = `bun ${JSON.stringify(cliEntry)} hook`;
157
+ const command = `${JSON.stringify(commandPath)} hook`;
158
+ const managedCommands = [command, `bun ${JSON.stringify(cliEntry)} hook`];
150
159
  const kept = prior.filter(
151
- (item: unknown) => !isManagedHook(item, event, command),
160
+ (item: unknown) =>
161
+ !managedCommands.some((managed) => isManagedHook(item, event, managed)),
152
162
  );
153
163
  const item =
154
164
  event === "preToolUse"
@@ -259,6 +269,7 @@ async function removeHook(
259
269
  file: string,
260
270
  event: "PreToolUse" | "preToolUse",
261
271
  commandPath: string,
272
+ cliAlias: string,
262
273
  ) {
263
274
  const value = await json(file);
264
275
  const hooks = value.hooks;
@@ -266,6 +277,7 @@ async function removeHook(
266
277
  const commands = [
267
278
  `bun ${JSON.stringify(commandPath)}`,
268
279
  `bun ${JSON.stringify(cliEntry)} hook`,
280
+ `${JSON.stringify(cliAlias)} hook`,
269
281
  ];
270
282
  hooks[event] = hooks[event].filter(
271
283
  (item: unknown) =>
@@ -355,12 +367,14 @@ async function selectedTransport(
355
367
  }
356
368
 
357
369
  function serviceConfiguration(options: InstallOptions, endpoint: HttpEndpoint) {
370
+ const root = path.resolve(options.root);
371
+ const home = options.home ?? os.homedir();
358
372
  return {
359
- cliEntry,
373
+ cliEntry: cliEntryFor(options.scope === "local" ? root : undefined, home),
360
374
  endpoint,
361
- home: options.home ?? os.homedir(),
375
+ home,
362
376
  platform: options.platform,
363
- root: path.resolve(options.root),
377
+ root,
364
378
  runner: options.serviceRunner,
365
379
  scope: options.scope,
366
380
  };
@@ -426,6 +440,7 @@ async function installCodexTarget(
426
440
  await codexMcp(
427
441
  path.join(base, "config.toml"),
428
442
  global ? undefined : root,
443
+ home,
429
444
  transport,
430
445
  endpoint,
431
446
  );
@@ -433,7 +448,7 @@ async function installCodexTarget(
433
448
  path.join(base, "hooks.json"),
434
449
  "PreToolUse",
435
450
  path.join(base, "hooks/ast-mcp.ts"),
436
- global ? path.join(base, "hooks/ast-mcp.ts") : ".codex/hooks/ast-mcp.ts",
451
+ cliEntryFor(global ? undefined : root, home),
437
452
  );
438
453
  await installTargetAssets(
439
454
  base,
@@ -452,6 +467,7 @@ async function installClaudeTarget(
452
467
  await jsonMcp(
453
468
  global ? path.join(home, ".claude.json") : path.join(root, ".mcp.json"),
454
469
  global ? undefined : root,
470
+ home,
455
471
  false,
456
472
  transport,
457
473
  endpoint,
@@ -460,9 +476,7 @@ async function installClaudeTarget(
460
476
  path.join(base, "settings.json"),
461
477
  "PreToolUse",
462
478
  path.join(base, "hooks/ast-mcp.ts"),
463
- global
464
- ? path.join(base, "hooks/ast-mcp.ts")
465
- : `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
479
+ cliEntryFor(global ? undefined : root, home),
466
480
  );
467
481
  await installTargetAssets(
468
482
  base,
@@ -484,6 +498,7 @@ async function installCopilotTarget(
484
498
  await jsonMcp(
485
499
  path.join(base, "mcp-config.json"),
486
500
  undefined,
501
+ home,
487
502
  true,
488
503
  transport,
489
504
  endpoint,
@@ -492,6 +507,7 @@ async function installCopilotTarget(
492
507
  await jsonMcp(
493
508
  path.join(root, ".github/mcp.json"),
494
509
  root,
510
+ home,
495
511
  true,
496
512
  transport,
497
513
  endpoint,
@@ -499,6 +515,7 @@ async function installCopilotTarget(
499
515
  await vscodeMcp(
500
516
  path.join(root, ".vscode/mcp.json"),
501
517
  root,
518
+ home,
502
519
  transport,
503
520
  endpoint,
504
521
  );
@@ -507,7 +524,7 @@ async function installCopilotTarget(
507
524
  path.join(base, "hooks/ast-mcp.json"),
508
525
  "preToolUse",
509
526
  path.join(base, "hooks/ast-mcp.ts"),
510
- global ? path.join(base, "hooks/ast-mcp.ts") : ".github/hooks/ast-mcp.ts",
527
+ cliEntryFor(global ? undefined : root, home),
511
528
  );
512
529
  await installTargetAssets(
513
530
  base,
@@ -715,6 +732,7 @@ async function uninstallCodexTarget(
715
732
  path.join(base, "hooks.json"),
716
733
  "PreToolUse",
717
734
  global ? path.join(base, "hooks/ast-mcp.ts") : ".codex/hooks/ast-mcp.ts",
735
+ cliEntryFor(global ? undefined : root, home),
718
736
  );
719
737
  await removeTargetAssets(
720
738
  base,
@@ -737,6 +755,7 @@ async function uninstallClaudeTarget(
737
755
  global
738
756
  ? path.join(base, "hooks/ast-mcp.ts")
739
757
  : `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
758
+ cliEntryFor(global ? undefined : root, home),
740
759
  );
741
760
  await removeTargetAssets(
742
761
  base,
@@ -761,6 +780,7 @@ async function uninstallCopilotTarget(
761
780
  path.join(base, "hooks/ast-mcp.json"),
762
781
  "preToolUse",
763
782
  global ? path.join(base, "hooks/ast-mcp.ts") : ".github/hooks/ast-mcp.ts",
783
+ cliEntryFor(global ? undefined : root, home),
764
784
  );
765
785
  await removeTargetAssets(
766
786
  base,
@@ -847,7 +867,7 @@ export async function runInstallerCli(
847
867
  : undefined;
848
868
  const manualStart =
849
869
  endpoint && options.service !== true
850
- ? `${global ? "" : `cd ${JSON.stringify(root)} && `}bun ${JSON.stringify(cliEntry)} mcp --transport http --host ${JSON.stringify(endpoint.host)} --port ${endpoint.port}`
870
+ ? `${global ? "" : `cd ${JSON.stringify(root)} && `}${JSON.stringify(cliEntryFor(global ? undefined : root, options.home ?? os.homedir()))} mcp --transport http --host ${JSON.stringify(endpoint.host)} --port ${endpoint.port}`
851
871
  : undefined;
852
872
  const result = {
853
873
  changed,
@@ -6,7 +6,11 @@ import { parseAstBroJson } from "../ast-bro/result";
6
6
  import { detectAstLanguage } from "../patch/languages";
7
7
  import { sha256File } from "./hash";
8
8
  import { withFileLocks } from "./locks";
9
- import { resolveWritablePath, rootForPath } from "./paths";
9
+ import {
10
+ configuredRootForPath,
11
+ resolveWritablePath,
12
+ rootForPath,
13
+ } from "./paths";
10
14
  import { requireExpectedHash, verifyExpectedHash } from "./policy";
11
15
 
12
16
  export interface FileDeleteRequest {
@@ -64,12 +68,30 @@ export async function deleteFilesSafely(requests: FileDeleteBatch) {
64
68
  if (!metadata.isFile())
65
69
  throw new Error(`file_delete accepts files only: ${inputPath}`);
66
70
  const root = await rootForPath(filePath);
67
- const importers = await importersFor(filePath, root);
71
+ const referenceRoot = await configuredRootForPath(filePath);
72
+ const language = detectAstLanguage(filePath);
73
+ if (
74
+ !referenceRoot &&
75
+ (await astCapable(filePath, language)) &&
76
+ !request.forceReferences
77
+ )
78
+ throw new Error(
79
+ `file_delete rejected ${inputPath}: references cannot be verified outside configured file-operation roots; set forceReferences to override`,
80
+ );
81
+ const importers = referenceRoot
82
+ ? await importersFor(filePath, referenceRoot)
83
+ : [];
68
84
  if (importers.length > 0 && !request.forceReferences)
69
85
  throw new Error(
70
86
  `file_delete rejected ${inputPath}: referenced by ${importers.join(", ")}; set forceReferences to override`,
71
87
  );
72
- return { filePath, importers, request, root };
88
+ return {
89
+ filePath,
90
+ importers,
91
+ referencesVerified: referenceRoot !== undefined,
92
+ request,
93
+ root,
94
+ };
73
95
  }),
74
96
  );
75
97
  const files = await withFileLocks(
@@ -81,12 +103,19 @@ export async function deleteFilesSafely(requests: FileDeleteBatch) {
81
103
  await requireExpectedHash(request.expectedSha256, "file_delete");
82
104
  verifyExpectedHash(request.expectedSha256, actual);
83
105
  }
84
- for (const { filePath, importers, request } of entries) {
106
+ for (const {
107
+ filePath,
108
+ importers,
109
+ referencesVerified,
110
+ request,
111
+ } of entries) {
85
112
  await rm(filePath);
86
113
  deleted[filePath] = {
87
114
  deleted: true,
88
115
  forcedReferences:
89
116
  importers.length > 0 && request.forceReferences === true,
117
+ referenceVerificationBypassed:
118
+ !referencesVerified && request.forceReferences === true,
90
119
  };
91
120
  }
92
121
  return deleted;
@@ -4,7 +4,7 @@ import { dirname, join } from "node:path";
4
4
 
5
5
  import { sha256File } from "./hash";
6
6
  import { withFileLocks } from "./locks";
7
- import { resolveWritablePath, rootForPath } from "./paths";
7
+ import { pathsShareRoot, resolveWritablePath } from "./paths";
8
8
  import { requireExpectedHash, verifyExpectedHash } from "./policy";
9
9
 
10
10
  export interface FileRenameRequest {
@@ -112,9 +112,7 @@ export async function renameFilesSafely(requests: FileRenameBatch) {
112
112
  throw new Error(
113
113
  `file_rename source and destination must differ: ${inputPath}`,
114
114
  );
115
- if (
116
- (await rootForPath(filePath)) !== (await rootForPath(destinationPath))
117
- )
115
+ if (!(await pathsShareRoot(filePath, destinationPath)))
118
116
  throw new Error(
119
117
  `file_rename source and destination must share a root: ${inputPath}`,
120
118
  );
@@ -1,4 +1,5 @@
1
1
  import { lstat, realpath } from "node:fs/promises";
2
+ import os from "node:os";
2
3
  import path from "node:path";
3
4
  import { currentConfig } from "../config";
4
5
 
@@ -13,7 +14,7 @@ function within(root: string, target: string): boolean {
13
14
  );
14
15
  }
15
16
 
16
- async function allowedRoots(): Promise<string[]> {
17
+ async function workspaceRoots(): Promise<string[]> {
17
18
  const config = await currentConfig();
18
19
  const roots = await Promise.all(
19
20
  (await configuredRoots()).map(async (root) =>
@@ -35,29 +36,55 @@ async function allowedRoots(): Promise<string[]> {
35
36
  return roots;
36
37
  }
37
38
 
39
+ async function fileOperationRoots(): Promise<string[]> {
40
+ 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];
44
+ }
45
+
38
46
  export async function primaryRoot(): Promise<string> {
39
- return (await allowedRoots())[0] as string;
47
+ return (await workspaceRoots())[0] as string;
40
48
  }
41
49
 
42
- export async function rootForPath(filePath: string): Promise<string> {
43
- const root = (await allowedRoots()).find((candidate) =>
50
+ export async function configuredRootForPath(
51
+ filePath: string,
52
+ ): Promise<string | undefined> {
53
+ return (await fileOperationRoots()).find((candidate) =>
44
54
  within(candidate, filePath),
45
55
  );
46
- if (!root) throw new Error(`Path is outside AST_MCP_ROOTS: ${filePath}`);
47
- return root;
48
56
  }
49
- export async function resolveWritablePath(filePath: string): Promise<string> {
50
- const config = await currentConfig();
51
- const roots = await allowedRoots();
57
+
58
+ export async function rootForPath(filePath: string): Promise<string> {
59
+ const root = await configuredRootForPath(filePath);
60
+ if (root) return root;
61
+ if ((await currentConfig()).safety.allowAnyPath)
62
+ return path.dirname(filePath);
63
+ throw new Error(
64
+ `Path is outside configured file-operation roots: ${filePath}`,
65
+ );
66
+ }
67
+
68
+ export async function pathsShareRoot(
69
+ first: string,
70
+ second: string,
71
+ ): Promise<boolean> {
72
+ if ((await currentConfig()).safety.allowAnyPath) return true;
73
+ return (await rootForPath(first)) === (await rootForPath(second));
74
+ }
75
+ async function canonicalCandidate(filePath: string, root: string) {
52
76
  const absolute = path.isAbsolute(filePath)
53
77
  ? filePath
54
- : path.resolve(roots[0] as string, filePath);
78
+ : path.resolve(root, filePath);
55
79
  let existing = path.dirname(absolute);
56
80
  const missing: string[] = [];
57
81
  while (true) {
58
82
  try {
59
- existing = await realpath(existing);
60
- break;
83
+ return path.join(
84
+ await realpath(existing),
85
+ ...missing,
86
+ path.basename(absolute),
87
+ );
61
88
  } catch (error) {
62
89
  if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
63
90
  const parent = path.dirname(existing);
@@ -66,22 +93,58 @@ export async function resolveWritablePath(filePath: string): Promise<string> {
66
93
  existing = parent;
67
94
  }
68
95
  }
69
- const resolved = path.join(existing, ...missing, path.basename(absolute));
70
- if (!roots.some((root) => within(root, resolved)))
71
- throw new Error(
72
- `Path is outside AST_MCP_ROOTS/configured workspace roots: ${filePath}`,
73
- );
96
+ }
97
+
98
+ function assertWithinBoundary(
99
+ target: string,
100
+ roots: string[],
101
+ allowAnyPath: boolean,
102
+ message: string,
103
+ ) {
104
+ if (!allowAnyPath && !roots.some((root) => within(root, target)))
105
+ throw new Error(message);
106
+ }
107
+
108
+ async function resolvePath(
109
+ filePath: string,
110
+ roots: string[],
111
+ allowAnyPath: boolean,
112
+ boundary: string,
113
+ ): Promise<string> {
114
+ const resolved = await canonicalCandidate(filePath, roots[0] as string);
115
+ assertWithinBoundary(
116
+ resolved,
117
+ roots,
118
+ allowAnyPath,
119
+ `Path is outside ${boundary}: ${filePath}`,
120
+ );
74
121
  const metadata = await lstat(resolved).catch(() => undefined);
75
122
  if (!metadata?.isSymbolicLink()) return resolved;
76
- if (!config.safety.followSymlinks)
123
+ if (!(await currentConfig()).safety.followSymlinks)
77
124
  throw new Error(`Symbolic-link targets are not permitted: ${filePath}`);
78
125
  const target = await realpath(resolved);
79
- if (!roots.some((root) => within(root, target)))
80
- throw new Error(
81
- `Symbolic-link target is outside configured workspace roots: ${filePath}`,
82
- );
126
+ assertWithinBoundary(
127
+ target,
128
+ roots,
129
+ allowAnyPath,
130
+ `Symbolic-link target is outside ${boundary}: ${filePath}`,
131
+ );
83
132
  return target;
84
133
  }
134
+
135
+ export async function resolveWritablePath(filePath: string): Promise<string> {
136
+ const config = await currentConfig();
137
+ return resolvePath(
138
+ filePath,
139
+ await fileOperationRoots(),
140
+ config.safety.allowAnyPath,
141
+ "configured file-operation roots; use the OS temporary directory, configure workspace.roots, or explicitly set safety.allow_any_path=true",
142
+ );
143
+ }
144
+
145
+ export async function resolveWorkspacePath(filePath: string): Promise<string> {
146
+ return resolvePath(filePath, await workspaceRoots(), false, "AST_MCP_ROOTS");
147
+ }
85
148
  export async function rootsForDisplay(): Promise<string[]> {
86
149
  return configuredRoots();
87
150
  }
package/src/service.ts CHANGED
@@ -83,7 +83,7 @@ export function createServicePlan(options: ServiceOptions): ServicePlan {
83
83
  `${label}.plist`,
84
84
  );
85
85
  const state = path.join(options.home, ".local/state/ast-mcp");
86
- const programArguments = [process.execPath, ...args]
86
+ const programArguments = args
87
87
  .map((value) => ` <string>${xml(value)}</string>`)
88
88
  .join("\n");
89
89
  const environmentVariables = Object.entries(environment)
@@ -113,9 +113,7 @@ export function createServicePlan(options: ServiceOptions): ServicePlan {
113
113
  const environmentLines = Object.entries(environment)
114
114
  .map(([name, value]) => `Environment="${name}=${systemd(value)}"`)
115
115
  .join("\n");
116
- const command = [process.execPath, ...args]
117
- .map((value) => `"${systemd(value)}"`)
118
- .join(" ");
116
+ const command = args.map((value) => `"${systemd(value)}"`).join(" ");
119
117
  const workingDirectory = systemd(
120
118
  options.scope === "local" ? path.resolve(options.root) : options.home,
121
119
  );
@@ -14,7 +14,7 @@ import metadata from "../ast-bro/tools.json";
14
14
  import { replaceFileAtomically } from "../runtime/atomic";
15
15
  import { assertFormattable, formatFileAtomically } from "../runtime/format";
16
16
  import { withFileLocks } from "../runtime/locks";
17
- import { primaryRoot, resolveWritablePath } from "../runtime/paths";
17
+ import { primaryRoot, resolveWorkspacePath } from "../runtime/paths";
18
18
  import { type ConfiguredExecution, localExecution } from "./configured";
19
19
 
20
20
  function upstreamSchema(
@@ -39,7 +39,7 @@ function upstreamSchema(
39
39
  };
40
40
  }
41
41
  async function boundedPath(root: string, value: string): Promise<string> {
42
- return resolveWritablePath(
42
+ return resolveWorkspacePath(
43
43
  path.isAbsolute(value) ? value : path.resolve(root, value),
44
44
  );
45
45
  }
@@ -147,7 +147,7 @@ export default function registerAstBroTools(
147
147
  {
148
148
  description:
149
149
  toolName === "run"
150
- ? "AST structural search and rewrite. Use for bounded inspection and previews; direct write is a lower-level escape hatch for exceptional cases. Normal agent edits belong in keyed file_patch, which supports ordered AST rules and Aider blocks."
150
+ ? "AST structural search and rewrite. Use for bounded inspection and previews; direct write is a lower-level escape hatch for exceptional cases. Normal agent edits belong in file_patch's declared files batch, which supports ordered AST rules and Aider blocks."
151
151
  : definition.description,
152
152
  inputSchema: upstreamSchema(definition.inputSchema),
153
153
  title: `ast-bro ${toolName}`,