UNPKG

@cequenceai/mcp-cli

Version:

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

104 lines (87 loc) 2.85 kB
import * as fs from 'fs-extra'; import * as path from 'path'; import * as os from 'os'; import { McpServerConfig, CursorConfig, SetupResult } from '../types'; import { sanitizeServerName } from '../utils/validation'; /** * Gets the Cursor MCP configuration file path */ function getCursorMcpConfigPath(): string { const homeDir = os.homedir(); return path.join(homeDir, '.cursor', 'mcp.json'); } /** * Reads the current Cursor MCP configuration */ async function readCursorMcpConfig(): Promise<any> { const configPath = getCursorMcpConfigPath(); 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 Cursor MCP configuration, creating new configuration'); } return {}; } /** * Writes the Cursor MCP configuration */ async function writeCursorMcpConfig(config: any): Promise<void> { const configPath = getCursorMcpConfigPath(); const configDir = path.dirname(configPath); // 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 Cursor IDE */ export async function setupCursor(config: McpServerConfig): Promise<SetupResult> { try { const serverKey = sanitizeServerName(config.name).toLowerCase().replace(/\s+/g, '-'); // Read existing MCP configuration const mcpConfig = await readCursorMcpConfig(); // Initialize mcpServers if it doesn't exist if (!mcpConfig.mcpServers) { mcpConfig.mcpServers = {}; } // Create the MCP server configuration using mcp-remote const serverConfig: 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) { serverConfig.env = { API_KEY: config.apiKey, MCP_API_KEY: config.apiKey }; } // Add the server configuration mcpConfig.mcpServers[serverKey] = serverConfig; // Write the updated configuration await writeCursorMcpConfig(mcpConfig); return { success: true, message: `Cequence MCP server "${config.name}" configured successfully for Cursor IDE`, configPath: getCursorMcpConfigPath() }; } catch (error) { return { success: false, message: `Failed to configure Cequence MCP server for Cursor IDE: ${error instanceof Error ? error.message : String(error)}` }; } }