@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.
- package/CONTRIBUTING.md +13 -0
- package/README.md +18 -16
- package/dist/ast-mcp.js +209 -147
- package/dist/ast-mcp.js.map +70 -50
- package/package.json +21 -15
- package/src/ast-bro/capability.ts +20 -10
- package/src/ast-bro/client.ts +12 -6
- package/src/ast-bro/result.ts +79 -0
- package/src/ast-bro/tools.json +29 -4
- package/src/cli.ts +10 -7
- package/src/config-cli.ts +166 -19
- package/src/config-edit.ts +236 -0
- package/src/config-migrate.ts +206 -0
- package/src/config-registry.ts +329 -0
- package/src/config-schema-common.ts +88 -0
- package/src/config-v2-schema.ts +109 -0
- package/src/config.ts +523 -170
- package/src/helpers/ast-bro.ts +1 -7
- package/src/helpers/config.ts +16 -3
- package/src/helpers/installer.ts +8 -0
- package/src/helpers/mcp-schema.ts +98 -4
- package/src/host-smoke.ts +182 -0
- package/src/http.ts +8 -3
- package/src/installer-transport.ts +6 -4
- package/src/installer.ts +242 -54
- package/src/patch/engine.ts +575 -280
- package/src/patch/languages.ts +9 -0
- package/src/patch/strategy/aiderBlock.ts +110 -0
- package/src/patch/strategy/ast.ts +170 -0
- package/src/patch/strategy.ts +61 -0
- package/src/runtime/approval.ts +438 -0
- package/src/runtime/config-write.ts +327 -0
- package/src/runtime/dependencies.ts +14 -78
- package/src/runtime/document-inspection.ts +78 -0
- package/src/runtime/file-capabilities.ts +250 -0
- package/src/runtime/file-delete.ts +110 -37
- package/src/runtime/file-read.ts +134 -15
- package/src/runtime/file-rename.ts +5 -2
- package/src/runtime/format.ts +90 -30
- package/src/runtime/git-worktrees.ts +179 -0
- package/src/runtime/path-policy.ts +643 -0
- package/src/runtime/path-utils.ts +80 -0
- package/src/runtime/paths.ts +171 -47
- package/src/server.ts +2 -1
- package/src/tools/ast-bro.ts +294 -75
- package/src/tools/configuration.ts +668 -0
- package/src/tools/configured.ts +49 -21
- package/src/tools/files.ts +169 -62
- package/src/tools/index.ts +2 -0
- package/src/tools/lifecycle.ts +82 -38
- package/templates/AGENTS.md +17 -15
- package/templates/skills/ast-mcp/SKILL.md +57 -20
- package/templates/skills/ast-mcp/evals/evals.json +319 -9
- package/templates/skills/ast-mcp/evals/score.ts +80 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +3 -1
- package/templates/skills/ast-mcp/references/installation.md +12 -12
- package/templates/skills/ast-mcp/references/patch-state-machine.md +15 -14
- package/templates/skills/ast-mcp/references/tool-catalog.md +18 -8
- package/templates/skills/ast-mcp/scripts/binary-resolution.ts +112 -0
- package/templates/skills/ast-mcp/scripts/check-install.ts +165 -51
- package/templates/skills/ast-mcp/scripts/managed-hook.ts +34 -2
- package/src/ast-bro/rewrite-capability.ts +0 -9
package/package.json
CHANGED
|
@@ -7,25 +7,25 @@
|
|
|
7
7
|
"url": "https://github.com/mwillbanks/ast-mcp/issues"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@ast-bro/cli": "
|
|
11
|
-
"@modelcontextprotocol/client": "2.0.0
|
|
12
|
-
"@modelcontextprotocol/server": "
|
|
10
|
+
"@ast-bro/cli": "4.2.0",
|
|
11
|
+
"@modelcontextprotocol/client": "2.0.0",
|
|
12
|
+
"@modelcontextprotocol/server": "2.0.0",
|
|
13
13
|
"diff-match-patch-ts": "^2.0.0",
|
|
14
|
-
"dprint": "0.
|
|
15
|
-
"unbash": "^4.0.
|
|
14
|
+
"dprint": "0.56.1",
|
|
15
|
+
"unbash": "^4.0.10",
|
|
16
16
|
"zod": "^4.4.3"
|
|
17
17
|
},
|
|
18
18
|
"description": "Zero-trust MCP file writes, AST edits, ast-bro intelligence, hooks, and agent skills",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@biomejs/biome": "^2.5.
|
|
21
|
-
"@modelcontextprotocol/inspector": "^
|
|
22
|
-
"@types/bun": "^1.
|
|
23
|
-
"@types/node": "^26.
|
|
24
|
-
"fallow": "^3.
|
|
20
|
+
"@biomejs/biome": "^2.5.10",
|
|
21
|
+
"@modelcontextprotocol/inspector": "^2.4.0",
|
|
22
|
+
"@types/bun": "^1.4.0",
|
|
23
|
+
"@types/node": "^26.3.0",
|
|
24
|
+
"fallow": "^3.19.0",
|
|
25
25
|
"typescript": "^7.0.2"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
|
-
"bun": ">=1.
|
|
28
|
+
"bun": ">=1.4.0"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
31
|
"dist",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
],
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"name": "@mwillbanks/ast-mcp",
|
|
51
|
-
"packageManager": "bun@1.
|
|
51
|
+
"packageManager": "bun@1.4.0",
|
|
52
52
|
"preferUnplugged": true,
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
@@ -58,8 +58,10 @@
|
|
|
58
58
|
"url": "git+https://github.com/mwillbanks/ast-mcp.git"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
|
+
"ast-bro:schemas": "bun run scripts/sync-ast-bro-tools.ts --write",
|
|
62
|
+
"ast-bro:schemas:check": "bun run scripts/sync-ast-bro-tools.ts",
|
|
61
63
|
"build": "bun run scripts/build.ts",
|
|
62
|
-
"check": "bun run tools:check && bun run format:check && bun run typecheck && bun run test",
|
|
64
|
+
"check": "bun run tools:check && bun run templates:check && bun run prose:check && bun run format:check && bun run typecheck && bun run test",
|
|
63
65
|
"dev": "bun run --watch src/index.ts",
|
|
64
66
|
"dev:http": "bun run --watch src/http-entry.ts",
|
|
65
67
|
"evals:check": "bun run templates/skills/ast-mcp/evals/validate.ts",
|
|
@@ -73,11 +75,15 @@
|
|
|
73
75
|
"inspect:http": "bunx @modelcontextprotocol/inspector",
|
|
74
76
|
"lint": "biome check . --reporter concise --error-on-warnings",
|
|
75
77
|
"prepack": "bun run build",
|
|
78
|
+
"prose:check": "bun run scripts/check-prose-cli.ts",
|
|
76
79
|
"skill:check": "bunx skills-ref validate templates/skills/ast-mcp",
|
|
80
|
+
"smoke:hosts": "bun run scripts/host-smoke.ts",
|
|
77
81
|
"start": "bun run src/index.ts",
|
|
78
82
|
"start:http": "bun run src/http-entry.ts",
|
|
83
|
+
"templates:check": "bun run scripts/check-template-drift.ts",
|
|
84
|
+
"templates:sync": "bun run scripts/check-template-drift.ts --write",
|
|
79
85
|
"test": "bun test --bail --max-concurrency=1 --timeout=30000",
|
|
80
|
-
"tools:check": "bun run scripts/verify-tools.ts",
|
|
86
|
+
"tools:check": "bun run scripts/verify-tools.ts && bun run ast-bro:schemas:check",
|
|
81
87
|
"typecheck": "bunx tsc --noEmit"
|
|
82
88
|
},
|
|
83
89
|
"sideEffects": false,
|
|
@@ -86,5 +92,5 @@
|
|
|
86
92
|
"dprint"
|
|
87
93
|
],
|
|
88
94
|
"type": "module",
|
|
89
|
-
"version": "0.
|
|
95
|
+
"version": "0.4.0"
|
|
90
96
|
}
|
|
@@ -6,21 +6,31 @@ type AstBroMapPayload = {
|
|
|
6
6
|
files?: Array<{ error_count?: number; path?: unknown }>;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
export async function
|
|
9
|
+
export async function astErrorCount(
|
|
10
10
|
filePath: string,
|
|
11
11
|
language?: string,
|
|
12
|
-
): Promise<
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
): Promise<number | undefined> {
|
|
13
|
+
const command = [AST_BRO_BINARY, "map", "--json", filePath];
|
|
14
|
+
void language;
|
|
15
|
+
const process = Bun.spawn(command, {
|
|
15
16
|
cwd: path.dirname(filePath),
|
|
17
|
+
env: { ...Bun.env, NO_COLOR: "1" },
|
|
16
18
|
stderr: "pipe",
|
|
17
19
|
stdout: "pipe",
|
|
18
20
|
});
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const file = (payload.files ?? []).find(
|
|
23
|
-
(candidate) => candidate.path === filePath,
|
|
21
|
+
const output = successfulProcessOutput(
|
|
22
|
+
"ast-bro map",
|
|
23
|
+
await captureProcess(process),
|
|
24
24
|
);
|
|
25
|
-
|
|
25
|
+
if (!output.trim()) return undefined;
|
|
26
|
+
const payload = JSON.parse(output) as AstBroMapPayload;
|
|
27
|
+
const file = (payload.files ?? []).find((entry) => entry.path === filePath);
|
|
28
|
+
return file ? (file.error_count ?? 0) : undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function astCapable(
|
|
32
|
+
filePath: string,
|
|
33
|
+
language?: string,
|
|
34
|
+
): Promise<boolean> {
|
|
35
|
+
return (await astErrorCount(filePath, language)) === 0;
|
|
26
36
|
}
|
package/src/ast-bro/client.ts
CHANGED
|
@@ -2,7 +2,10 @@ import { Client } from "@modelcontextprotocol/client";
|
|
|
2
2
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
3
3
|
import { indexedAstBroCommand } from "../helpers/ast-bro";
|
|
4
4
|
import { captureProcess, successfulProcessOutput } from "../helpers/process";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
assertAstBroAvailable,
|
|
7
|
+
configuredAstBroBinary,
|
|
8
|
+
} from "../runtime/dependencies";
|
|
6
9
|
|
|
7
10
|
export { AST_BRO_BINARY } from "../runtime/dependencies";
|
|
8
11
|
export const AST_BRO_TOOLS = [
|
|
@@ -31,16 +34,19 @@ export async function callAstBro(
|
|
|
31
34
|
args: Record<string, unknown>,
|
|
32
35
|
root: string,
|
|
33
36
|
) {
|
|
37
|
+
const binary = await configuredAstBroBinary();
|
|
38
|
+
assertAstBroAvailable(binary);
|
|
34
39
|
if (
|
|
35
40
|
toolName === "search" ||
|
|
36
41
|
toolName === "find_related" ||
|
|
37
42
|
toolName === "index"
|
|
38
43
|
) {
|
|
39
44
|
const commandArgs = indexedAstBroCommand(toolName, args, root);
|
|
40
|
-
const processHandle = Bun.spawn(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
const processHandle = Bun.spawn([binary, ...commandArgs], {
|
|
46
|
+
cwd: root,
|
|
47
|
+
stderr: "pipe",
|
|
48
|
+
stdout: "pipe",
|
|
49
|
+
});
|
|
44
50
|
const result = await captureProcess(processHandle);
|
|
45
51
|
const stdout = successfulProcessOutput(`ast-bro ${commandArgs[0]}`, result);
|
|
46
52
|
return { content: [{ text: stdout, type: "text" as const }] };
|
|
@@ -48,7 +54,7 @@ export async function callAstBro(
|
|
|
48
54
|
const client = new Client({ name: "ast-mcp", version: "1.0.0" });
|
|
49
55
|
const transport = new StdioClientTransport({
|
|
50
56
|
args: ["mcp"],
|
|
51
|
-
command:
|
|
57
|
+
command: binary,
|
|
52
58
|
cwd: root,
|
|
53
59
|
});
|
|
54
60
|
try {
|
package/src/ast-bro/result.ts
CHANGED
|
@@ -11,6 +11,85 @@ export function parseAstBroJson(result: AstBroResult): AstBroJson {
|
|
|
11
11
|
return parseTextResultJson(result);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
export interface AstBroShowMatch {
|
|
15
|
+
end_line: number;
|
|
16
|
+
kind: string;
|
|
17
|
+
qualified_name: string;
|
|
18
|
+
source: string;
|
|
19
|
+
start_line: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface AstBroShowV2 {
|
|
23
|
+
files: Array<{
|
|
24
|
+
language: string;
|
|
25
|
+
matches: AstBroShowMatch[];
|
|
26
|
+
path: string;
|
|
27
|
+
}>;
|
|
28
|
+
files_matched: number;
|
|
29
|
+
files_scanned: number;
|
|
30
|
+
schema: "ast-bro.show.v2";
|
|
31
|
+
shown: number;
|
|
32
|
+
symbols: string[];
|
|
33
|
+
total: number;
|
|
34
|
+
truncated: boolean;
|
|
35
|
+
unmatched: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function nonnegativeInteger(value: unknown): value is number {
|
|
39
|
+
return Number.isInteger(value) && (value as number) >= 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function stringArray(value: unknown): value is string[] {
|
|
43
|
+
return (
|
|
44
|
+
Array.isArray(value) && value.every((item) => typeof item === "string")
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function showMatch(value: unknown): value is AstBroShowMatch {
|
|
49
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
50
|
+
const match = value as Record<string, unknown>;
|
|
51
|
+
return (
|
|
52
|
+
typeof match.qualified_name === "string" &&
|
|
53
|
+
typeof match.kind === "string" &&
|
|
54
|
+
nonnegativeInteger(match.start_line) &&
|
|
55
|
+
nonnegativeInteger(match.end_line) &&
|
|
56
|
+
match.end_line >= match.start_line &&
|
|
57
|
+
typeof match.source === "string"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function parseAstBroShowV2(result: AstBroResult): AstBroShowV2 {
|
|
62
|
+
const payload = parseAstBroJson(result);
|
|
63
|
+
const files = payload.files;
|
|
64
|
+
const validFiles =
|
|
65
|
+
Array.isArray(files) &&
|
|
66
|
+
files.every(
|
|
67
|
+
(file) =>
|
|
68
|
+
file &&
|
|
69
|
+
typeof file === "object" &&
|
|
70
|
+
!Array.isArray(file) &&
|
|
71
|
+
typeof file.path === "string" &&
|
|
72
|
+
typeof file.language === "string" &&
|
|
73
|
+
Array.isArray(file.matches) &&
|
|
74
|
+
file.matches.every(showMatch),
|
|
75
|
+
);
|
|
76
|
+
if (
|
|
77
|
+
payload.schema !== "ast-bro.show.v2" ||
|
|
78
|
+
!stringArray(payload.symbols) ||
|
|
79
|
+
!stringArray(payload.unmatched) ||
|
|
80
|
+
!validFiles ||
|
|
81
|
+
!nonnegativeInteger(payload.files_scanned) ||
|
|
82
|
+
!nonnegativeInteger(payload.files_matched) ||
|
|
83
|
+
payload.files_matched > payload.files_scanned ||
|
|
84
|
+
!nonnegativeInteger(payload.total) ||
|
|
85
|
+
!nonnegativeInteger(payload.shown) ||
|
|
86
|
+
payload.shown > payload.total ||
|
|
87
|
+
typeof payload.truncated !== "boolean"
|
|
88
|
+
)
|
|
89
|
+
throw new Error("Invalid ast-bro.show.v2 response");
|
|
90
|
+
return payload as AstBroShowV2;
|
|
91
|
+
}
|
|
92
|
+
|
|
14
93
|
export function astBroMatchFiles(result: AstBroResult): string[] {
|
|
15
94
|
const payload = parseAstBroJson(result);
|
|
16
95
|
return [
|
package/src/ast-bro/tools.json
CHANGED
|
@@ -15,6 +15,11 @@
|
|
|
15
15
|
"json": {
|
|
16
16
|
"type": "boolean"
|
|
17
17
|
},
|
|
18
|
+
"limit": {
|
|
19
|
+
"description": "Cap result count (default 200). The reported total stays exact.",
|
|
20
|
+
"minimum": 1,
|
|
21
|
+
"type": "integer"
|
|
22
|
+
},
|
|
18
23
|
"path": {
|
|
19
24
|
"description": "Repo root (default \".\").",
|
|
20
25
|
"type": "string"
|
|
@@ -146,9 +151,13 @@
|
|
|
146
151
|
}
|
|
147
152
|
},
|
|
148
153
|
"digest": {
|
|
149
|
-
"description": "One-page module map for an unfamiliar directory: every file's types and public methods. Returns text by default; set `json: true` for `ast-bro.map.v1`.",
|
|
154
|
+
"description": "One-page module map for an unfamiliar directory: every file's types and public methods. Alias for `map` with detail=names, public-only, max_members=50. Returns text by default; set `json: true` for `ast-bro.map.v1`.",
|
|
150
155
|
"inputSchema": {
|
|
151
156
|
"properties": {
|
|
157
|
+
"glob": {
|
|
158
|
+
"description": "Glob filter applied during directory walk.",
|
|
159
|
+
"type": "string"
|
|
160
|
+
},
|
|
152
161
|
"include_fields": {
|
|
153
162
|
"type": "boolean"
|
|
154
163
|
},
|
|
@@ -159,7 +168,8 @@
|
|
|
159
168
|
"type": "boolean"
|
|
160
169
|
},
|
|
161
170
|
"max_members": {
|
|
162
|
-
"description": "Cap members per type (default 50).",
|
|
171
|
+
"description": "Cap members per type (default 50); the output reports what was cut.",
|
|
172
|
+
"minimum": 0,
|
|
163
173
|
"type": "integer"
|
|
164
174
|
},
|
|
165
175
|
"paths": {
|
|
@@ -330,6 +340,11 @@
|
|
|
330
340
|
"description": "AST-based structural map of source files — signatures with line ranges, no method bodies. Returns text by default (5–10× smaller than reading the file). Set `json: true` for the machine-readable schema `ast-bro.map.v1`.",
|
|
331
341
|
"inputSchema": {
|
|
332
342
|
"properties": {
|
|
343
|
+
"detail": {
|
|
344
|
+
"description": "Detail level: `full` (signatures + docs, default), `signatures` (no docs), `names` (bare member names — the digest renderer).",
|
|
345
|
+
"enum": ["names", "signatures", "full"],
|
|
346
|
+
"type": "string"
|
|
347
|
+
},
|
|
333
348
|
"glob": {
|
|
334
349
|
"description": "Glob filter applied during directory walk.",
|
|
335
350
|
"type": "string"
|
|
@@ -338,6 +353,11 @@
|
|
|
338
353
|
"description": "Return JSON (schema `ast-bro.map.v1`) instead of text.",
|
|
339
354
|
"type": "boolean"
|
|
340
355
|
},
|
|
356
|
+
"max_members": {
|
|
357
|
+
"description": "Cap members shown per type; the output reports what was cut.",
|
|
358
|
+
"minimum": 0,
|
|
359
|
+
"type": "integer"
|
|
360
|
+
},
|
|
341
361
|
"no_attrs": {
|
|
342
362
|
"description": "Hide attributes / decorators.",
|
|
343
363
|
"type": "boolean"
|
|
@@ -479,14 +499,19 @@
|
|
|
479
499
|
}
|
|
480
500
|
},
|
|
481
501
|
"show": {
|
|
482
|
-
"description": "Extract source of one or more symbols from a
|
|
502
|
+
"description": "Extract source of one or more symbols from a file, a directory, or a glob — pass a directory when you know the symbol but not the file. Suffix matching: `TakeDamage`, or `Player.TakeDamage` when ambiguous. For markdown the symbol is a heading or `frontmatter`. Returns text by default; set `json: true` for `ast-bro.show.v2`.",
|
|
483
503
|
"inputSchema": {
|
|
484
504
|
"properties": {
|
|
485
505
|
"json": {
|
|
486
506
|
"type": "boolean"
|
|
487
507
|
},
|
|
508
|
+
"limit": {
|
|
509
|
+
"description": "Cap on rendered bodies when the target is a directory or glob (default 20). The reported total is always exact.",
|
|
510
|
+
"minimum": 0,
|
|
511
|
+
"type": "integer"
|
|
512
|
+
},
|
|
488
513
|
"path": {
|
|
489
|
-
"description": "File to search.",
|
|
514
|
+
"description": "File, directory, or glob to search.",
|
|
490
515
|
"type": "string"
|
|
491
516
|
},
|
|
492
517
|
"symbols": {
|
package/src/cli.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface CliHandlers {
|
|
2
|
-
config(args: string[]): Promise<void>;
|
|
2
|
+
config(args: string[]): Promise<number | undefined> | Promise<void>;
|
|
3
3
|
hook(): Promise<number>;
|
|
4
4
|
installer(args: string[]): Promise<void>;
|
|
5
5
|
mcp(args: string[]): Promise<void>;
|
|
@@ -7,12 +7,12 @@ export interface CliHandlers {
|
|
|
7
7
|
stdout?(text: string): void;
|
|
8
8
|
}
|
|
9
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
|
|
10
|
+
const rootHelp = `ast-mcp - AST-aware Model Context Protocol server\n\nUsage:\n ast-mcp <command> [options]\n\nCommands:\n config Validate, display, or migrate 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
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>
|
|
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> Deprecated root override (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
13
|
|
|
14
14
|
const commandHelp: Record<string, string> = {
|
|
15
|
-
config: `Usage:\n ast-mcp config <validate|show> [options]\n\nValidate
|
|
15
|
+
config: `Usage:\n ast-mcp config <validate|show|migrate> [options]\n\nValidate, display, or migrate layered ast-mcp.toml configuration. Migration previews by default and never rewrites automatically.\n\nOptions:\n -r, --root <path> Deprecated root or migration target directory\n --file <path> Migrate an explicit configuration file\n --global Migrate the user-global configuration\n --to <version> Migration target version (currently 2)\n --check Exit 2 when migration is required\n --write Atomically write the migration\n --no-backup Do not create the default v1 backup\n -h, --help Display help\n`,
|
|
16
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
17
|
install: `Usage:\n ast-mcp install [options]\n\nConfigure ast-mcp for supported hosts.\n\n${installerOptions}`,
|
|
18
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`,
|
|
@@ -91,10 +91,13 @@ async function runConfiguredCommand(
|
|
|
91
91
|
command: string,
|
|
92
92
|
rest: string[],
|
|
93
93
|
handlers: CliHandlers,
|
|
94
|
-
) {
|
|
94
|
+
): Promise<number | undefined> {
|
|
95
95
|
try {
|
|
96
|
-
if (command === "config")
|
|
97
|
-
|
|
96
|
+
if (command === "config") {
|
|
97
|
+
const result = await handlers.config(rest);
|
|
98
|
+
return typeof result === "number" ? result : undefined;
|
|
99
|
+
}
|
|
100
|
+
await handlers.installer([command, ...rest]);
|
|
98
101
|
return undefined;
|
|
99
102
|
} catch (error) {
|
|
100
103
|
if (isCliUsageError(error))
|
package/src/config-cli.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
1
2
|
import path from "node:path";
|
|
2
|
-
import { resolveConfig } from "./config";
|
|
3
|
+
import { globalConfigPath, resolveConfig } from "./config";
|
|
4
|
+
import { migrateConfigSource, writeMigratedConfig } from "./config-migrate";
|
|
3
5
|
|
|
4
6
|
class ConfigurationUsageError extends Error {
|
|
5
7
|
constructor(message: string) {
|
|
@@ -19,29 +21,99 @@ function valueAfter(tokens: string[], index: number) {
|
|
|
19
21
|
return value;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
type ConfigOperation = "validate" | "show" | "migrate";
|
|
25
|
+
|
|
26
|
+
type ConfigOptions = {
|
|
27
|
+
backup: boolean;
|
|
28
|
+
check: boolean;
|
|
29
|
+
file?: string;
|
|
30
|
+
global: boolean;
|
|
31
|
+
root: string;
|
|
32
|
+
write: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function normalizeTokens(args: string[]): string[] {
|
|
36
|
+
return args.flatMap((token) => {
|
|
37
|
+
for (const option of ["root", "file", "to"])
|
|
38
|
+
if (token.startsWith(`--${option}=`))
|
|
39
|
+
return [`--${option}`, token.slice(option.length + 3)];
|
|
40
|
+
return [token];
|
|
29
41
|
});
|
|
30
|
-
|
|
31
|
-
if (operation !== "validate" && operation !== "show")
|
|
32
|
-
throw new ConfigurationUsageError(
|
|
33
|
-
`Expected config validate or config show; received "${operation ?? ""}"`,
|
|
34
|
-
);
|
|
42
|
+
}
|
|
35
43
|
|
|
36
|
-
|
|
44
|
+
function configOperation(token: string | undefined): ConfigOperation {
|
|
45
|
+
if (token === "validate" || token === "show" || token === "migrate")
|
|
46
|
+
return token;
|
|
47
|
+
throw new ConfigurationUsageError(
|
|
48
|
+
`Expected config validate, config show, or config migrate; received "${token ?? ""}"`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function valueOption(
|
|
53
|
+
token: string | undefined,
|
|
54
|
+
tokens: string[],
|
|
55
|
+
index: number,
|
|
56
|
+
options: ConfigOptions,
|
|
57
|
+
): boolean {
|
|
58
|
+
if (token === "--root" || token === "-r")
|
|
59
|
+
options.root = valueAfter(tokens, index);
|
|
60
|
+
else if (token === "--file") options.file = valueAfter(tokens, index);
|
|
61
|
+
else if (token === "--to") {
|
|
62
|
+
if (valueAfter(tokens, index) !== "2")
|
|
63
|
+
throw new ConfigurationUsageError("Only --to 2 is supported");
|
|
64
|
+
} else return false;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function flagOption(
|
|
69
|
+
token: string | undefined,
|
|
70
|
+
options: ConfigOptions,
|
|
71
|
+
): boolean {
|
|
72
|
+
if (token === "--global") options.global = true;
|
|
73
|
+
else if (token === "--check") options.check = true;
|
|
74
|
+
else if (token === "--write") options.write = true;
|
|
75
|
+
else if (token === "--no-backup") options.backup = false;
|
|
76
|
+
else return false;
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function configOptions(tokens: string[]): ConfigOptions {
|
|
81
|
+
const options: ConfigOptions = {
|
|
82
|
+
backup: true,
|
|
83
|
+
check: false,
|
|
84
|
+
global: false,
|
|
85
|
+
root: process.cwd(),
|
|
86
|
+
write: false,
|
|
87
|
+
};
|
|
37
88
|
for (let index = 0; index < tokens.length; index += 1) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
89
|
+
const token = tokens[index];
|
|
90
|
+
if (valueOption(token, tokens, index, options)) index += 1;
|
|
91
|
+
else if (!flagOption(token, options))
|
|
92
|
+
throw new ConfigurationUsageError(`Unknown option: ${token}`);
|
|
42
93
|
}
|
|
94
|
+
return options;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function assertReadOptions(options: ConfigOptions): void {
|
|
98
|
+
if (
|
|
99
|
+
options.file ||
|
|
100
|
+
options.global ||
|
|
101
|
+
options.check ||
|
|
102
|
+
options.write ||
|
|
103
|
+
!options.backup
|
|
104
|
+
)
|
|
105
|
+
throw new ConfigurationUsageError(
|
|
106
|
+
"Migration options require config migrate",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
43
109
|
|
|
44
|
-
|
|
110
|
+
async function runConfigRead(
|
|
111
|
+
operation: Exclude<ConfigOperation, "migrate">,
|
|
112
|
+
options: ConfigOptions,
|
|
113
|
+
write: (text: string) => void,
|
|
114
|
+
): Promise<void> {
|
|
115
|
+
assertReadOptions(options);
|
|
116
|
+
const config = await resolveConfig({ cwd: path.resolve(options.root) });
|
|
45
117
|
const output =
|
|
46
118
|
operation === "show"
|
|
47
119
|
? config
|
|
@@ -49,6 +121,81 @@ export async function runConfigCli(
|
|
|
49
121
|
projectRoot: config.projectRoot,
|
|
50
122
|
sources: config.sources,
|
|
51
123
|
valid: true,
|
|
124
|
+
version: config.version,
|
|
52
125
|
};
|
|
53
126
|
write(`${JSON.stringify(output, null, 2)}\n`);
|
|
54
127
|
}
|
|
128
|
+
|
|
129
|
+
function migrationTarget(options: ConfigOptions): string {
|
|
130
|
+
if (options.file) return path.resolve(options.file);
|
|
131
|
+
if (options.global) return path.resolve(globalConfigPath());
|
|
132
|
+
return path.resolve(options.root, "ast-mcp.toml");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function migrationBackup(
|
|
136
|
+
options: ConfigOptions,
|
|
137
|
+
migration: ReturnType<typeof migrateConfigSource>,
|
|
138
|
+
target: string,
|
|
139
|
+
) {
|
|
140
|
+
if (!options.write || !migration.changed) return undefined;
|
|
141
|
+
return writeMigratedConfig(target, migration.source, options.backup);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function migrationReport(
|
|
145
|
+
options: ConfigOptions,
|
|
146
|
+
migration: ReturnType<typeof migrateConfigSource>,
|
|
147
|
+
target: string,
|
|
148
|
+
backupPath: string | undefined,
|
|
149
|
+
) {
|
|
150
|
+
return {
|
|
151
|
+
backupPath,
|
|
152
|
+
changed: migration.changed,
|
|
153
|
+
file: target,
|
|
154
|
+
fromVersion: migration.fromVersion,
|
|
155
|
+
preview: options.write ? undefined : migration.source,
|
|
156
|
+
toVersion: migration.toVersion,
|
|
157
|
+
warnings: migration.warnings,
|
|
158
|
+
written: options.write && migration.changed,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function runMigration(
|
|
163
|
+
options: ConfigOptions,
|
|
164
|
+
write: (text: string) => void,
|
|
165
|
+
): Promise<number> {
|
|
166
|
+
if (options.file && options.global)
|
|
167
|
+
throw new ConfigurationUsageError("Choose only one of --file or --global");
|
|
168
|
+
if (options.check && options.write)
|
|
169
|
+
throw new ConfigurationUsageError("--check and --write cannot be combined");
|
|
170
|
+
const target = migrationTarget(options);
|
|
171
|
+
const migration = migrateConfigSource(await readFile(target, "utf8"), target);
|
|
172
|
+
const backupPath = await migrationBackup(options, migration, target);
|
|
173
|
+
write(
|
|
174
|
+
`${JSON.stringify(
|
|
175
|
+
migrationReport(options, migration, target, backupPath),
|
|
176
|
+
null,
|
|
177
|
+
2,
|
|
178
|
+
)}\n`,
|
|
179
|
+
);
|
|
180
|
+
return options.check && migration.changed ? 2 : 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function runConfigCli(
|
|
184
|
+
args: string[],
|
|
185
|
+
write: (text: string) => void = stdout.write.bind(stdout),
|
|
186
|
+
): Promise<number | undefined> {
|
|
187
|
+
if (
|
|
188
|
+
args.some(
|
|
189
|
+
(token) =>
|
|
190
|
+
token === "--root" || token === "-r" || token.startsWith("--root="),
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
process.stderr.write(
|
|
194
|
+
"ast-mcp: --root is deprecated; run this command from the project root.\n",
|
|
195
|
+
);
|
|
196
|
+
const tokens = normalizeTokens(args);
|
|
197
|
+
const operation = configOperation(tokens.shift());
|
|
198
|
+
const options = configOptions(tokens);
|
|
199
|
+
if (operation === "migrate") return runMigration(options, write);
|
|
200
|
+
await runConfigRead(operation, options, write);
|
|
201
|
+
}
|