forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
174 lines (142 loc) · 5.18 KB
/
cli.js
File metadata and controls
174 lines (142 loc) · 5.18 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
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import { pathToFileURL } from "node:url";
const HOME = os.homedir();
const PI_CONFIG_PATH = path.join(HOME, ".pi", "agent", "mcp.json");
const GENERIC_GLOBAL_CONFIG_PATH = path.join(HOME, ".config", "mcp", "mcp.json");
const PROJECT_CONFIG_PATH = path.resolve(process.cwd(), ".mcp.json");
const PROJECT_PI_CONFIG_PATH = path.resolve(process.cwd(), ".pi", "mcp.json");
const IMPORT_PATHS = {
cursor: [path.join(HOME, ".cursor", "mcp.json")],
"claude-code": [
path.join(HOME, ".claude", "mcp.json"),
path.join(HOME, ".claude.json"),
path.join(HOME, ".claude", "claude_desktop_config.json"),
],
"claude-desktop": [path.join(HOME, "Library", "Application Support", "Claude", "claude_desktop_config.json")],
codex: [path.join(HOME, ".codex", "config.json")],
windsurf: [path.join(HOME, ".windsurf", "mcp.json")],
vscode: [path.resolve(process.cwd(), ".vscode", "mcp.json")],
};
function printHelp(log = console.log) {
log("pi-mcp-adapter helper\n");
log("Install the package with:");
log(" pi install npm:pi-mcp-adapter\n");
log("Then optionally run:");
log(" pi-mcp-adapter init Detect host configs and scaffold Pi imports");
log(" pi-mcp-adapter init --dry-run");
}
function readJsonFile(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
function loadPiConfig() {
if (!fs.existsSync(PI_CONFIG_PATH)) {
return { mcpServers: {} };
}
const raw = readJsonFile(PI_CONFIG_PATH);
const mcpServers = raw.mcpServers ?? raw["mcp-servers"] ?? {};
if (!mcpServers || typeof mcpServers !== "object" || Array.isArray(mcpServers)) {
throw new Error(`Invalid MCP config at ${PI_CONFIG_PATH}: expected \"mcpServers\" to be an object`);
}
const normalized = { ...raw };
delete normalized["mcp-servers"];
const imports = Array.isArray(raw.imports) ? raw.imports.filter((value) => typeof value === "string") : undefined;
return {
...normalized,
mcpServers,
imports,
};
}
function findAvailableImports() {
const found = [];
for (const [kind, candidates] of Object.entries(IMPORT_PATHS)) {
const existing = candidates.find((candidate) => fs.existsSync(candidate));
if (existing) {
found.push({ kind, path: existing });
}
}
return found;
}
function printDiscovery(log, imports) {
log("Config discovery:\n");
const paths = [
["User-global standard MCP", GENERIC_GLOBAL_CONFIG_PATH],
["Pi global override", PI_CONFIG_PATH],
["Project standard MCP", PROJECT_CONFIG_PATH],
["Project Pi override", PROJECT_PI_CONFIG_PATH],
];
for (const [label, filePath] of paths) {
const prefix = fs.existsSync(filePath) ? "✓" : "-";
log(`${prefix} ${label}: ${filePath}`);
}
log("\nCompatibility imports:\n");
if (imports.length === 0) {
log("- No host-specific MCP configs detected");
return;
}
for (const entry of imports) {
log(`✓ ${entry.kind}: ${entry.path}`);
}
}
function writePiConfig(config) {
fs.mkdirSync(path.dirname(PI_CONFIG_PATH), { recursive: true });
fs.writeFileSync(PI_CONFIG_PATH, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
}
async function runInit(argv, log = console.log) {
const dryRun = argv.includes("--dry-run");
const foundImports = findAvailableImports();
const existingConfig = loadPiConfig();
const existingImports = new Set(existingConfig.imports ?? []);
const importsToAdd = foundImports
.map((entry) => entry.kind)
.filter((kind) => !existingImports.has(kind));
printDiscovery(log, foundImports);
if (importsToAdd.length === 0) {
log("\nNo Pi config changes needed.");
log("Standard MCP configs are discovered automatically, and host-specific imports are already configured or unavailable.");
return 0;
}
const nextConfig = {
...existingConfig,
imports: [...existingImports, ...importsToAdd],
mcpServers: existingConfig.mcpServers ?? {},
};
log(`\nDetected host configs to import into Pi: ${importsToAdd.join(", ")}`);
if (dryRun) {
log(`Dry run: would update ${PI_CONFIG_PATH}`);
return 0;
}
writePiConfig(nextConfig);
log(`Updated ${PI_CONFIG_PATH}`);
log("Pi will now keep reading standard MCP configs automatically, while these imports cover host-specific config formats.");
return 0;
}
export async function main(argv = process.argv.slice(2), log = console.log, error = console.error) {
const [command, ...rest] = argv;
if (!command || command === "help" || command === "--help" || command === "-h") {
printHelp(log);
return 0;
}
if (command === "install") {
error("The custom downloader has been retired.");
error("Use `pi install npm:pi-mcp-adapter` instead, then optionally run `pi-mcp-adapter init`.");
return 1;
}
if (command === "init") {
return runInit(rest, log);
}
error(`Unknown command: ${command}`);
printHelp(log);
return 1;
}
const isEntrypoint = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isEntrypoint) {
main().then((code) => {
process.exitCode = code;
}).catch((err) => {
console.error(`\nHelper failed: ${err instanceof Error ? err.message : String(err)}`);
process.exit(1);
});
}