UNPKG

@cequenceai/mcp-cli

Version:

Cequence MCP CLI - Command-line tool for setting up Cequence MCP servers with AI clients

121 lines (102 loc) 3.29 kB
import * as fs from 'fs-extra'; import * as path from 'path'; import * as os from 'os'; import { McpServerConfig, ClaudeConfig, SetupResult } from '../types'; import { sanitizeServerName } from '../utils/validation'; /** * Gets the Claude configuration directory path */ function getClaudeConfigDir(): string { const homeDir = os.homedir(); switch (process.platform) { case 'darwin': // macOS return path.join(homeDir, 'Library', 'Application Support', 'Claude'); case 'win32': // Windows return path.join(homeDir, 'AppData', 'Roaming', 'Claude'); case 'linux': // Linux return path.join(homeDir, '.config', 'claude'); default: throw new Error(`Unsupported platform: ${process.platform}`); } } /** * Gets the Claude MCP configuration file path */ function getClaudeConfigPath(): string { return path.join(getClaudeConfigDir(), 'claude_desktop_config.json'); } /** * Reads the current Claude configuration */ async function readClaudeConfig(): Promise<any> { const configPath = getClaudeConfigPath(); try { if (await fs.pathExists(configPath)) { const content = await fs.readFile(configPath, 'utf-8'); return JSON.parse(content); } } catch (error) { // If file doesn't exist or is invalid JSON, return empty object console.warn('Could not read existing Claude configuration, creating new configuration'); } return {}; } /** * Writes the Claude configuration */ async function writeClaudeConfig(config: any): Promise<void> { const configPath = getClaudeConfigPath(); const configDir = getClaudeConfigDir(); // Ensure the directory exists await fs.ensureDir(configDir); // Write configuration with proper formatting await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8'); } /** * Sets up Cequence MCP server configuration for Claude AI */ export async function setupClaude(config: McpServerConfig): Promise<SetupResult> { try { const serverKey = sanitizeServerName(config.name).toLowerCase().replace(/\s+/g, '-'); // Read existing configuration const claudeConfig = await readClaudeConfig(); // Initialize mcpServers if it doesn't exist if (!claudeConfig.mcpServers) { claudeConfig.mcpServers = {}; } // Use mcp-remote const mcpConfig: any = { command: "npx", args: [ "-y", "@cequenceai/mcp-remote", config.url, "--timeout", "120000", "--retries", "3" ] }; // Add API key as environment variable if provided if (config.apiKey) { mcpConfig.env = { API_KEY: config.apiKey, MCP_API_KEY: config.apiKey }; } // Add the server configuration claudeConfig.mcpServers[serverKey] = mcpConfig; // Write the updated configuration await writeClaudeConfig(claudeConfig); return { success: true, message: `Cequence MCP server "${config.name}" configured successfully for Claude AI`, configPath: getClaudeConfigPath() }; } catch (error) { return { success: false, message: `Failed to configure Cequence MCP server for Claude AI: ${error instanceof Error ? error.message : String(error)}` }; } }