|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Interactive installer for mcp-dataverse. |
| 4 | + * Invoked via: npx mcp-dataverse install |
| 5 | + * |
| 6 | + * Steps: |
| 7 | + * 1. Prompt for Dataverse environment URL |
| 8 | + * 2. Save config to ~/.mcp-dataverse/config.json |
| 9 | + * 3. Register the server in VS Code user mcp.json |
| 10 | + * 4. Authenticate via Microsoft device code flow |
| 11 | + */ |
| 12 | +import { createInterface } from "readline/promises"; |
| 13 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; |
| 14 | +import { homedir } from "os"; |
| 15 | +import { dirname, join } from "path"; |
| 16 | +import { DeviceCodeAuthProvider } from "./auth/device-code-auth-provider.js"; |
| 17 | + |
| 18 | +// ── Constants ───────────────────────────────────────────────────────────────── |
| 19 | + |
| 20 | +const CACHE_DIR = join(homedir(), ".mcp-dataverse"); |
| 21 | +const CONFIG_FILE = join(CACHE_DIR, "config.json"); |
| 22 | + |
| 23 | +// ── Output helpers ───────────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +const out = (msg: string) => process.stdout.write(msg + "\n"); |
| 26 | +const hr = () => process.stdout.write("─".repeat(58) + "\n"); |
| 27 | + |
| 28 | +// ── VS Code mcp.json locations ───────────────────────────────────────────────── |
| 29 | + |
| 30 | +function getVSCodeUserDirs(): { label: string; mcpJson: string }[] { |
| 31 | + const home = homedir(); |
| 32 | + if (process.platform === "win32") { |
| 33 | + const appData = process.env["APPDATA"] ?? join(home, "AppData", "Roaming"); |
| 34 | + return [ |
| 35 | + { label: "VS Code Insiders", mcpJson: join(appData, "Code - Insiders", "User", "mcp.json") }, |
| 36 | + { label: "VS Code Stable", mcpJson: join(appData, "Code", "User", "mcp.json") }, |
| 37 | + ]; |
| 38 | + } |
| 39 | + if (process.platform === "darwin") { |
| 40 | + const base = join(home, "Library", "Application Support"); |
| 41 | + return [ |
| 42 | + { label: "VS Code Insiders", mcpJson: join(base, "Code - Insiders", "User", "mcp.json") }, |
| 43 | + { label: "VS Code Stable", mcpJson: join(base, "Code", "User", "mcp.json") }, |
| 44 | + ]; |
| 45 | + } |
| 46 | + const base = join(home, ".config"); |
| 47 | + return [ |
| 48 | + { label: "VS Code Insiders", mcpJson: join(base, "Code - Insiders", "User", "mcp.json") }, |
| 49 | + { label: "VS Code Stable", mcpJson: join(base, "Code", "User", "mcp.json") }, |
| 50 | + ]; |
| 51 | +} |
| 52 | + |
| 53 | +// ── mcp.json patching ───────────────────────────────────────────────────────── |
| 54 | + |
| 55 | +function patchVSCodeMcpJson(configFilePath: string): { label: string; path: string }[] { |
| 56 | + const updated: { label: string; path: string }[] = []; |
| 57 | + const serverEntry = { |
| 58 | + type: "stdio", |
| 59 | + command: "npx", |
| 60 | + args: ["-y", "mcp-dataverse"], |
| 61 | + env: { MCP_CONFIG_PATH: configFilePath }, |
| 62 | + }; |
| 63 | + |
| 64 | + for (const { label, mcpJson } of getVSCodeUserDirs()) { |
| 65 | + // Only act when the VS Code User/ directory exists (VS Code variant is installed) |
| 66 | + if (!existsSync(dirname(mcpJson))) continue; |
| 67 | + |
| 68 | + let data: Record<string, unknown> = {}; |
| 69 | + if (existsSync(mcpJson)) { |
| 70 | + try { data = JSON.parse(readFileSync(mcpJson, "utf-8")) as Record<string, unknown>; } catch { /* start fresh */ } |
| 71 | + } |
| 72 | + if (!data["servers"] || typeof data["servers"] !== "object") data["servers"] = {}; |
| 73 | + (data["servers"] as Record<string, unknown>)["mcp-dataverse"] = serverEntry; |
| 74 | + writeFileSync(mcpJson, JSON.stringify(data, null, 2) + "\n", "utf-8"); |
| 75 | + updated.push({ label, path: mcpJson }); |
| 76 | + } |
| 77 | + |
| 78 | + return updated; |
| 79 | +} |
| 80 | + |
| 81 | +// ── URL validation ───────────────────────────────────────────────────────────── |
| 82 | + |
| 83 | +function isValidDataverseUrl(raw: string): string | null { |
| 84 | + if (!raw.startsWith("https://")) return "URL must start with https://"; |
| 85 | + try { |
| 86 | + const hostname = new URL(raw).hostname.toLowerCase(); |
| 87 | + if (!hostname.endsWith(".dynamics.com")) return "Must be a *.dynamics.com URL (your Power Platform environment)"; |
| 88 | + } catch { |
| 89 | + return "Invalid URL format"; |
| 90 | + } |
| 91 | + return null; |
| 92 | +} |
| 93 | + |
| 94 | +// ── Manual mcp.json snippet ──────────────────────────────────────────────────── |
| 95 | + |
| 96 | +function printManualSnippet(configFilePath: string): void { |
| 97 | + out(" Add this to your VS Code user mcp.json (Ctrl+Shift+P → Open User MCP Configuration):"); |
| 98 | + out(""); |
| 99 | + out(' "mcp-dataverse": {'); |
| 100 | + out(' "type": "stdio",'); |
| 101 | + out(' "command": "npx",'); |
| 102 | + out(' "args": ["-y", "mcp-dataverse"],'); |
| 103 | + out(` "env": { "MCP_CONFIG_PATH": "${configFilePath.replace(/\\/g, "\\\\")}" }`); |
| 104 | + out(" }"); |
| 105 | +} |
| 106 | + |
| 107 | +// ── Main export ──────────────────────────────────────────────────────────────── |
| 108 | + |
| 109 | +export async function runInstall(): Promise<void> { |
| 110 | + out(""); |
| 111 | + out("╔════════════════════════════════════════════════════════╗"); |
| 112 | + out("║ MCP Dataverse — Interactive Setup ║"); |
| 113 | + out("╚════════════════════════════════════════════════════════╝"); |
| 114 | + out(""); |
| 115 | + out("This wizard will:"); |
| 116 | + out(" 1. Ask for your Dataverse environment URL"); |
| 117 | + out(" 2. Save configuration to ~/.mcp-dataverse/config.json"); |
| 118 | + out(" 3. Register the server in VS Code (user mcp.json)"); |
| 119 | + out(" 4. Authenticate with your Microsoft account"); |
| 120 | + out(""); |
| 121 | + hr(); |
| 122 | + |
| 123 | + // ── Step 1: Prompt for environment URL ────────────────────────────────────── |
| 124 | + const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 125 | + let environmentUrl = ""; |
| 126 | + |
| 127 | + while (!environmentUrl) { |
| 128 | + const raw = (await rl.question( |
| 129 | + "Dataverse environment URL\n e.g. https://contoso.crm.dynamics.com\n› " |
| 130 | + )).trim().replace(/\/$/, ""); |
| 131 | + |
| 132 | + const error = isValidDataverseUrl(raw); |
| 133 | + if (error) { out(` ✗ ${error}\n`); continue; } |
| 134 | + environmentUrl = raw; |
| 135 | + } |
| 136 | + |
| 137 | + rl.close(); |
| 138 | + out(""); |
| 139 | + |
| 140 | + // ── Step 2: Write config ───────────────────────────────────────────────────── |
| 141 | + out("Saving configuration…"); |
| 142 | + mkdirSync(CACHE_DIR, { recursive: true }); |
| 143 | + writeFileSync(CONFIG_FILE, JSON.stringify({ environmentUrl }, null, 2) + "\n", "utf-8"); |
| 144 | + out(` ✓ ${CONFIG_FILE}`); |
| 145 | + out(""); |
| 146 | + |
| 147 | + // ── Step 3: Patch VS Code mcp.json ─────────────────────────────────────────── |
| 148 | + out("Registering in VS Code…"); |
| 149 | + const patched = patchVSCodeMcpJson(CONFIG_FILE); |
| 150 | + if (patched.length > 0) { |
| 151 | + for (const { label, path } of patched) out(` ✓ ${label}: ${path}`); |
| 152 | + } else { |
| 153 | + out(" ℹ No VS Code installation detected — configure manually:"); |
| 154 | + out(""); |
| 155 | + printManualSnippet(CONFIG_FILE); |
| 156 | + } |
| 157 | + out(""); |
| 158 | + hr(); |
| 159 | + |
| 160 | + // ── Step 4: Authenticate ───────────────────────────────────────────────────── |
| 161 | + out("Authenticating with Microsoft…"); |
| 162 | + out("(A prompt will appear below — open the URL and enter the code)"); |
| 163 | + out(""); |
| 164 | + |
| 165 | + try { |
| 166 | + const auth = new DeviceCodeAuthProvider(environmentUrl); |
| 167 | + await auth.setupViaDeviceCode(); |
| 168 | + |
| 169 | + hr(); |
| 170 | + out(""); |
| 171 | + out(" ✓ Setup complete!"); |
| 172 | + out(""); |
| 173 | + out("Next steps:"); |
| 174 | + out(" • Reload VS Code: Ctrl+Shift+P → Reload Window"); |
| 175 | + out(" • MCP Dataverse tools will appear in GitHub Copilot chat (🔧)"); |
| 176 | + out(""); |
| 177 | + } catch (err) { |
| 178 | + const msg = err instanceof Error ? err.message : String(err); |
| 179 | + hr(); |
| 180 | + out(` ✗ Authentication failed: ${msg}`); |
| 181 | + out(""); |
| 182 | + out(" Configuration was saved. Authenticate later with:"); |
| 183 | + out(` npx mcp-dataverse-auth ${environmentUrl}`); |
| 184 | + out(""); |
| 185 | + process.exit(1); |
| 186 | + } |
| 187 | +} |
0 commit comments