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
+9
View File
@@ -240,6 +240,15 @@ export interface MetonaDesktopAPI {
/** 终止当前 AI 命令 */
cmdKill: () => Promise<{ killed: boolean }>;
};
mcp: {
startServer: (config: { name: string; command: string; args: string[]; enabled: boolean; description?: string }) => Promise<{ success: boolean; error?: string }>;
stopServer: (name: string) => Promise<{ success: boolean }>;
stopAll: () => Promise<{ success: boolean }>;
callTool: (serverName: string, toolName: string, args: Record<string, unknown>) => Promise<{ success: boolean; result?: unknown; error?: string }>;
getTools: () => Promise<Array<{ serverName: string; name: string; fullName: string; description: string; inputSchema: { type: string; properties?: Record<string, unknown>; required?: string[] } }>>;
getStatuses: () => Promise<Array<{ name: string; running: boolean; toolCount: number }>>;
refreshTools: (name: string) => Promise<Array<{ name: string; description?: string; inputSchema: Record<string, unknown> }>>;
};
}
export interface AppInfo {