|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { mkdir, readFile, writeFile } from "node:fs/promises"; |
| 3 | +import { homedir, platform } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { createInterface, type Interface } from "node:readline/promises"; |
| 6 | + |
| 7 | +export interface ClientTarget { |
| 8 | + id: string; |
| 9 | + name: string; |
| 10 | + configPath: string; |
| 11 | +} |
| 12 | + |
| 13 | +export interface InitArgs { |
| 14 | + url?: string; |
| 15 | + username?: string; |
| 16 | + password?: string; |
| 17 | + client?: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function getClientTargets( |
| 21 | + home: string = homedir(), |
| 22 | + plat: string = platform(), |
| 23 | +): ClientTarget[] { |
| 24 | + const targets: ClientTarget[] = []; |
| 25 | + |
| 26 | + let claudePath: string; |
| 27 | + if (plat === "darwin") { |
| 28 | + claudePath = join( |
| 29 | + home, |
| 30 | + "Library", |
| 31 | + "Application Support", |
| 32 | + "Claude", |
| 33 | + "claude_desktop_config.json", |
| 34 | + ); |
| 35 | + } else if (plat === "win32") { |
| 36 | + claudePath = join(process.env.APPDATA ?? home, "Claude", "claude_desktop_config.json"); |
| 37 | + } else { |
| 38 | + claudePath = join(home, ".config", "Claude", "claude_desktop_config.json"); |
| 39 | + } |
| 40 | + targets.push({ id: "claude-desktop", name: "Claude Desktop", configPath: claudePath }); |
| 41 | + |
| 42 | + targets.push({ id: "cursor", name: "Cursor", configPath: join(home, ".cursor", "mcp.json") }); |
| 43 | + |
| 44 | + return targets; |
| 45 | +} |
| 46 | + |
| 47 | +export function parseInitArgs(argv: string[]): InitArgs { |
| 48 | + const args: InitArgs = {}; |
| 49 | + for (let i = 0; i < argv.length; i++) { |
| 50 | + const a = argv[i]; |
| 51 | + if (a === "--url" && argv[i + 1]) args.url = argv[++i]; |
| 52 | + else if (a === "--username" && argv[i + 1]) args.username = argv[++i]; |
| 53 | + else if (a === "--password" && argv[i + 1]) args.password = argv[++i]; |
| 54 | + else if (a === "--client" && argv[i + 1]) args.client = argv[++i]; |
| 55 | + } |
| 56 | + return args; |
| 57 | +} |
| 58 | + |
| 59 | +export function mergeConfig( |
| 60 | + existing: Record<string, unknown>, |
| 61 | + creds: { url: string; username: string; password: string }, |
| 62 | +): Record<string, unknown> { |
| 63 | + const entry = { |
| 64 | + command: "npx", |
| 65 | + args: ["-y", "@parseable/parseable-mcp-server"], |
| 66 | + env: { |
| 67 | + PARSEABLE_URL: creds.url, |
| 68 | + PARSEABLE_USERNAME: creds.username, |
| 69 | + PARSEABLE_PASSWORD: creds.password, |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + const servers = (existing.mcpServers as Record<string, unknown>) ?? {}; |
| 74 | + servers.parseable = entry; |
| 75 | + return { ...existing, mcpServers: servers }; |
| 76 | +} |
| 77 | + |
| 78 | +async function writeClientConfig( |
| 79 | + target: ClientTarget, |
| 80 | + creds: { url: string; username: string; password: string }, |
| 81 | +): Promise<void> { |
| 82 | + let existing: Record<string, unknown> = {}; |
| 83 | + if (existsSync(target.configPath)) { |
| 84 | + const raw = await readFile(target.configPath, "utf8"); |
| 85 | + if (raw.trim()) { |
| 86 | + try { |
| 87 | + existing = JSON.parse(raw); |
| 88 | + } catch { |
| 89 | + throw new Error( |
| 90 | + `Existing config at ${target.configPath} is not valid JSON. Refusing to overwrite.`, |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + await writeFile(`${target.configPath}.bak`, raw, "utf8"); |
| 95 | + } else { |
| 96 | + await mkdir(dirname(target.configPath), { recursive: true }); |
| 97 | + } |
| 98 | + |
| 99 | + const merged = mergeConfig(existing, creds); |
| 100 | + await writeFile(target.configPath, `${JSON.stringify(merged, null, 2)}\n`, "utf8"); |
| 101 | +} |
| 102 | + |
| 103 | +async function ask(rl: Interface, question: string, fallback?: string): Promise<string> { |
| 104 | + const q = fallback ? `${question} [${fallback}]: ` : `${question}: `; |
| 105 | + const answer = (await rl.question(q)).trim(); |
| 106 | + return answer || fallback || ""; |
| 107 | +} |
| 108 | + |
| 109 | +export async function runInit(argv: string[] = process.argv.slice(3)): Promise<void> { |
| 110 | + const args = parseInitArgs(argv); |
| 111 | + |
| 112 | + console.log("Parseable MCP server — interactive setup\n"); |
| 113 | + |
| 114 | + const all = getClientTargets(); |
| 115 | + const detected = all.filter((t) => existsSync(t.configPath)); |
| 116 | + |
| 117 | + if (detected.length === 0) { |
| 118 | + console.log( |
| 119 | + "No MCP clients detected yet. Creating config files for both Claude Desktop and Cursor.", |
| 120 | + ); |
| 121 | + console.log("If you only use one, you can delete the other later.\n"); |
| 122 | + } else { |
| 123 | + console.log("Detected MCP clients:"); |
| 124 | + for (const [i, t] of detected.entries()) { |
| 125 | + console.log(` ${i + 1}. ${t.name} (${t.configPath})`); |
| 126 | + } |
| 127 | + console.log(); |
| 128 | + } |
| 129 | + |
| 130 | + const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 131 | + |
| 132 | + let chosen: ClientTarget[]; |
| 133 | + if (args.client) { |
| 134 | + const match = all.find((t) => t.id === args.client); |
| 135 | + if (!match) { |
| 136 | + rl.close(); |
| 137 | + console.error( |
| 138 | + `Unknown client "${args.client}". Use one of: ${all.map((t) => t.id).join(", ")}`, |
| 139 | + ); |
| 140 | + process.exit(1); |
| 141 | + } |
| 142 | + chosen = [match]; |
| 143 | + } else if (detected.length === 0) { |
| 144 | + chosen = all; |
| 145 | + } else { |
| 146 | + const pick = await ask(rl, "Configure which? (comma-separated numbers, or 'all')", "all"); |
| 147 | + if (pick === "all") { |
| 148 | + chosen = detected; |
| 149 | + } else { |
| 150 | + const indices = pick.split(",").map((s) => Number.parseInt(s.trim(), 10) - 1); |
| 151 | + chosen = indices.map((i) => detected[i]).filter(Boolean); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (chosen.length === 0) { |
| 156 | + rl.close(); |
| 157 | + console.error("No clients selected. Aborting."); |
| 158 | + process.exit(1); |
| 159 | + } |
| 160 | + |
| 161 | + const url = args.url || (await ask(rl, "Parseable URL")); |
| 162 | + const username = args.username || (await ask(rl, "Username", "admin")); |
| 163 | + const password = args.password || (await ask(rl, "Password")); |
| 164 | + |
| 165 | + rl.close(); |
| 166 | + |
| 167 | + if (!url || !username || !password) { |
| 168 | + console.error("URL, username, and password are all required."); |
| 169 | + process.exit(1); |
| 170 | + } |
| 171 | + |
| 172 | + for (const target of chosen) { |
| 173 | + try { |
| 174 | + await writeClientConfig(target, { url, username, password }); |
| 175 | + console.log(`✓ Configured ${target.name}: ${target.configPath}`); |
| 176 | + } catch (err) { |
| 177 | + console.error(`✗ Failed to configure ${target.name}: ${(err as Error).message}`); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + console.log(`\nRestart ${chosen.map((t) => t.name).join(" / ")} to load 27 Parseable tools.`); |
| 182 | +} |
0 commit comments