feat: 实现 MCP (Model Context Protocol) 完整功能

主进程:
- 新增 mcp-manager.ts: JSON-RPC 2.0 over stdio 通信
  - 服务器生命周期管理 (spawn/initialize/handshake/stop)
  - tools/list 动态工具发现
  - tools/call 工具执行 (30 秒超时)
  - 消息分帧 (newline-delimited JSON)
  - 优雅清理 (pending 请求超时 + SIGTERM)

IPC 通道 (7 个):
- mcp:startServer / mcp:stopServer / mcp:stopAll
- mcp:callTool / mcp:getTools / mcp:getStatuses / mcp:refreshTools

渲染端:
- mcp-client.ts 完整重写: 配置管理 + 服务器生命周期 + 工具定义获取 + 工具执行
- tool-registry.ts: MCP 工具路由 (mcp_ 前缀识别 → executeMCPTool)
- settings-modal.ts: MCP 服务器管理 UI (添加/启用/禁用/删除/状态显示)
- main.ts: 启动时自动启动已启用的 MCP 服务器

数据流:
settings.mcp_servers → startAllMCPServers → initialize → tools/list → getMCPToolDefinitions → Agent Loop → mcp_ 工具调用 → callTool → JSON-RPC → MCP Server
This commit is contained in:
thzxx
2026-04-18 10:14:33 +08:00
parent d2468cd6fc
commit 22a33ae241
10 changed files with 539 additions and 27 deletions
+3
View File
@@ -11,6 +11,7 @@ import { createMenu } from './menu.js';
import { showNotification } from './utils.js';
import { ensureWorkspaceDir, killAllProcesses } from './workspace.js';
import { browserClose } from './browser.js';
import { stopAllServers } from './mcp-manager.js';
// ── 全局错误处理:写入文件 + 弹窗提示 ──
const ERROR_LOG = path.join(app.getPath('userData'), 'startup-error.log');
@@ -197,6 +198,8 @@ app.on('before-quit', () => {
isQuitting = true;
// 清理浏览器
browserClose();
// 清理 MCP 服务器
stopAllServers();
// 清理所有工作空间进程
killAllProcesses();
// 通知渲染进程释放显存