@mwillbanks/ast-mcp 0.8.0 → 0.9.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.
@@ -10,8 +10,9 @@ import {
10
10
  executableNames,
11
11
  globalBinDirectories,
12
12
  isExecutable,
13
+ npmNativeBinaries,
13
14
  } from "./binary-resolution";
14
- import { managedAstMcpHookEntry } from "./managed-hook";
15
+ import { isPackageHookEntry, managedAstMcpHookEntry } from "./managed-hook";
15
16
  import {
16
17
  checkLatestVersion,
17
18
  compareVersions,
@@ -763,13 +764,18 @@ async function matchingSourceLaunch(
763
764
  }
764
765
 
765
766
  function* installedBinaryCandidates(root: string | undefined, home: string) {
766
- const names = executableNames("ast-mcp", process.platform);
767
+ const names = ["ast-mcp", "ast-mcp-bun"].flatMap((name) =>
768
+ executableNames(name, process.platform),
769
+ );
767
770
  for (const discover of root ? [false] : [false, true]) {
768
771
  const directories = root
769
772
  ? [path.join(root, "node_modules/.bin")]
770
773
  : globalBinDirectories("ast-mcp", process.platform, home, discover);
771
- for (const candidate of directoryBinaryCandidates(directories, names))
772
- if (isExecutable(candidate, process.platform)) yield candidate;
774
+ for (const candidate of directoryBinaryCandidates(directories, names)) {
775
+ if (!isExecutable(candidate, process.platform)) continue;
776
+ yield candidate;
777
+ yield* npmNativeBinaries(candidate);
778
+ }
773
779
  }
774
780
  }
775
781
 
@@ -849,26 +855,33 @@ async function hookCurrent(
849
855
  const entries = Array.isArray(hostConfig.hooks?.[event])
850
856
  ? hostConfig.hooks[event]
851
857
  : [];
852
- const commands =
858
+ type Hook = { command?: unknown; type?: unknown };
859
+ const hooks: Hook[] =
853
860
  event === "preToolUse"
854
- ? entries.map((item: { command?: unknown }) => item.command)
855
- : entries.flatMap((item: { hooks?: Array<{ command?: unknown }> }) =>
856
- (item.hooks ?? []).map((child) => child.command),
857
- );
858
- const managed = commands
859
- .map((command) => ({
860
- command,
861
- entry: managedAstMcpHookEntry(command),
862
- }))
863
- .filter(
864
- (item): item is { command: string; entry: string } =>
865
- typeof item.command === "string" && typeof item.entry === "string",
866
- );
861
+ ? entries
862
+ : entries.flatMap((item: { hooks?: Hook[] }) => item.hooks ?? []);
863
+ const managed: Array<{ command: string; entry: string; verified: boolean }> =
864
+ [];
865
+ for (const hook of hooks) {
866
+ const command = hook?.command;
867
+ const entry = managedAstMcpHookEntry(command);
868
+ if (typeof command !== "string" || entry === undefined) continue;
869
+ const packageHook = isPackageHookEntry(entry);
870
+ if (
871
+ packageHook &&
872
+ (hook.type !== "command" ||
873
+ command !== `${JSON.stringify(entry)} hook` ||
874
+ !(await astMcpEntry(entry, root, home)))
875
+ )
876
+ continue;
877
+ managed.push({ command, entry, verified: packageHook });
878
+ }
867
879
  if (managed.length !== 1) return false;
868
- const [{ command, entry }] = managed;
880
+ const [{ command, entry, verified }] = managed;
869
881
  if (
870
- command === `${JSON.stringify(entry)} hook` &&
871
- (await astMcpEntry(entry, root, home))
882
+ verified ||
883
+ (command === `${JSON.stringify(entry)} hook` &&
884
+ (await astMcpEntry(entry, root, home)))
872
885
  )
873
886
  return true;
874
887
  const receipt = await sourceLaunchReceipt(root, home);
@@ -2,6 +2,30 @@ import path from "node:path";
2
2
 
3
3
  export type HookEvent = "PreToolUse" | "preToolUse";
4
4
 
5
+ const legacyHookNames = [
6
+ "ast-mcp",
7
+ "ast-mcp.bat",
8
+ "ast-mcp.cmd",
9
+ "ast-mcp.com",
10
+ "ast-mcp.exe",
11
+ "ast-mcp.js",
12
+ "ast-mcp.ts",
13
+ ];
14
+ const packageHookNames = [
15
+ "ast-mcp-bun",
16
+ "ast-mcp-bun.cmd",
17
+ "ast-mcp-bytecode",
18
+ "ast-mcp-bytecode.exe",
19
+ ];
20
+
21
+ function hookBasename(entry: string) {
22
+ return path.basename(entry.replaceAll("\\", "/")).toLowerCase();
23
+ }
24
+
25
+ export function isPackageHookEntry(entry: string) {
26
+ return packageHookNames.includes(hookBasename(entry));
27
+ }
28
+
5
29
  export function managedAstMcpHookEntry(command: unknown) {
6
30
  if (typeof command !== "string") return undefined;
7
31
  const normalized = command.trim();
@@ -26,39 +50,50 @@ export function managedAstMcpHookEntry(command: unknown) {
26
50
  invocation = JSON.parse(invocation);
27
51
  } catch {}
28
52
  if (typeof invocation !== "string") return undefined;
29
- return [
30
- "ast-mcp",
31
- "ast-mcp.bat",
32
- "ast-mcp.cmd",
33
- "ast-mcp.com",
34
- "ast-mcp.exe",
35
- "ast-mcp.js",
36
- "ast-mcp.ts",
37
- ].includes(path.basename(invocation.replaceAll("\\", "/")).toLowerCase())
53
+ return legacyHookNames.includes(hookBasename(invocation)) ||
54
+ isPackageHookEntry(invocation)
38
55
  ? invocation
39
56
  : undefined;
40
57
  }
41
58
 
42
- function commandMatches(actual: unknown, expected: string) {
43
- return actual === expected || managedAstMcpHookEntry(actual) !== undefined;
59
+ function commandMatches(
60
+ actual: unknown,
61
+ expected: string,
62
+ ownsPackageHook?: (command: string) => boolean,
63
+ ) {
64
+ if (actual === expected) return true;
65
+ const entry = managedAstMcpHookEntry(actual);
66
+ return (
67
+ entry !== undefined &&
68
+ (legacyHookNames.includes(hookBasename(entry)) ||
69
+ (typeof actual === "string" && ownsPackageHook?.(actual) === true))
70
+ );
44
71
  }
45
72
 
46
73
  export function isManagedHook(
47
74
  item: unknown,
48
75
  event: HookEvent,
49
76
  command: string,
77
+ ownsPackageHook?: (command: string) => boolean,
50
78
  ): boolean {
51
79
  if (!item || typeof item !== "object") return false;
52
80
  const value = item as Record<string, unknown>;
53
81
  if (event === "preToolUse")
54
- return commandMatches(value.command, command) && value.type === "command";
82
+ return (
83
+ commandMatches(value.command, command, ownsPackageHook) &&
84
+ value.type === "command"
85
+ );
55
86
  return (
56
87
  Array.isArray(value.hooks) &&
57
88
  value.hooks.some(
58
89
  (child) =>
59
90
  child &&
60
91
  typeof child === "object" &&
61
- commandMatches((child as Record<string, unknown>).command, command) &&
92
+ commandMatches(
93
+ (child as Record<string, unknown>).command,
94
+ command,
95
+ ownsPackageHook,
96
+ ) &&
62
97
  (child as Record<string, unknown>).type === "command",
63
98
  )
64
99
  );