|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as util from "util"; |
| 5 | +import * as vscode from "vscode"; |
| 6 | +import * as path from "path"; |
| 7 | +import fs from "fs-extra"; |
| 8 | +import { context, workspaceUri } from "../globalVariables"; |
| 9 | +import { localize } from "./localizeUtils"; |
| 10 | +import { ExtTelemetry } from "../telemetry/extTelemetry"; |
| 11 | +import { TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents"; |
| 12 | +import { FxError } from "@microsoft/teamsfx-api"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Setup MCP Server by checking for required files and prompting user to create them if missing |
| 16 | + */ |
| 17 | +export async function setupMCPServer(): Promise<void> { |
| 18 | + if (!workspaceUri) { |
| 19 | + return; // No workspace opened |
| 20 | + } |
| 21 | + const workspaceRoot = workspaceUri.path; |
| 22 | + |
| 23 | + // Check which files are missing |
| 24 | + const copilotInstructionsPath = path.join(workspaceRoot, ".github", "copilot-instructions.md"); |
| 25 | + const mcpConfigPath = path.join(workspaceRoot, ".vscode", "mcp.json"); |
| 26 | + const missingCopilotInstructions = !fs.existsSync(copilotInstructionsPath); |
| 27 | + const missingMcpConfig = checkMCPConfigNeedsUpdate(); |
| 28 | + |
| 29 | + if (!missingCopilotInstructions && !missingMcpConfig) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Create message based on which files are missing or need updates |
| 34 | + const filesToModify: string[] = []; |
| 35 | + if (missingCopilotInstructions) { |
| 36 | + filesToModify.push(".github/copilot-instructions.md"); |
| 37 | + } |
| 38 | + if (missingMcpConfig) { |
| 39 | + filesToModify.push(".vscode/mcp.json"); |
| 40 | + } |
| 41 | + |
| 42 | + const fileList = |
| 43 | + filesToModify.length === 1 |
| 44 | + ? filesToModify[0] |
| 45 | + : filesToModify.length === 2 |
| 46 | + ? `${filesToModify[0]} and ${filesToModify[1]}` |
| 47 | + : filesToModify.join(", "); |
| 48 | + |
| 49 | + const message = util.format(localize("teamstoolkit.mcpUtils.setupMcpServer.message"), fileList); |
| 50 | + |
| 51 | + ExtTelemetry.sendTelemetryEvent(TelemetryEvent.PromptMCPServer, { |
| 52 | + [TelemetryProperty.MissingCopilotInstructions]: String(missingCopilotInstructions), |
| 53 | + [TelemetryProperty.MissingMCPConfig]: String(missingMcpConfig), |
| 54 | + }); |
| 55 | + await vscode.window |
| 56 | + .showInformationMessage( |
| 57 | + message, |
| 58 | + localize("teamstoolkit.mcpUtils.setupMcpServer.confirm"), |
| 59 | + localize("teamstoolkit.mcpUtils.setupMcpServer.skip") |
| 60 | + ) |
| 61 | + .then((selection) => { |
| 62 | + if (selection !== localize("teamstoolkit.mcpUtils.setupMcpServer.confirm")) { |
| 63 | + ExtTelemetry.sendTelemetryEvent(TelemetryEvent.PromptMCPServer, { |
| 64 | + [TelemetryProperty.UserSelection]: "skip", |
| 65 | + }); |
| 66 | + return; // User chose to skip setup |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + if (missingCopilotInstructions) { |
| 71 | + createCopilotInstructionsFile(workspaceRoot, copilotInstructionsPath); |
| 72 | + } |
| 73 | + |
| 74 | + if (missingMcpConfig) { |
| 75 | + updateMCPConfigFile(workspaceRoot, mcpConfigPath); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + const errorMessage = util.format( |
| 79 | + localize("teamstoolkit.mcpUtils.setupMcpServer.errorMessage"), |
| 80 | + (error as Error).toString() |
| 81 | + ); |
| 82 | + void vscode.window.showErrorMessage(errorMessage); |
| 83 | + ExtTelemetry.sendTelemetryErrorEvent(TelemetryEvent.PromptMCPServer, error as FxError, { |
| 84 | + [TelemetryProperty.UserSelection]: "confirm", |
| 85 | + }); |
| 86 | + return; // Exit if there was an error creating files |
| 87 | + } |
| 88 | + |
| 89 | + const successMessage = localize("teamstoolkit.mcpUtils.setupMcpServer.successMessage"); |
| 90 | + void vscode.window.showInformationMessage(successMessage); |
| 91 | + ExtTelemetry.sendTelemetryEvent(TelemetryEvent.PromptMCPServer, { |
| 92 | + [TelemetryProperty.UserSelection]: "confirm", |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Create .github/copilot-instructions.md file with default content |
| 99 | + */ |
| 100 | +function createCopilotInstructionsFile( |
| 101 | + workspaceRoot: string, |
| 102 | + copilotInstructionsPath: string |
| 103 | +): void { |
| 104 | + const githubDir = path.join(workspaceRoot, ".github"); |
| 105 | + // Create .github directory if it doesn't exist |
| 106 | + if (!fs.existsSync(githubDir)) { |
| 107 | + fs.mkdirSync(githubDir, { recursive: true }); |
| 108 | + } |
| 109 | + |
| 110 | + // Get default content from template |
| 111 | + const templatePath = path.join( |
| 112 | + context?.extensionPath || "", |
| 113 | + "media/mcp", |
| 114 | + "copilot-instructions.md" |
| 115 | + ); |
| 116 | + |
| 117 | + const defaultContent = fs.readFileSync(templatePath, "utf8"); |
| 118 | + // Create copilot-instructions.md file with default content |
| 119 | + const fd = fs.openSync( |
| 120 | + copilotInstructionsPath, |
| 121 | + fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR, |
| 122 | + 0o600 |
| 123 | + ); |
| 124 | + fs.writeFileSync(fd, defaultContent, "utf8"); |
| 125 | + fs.closeSync(fd); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Create .vscode/mcp.json file with m365agentstoolkit mcp server configuration |
| 130 | + */ |
| 131 | +function updateMCPConfigFile(workspaceRoot: string, mcpConfigPath: string): void { |
| 132 | + if (!fs.existsSync(mcpConfigPath)) { |
| 133 | + const vscodeDir = path.join(workspaceRoot, ".vscode"); |
| 134 | + |
| 135 | + // Create .vscode directory if it doesn't exist |
| 136 | + if (!fs.existsSync(vscodeDir)) { |
| 137 | + fs.mkdirSync(vscodeDir, { recursive: true }); |
| 138 | + } |
| 139 | + |
| 140 | + // Create mcp.json with m365agentstoolkit server configuration |
| 141 | + const mcpConfig = { |
| 142 | + servers: { |
| 143 | + m365agentstoolkit: { |
| 144 | + command: "npx", |
| 145 | + args: ["@microsoft/m365agentstoolkit-mcp@latest", "server", "start"], |
| 146 | + }, |
| 147 | + }, |
| 148 | + }; |
| 149 | + |
| 150 | + fs.writeFileSync(mcpConfigPath, JSON.stringify(mcpConfig, null, 2), "utf8"); |
| 151 | + } else { |
| 152 | + // If mcp.json already exists, check if it needs to be updated |
| 153 | + const configContent = fs.readFileSync(mcpConfigPath, "utf8"); |
| 154 | + const config = JSON.parse(configContent); |
| 155 | + |
| 156 | + // Ensure servers object exists |
| 157 | + if (!config.servers || typeof config.servers !== "object") { |
| 158 | + config.servers = {}; |
| 159 | + } |
| 160 | + |
| 161 | + // Add m365agentstoolkit server configuration |
| 162 | + config.servers["M365AgentsToolkit MCP Server"] = { |
| 163 | + command: "npx", |
| 164 | + args: ["@microsoft/m365agentstoolkit-mcp@latest", "server", "start"], |
| 165 | + }; |
| 166 | + |
| 167 | + const fd = fs.openSync(mcpConfigPath, fs.constants.O_RDWR, 0o600); |
| 168 | + fs.writeFileSync(fd, JSON.stringify(config, null, 2), "utf8"); |
| 169 | + fs.closeSync(fd); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Check if MCP config file needs to be updated with proper m365agentstoolkit server configuration |
| 175 | + */ |
| 176 | +function checkMCPConfigNeedsUpdate(): boolean { |
| 177 | + try { |
| 178 | + const userMCPSettings = vscode.workspace.getConfiguration("mcp"); |
| 179 | + const userServers = userMCPSettings.get("servers"); |
| 180 | + if (userServers && JSON.stringify(userServers).includes("@microsoft/m365agentstoolkit-mcp")) { |
| 181 | + return false; // User already has m365agentstoolkit server configured |
| 182 | + } |
| 183 | + |
| 184 | + return true; |
| 185 | + } catch (error) { |
| 186 | + return true; // If we can't parse it, assume it needs update |
| 187 | + } |
| 188 | +} |
0 commit comments