@cequenceai/mcp-cli
Version:
Cequence MCP CLI - Command-line tool for setting up Cequence MCP servers with AI clients
117 lines (98 loc) • 3.84 kB
text/typescript
import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import { McpServerConfig, SetupResult } from '../types';
import { sanitizeServerName } from '../utils/validation';
/**
* Gets the VS Code MCP configuration file path
* @param global - If true, returns the global user config path; otherwise returns workspace path
* @param workspacePath - The workspace path for project-specific config
*/
function getVSCodeMcpConfigPath(global: boolean = false, workspacePath?: string): string {
if (global) {
const homeDir = os.homedir();
// VS Code global settings location varies by platform
switch (process.platform) {
case 'darwin': // macOS
return path.join(homeDir, 'Library', 'Application Support', 'Code', 'User', 'mcp.json');
case 'win32': // Windows
return path.join(homeDir, 'AppData', 'Roaming', 'Code', 'User', 'mcp.json');
case 'linux': // Linux
return path.join(homeDir, '.config', 'Code', 'User', 'mcp.json');
default:
throw new Error(`Unsupported platform: ${process.platform}`);
}
} else {
// Workspace-specific config
const basePath = workspacePath || process.cwd();
return path.join(basePath, '.vscode', 'mcp.json');
}
}
/**
* Reads the current VS Code MCP configuration
*/
async function readVSCodeMcpConfig(configPath: string): Promise<any> {
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 VS Code MCP configuration, creating new configuration');
}
return {};
}
/**
* Writes the VS Code MCP configuration
*/
async function writeVSCodeMcpConfig(configPath: string, config: any): Promise<void> {
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 VS Code
* @param config - The MCP server configuration
* @param global - If true, configures globally; otherwise configures for current workspace
* @param workspacePath - Optional workspace path for project-specific config
*/
export async function setupVSCode(
config: McpServerConfig,
global: boolean = false,
workspacePath?: string
): Promise<SetupResult> {
try {
const serverKey = sanitizeServerName(config.name).toLowerCase().replace(/\s+/g, '-');
const configPath = getVSCodeMcpConfigPath(global, workspacePath);
// Read existing MCP configuration
const mcpConfig = await readVSCodeMcpConfig(configPath);
// Initialize servers if it doesn't exist (VS Code uses "servers" not "mcpServers")
if (!mcpConfig.servers) {
mcpConfig.servers = {};
}
// Create the MCP server configuration using VS Code's format
// VS Code supports type: "http" for HTTP/SSE servers
const serverConfig: any = {
type: 'http',
url: config.url
};
// Add the server configuration
mcpConfig.servers[serverKey] = serverConfig;
// Write the updated configuration
await writeVSCodeMcpConfig(configPath, mcpConfig);
const configType = global ? 'global (user)' : 'workspace';
return {
success: true,
message: `Cequence MCP server "${config.name}" configured successfully for VS Code (${configType})`,
configPath: configPath
};
} catch (error) {
return {
success: false,
message: `Failed to configure Cequence MCP server for VS Code: ${error instanceof Error ? error.message : String(error)}`
};
}
}