|
| 1 | +import * as fs from "fs/promises" |
| 2 | +import * as path from "path" |
| 3 | +import * as vscode from "vscode" |
| 4 | +import { fileExistsAtPath } from "../../utils/fs" |
| 5 | + |
| 6 | +export interface ProjectDependency { |
| 7 | + name: string |
| 8 | + type: "npm" | "docker" | "python" | "config" |
| 9 | + version?: string |
| 10 | +} |
| 11 | + |
| 12 | +export interface McpRecommendation { |
| 13 | + serverName: string |
| 14 | + config: any |
| 15 | + confidence: number // 0-100 |
| 16 | + reason: string |
| 17 | + dependencies: ProjectDependency[] |
| 18 | +} |
| 19 | + |
| 20 | +export interface AnalysisResult { |
| 21 | + recommendations: McpRecommendation[] |
| 22 | + detectedDependencies: ProjectDependency[] |
| 23 | + projectType?: string |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Default MCP configurations based on common project patterns |
| 28 | + */ |
| 29 | +const DEFAULT_MCP_CONFIGS: Record<string, any> = { |
| 30 | + github: { |
| 31 | + command: "docker", |
| 32 | + args: [ |
| 33 | + "run", |
| 34 | + "-i", |
| 35 | + "--rm", |
| 36 | + "-e", |
| 37 | + "GITHUB_PERSONAL_ACCESS_TOKEN", |
| 38 | + "-e", |
| 39 | + "GITHUB_TOOLSETS", |
| 40 | + "-e", |
| 41 | + "GITHUB_READ_ONLY", |
| 42 | + "ghcr.io/github/github-mcp-server", |
| 43 | + ], |
| 44 | + env: { |
| 45 | + GITHUB_PERSONAL_ACCESS_TOKEN: "${env:GITHUB_PERSONAL_ACCESS_TOKEN}", |
| 46 | + GITHUB_TOOLSETS: "", |
| 47 | + GITHUB_READ_ONLY: "", |
| 48 | + }, |
| 49 | + alwaysAllow: [ |
| 50 | + "get_file_contents", |
| 51 | + "list_issues", |
| 52 | + "create_issue", |
| 53 | + "get_pull_request", |
| 54 | + "create_pull_request", |
| 55 | + "list_branches", |
| 56 | + "list_commits", |
| 57 | + ], |
| 58 | + disabled: false, |
| 59 | + }, |
| 60 | + "neo4j-memory": { |
| 61 | + command: "docker", |
| 62 | + args: [ |
| 63 | + "run", |
| 64 | + "-i", |
| 65 | + "--rm", |
| 66 | + "-e", |
| 67 | + "NEO4J_URL", |
| 68 | + "-e", |
| 69 | + "NEO4J_USERNAME", |
| 70 | + "-e", |
| 71 | + "NEO4J_PASSWORD", |
| 72 | + "mcp/neo4j-memory", |
| 73 | + ], |
| 74 | + env: { |
| 75 | + NEO4J_URL: "bolt://host.docker.internal:7687", |
| 76 | + NEO4J_USERNAME: "neo4j", |
| 77 | + NEO4J_PASSWORD: "password", |
| 78 | + }, |
| 79 | + alwaysAllow: ["read_graph", "create_entities", "create_relations", "find_nodes", "search_nodes"], |
| 80 | + disabled: true, |
| 81 | + }, |
| 82 | + playwright: { |
| 83 | + command: "npx", |
| 84 | + args: ["-y", "@playwright/mcp@latest", "--browser=", "--viewport-size="], |
| 85 | + alwaysAllow: [ |
| 86 | + "browser_navigate", |
| 87 | + "browser_click", |
| 88 | + "browser_type", |
| 89 | + "browser_take_screenshot", |
| 90 | + "browser_snapshot", |
| 91 | + ], |
| 92 | + disabled: true, |
| 93 | + }, |
| 94 | + puppeteer: { |
| 95 | + command: "npx", |
| 96 | + args: ["-y", "@modelcontextprotocol/server-puppeteer"], |
| 97 | + disabled: true, |
| 98 | + alwaysAllow: ["puppeteer_screenshot", "puppeteer_click", "puppeteer_fill", "puppeteer_evaluate"], |
| 99 | + }, |
| 100 | + excel: { |
| 101 | + command: "cmd", |
| 102 | + args: ["/c", "npx", "--yes", "@negokaz/excel-mcp-server"], |
| 103 | + env: { |
| 104 | + EXCEL_MCP_PAGING_CELLS_LIMIT: "4000", |
| 105 | + }, |
| 106 | + alwaysAllow: ["excel_read_sheet", "excel_write_to_sheet", "excel_describe_sheets"], |
| 107 | + disabled: true, |
| 108 | + }, |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Mapping of project dependencies to recommended MCP servers |
| 113 | + */ |
| 114 | +const DEPENDENCY_TO_MCP_MAP: Record<string, { servers: string[]; confidence: number }> = { |
| 115 | + // Version control |
| 116 | + ".git": { servers: ["github"], confidence: 95 }, |
| 117 | + ".github": { servers: ["github"], confidence: 95 }, |
| 118 | + |
| 119 | + // Database |
| 120 | + neo4j: { servers: ["neo4j-memory", "neo4j-cypher"], confidence: 90 }, |
| 121 | + "@neo4j/driver": { servers: ["neo4j-memory", "neo4j-cypher"], confidence: 90 }, |
| 122 | + "neo4j-driver": { servers: ["neo4j-memory", "neo4j-cypher"], confidence: 90 }, |
| 123 | + |
| 124 | + // Testing & Automation |
| 125 | + playwright: { servers: ["playwright"], confidence: 85 }, |
| 126 | + "@playwright/test": { servers: ["playwright"], confidence: 90 }, |
| 127 | + puppeteer: { servers: ["puppeteer"], confidence: 85 }, |
| 128 | + "selenium-webdriver": { servers: ["playwright", "puppeteer"], confidence: 75 }, |
| 129 | + |
| 130 | + // Data processing |
| 131 | + xlsx: { servers: ["excel"], confidence: 80 }, |
| 132 | + exceljs: { servers: ["excel"], confidence: 80 }, |
| 133 | + pandas: { servers: ["excel"], confidence: 70 }, |
| 134 | + openpyxl: { servers: ["excel"], confidence: 75 }, |
| 135 | +} |
| 136 | + |
| 137 | +export class McpConfigAnalyzer { |
| 138 | + constructor(private workspaceFolder: vscode.WorkspaceFolder) {} |
| 139 | + |
| 140 | + /** |
| 141 | + * Analyzes the project and returns MCP configuration recommendations |
| 142 | + */ |
| 143 | + async analyzeProject(): Promise<AnalysisResult> { |
| 144 | + const dependencies = await this.detectProjectDependencies() |
| 145 | + const recommendations = this.generateRecommendations(dependencies) |
| 146 | + const projectType = await this.detectProjectType() |
| 147 | + |
| 148 | + return { |
| 149 | + recommendations, |
| 150 | + detectedDependencies: dependencies, |
| 151 | + projectType, |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Detects project dependencies from various sources |
| 157 | + */ |
| 158 | + private async detectProjectDependencies(): Promise<ProjectDependency[]> { |
| 159 | + const dependencies: ProjectDependency[] = [] |
| 160 | + const workspacePath = this.workspaceFolder.uri.fsPath |
| 161 | + |
| 162 | + // Check for package.json (Node.js projects) |
| 163 | + const packageJsonPath = path.join(workspacePath, "package.json") |
| 164 | + if (await fileExistsAtPath(packageJsonPath)) { |
| 165 | + try { |
| 166 | + const content = await fs.readFile(packageJsonPath, "utf-8") |
| 167 | + const packageData = JSON.parse(content) |
| 168 | + |
| 169 | + // Add npm dependencies |
| 170 | + const allDeps = { |
| 171 | + ...packageData.dependencies, |
| 172 | + ...packageData.devDependencies, |
| 173 | + } |
| 174 | + |
| 175 | + for (const [name, version] of Object.entries(allDeps)) { |
| 176 | + dependencies.push({ |
| 177 | + name, |
| 178 | + type: "npm", |
| 179 | + version: version as string, |
| 180 | + }) |
| 181 | + } |
| 182 | + } catch (error) { |
| 183 | + console.error("Error parsing package.json:", error) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Check for requirements.txt (Python projects) |
| 188 | + const requirementsPath = path.join(workspacePath, "requirements.txt") |
| 189 | + if (await fileExistsAtPath(requirementsPath)) { |
| 190 | + try { |
| 191 | + const content = await fs.readFile(requirementsPath, "utf-8") |
| 192 | + const lines = content.split("\n").filter((line) => line.trim() && !line.startsWith("#")) |
| 193 | + |
| 194 | + for (const line of lines) { |
| 195 | + const match = line.match(/^([^=<>!]+)/) |
| 196 | + if (match) { |
| 197 | + dependencies.push({ |
| 198 | + name: match[1].trim(), |
| 199 | + type: "python", |
| 200 | + }) |
| 201 | + } |
| 202 | + } |
| 203 | + } catch (error) { |
| 204 | + console.error("Error parsing requirements.txt:", error) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // Check for docker-compose.yml |
| 209 | + const dockerComposePath = path.join(workspacePath, "docker-compose.yml") |
| 210 | + const dockerComposeAltPath = path.join(workspacePath, "docker-compose.yaml") |
| 211 | + |
| 212 | + for (const composePath of [dockerComposePath, dockerComposeAltPath]) { |
| 213 | + if (await fileExistsAtPath(composePath)) { |
| 214 | + try { |
| 215 | + const content = await fs.readFile(composePath, "utf-8") |
| 216 | + |
| 217 | + // Simple pattern matching for common services |
| 218 | + if (content.includes("neo4j")) { |
| 219 | + dependencies.push({ name: "neo4j", type: "docker" }) |
| 220 | + } |
| 221 | + if (content.includes("postgres") || content.includes("postgresql")) { |
| 222 | + dependencies.push({ name: "postgresql", type: "docker" }) |
| 223 | + } |
| 224 | + if (content.includes("redis")) { |
| 225 | + dependencies.push({ name: "redis", type: "docker" }) |
| 226 | + } |
| 227 | + } catch (error) { |
| 228 | + console.error("Error parsing docker-compose file:", error) |
| 229 | + } |
| 230 | + break |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // Check for .git directory |
| 235 | + const gitPath = path.join(workspacePath, ".git") |
| 236 | + if (await fileExistsAtPath(gitPath)) { |
| 237 | + dependencies.push({ name: ".git", type: "config" }) |
| 238 | + } |
| 239 | + |
| 240 | + // Check for .github directory |
| 241 | + const githubPath = path.join(workspacePath, ".github") |
| 242 | + if (await fileExistsAtPath(githubPath)) { |
| 243 | + dependencies.push({ name: ".github", type: "config" }) |
| 244 | + } |
| 245 | + |
| 246 | + return dependencies |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Generates MCP server recommendations based on detected dependencies |
| 251 | + */ |
| 252 | + private generateRecommendations(dependencies: ProjectDependency[]): McpRecommendation[] { |
| 253 | + const recommendations: McpRecommendation[] = [] |
| 254 | + const addedServers = new Set<string>() |
| 255 | + |
| 256 | + for (const dep of dependencies) { |
| 257 | + const mapping = DEPENDENCY_TO_MCP_MAP[dep.name] |
| 258 | + |
| 259 | + if (mapping) { |
| 260 | + for (const serverName of mapping.servers) { |
| 261 | + if (!addedServers.has(serverName)) { |
| 262 | + const config = DEFAULT_MCP_CONFIGS[serverName] |
| 263 | + |
| 264 | + if (config) { |
| 265 | + recommendations.push({ |
| 266 | + serverName, |
| 267 | + config: { ...config, disabled: false }, // Enable by default for recommendations |
| 268 | + confidence: mapping.confidence, |
| 269 | + reason: `Detected ${dep.name} in your project`, |
| 270 | + dependencies: [dep], |
| 271 | + }) |
| 272 | + addedServers.add(serverName) |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + // Sort by confidence (highest first) |
| 280 | + recommendations.sort((a, b) => b.confidence - a.confidence) |
| 281 | + |
| 282 | + return recommendations |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Detects the general type of project |
| 287 | + */ |
| 288 | + private async detectProjectType(): Promise<string | undefined> { |
| 289 | + const workspacePath = this.workspaceFolder.uri.fsPath |
| 290 | + |
| 291 | + // Check for various project indicators |
| 292 | + if (await fileExistsAtPath(path.join(workspacePath, "package.json"))) { |
| 293 | + const content = await fs.readFile(path.join(workspacePath, "package.json"), "utf-8") |
| 294 | + const data = JSON.parse(content) |
| 295 | + |
| 296 | + if (data.dependencies?.react || data.dependencies?.["react-dom"]) { |
| 297 | + return "react" |
| 298 | + } |
| 299 | + if (data.dependencies?.vue) { |
| 300 | + return "vue" |
| 301 | + } |
| 302 | + if (data.dependencies?.express || data.dependencies?.fastify) { |
| 303 | + return "node-backend" |
| 304 | + } |
| 305 | + return "node" |
| 306 | + } |
| 307 | + |
| 308 | + if (await fileExistsAtPath(path.join(workspacePath, "requirements.txt"))) { |
| 309 | + const content = await fs.readFile(path.join(workspacePath, "requirements.txt"), "utf-8") |
| 310 | + |
| 311 | + if (content.includes("django")) { |
| 312 | + return "django" |
| 313 | + } |
| 314 | + if (content.includes("flask")) { |
| 315 | + return "flask" |
| 316 | + } |
| 317 | + if (content.includes("fastapi")) { |
| 318 | + return "fastapi" |
| 319 | + } |
| 320 | + return "python" |
| 321 | + } |
| 322 | + |
| 323 | + if (await fileExistsAtPath(path.join(workspacePath, "go.mod"))) { |
| 324 | + return "go" |
| 325 | + } |
| 326 | + |
| 327 | + if (await fileExistsAtPath(path.join(workspacePath, "Cargo.toml"))) { |
| 328 | + return "rust" |
| 329 | + } |
| 330 | + |
| 331 | + return undefined |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * Applies recommended configurations to the MCP settings |
| 336 | + */ |
| 337 | + async applyRecommendations( |
| 338 | + recommendations: McpRecommendation[], |
| 339 | + target: "global" | "project" = "project", |
| 340 | + ): Promise<void> { |
| 341 | + // This will be implemented to actually write the configurations |
| 342 | + // For now, it's a placeholder |
| 343 | + console.log("Applying recommendations:", recommendations) |
| 344 | + } |
| 345 | +} |
0 commit comments