-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-presets-tool.ts
More file actions
137 lines (132 loc) · 4.09 KB
/
list-presets-tool.ts
File metadata and controls
137 lines (132 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { join } from "node:path";
import type { FastMCP } from "fastmcp";
import { gitTopLevel } from "./git.js";
import { jsonRespond, spreadDefined } from "./json.js";
import { loadPresetsFromGitTop, PRESET_FILE_PATH, presetLoadErrorPayload } from "./presets.js";
import { requireGitAndRoots } from "./roots.js";
import { WorkspacePickSchema } from "./schemas.js";
export function registerListPresetsTool(server: FastMCP): void {
server.addTool({
name: "list_presets",
description: "List presets from .rethunk/git-mcp-presets.json.",
annotations: {
readOnlyHint: true,
},
parameters: WorkspacePickSchema.pick({
workspaceRoot: true,
rootIndex: true,
allWorkspaceRoots: true,
absoluteGitRoots: true,
format: true,
}),
execute: async (args) => {
const pre = requireGitAndRoots(server, args, undefined);
if (!pre.ok) {
return jsonRespond(pre.error);
}
const out: {
workspaceRoot: string;
gitTop: string | null;
presetFile: string;
fileExists: boolean;
presetSchemaVersion?: string;
presets: {
name: string;
nestedRootsCount: number;
parityPairsCount: number;
workspaceRootHint?: string;
}[];
error?: Record<string, unknown>;
}[] = [];
for (const ws of pre.roots) {
const top = gitTopLevel(ws);
const presetFile = top ? join(top, PRESET_FILE_PATH) : join(ws, PRESET_FILE_PATH);
if (!top) {
out.push({
workspaceRoot: ws,
gitTop: null,
presetFile,
fileExists: false,
presets: [],
error: { error: "not_a_git_repository", path: ws },
});
continue;
}
const loaded = loadPresetsFromGitTop(top);
if (!loaded.ok) {
if (loaded.reason === "missing") {
out.push({
workspaceRoot: ws,
gitTop: top,
presetFile,
fileExists: false,
presets: [],
});
} else {
out.push({
workspaceRoot: ws,
gitTop: top,
presetFile,
fileExists: true,
presets: [],
error: presetLoadErrorPayload(top, loaded),
});
}
continue;
}
const presets = Object.entries(loaded.data).map(([name, e]) => ({
name,
nestedRootsCount: e.nestedRoots?.length ?? 0,
parityPairsCount: e.parityPairs?.length ?? 0,
...spreadDefined(
"workspaceRootHint",
e.workspaceRootHint ? e.workspaceRootHint : undefined,
),
}));
out.push({
workspaceRoot: ws,
gitTop: top,
presetFile,
fileExists: true,
...spreadDefined("presetSchemaVersion", loaded.schemaVersion),
presets,
});
}
if (args.format === "json") {
return jsonRespond({ roots: out });
}
const lines: string[] = ["# Git MCP presets", ""];
for (const row of out) {
lines.push(
`## ${row.workspaceRoot}`,
`git_top: ${row.gitTop ?? "(none)"}`,
`preset_file: ${row.presetFile}`,
"",
);
if (row.error) {
lines.push("```json", JSON.stringify(row.error, null, 2), "```", "");
continue;
}
if (!row.fileExists) {
lines.push("(no preset file)", "");
continue;
}
if (row.presets.length === 0) {
lines.push("(empty preset file)", "");
continue;
}
if (row.presetSchemaVersion !== undefined) {
lines.push(`preset_schema_version: ${row.presetSchemaVersion}`, "");
}
for (const p of row.presets) {
lines.push(
`- **${p.name}**: nestedRoots=${p.nestedRootsCount}, parityPairs=${p.parityPairsCount}` +
(p.workspaceRootHint ? `, hint=${p.workspaceRootHint}` : ""),
);
}
lines.push("");
}
return lines.join("\n");
},
});
}