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
+10
View File
@@ -347,6 +347,16 @@
<button class="btn btn-outline" id="btnDoctor" style="margin-bottom:8px;">🩺 一键诊断</button>
<div id="doctorResults" style="display:none;margin-top:8px;padding:10px;background:var(--bg-layer);border-radius:var(--radius-control);font-size:12px;max-height:200px;overflow-y:auto;"></div>
</div>
<div class="setting-group">
<label class="setting-label">🔌 MCP 服务器</label>
<div style="display:flex;gap:8px;margin-bottom:8px;">
<input class="setting-input" id="inputMCPName" type="text" placeholder="名称" style="margin-bottom:0;width:100px;">
<input class="setting-input" id="inputMCPCommand" type="text" placeholder="命令 (如: npx)" style="margin-bottom:0;width:100px;">
<input class="setting-input" id="inputMCPArgs" type="text" placeholder="参数 (空格分隔)" style="margin-bottom:0;flex:1;">
<button class="btn btn-sm btn-outline" id="btnAddMCPServer"> 添加</button>
</div>
<div id="mcpServerList" style="font-size:12px;"></div>
</div>
<div class="setting-group">
<label class="setting-label">⏰ 定时任务 (Cron)</label>
<div style="display:flex;gap:8px;margin-bottom:8px;">