From e894ab0c4cf9baa7f9615c997d58ac69ac9997a5 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Sun, 27 Apr 2025 14:25:37 +0800 Subject: [PATCH] feat(MCPService): add method to find PowerShell executable path (#5393) - Implemented `findPowerShellExecutable` to determine the correct PowerShell executable path on Windows. - Updated `getSystemPath` to utilize the new method for improved path retrieval. --- src/main/services/MCPService.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index 49e648336c..2bdb31d228 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -567,13 +567,26 @@ class McpService { return await cachedGetResource(server, uri) } + private findPowerShellExecutable() { + const psPath = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' // Standard WinPS path + const pwshPath = 'C:\\Program Files\\PowerShell\\7\\pwsh.exe' + + if (fs.existsSync(psPath)) { + return psPath + } + if (fs.existsSync(pwshPath)) { + return pwshPath + } + return 'powershell.exe' + } + private getSystemPath = memoize(async (): Promise => { return new Promise((resolve, reject) => { let command: string let shell: string if (process.platform === 'win32') { - shell = 'powershell.exe' + shell = this.findPowerShellExecutable() command = '$env:PATH' } else { // 尝试获取当前用户的默认 shell @@ -625,6 +638,10 @@ class McpService { console.error('Error getting PATH:', data.toString()) }) + child.on('error', (error: Error) => { + reject(new Error(`Failed to get system PATH, ${error.message}`)) + }) + child.on('close', (code: number) => { if (code === 0) { const trimmedPath = path.trim()