-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroots.ts
More file actions
54 lines (48 loc) · 1.43 KB
/
roots.ts
File metadata and controls
54 lines (48 loc) · 1.43 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
import { execFileSync } from "node:child_process";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { FastMCP } from "fastmcp";
function uriToPath(uri: string): string | null {
if (!uri.startsWith("file://")) return null;
try {
return fileURLToPath(uri);
} catch {
return null;
}
}
function gitTopLevel(localPath: string): string | null {
try {
return execFileSync("git", ["-C", localPath, "rev-parse", "--show-toplevel"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
}).trim();
} catch {
return null;
}
}
export function listFileRoots(server: FastMCP): string[] {
const paths: string[] = [];
const seen = new Set<string>();
for (const session of server.sessions) {
for (const root of session.roots ?? []) {
const path = uriToPath(root.uri);
if (!path || seen.has(path)) continue;
seen.add(path);
paths.push(path);
}
}
return paths;
}
export function normalizeLocalPath(localPath: string): string {
const absolute = resolve(localPath);
return gitTopLevel(absolute) ?? absolute;
}
export function resolveOptionalLocalPath(
server: FastMCP,
explicitLocalPath?: string,
): string | undefined {
const explicit = explicitLocalPath?.trim();
if (explicit) return normalizeLocalPath(explicit);
const primaryRoot = listFileRoots(server)[0];
return primaryRoot ? normalizeLocalPath(primaryRoot) : undefined;
}