@mwillbanks/ast-mcp 0.2.0 → 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.
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.1"
90
90
  }
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,
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
  );
@@ -96,23 +96,13 @@ const instructionsBegin = "<!-- ast-mcp:begin -->";
96
96
  const instructionsEnd = "<!-- ast-mcp:end -->";
97
97
 
98
98
  async function astMcpEntry(entry: unknown): Promise<boolean> {
99
- if (typeof entry !== "string" || path.basename(entry) !== "ast-mcp.js")
99
+ if (entry === "./node_modules/.bin/ast-mcp") return true;
100
+ if (typeof entry !== "string" || path.basename(entry) !== "ast-mcp")
100
101
  return false;
101
- let folder = path.dirname(path.resolve(entry));
102
- while (true) {
103
- try {
104
- const manifest = JSON.parse(
105
- await readFile(path.join(folder, "package.json"), "utf8"),
106
- );
107
- return (
108
- manifest.name === "@mwillbanks/ast-mcp" &&
109
- path.resolve(entry) === path.join(folder, "dist/ast-mcp.js")
110
- );
111
- } catch {}
112
- const parent = path.dirname(folder);
113
- if (parent === folder) return false;
114
- folder = parent;
115
- }
102
+ const bin = path.dirname(entry);
103
+ return (
104
+ path.basename(bin) === "bin" && path.basename(path.dirname(bin)) === ".bun"
105
+ );
116
106
  }
117
107
 
118
108
  async function expectedReference(
@@ -173,7 +163,7 @@ async function hookCurrent(
173
163
  );
174
164
  for (const command of commands) {
175
165
  if (typeof command !== "string") continue;
176
- const match = command.match(/^bun (.+) hook$/);
166
+ const match = command.match(/^(.+) hook$/);
177
167
  if (!match) continue;
178
168
  try {
179
169
  if (await astMcpEntry(JSON.parse(match[1]))) return true;
@@ -203,10 +193,10 @@ function httpJsonMcpCurrent(
203
193
  }
204
194
  async function stdioCommandCurrent(entry: McpEntry | undefined) {
205
195
  return (
206
- entry?.command === "bun" &&
207
- Array.isArray(entry.args) &&
208
- entry.args[1] === "mcp" &&
209
- (await astMcpEntry(entry.args[0]))
196
+ (await astMcpEntry(entry?.command)) &&
197
+ Array.isArray(entry?.args) &&
198
+ entry.args.length === 1 &&
199
+ entry.args[0] === "mcp"
210
200
  );
211
201
  }
212
202
 
@@ -266,12 +256,12 @@ function codexHttpMcpCurrent(block: string, url: string | undefined) {
266
256
  );
267
257
  }
268
258
  async function codexStdioMcpCurrent(block: string, root: string | undefined) {
269
- const args = block.match(/args = \[(".*"), "mcp"\]/);
259
+ const command = block.match(/command = (".*")/);
270
260
  if (
271
261
  !block.includes("[mcp_servers.ast-mcp]") ||
272
- !block.includes('command = "bun"') ||
273
- !args ||
274
- !(await astMcpEntry(JSON.parse(args[1])))
262
+ !block.includes('args = ["mcp"]') ||
263
+ !command ||
264
+ !(await astMcpEntry(JSON.parse(command[1])))
275
265
  )
276
266
  return false;
277
267
  return root