diff --git a/CHANGELOG.md b/CHANGELOG.md index cdb0392..373c0c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [main] 0.0.4 - 修复选择模型后点击确认跳转回 deepseek 的 bug - 修复 mcp 项目初始化点击工具全部都是空的 bug +- 修复无法重新连接的 bug ## [main] 0.0.3 diff --git a/service/src/controller/index.ts b/service/src/controller/index.ts index 2dd6819..ad8671d 100644 --- a/service/src/controller/index.ts +++ b/service/src/controller/index.ts @@ -7,10 +7,32 @@ import { panelLoadHandler, panelSaveHandler } from './panel'; import { settingLoadHandler, settingSaveHandler } from './setting'; import { ping } from './util'; +import { spawnSync } from 'node:child_process'; + // TODO: 支持更多的 client let client: MCPClient | undefined = undefined; +function tryGetRunCommandError(command: string, args: string[] = [], cwd?: string): string | null { + try { + const result = spawnSync(command, args, { + cwd: cwd || process.cwd(), + stdio: 'pipe', + encoding: 'utf-8' + }); + + if (result.error) { + return result.error.message; + } + if (result.status !== 0) { + return result.stderr || `Command failed with code ${result.status}`; + } + return null; + } catch (error) { + return error instanceof Error ? error.message : String(error); + } +} + async function connectHandler(option: MCPOptions, webview: PostMessageble) { try { console.log('ready to connect', option); @@ -27,11 +49,17 @@ async function connectHandler(option: MCPOptions, webview: PostMessageble) { // 比如 error: Failed to spawn: `server.py` // Caused by: No such file or directory (os error 2) - console.log('error', error); + let errorMsg = ''; + + if (option.command) { + errorMsg += tryGetRunCommandError(option.command, option.args, option.cwd); + } + + errorMsg += (error as any).toString(); const connectResult = { code: 500, - msg: (error as any).toString() + msg: errorMsg }; webview.postMessage({ command: 'connect', data: connectResult }); }