forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
677 lines (582 loc) · 20.8 KB
/
Copy pathconfig.ts
File metadata and controls
677 lines (582 loc) · 20.8 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// config.ts - Config loading with import support
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { getAgentPath } from "./agent-dir.ts";
import type { McpConfig, ServerEntry, McpSettings, ImportKind, ServerProvenance } from "./types.ts";
const GENERIC_GLOBAL_CONFIG_PATH = join(homedir(), ".config", "mcp", "mcp.json");
const PROJECT_CONFIG_NAME = ".mcp.json";
const PROJECT_PI_CONFIG_NAME = ".pi/mcp.json";
const REPOPROMPT_BINARY_CANDIDATES = [
join(homedir(), "RepoPrompt", "repoprompt_cli"),
"/Applications/Repo Prompt.app/Contents/MacOS/repoprompt-mcp",
];
const IMPORT_PATHS: Record<ImportKind, string[]> = {
cursor: [join(homedir(), ".cursor", "mcp.json")],
"claude-code": [
join(homedir(), ".claude", "mcp.json"),
join(homedir(), ".claude.json"),
join(homedir(), ".claude", "claude_desktop_config.json"),
],
"claude-desktop": [join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json")],
codex: [join(homedir(), ".codex", "config.json")],
windsurf: [join(homedir(), ".windsurf", "mcp.json")],
vscode: [".vscode/mcp.json"],
};
interface ConfigSourceSpec {
id: "shared-global" | "pi-global" | "shared-project" | "pi-project";
label: string;
readPath: string;
writePath: string;
kind: "user" | "project" | "import";
importKind?: string;
shared: boolean;
scope: "global" | "project";
}
export interface ConfigDiscoveryPath {
label: string;
path: string;
exists: boolean;
}
export interface DiscoveredImportConfig {
kind: ImportKind;
path: string;
}
export interface ConfigDiscoverySource extends ConfigDiscoveryPath {
id: ConfigSourceSpec["id"];
scope: ConfigSourceSpec["scope"];
kind: "shared" | "pi";
serverCount: number;
}
export interface ImportConfigSummary extends DiscoveredImportConfig {
serverCount: number;
}
export interface RepoPromptDiscovery {
configured: boolean;
configuredPath?: string;
executablePath?: string;
targetPath?: string;
serverName?: string;
entry?: ServerEntry;
}
export interface McpDiscoverySummary {
sources: ConfigDiscoverySource[];
imports: ImportConfigSummary[];
hasAnyConfig: boolean;
hasAnyDetectedPaths: boolean;
hasSharedServers: boolean;
hasPiOwnedServers: boolean;
totalServerCount: number;
fingerprint: string;
repoPrompt: RepoPromptDiscovery;
}
export interface ConfigWritePreview {
path: string;
existed: boolean;
changed: boolean;
beforeText: string;
afterText: string;
diffText: string;
}
export function getPiGlobalConfigPath(overridePath?: string): string {
return overridePath ? resolve(overridePath) : getAgentPath("mcp.json");
}
export function getGenericGlobalConfigPath(): string {
return GENERIC_GLOBAL_CONFIG_PATH;
}
export function getProjectConfigPath(cwd = process.cwd()): string {
return resolve(cwd, PROJECT_CONFIG_NAME);
}
export function getProjectPiConfigPath(cwd = process.cwd()): string {
return resolve(cwd, PROJECT_PI_CONFIG_NAME);
}
export function getConfigDiscoveryPaths(overridePath?: string, cwd = process.cwd()): ConfigDiscoveryPath[] {
return getConfigSources(overridePath, cwd).map((source) => ({
label: source.label,
path: source.readPath,
exists: existsSync(source.readPath),
}));
}
export function findAvailableImportConfigs(cwd = process.cwd()): DiscoveredImportConfig[] {
const discovered: DiscoveredImportConfig[] = [];
for (const importKind of Object.keys(IMPORT_PATHS) as ImportKind[]) {
const importPath = resolveImportPath(importKind, cwd);
if (importPath) {
discovered.push({ kind: importKind, path: importPath });
}
}
return discovered;
}
export function getMcpDiscoverySummary(overridePath?: string, cwd = process.cwd()): McpDiscoverySummary {
const sources = getConfigSources(overridePath, cwd).map((source) => {
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
return {
id: source.id,
label: source.label,
path: source.readPath,
exists: existsSync(source.readPath),
scope: source.scope,
kind: source.shared ? "shared" : "pi",
serverCount: loaded ? Object.keys(loaded.mcpServers).length : 0,
} satisfies ConfigDiscoverySource;
});
const imports = (Object.keys(IMPORT_PATHS) as ImportKind[])
.map((kind) => {
const path = resolveImportPath(kind, cwd);
if (!path) return null;
return {
kind,
path,
serverCount: getImportServerCount(kind, path),
} satisfies ImportConfigSummary;
})
.filter((value): value is ImportConfigSummary => value !== null);
const totalServerCount = sources.reduce((sum, source) => sum + source.serverCount, 0);
const hasSharedServers = sources.some((source) => source.kind === "shared" && source.serverCount > 0);
const hasPiOwnedServers = sources.some((source) => source.kind === "pi" && source.serverCount > 0);
const hasAnyDetectedPaths = sources.some((source) => source.exists) || imports.length > 0;
const hasAnyConfig = totalServerCount > 0 || imports.some((entry) => entry.serverCount > 0) || hasAnyDetectedPaths;
const summaryWithoutRepoPrompt = {
sources,
imports,
hasAnyConfig,
hasAnyDetectedPaths,
hasSharedServers,
hasPiOwnedServers,
totalServerCount,
};
const fingerprint = JSON.stringify({
sources: sources.map((source) => [source.id, source.exists, source.serverCount]),
imports: imports.map((entry) => [entry.kind, entry.path, entry.serverCount]),
});
return {
...summaryWithoutRepoPrompt,
fingerprint,
repoPrompt: detectRepoPrompt(summaryWithoutRepoPrompt, cwd),
};
}
export function loadMcpConfig(overridePath?: string, cwd = process.cwd()): McpConfig {
let config: McpConfig = { mcpServers: {} };
for (const source of getConfigSources(overridePath, cwd)) {
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
if (!loaded) continue;
config = mergeConfigs(config, expandImports(loaded, cwd));
}
return config;
}
function getConfigSources(overridePath?: string, cwd = process.cwd()): ConfigSourceSpec[] {
const userPath = getPiGlobalConfigPath(overridePath);
const projectPath = getProjectConfigPath(cwd);
const projectPiPath = getProjectPiConfigPath(cwd);
const sources: ConfigSourceSpec[] = [];
if (GENERIC_GLOBAL_CONFIG_PATH !== userPath) {
sources.push({
id: "shared-global",
label: "user-global standard MCP",
readPath: GENERIC_GLOBAL_CONFIG_PATH,
writePath: userPath,
kind: "import",
importKind: "global MCP config",
shared: true,
scope: "global",
});
}
sources.push({
id: "pi-global",
label: "Pi global override",
readPath: userPath,
writePath: userPath,
kind: "user",
shared: false,
scope: "global",
});
if (projectPath !== userPath) {
sources.push({
id: "shared-project",
label: "project standard MCP",
readPath: projectPath,
writePath: projectPath,
kind: "project",
shared: true,
scope: "project",
});
}
if (projectPiPath !== userPath && projectPiPath !== projectPath) {
sources.push({
id: "pi-project",
label: "project Pi override",
readPath: projectPiPath,
writePath: projectPiPath,
kind: "project",
shared: false,
scope: "project",
});
}
return sources;
}
function mergeConfigs(base: McpConfig, next: McpConfig): McpConfig {
return {
mcpServers: mergeServerMaps(base.mcpServers, next.mcpServers),
imports: mergeImports(base.imports, next.imports),
settings: next.settings ? { ...base.settings, ...next.settings } : base.settings,
};
}
function mergeServerMaps(
base: Record<string, ServerEntry>,
next: Record<string, ServerEntry>,
): Record<string, ServerEntry> {
const merged = { ...base };
for (const [name, definition] of Object.entries(next)) {
merged[name] = { ...(merged[name] ?? {}), ...definition };
}
return merged;
}
function mergeImports(left: ImportKind[] | undefined, right: ImportKind[] | undefined): ImportKind[] | undefined {
const merged = [...(left ?? []), ...(right ?? [])];
if (merged.length === 0) return undefined;
return [...new Set(merged)];
}
function expandImports(config: McpConfig, cwd = process.cwd()): McpConfig {
if (!config.imports?.length) return config;
const importedServers: Record<string, ServerEntry> = {};
for (const importKind of config.imports) {
const importPath = resolveImportPath(importKind, cwd);
if (!importPath) continue;
try {
const imported = JSON.parse(readFileSync(importPath, "utf-8"));
const servers = extractServers(imported, importKind);
for (const [name, definition] of Object.entries(servers)) {
if (!importedServers[name]) {
importedServers[name] = definition;
}
}
} catch (error) {
console.warn(`Failed to import MCP config from ${importKind}:`, error);
}
}
return {
imports: config.imports,
settings: config.settings,
mcpServers: mergeServerMaps(importedServers, config.mcpServers),
};
}
function resolveImportPath(importKind: ImportKind, cwd = process.cwd()): string | null {
const candidates = IMPORT_PATHS[importKind] ?? [];
for (const candidate of candidates) {
const fullPath = candidate.startsWith(".") ? resolve(cwd, candidate) : candidate;
if (existsSync(fullPath)) {
return fullPath;
}
}
return null;
}
function getImportServerCount(importKind: ImportKind, path: string): number {
try {
const raw = JSON.parse(readFileSync(path, "utf-8"));
return Object.keys(extractServers(raw, importKind)).length;
} catch {
return 0;
}
}
function readValidatedConfig(path: string, label: string): McpConfig | null {
if (!existsSync(path)) return null;
try {
return validateConfig(JSON.parse(readFileSync(path, "utf-8")));
} catch (error) {
console.warn(`Failed to load ${label}:`, error);
return null;
}
}
function validateConfig(raw: unknown): McpConfig {
if (!raw || typeof raw !== "object") {
return { mcpServers: {} };
}
const obj = raw as Record<string, unknown>;
const servers = obj.mcpServers ?? obj["mcp-servers"] ?? {};
if (typeof servers !== "object" || servers === null || Array.isArray(servers)) {
return { mcpServers: {} };
}
return {
mcpServers: servers as Record<string, ServerEntry>,
imports: Array.isArray(obj.imports) ? (obj.imports as ImportKind[]) : undefined,
settings: obj.settings as McpSettings | undefined,
};
}
function extractServers(config: unknown, kind: ImportKind): Record<string, ServerEntry> {
if (!config || typeof config !== "object") return {};
const obj = config as Record<string, unknown>;
let servers: unknown;
switch (kind) {
case "claude-desktop":
case "claude-code":
case "codex":
servers = obj.mcpServers;
break;
case "cursor":
case "windsurf":
case "vscode":
servers = obj.mcpServers ?? obj["mcp-servers"];
break;
default:
return {};
}
if (!servers || typeof servers !== "object" || Array.isArray(servers)) {
return {};
}
return servers as Record<string, ServerEntry>;
}
function serializeRawConfig(raw: Record<string, unknown>): string {
return `${JSON.stringify(raw, null, 2)}\n`;
}
function buildUnifiedDiff(beforeText: string, afterText: string): string {
if (beforeText === afterText) return "(no changes)";
const before = beforeText.split("\n");
const after = afterText.split("\n");
const rows = before.length;
const cols = after.length;
const lcs = Array.from({ length: rows + 1 }, () => Array<number>(cols + 1).fill(0));
for (let i = rows - 1; i >= 0; i--) {
for (let j = cols - 1; j >= 0; j--) {
lcs[i][j] = before[i] === after[j]
? lcs[i + 1][j + 1] + 1
: Math.max(lcs[i + 1][j], lcs[i][j + 1]);
}
}
const lines: string[] = ["--- before", "+++ after"];
let i = 0;
let j = 0;
while (i < rows || j < cols) {
if (i < rows && j < cols && before[i] === after[j]) {
lines.push(` ${before[i]}`);
i++;
j++;
continue;
}
if (j < cols && (i === rows || lcs[i][j + 1] >= lcs[i + 1][j])) {
lines.push(`+ ${after[j]}`);
j++;
continue;
}
if (i < rows) {
lines.push(`- ${before[i]}`);
i++;
}
}
return lines.join("\n");
}
function buildConfigWritePreview(filePath: string, nextRaw: Record<string, unknown>): ConfigWritePreview {
const existed = existsSync(filePath);
const beforeRaw = readRawConfigObject(filePath);
const beforeText = existed ? serializeRawConfig(beforeRaw) : "";
const afterText = serializeRawConfig(nextRaw);
return {
path: filePath,
existed,
changed: beforeText !== afterText,
beforeText,
afterText,
diffText: buildUnifiedDiff(beforeText, afterText),
};
}
function readRawConfigObject(filePath: string): Record<string, unknown> {
if (!existsSync(filePath)) return {};
try {
const raw = JSON.parse(readFileSync(filePath, "utf-8"));
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw as Record<string, unknown> : {};
} catch {
return {};
}
}
function writeRawConfigObject(filePath: string, raw: Record<string, unknown>): void {
mkdirSync(dirname(filePath), { recursive: true });
const tmpPath = `${filePath}.${process.pid}.tmp`;
writeFileSync(tmpPath, `${JSON.stringify(raw, null, 2)}\n`, "utf-8");
renameSync(tmpPath, filePath);
}
function getServersObject(raw: Record<string, unknown>): Record<string, ServerEntry> {
const existing = raw.mcpServers ?? raw["mcp-servers"] ?? {};
if (!existing || typeof existing !== "object" || Array.isArray(existing)) {
return {};
}
return existing as Record<string, ServerEntry>;
}
function setServersObject(raw: Record<string, unknown>, servers: Record<string, ServerEntry>): void {
delete raw["mcp-servers"];
raw.mcpServers = servers;
}
function isRepoPromptServer(name: string, entry: ServerEntry): boolean {
const normalizedName = name.toLowerCase();
if (normalizedName.includes("repoprompt") || normalizedName === "rp") {
return true;
}
const command = entry.command?.toLowerCase() ?? "";
if (command.includes("repoprompt") || command.includes("rp-mcp") || command.endsWith("repoprompt_cli")) {
return true;
}
return (entry.args ?? []).some((arg) => typeof arg === "string" && arg.toLowerCase().includes("repoprompt"));
}
function findProjectRoot(cwd = process.cwd()): string | null {
let current = resolve(cwd);
while (true) {
if (
existsSync(join(current, ".git"))
|| existsSync(join(current, "package.json"))
|| existsSync(join(current, PROJECT_CONFIG_NAME))
|| existsSync(join(current, ".pi"))
) {
return current;
}
const parent = dirname(current);
if (parent === current) return null;
current = parent;
}
}
function buildRepoPromptEntry(executablePath: string): ServerEntry {
return {
command: executablePath,
args: [],
lifecycle: "lazy",
};
}
function detectRepoPrompt(summary: Omit<McpDiscoverySummary, "fingerprint" | "repoPrompt">, cwd = process.cwd()): RepoPromptDiscovery {
for (const source of summary.sources) {
if (source.kind !== "shared" || source.serverCount === 0) continue;
const config = readValidatedConfig(source.path, `MCP config from ${source.path}`);
if (!config) continue;
for (const [name, entry] of Object.entries(config.mcpServers)) {
if (isRepoPromptServer(name, entry)) {
return { configured: true, configuredPath: source.path };
}
}
}
const executablePath = REPOPROMPT_BINARY_CANDIDATES.find((candidate) => existsSync(candidate));
if (!executablePath) {
return { configured: false };
}
const projectRoot = findProjectRoot(cwd);
const targetPath = projectRoot ? join(projectRoot, PROJECT_CONFIG_NAME) : GENERIC_GLOBAL_CONFIG_PATH;
return {
configured: false,
executablePath,
targetPath,
serverName: "repoprompt",
entry: buildRepoPromptEntry(executablePath),
};
}
export function previewCompatibilityImports(importKinds: ImportKind[], overridePath?: string): ConfigWritePreview {
const targetPath = getPiGlobalConfigPath(overridePath);
const raw = readRawConfigObject(targetPath);
const currentImports = Array.isArray(raw.imports) ? raw.imports.filter((value): value is ImportKind => typeof value === "string") : [];
const merged = [...new Set([...currentImports, ...importKinds])];
const nextRaw = { ...raw, imports: merged };
setServersObject(nextRaw, getServersObject(nextRaw));
return buildConfigWritePreview(targetPath, nextRaw);
}
export function ensureCompatibilityImports(importKinds: ImportKind[], overridePath?: string): { path: string; added: ImportKind[] } {
const targetPath = getPiGlobalConfigPath(overridePath);
const raw = readRawConfigObject(targetPath);
const currentImports = Array.isArray(raw.imports) ? raw.imports.filter((value): value is ImportKind => typeof value === "string") : [];
const merged = [...new Set([...currentImports, ...importKinds])];
const added = merged.filter((kind) => !currentImports.includes(kind));
if (added.length === 0) {
return { path: targetPath, added: [] };
}
raw.imports = merged;
const servers = getServersObject(raw);
setServersObject(raw, servers);
writeRawConfigObject(targetPath, raw);
return { path: targetPath, added };
}
export function buildStarterProjectConfig(): McpConfig {
return {
mcpServers: {},
};
}
export function previewStarterProjectConfig(cwd = process.cwd()): ConfigWritePreview {
const targetPath = getProjectConfigPath(cwd);
const nextRaw = { mcpServers: buildStarterProjectConfig().mcpServers };
return buildConfigWritePreview(targetPath, nextRaw);
}
export function writeStarterProjectConfig(cwd = process.cwd()): string {
const targetPath = getProjectConfigPath(cwd);
const raw = { mcpServers: buildStarterProjectConfig().mcpServers };
writeRawConfigObject(targetPath, raw);
return targetPath;
}
export function previewSharedServerEntry(filePath: string, serverName: string, entry: ServerEntry): ConfigWritePreview {
const raw = readRawConfigObject(filePath);
const nextRaw = { ...raw };
const servers = getServersObject(nextRaw);
servers[serverName] = entry;
setServersObject(nextRaw, servers);
return buildConfigWritePreview(filePath, nextRaw);
}
export function writeSharedServerEntry(filePath: string, serverName: string, entry: ServerEntry): string {
const raw = readRawConfigObject(filePath);
const servers = getServersObject(raw);
servers[serverName] = entry;
setServersObject(raw, servers);
writeRawConfigObject(filePath, raw);
return filePath;
}
export function getServerProvenance(overridePath?: string, cwd = process.cwd()): Map<string, ServerProvenance> {
const provenance = new Map<string, ServerProvenance>();
const userPath = getPiGlobalConfigPath(overridePath);
for (const source of getConfigSources(overridePath, cwd)) {
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
if (!loaded) continue;
if (loaded.imports?.length) {
for (const importKind of loaded.imports) {
const importPath = resolveImportPath(importKind, cwd);
if (!importPath) continue;
try {
const imported = JSON.parse(readFileSync(importPath, "utf-8"));
const servers = extractServers(imported, importKind);
for (const name of Object.keys(servers)) {
if (!provenance.has(name)) {
provenance.set(name, { path: userPath, kind: "import", importKind });
}
}
} catch {}
}
}
for (const name of Object.keys(loaded.mcpServers)) {
provenance.set(name, {
path: source.writePath,
kind: source.kind,
importKind: source.importKind,
});
}
}
return provenance;
}
export function writeDirectToolsConfig(
changes: Map<string, true | string[] | false>,
provenance: Map<string, ServerProvenance>,
fullConfig: McpConfig,
): void {
const byPath = new Map<string, { name: string; value: true | string[] | false; prov: ServerProvenance }[]>();
for (const [serverName, value] of changes) {
const prov = provenance.get(serverName);
if (!prov) continue;
const targetPath = prov.path;
if (!byPath.has(targetPath)) byPath.set(targetPath, []);
byPath.get(targetPath)!.push({ name: serverName, value, prov });
}
for (const [filePath, entries] of byPath) {
const raw = readRawConfigObject(filePath);
const servers = getServersObject(raw);
for (const { name, value, prov } of entries) {
if (prov.kind === "import") {
const fullDef = fullConfig.mcpServers[name];
if (fullDef) {
servers[name] = { ...fullDef, directTools: value };
}
} else if (servers[name]) {
servers[name] = { ...servers[name], directTools: value };
}
}
setServersObject(raw, servers);
writeRawConfigObject(filePath, raw);
}
}