@mwillbanks/ast-mcp 0.2.2 → 0.3.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 +183 -137
- package/dist/ast-mcp.js.map +58 -42
- package/package.json +17 -11
- package/src/ast-bro/capability.ts +20 -10
- package/src/ast-bro/tools.json +22 -2
- package/src/cli.ts +10 -7
- package/src/config-cli.ts +166 -19
- package/src/config-migrate.ts +223 -0
- package/src/config-registry.ts +321 -0
- package/src/config-schema-common.ts +81 -0
- package/src/config-v2-schema.ts +97 -0
- package/src/config.ts +346 -148
- package/src/helpers/ast-bro.ts +1 -7
- package/src/helpers/config.ts +7 -0
- 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 +192 -51
- package/src/patch/engine.ts +555 -279
- 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/dependencies.ts +1 -1
- 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 +142 -14
- package/src/runtime/file-rename.ts +5 -2
- package/src/runtime/format.ts +65 -27
- package/src/runtime/path-policy.ts +557 -0
- package/src/runtime/path-utils.ts +40 -0
- package/src/runtime/paths.ts +142 -44
- package/src/server.ts +2 -1
- package/src/tools/ast-bro.ts +196 -75
- package/src/tools/configuration.ts +450 -0
- package/src/tools/configured.ts +49 -21
- package/src/tools/files.ts +168 -61
- package/src/tools/index.ts +2 -0
- package/src/tools/lifecycle.ts +82 -38
- package/templates/AGENTS.md +13 -15
- package/templates/skills/ast-mcp/SKILL.md +28 -20
- package/templates/skills/ast-mcp/evals/evals.json +221 -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 +13 -7
- package/templates/skills/ast-mcp/scripts/check-install.ts +8 -17
- package/src/ast-bro/rewrite-capability.ts +0 -9
package/src/patch/languages.ts
CHANGED
|
@@ -13,6 +13,8 @@ const languages: Record<string, string> = {
|
|
|
13
13
|
".hpp": "cpp",
|
|
14
14
|
".java": "java",
|
|
15
15
|
".js": "javascript",
|
|
16
|
+
".json": "json",
|
|
17
|
+
".jsonc": "json",
|
|
16
18
|
".jsx": "javascript",
|
|
17
19
|
".kt": "kotlin",
|
|
18
20
|
".kts": "kotlin",
|
|
@@ -28,9 +30,16 @@ const languages: Record<string, string> = {
|
|
|
28
30
|
".rs": "rust",
|
|
29
31
|
".scala": "scala",
|
|
30
32
|
".sql": "sql",
|
|
33
|
+
".toml": "toml",
|
|
31
34
|
".ts": "typescript",
|
|
32
35
|
".tsx": "tsx",
|
|
36
|
+
".yaml": "yaml",
|
|
37
|
+
".yml": "yaml",
|
|
33
38
|
};
|
|
34
39
|
export function detectAstLanguage(filePath: string): string | undefined {
|
|
35
40
|
return languages[path.extname(filePath).toLowerCase()];
|
|
36
41
|
}
|
|
42
|
+
|
|
43
|
+
export function languageForExtension(extension: string): string | undefined {
|
|
44
|
+
return languages[extension.toLowerCase()];
|
|
45
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { applyAiderBlock } from "../aider";
|
|
2
|
+
import type { PatchStrategyAdapter, PatchStrategyContext } from "../strategy";
|
|
3
|
+
|
|
4
|
+
function validate(context: PatchStrategyContext): void {
|
|
5
|
+
if (!context.capabilities.effective.patch.includes("aider_block"))
|
|
6
|
+
throw Object.assign(
|
|
7
|
+
new Error(
|
|
8
|
+
`patchStrategy 'aider_block' is not available for this file; available strategies: ${context.capabilities.effective.patch.join(", ") || "none"}`,
|
|
9
|
+
),
|
|
10
|
+
{
|
|
11
|
+
code: "patch_strategy_unavailable",
|
|
12
|
+
details: { capabilities: context.capabilities },
|
|
13
|
+
retryable: true,
|
|
14
|
+
},
|
|
15
|
+
);
|
|
16
|
+
if (context.aiderBlocks.length === 0 || context.astRules.length > 0)
|
|
17
|
+
throw Object.assign(
|
|
18
|
+
new Error(
|
|
19
|
+
"patchStrategy 'aider_block' requires aiderBlocks and no astRules",
|
|
20
|
+
),
|
|
21
|
+
{ code: "patch_strategy_arguments", retryable: true },
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function assertAiderMatcher(
|
|
26
|
+
context: PatchStrategyContext,
|
|
27
|
+
method: string,
|
|
28
|
+
operation: number,
|
|
29
|
+
): void {
|
|
30
|
+
if (method === "append") return;
|
|
31
|
+
if (context.capabilities.effective.aiderMatchers.includes(method as never))
|
|
32
|
+
return;
|
|
33
|
+
throw Object.assign(
|
|
34
|
+
new Error(`Aider matcher '${method}' is disabled by policy`),
|
|
35
|
+
{
|
|
36
|
+
code: "aider_matcher_disabled",
|
|
37
|
+
details: {
|
|
38
|
+
capabilities: context.capabilities,
|
|
39
|
+
matcher: method,
|
|
40
|
+
operation,
|
|
41
|
+
},
|
|
42
|
+
retryable: true,
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function throwAiderFailure(
|
|
48
|
+
error: unknown,
|
|
49
|
+
operation: number,
|
|
50
|
+
search: string,
|
|
51
|
+
): never {
|
|
52
|
+
if (typeof error === "object" && error && "code" in error) throw error;
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
const code = /ambiguous|multiple/i.test(message)
|
|
55
|
+
? "aider_ambiguous"
|
|
56
|
+
: "aider_no_match";
|
|
57
|
+
throw Object.assign(error instanceof Error ? error : new Error(message), {
|
|
58
|
+
code,
|
|
59
|
+
details: { operation, searchPreview: search.slice(0, 200) },
|
|
60
|
+
retryable: true,
|
|
61
|
+
suggestedNextCall: "file_read",
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function applyConfiguredAiderBlock(
|
|
66
|
+
context: PatchStrategyContext,
|
|
67
|
+
content: string,
|
|
68
|
+
block: PatchStrategyContext["aiderBlocks"][number],
|
|
69
|
+
operation: number,
|
|
70
|
+
) {
|
|
71
|
+
try {
|
|
72
|
+
const result = applyAiderBlock(content, block.search, block.replace);
|
|
73
|
+
assertAiderMatcher(context, result.method, operation);
|
|
74
|
+
return result;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return throwAiderFailure(error, operation, block.search);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function prepareAiderBlocks(context: PatchStrategyContext) {
|
|
81
|
+
let content = context.original;
|
|
82
|
+
const methods: string[] = [];
|
|
83
|
+
for (const [index, block] of context.aiderBlocks.entries()) {
|
|
84
|
+
const result = applyConfiguredAiderBlock(
|
|
85
|
+
context,
|
|
86
|
+
content,
|
|
87
|
+
block,
|
|
88
|
+
index + 1,
|
|
89
|
+
);
|
|
90
|
+
content = result.content;
|
|
91
|
+
methods.push(result.method);
|
|
92
|
+
}
|
|
93
|
+
return { content, methods };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const aiderBlockStrategy: PatchStrategyAdapter = {
|
|
97
|
+
name: "aider_block",
|
|
98
|
+
async prepare(context) {
|
|
99
|
+
validate(context);
|
|
100
|
+
const { content, methods } = prepareAiderBlocks(context);
|
|
101
|
+
return {
|
|
102
|
+
candidate: content,
|
|
103
|
+
metadata: {
|
|
104
|
+
matchMethods: methods,
|
|
105
|
+
operations: context.aiderBlocks.length,
|
|
106
|
+
strategy: "aider_block",
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { callAstBro } from "../../ast-bro/client";
|
|
5
|
+
import { parseAstBroJson } from "../../ast-bro/result";
|
|
6
|
+
import type {
|
|
7
|
+
AstRule,
|
|
8
|
+
PatchStrategyAdapter,
|
|
9
|
+
PatchStrategyContext,
|
|
10
|
+
} from "../strategy";
|
|
11
|
+
|
|
12
|
+
function validate(
|
|
13
|
+
context: PatchStrategyContext,
|
|
14
|
+
): asserts context is PatchStrategyContext & { language: string } {
|
|
15
|
+
if (
|
|
16
|
+
!context.language ||
|
|
17
|
+
!context.capabilities.effective.patch.includes("ast")
|
|
18
|
+
)
|
|
19
|
+
throw Object.assign(
|
|
20
|
+
new Error(
|
|
21
|
+
`patchStrategy 'ast' is not available for this file; available strategies: ${context.capabilities.effective.patch.join(", ") || "none"}`,
|
|
22
|
+
),
|
|
23
|
+
{
|
|
24
|
+
code: "patch_strategy_unavailable",
|
|
25
|
+
details: { capabilities: context.capabilities },
|
|
26
|
+
retryable: true,
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
if (context.astRules.length === 0 || context.aiderBlocks.length > 0)
|
|
30
|
+
throw Object.assign(
|
|
31
|
+
new Error("patchStrategy 'ast' requires astRules and no aiderBlocks"),
|
|
32
|
+
{ code: "patch_strategy_arguments", retryable: true },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function temporary(filePath: string): string {
|
|
37
|
+
const extension = path.extname(filePath);
|
|
38
|
+
const base = extension ? filePath.slice(0, -extension.length) : filePath;
|
|
39
|
+
return `${base}.ast-mcp-patch-${randomUUID()}${extension}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function previewRule(
|
|
43
|
+
context: PatchStrategyContext & { language: string },
|
|
44
|
+
next: string,
|
|
45
|
+
rule: AstRule,
|
|
46
|
+
): Promise<number> {
|
|
47
|
+
const preview = parseAstBroJson(
|
|
48
|
+
await callAstBro(
|
|
49
|
+
"run",
|
|
50
|
+
{
|
|
51
|
+
json: true,
|
|
52
|
+
lang: context.language,
|
|
53
|
+
paths: [next],
|
|
54
|
+
pattern: rule.pattern,
|
|
55
|
+
},
|
|
56
|
+
path.dirname(context.filePath),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
if (preview.error_count)
|
|
60
|
+
throw Object.assign(
|
|
61
|
+
new Error(`ast-bro preview failed with ${preview.error_count} errors`),
|
|
62
|
+
{
|
|
63
|
+
code: "ast_preview_error",
|
|
64
|
+
details: { errors: preview.errors },
|
|
65
|
+
retryable: true,
|
|
66
|
+
suggestedNextCall: "run",
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
const locations = Array.isArray(preview.matches)
|
|
70
|
+
? preview.matches.slice(0, 10).map((match) => {
|
|
71
|
+
const value = match as Record<string, unknown>;
|
|
72
|
+
return {
|
|
73
|
+
endCol: value.end_col,
|
|
74
|
+
endLine: value.end_line,
|
|
75
|
+
file: value.file,
|
|
76
|
+
matchedText:
|
|
77
|
+
typeof value.matched_text === "string"
|
|
78
|
+
? value.matched_text.slice(0, 200)
|
|
79
|
+
: undefined,
|
|
80
|
+
startCol: value.start_col,
|
|
81
|
+
startLine: value.start_line,
|
|
82
|
+
};
|
|
83
|
+
})
|
|
84
|
+
: [];
|
|
85
|
+
const matches = Array.isArray(preview.matches) ? preview.matches.length : 0;
|
|
86
|
+
const expected = rule.expectedMatches ?? 1;
|
|
87
|
+
if (matches !== expected)
|
|
88
|
+
throw Object.assign(
|
|
89
|
+
new Error(`AST rule matched ${matches} nodes; expected ${expected}`),
|
|
90
|
+
{
|
|
91
|
+
code: "ast_match_count",
|
|
92
|
+
details: { expected, locations, matches },
|
|
93
|
+
retryable: true,
|
|
94
|
+
suggestedNextCall: "run",
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
if (expected !== 1)
|
|
98
|
+
throw Object.assign(
|
|
99
|
+
new Error(
|
|
100
|
+
"ast-bro run rewrites only the first match per file; narrow the AST rule to exactly one node",
|
|
101
|
+
),
|
|
102
|
+
{
|
|
103
|
+
code: "ast_rewrite_cardinality",
|
|
104
|
+
details: { expected, locations, matches },
|
|
105
|
+
retryable: true,
|
|
106
|
+
suggestedNextCall: "run",
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
return matches;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function rewriteRule(
|
|
113
|
+
context: PatchStrategyContext & { language: string },
|
|
114
|
+
next: string,
|
|
115
|
+
rule: AstRule,
|
|
116
|
+
): Promise<void> {
|
|
117
|
+
const rewritten = parseAstBroJson(
|
|
118
|
+
await callAstBro(
|
|
119
|
+
"run",
|
|
120
|
+
{
|
|
121
|
+
json: true,
|
|
122
|
+
lang: context.language,
|
|
123
|
+
paths: [next],
|
|
124
|
+
pattern: rule.pattern,
|
|
125
|
+
rewrite: rule.fix,
|
|
126
|
+
write: true,
|
|
127
|
+
},
|
|
128
|
+
path.dirname(context.filePath),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
if (
|
|
132
|
+
rewritten.error_count ||
|
|
133
|
+
rewritten.rewrite_count !== 1 ||
|
|
134
|
+
rewritten.files?.[0]?.status !== "rewritten"
|
|
135
|
+
)
|
|
136
|
+
throw new Error("ast-bro run did not rewrite exactly one file");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const astStrategy: PatchStrategyAdapter = {
|
|
140
|
+
name: "ast",
|
|
141
|
+
async prepare(context) {
|
|
142
|
+
validate(context);
|
|
143
|
+
const next = temporary(context.filePath);
|
|
144
|
+
try {
|
|
145
|
+
await writeFile(next, context.original, {
|
|
146
|
+
encoding: "utf8",
|
|
147
|
+
flag: "wx",
|
|
148
|
+
mode: context.mode,
|
|
149
|
+
});
|
|
150
|
+
let matches = 0;
|
|
151
|
+
for (const rule of context.astRules) {
|
|
152
|
+
matches += await previewRule(context, next, rule);
|
|
153
|
+
await rewriteRule(context, next, rule);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
candidate: await readFile(next, "utf8"),
|
|
157
|
+
metadata: {
|
|
158
|
+
engine: "ast-bro.run",
|
|
159
|
+
matches,
|
|
160
|
+
operations: context.astRules.length,
|
|
161
|
+
strategy: "ast",
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
} finally {
|
|
165
|
+
try {
|
|
166
|
+
await unlink(next);
|
|
167
|
+
} catch {}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FileCapabilities,
|
|
3
|
+
PatchStrategyName,
|
|
4
|
+
} from "../runtime/file-capabilities";
|
|
5
|
+
import { aiderBlockStrategy } from "./strategy/aiderBlock";
|
|
6
|
+
import { astStrategy } from "./strategy/ast";
|
|
7
|
+
|
|
8
|
+
export interface AstRule {
|
|
9
|
+
expectedMatches?: number;
|
|
10
|
+
fix: string;
|
|
11
|
+
pattern: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AiderBlock {
|
|
15
|
+
replace: string;
|
|
16
|
+
search: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PatchStrategyContext {
|
|
20
|
+
aiderBlocks: AiderBlock[];
|
|
21
|
+
astRules: AstRule[];
|
|
22
|
+
capabilities: FileCapabilities;
|
|
23
|
+
filePath: string;
|
|
24
|
+
language?: string;
|
|
25
|
+
mode: number;
|
|
26
|
+
original: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PatchStrategyMetadata {
|
|
30
|
+
engine?: string;
|
|
31
|
+
matches?: number;
|
|
32
|
+
matchMethods?: string[];
|
|
33
|
+
operations: number;
|
|
34
|
+
strategy: PatchStrategyName;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface PreparedStrategyPatch {
|
|
38
|
+
candidate: string;
|
|
39
|
+
metadata: PatchStrategyMetadata;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PatchStrategyAdapter {
|
|
43
|
+
readonly name: PatchStrategyName;
|
|
44
|
+
prepare(context: PatchStrategyContext): Promise<PreparedStrategyPatch>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const adapters: Record<PatchStrategyName, PatchStrategyAdapter> = {
|
|
48
|
+
aider_block: aiderBlockStrategy,
|
|
49
|
+
ast: astStrategy,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function patchStrategyAdapter(
|
|
53
|
+
strategy: PatchStrategyName | undefined,
|
|
54
|
+
): PatchStrategyAdapter {
|
|
55
|
+
if (!strategy)
|
|
56
|
+
throw Object.assign(new Error("patchStrategy is required"), {
|
|
57
|
+
code: "patch_strategy_required",
|
|
58
|
+
retryable: true,
|
|
59
|
+
});
|
|
60
|
+
return adapters[strategy];
|
|
61
|
+
}
|