UNPKG

@sigyl-dev/cli

Version:

Official Sigyl CLI for installing and managing MCP packages. Zero-config installation for public packages, secure API-based authentication.

208 lines 8.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getClaudeConfigPath = getClaudeConfigPath; exports.readClaudeConfig = readClaudeConfig; exports.writeClaudeConfig = writeClaudeConfig; exports.installMCPServer = installMCPServer; exports.listMCPServers = listMCPServers; exports.removeMCPServer = removeMCPServer; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const node_os_1 = require("node:os"); const chalk_1 = __importDefault(require("chalk")); const logger_1 = require("../logger"); /** * Get the Claude Desktop config file path based on platform */ function getClaudeConfigPath() { const platform = process.platform; let configPath; switch (platform) { case "win32": const appData = process.env.APPDATA || (0, node_path_1.join)((0, node_os_1.homedir)(), "AppData", "Roaming"); configPath = (0, node_path_1.join)(appData, "Claude", "claude_desktop_config.json"); break; case "darwin": configPath = (0, node_path_1.join)((0, node_os_1.homedir)(), "Library", "Application Support", "Claude", "claude_desktop_config.json"); break; case "linux": const xdgConfig = process.env.XDG_CONFIG_HOME || (0, node_path_1.join)((0, node_os_1.homedir)(), ".config"); configPath = (0, node_path_1.join)(xdgConfig, "Claude", "claude_desktop_config.json"); break; default: console.log(chalk_1.default.yellow(`⚠️ Unsupported platform: ${platform}`)); return null; } return configPath; } /** * Read the current Claude Desktop configuration */ function readClaudeConfig() { const configPath = getClaudeConfigPath(); if (!configPath) { throw new Error("Unsupported platform for Claude Desktop configuration"); } (0, logger_1.verboseLog)(`Reading Claude config from: ${configPath}`); if (!(0, node_fs_1.existsSync)(configPath)) { (0, logger_1.verboseLog)("Claude config file does not exist, returning empty config"); return { mcpServers: {} }; } try { const configContent = (0, node_fs_1.readFileSync)(configPath, "utf8"); const config = JSON.parse(configContent); (0, logger_1.verboseLog)(`Claude config loaded: ${JSON.stringify(config, null, 2)}`); return { ...config, mcpServers: config.mcpServers || {} }; } catch (error) { console.log(chalk_1.default.yellow(`⚠️ Error reading Claude config: ${error}`)); return { mcpServers: {} }; } } /** * Write configuration to Claude Desktop */ function writeClaudeConfig(config) { const configPath = getClaudeConfigPath(); if (!configPath) { throw new Error("Unsupported platform for Claude Desktop configuration"); } try { // Ensure the config directory exists const configDir = (0, node_path_1.dirname)(configPath); if (!(0, node_fs_1.existsSync)(configDir)) { (0, logger_1.verboseLog)(`Creating Claude config directory: ${configDir}`); (0, node_fs_1.mkdirSync)(configDir, { recursive: true }); } // Write the configuration (0, logger_1.verboseLog)(`Writing Claude config to: ${configPath}`); (0, logger_1.verboseLog)(`Config data: ${JSON.stringify(config, null, 2)}`); (0, node_fs_1.writeFileSync)(configPath, JSON.stringify(config, null, 2)); return true; } catch (error) { console.error(chalk_1.default.red(`❌ Failed to write Claude config: ${error}`)); return false; } } /** * Install an MCP server in Claude Desktop */ function installMCPServer(serverName, serverPath, options = {}) { try { (0, logger_1.verboseLog)(`Installing MCP server: ${serverName}`); // Read existing config const config = readClaudeConfig(); // Determine the command and args based on language const absoluteServerPath = (0, node_path_1.resolve)(serverPath); let command; let args; if (options.language === "typescript") { // For TypeScript, we need to run the compiled JavaScript const jsPath = absoluteServerPath.replace(/\.ts$/, ".js"); command = "node"; args = [jsPath]; } else { // For JavaScript command = "node"; args = [absoluteServerPath]; } // Create server configuration const serverConfig = { command, args }; // Add working directory if the server is in a subdirectory const serverDir = (0, node_path_1.dirname)(absoluteServerPath); if (serverDir !== process.cwd()) { serverConfig.cwd = serverDir; } // Add environment variables if provided if (options.env && Object.keys(options.env).length > 0) { serverConfig.env = options.env; } // Add or update the server in the config config.mcpServers[serverName] = serverConfig; // Write the updated config const success = writeClaudeConfig(config); if (success) { console.log(chalk_1.default.green(`✅ Successfully installed '${serverName}' in Claude Desktop`)); console.log(chalk_1.default.blue("📋 Server configuration:")); console.log(chalk_1.default.gray(` Command: ${command} ${args.join(" ")}`)); if (serverConfig.cwd) { console.log(chalk_1.default.gray(` Working Directory: ${serverConfig.cwd}`)); } if (serverConfig.env) { console.log(chalk_1.default.gray(` Environment: ${Object.keys(serverConfig.env).join(", ")}`)); } console.log(chalk_1.default.yellow("\n🔄 Please restart Claude Desktop to load the new server")); console.log(chalk_1.default.gray("💡 Look for the hammer icon (🔨) in Claude to confirm the server is loaded")); return true; } return false; } catch (error) { console.error(chalk_1.default.red(`❌ Failed to install MCP server: ${error}`)); return false; } } /** * List installed MCP servers in Claude Desktop */ function listMCPServers() { try { const config = readClaudeConfig(); const servers = config.mcpServers; if (Object.keys(servers).length === 0) { console.log(chalk_1.default.yellow("📭 No MCP servers installed in Claude Desktop")); return; } console.log(chalk_1.default.blue("📋 Installed MCP servers in Claude Desktop:\n")); Object.entries(servers).forEach(([name, serverConfig]) => { console.log(chalk_1.default.cyan(`• ${name}`)); console.log(chalk_1.default.gray(` Command: ${serverConfig.command} ${serverConfig.args.join(" ")}`)); if (serverConfig.cwd) { console.log(chalk_1.default.gray(` Working Directory: ${serverConfig.cwd}`)); } if (serverConfig.env) { console.log(chalk_1.default.gray(` Environment: ${Object.keys(serverConfig.env).join(", ")}`)); } console.log(); }); } catch (error) { console.error(chalk_1.default.red(`❌ Failed to list MCP servers: ${error}`)); } } /** * Remove an MCP server from Claude Desktop */ function removeMCPServer(serverName) { try { const config = readClaudeConfig(); if (!(serverName in config.mcpServers)) { console.log(chalk_1.default.yellow(`⚠️ Server '${serverName}' not found in Claude Desktop config`)); return false; } delete config.mcpServers[serverName]; const success = writeClaudeConfig(config); if (success) { console.log(chalk_1.default.green(`✅ Successfully removed '${serverName}' from Claude Desktop`)); console.log(chalk_1.default.yellow("🔄 Please restart Claude Desktop to apply changes")); return true; } return false; } catch (error) { console.error(chalk_1.default.red(`❌ Failed to remove MCP server: ${error}`)); return false; } } //# sourceMappingURL=claude-config.js.map