|
| 1 | +import { resolve } from 'node:path' |
| 2 | +import { promises as fs } from 'node:fs' |
| 3 | +import { homedir } from 'node:os' |
| 4 | +import { chalk, log } from './command-helpers.js' |
| 5 | +import type { ConsumerConfig } from '../recipes/ai-context/context.js' |
| 6 | + |
| 7 | +/** |
| 8 | + * Generate MCP configuration for the detected IDE or development environment |
| 9 | + */ |
| 10 | +export const generateMcpConfig = (ide: ConsumerConfig): Record<string, unknown> => { |
| 11 | + const configs: Record<string, Record<string, unknown>> = { |
| 12 | + vscode: { |
| 13 | + servers: { |
| 14 | + netlify: { |
| 15 | + type: 'stdio', |
| 16 | + command: 'npx', |
| 17 | + args: ['-y', '@netlify/mcp'], |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + cursor: { |
| 22 | + mcpServers: { |
| 23 | + netlify: { |
| 24 | + command: 'npx', |
| 25 | + args: ['-y', '@netlify/mcp'], |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + windsurf: { |
| 30 | + mcpServers: { |
| 31 | + netlify: { |
| 32 | + command: 'npx', |
| 33 | + args: ['-y', '@netlify/mcp'], |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + configs[ide.key] ?? { |
| 41 | + mcpServers: { |
| 42 | + netlify: { |
| 43 | + command: 'npx', |
| 44 | + args: ['-y', '@netlify/mcp'], |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * VS Code specific MCP configuration |
| 53 | + */ |
| 54 | +export const configureMcpForVSCode = async (config: Record<string, unknown>, projectPath: string): Promise<void> => { |
| 55 | + const vscodeDirPath = resolve(projectPath, '.vscode') |
| 56 | + const configPath = resolve(vscodeDirPath, 'mcp.json') |
| 57 | + |
| 58 | + try { |
| 59 | + // Create .vscode directory if it doesn't exist |
| 60 | + await fs.mkdir(vscodeDirPath, { recursive: true }) |
| 61 | + |
| 62 | + // Write or update mcp.json |
| 63 | + let existingConfig: Record<string, unknown> = {} |
| 64 | + try { |
| 65 | + const existingContent = await fs.readFile(configPath, 'utf-8') |
| 66 | + existingConfig = JSON.parse(existingContent) as Record<string, unknown> |
| 67 | + } catch { |
| 68 | + // File doesn't exist or is invalid JSON |
| 69 | + } |
| 70 | + |
| 71 | + const updatedConfig = { ...existingConfig, ...config } |
| 72 | + |
| 73 | + await fs.writeFile(configPath, JSON.stringify(updatedConfig, null, 2), 'utf-8') |
| 74 | + log(`${chalk.green('✅')} VS Code MCP configuration saved to ${chalk.cyan('.vscode/mcp.json')}`) |
| 75 | + } catch (error) { |
| 76 | + throw new Error(`Failed to configure VS Code MCP: ${error instanceof Error ? error.message : 'Unknown error'}`) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Cursor specific MCP configuration |
| 82 | + */ |
| 83 | +export const configureMcpForCursor = async (config: Record<string, unknown>, projectPath: string): Promise<void> => { |
| 84 | + const configPath = resolve(projectPath, '.cursor', 'mcp.json') |
| 85 | + |
| 86 | + try { |
| 87 | + await fs.mkdir(resolve(projectPath, '.cursor'), { recursive: true }) |
| 88 | + await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8') |
| 89 | + log(`${chalk.green('✅')} Cursor MCP configuration saved to ${chalk.cyan('.cursor/mcp.json')}`) |
| 90 | + } catch (error) { |
| 91 | + throw new Error(`Failed to configure Cursor MCP: ${error instanceof Error ? error.message : 'Unknown error'}`) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Windsurf specific MCP configuration |
| 97 | + */ |
| 98 | +export const configureMcpForWindsurf = async (config: Record<string, unknown>, _projectPath: string): Promise<void> => { |
| 99 | + const windsurfDirPath = resolve(homedir(), '.codeium', 'windsurf') |
| 100 | + const configPath = resolve(windsurfDirPath, 'mcp_config.json') |
| 101 | + |
| 102 | + try { |
| 103 | + // Create .codeium/windsurf directory if it doesn't exist |
| 104 | + await fs.mkdir(windsurfDirPath, { recursive: true }) |
| 105 | + |
| 106 | + // Read existing config or create new one |
| 107 | + let existingConfig: Record<string, unknown> = {} |
| 108 | + try { |
| 109 | + const existingContent = await fs.readFile(configPath, 'utf-8') |
| 110 | + existingConfig = JSON.parse(existingContent) as Record<string, unknown> |
| 111 | + } catch { |
| 112 | + // File doesn't exist or is invalid JSON |
| 113 | + } |
| 114 | + |
| 115 | + // Merge mcpServers from both configs |
| 116 | + const existingServers = (existingConfig.mcpServers as Record<string, unknown> | undefined) ?? {} |
| 117 | + const newServers = (config.mcpServers as Record<string, unknown> | undefined) ?? {} |
| 118 | + |
| 119 | + const updatedConfig = { |
| 120 | + ...existingConfig, |
| 121 | + mcpServers: { |
| 122 | + ...existingServers, |
| 123 | + ...newServers, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + await fs.writeFile(configPath, JSON.stringify(updatedConfig, null, 2), 'utf-8') |
| 128 | + log(`${chalk.green('✅')} Windsurf MCP configuration saved`) |
| 129 | + log(`${chalk.gray('💡')} Restart Windsurf to activate the MCP server`) |
| 130 | + } catch (error) { |
| 131 | + throw new Error(`Failed to configure Windsurf MCP: ${error instanceof Error ? error.message : 'Unknown error'}`) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Generic MCP configuration display |
| 137 | + */ |
| 138 | +export const showGenericMcpConfig = (config: Record<string, unknown>, ideName: string): void => { |
| 139 | + log(`\n${chalk.yellow('📋 Manual configuration required')}`) |
| 140 | + log(`Please add the following configuration to your ${ideName} settings:`) |
| 141 | + log(`\n${chalk.gray('--- Configuration ---')}`) |
| 142 | + log(JSON.stringify(config, null, 2)) |
| 143 | + log(`${chalk.gray('--- End Configuration ---')}\n`) |
| 144 | +} |
0 commit comments