From eb8fb51283f61c9b7deded93aa80dce8f0d9e7cd Mon Sep 17 00:00:00 2001 From: douboer Date: Wed, 15 Oct 2025 10:29:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20MCP=20=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=9C=AA=E6=B3=A8=E5=85=A5=E5=88=B0=20AI=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关键修复: - chatService 现在使用 mcpClientService 单例而不是创建新实例 - 这确保使用的是应用中实际连接的 MCP 服务器 问题原因: - chatService 之前创建了独立的 MCPClientService 实例 - 该实例没有任何已连接的 MCP 服务器 - 导致 getTools() 返回空数组 - AI 请求中没有包含工具定义 增强的调试日志: - MCPClientService.getTools 现在显示连接的服务器列表 - 显示 capabilities 和 tools 的详细信息 - chatService 显示 MCP 工具获取和转换过程 现在工具调用流程应该正常工作: 1. 用户选择 MCP 服务器 2. chatService 从 mcpClientService 获取工具列表 3. 工具被转换为 OpenAI 格式并注入请求 4. AI 可以识别并调用工具 --- web/src/components/Chat/ChatLayout.vue | 15 ++++----------- web/src/services/MCPClientService.ts | 15 +++++++++++++-- web/src/services/chatService.ts | 9 ++++++--- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/web/src/components/Chat/ChatLayout.vue b/web/src/components/Chat/ChatLayout.vue index 8bb81ab..e829ec1 100644 --- a/web/src/components/Chat/ChatLayout.vue +++ b/web/src/components/Chat/ChatLayout.vue @@ -114,16 +114,6 @@ - - - - 添加服务器... - - - -
+
- | + | + +
diff --git a/web/src/services/MCPClientService.ts b/web/src/services/MCPClientService.ts index e38d510..bd74b90 100644 --- a/web/src/services/MCPClientService.ts +++ b/web/src/services/MCPClientService.ts @@ -307,12 +307,23 @@ export class MCPClientService { * 获取服务器的工具列表 */ getTools(serverId: string): Tool[] { + console.log('🔍 [MCPClientService.getTools] 请求获取工具:', serverId); + console.log('🔍 [MCPClientService.getTools] 当前连接的服务器:', Array.from(this.clients.keys())); + const serverInfo = this.clients.get(serverId); if (!serverInfo) { - console.warn(`服务器 ${serverId} 未连接`); + console.warn(`❌ [MCPClientService.getTools] 服务器 ${serverId} 未连接`); return []; } - return serverInfo.capabilities?.tools || []; + + console.log('✅ [MCPClientService.getTools] 找到服务器信息'); + console.log('🔍 [MCPClientService.getTools] capabilities:', serverInfo.capabilities); + console.log('🔍 [MCPClientService.getTools] tools:', serverInfo.capabilities?.tools); + + const tools = serverInfo.capabilities?.tools || []; + console.log(`📋 [MCPClientService.getTools] 返回 ${tools.length} 个工具`); + + return tools; } /** diff --git a/web/src/services/chatService.ts b/web/src/services/chatService.ts index 10a8293..f3b7c94 100644 --- a/web/src/services/chatService.ts +++ b/web/src/services/chatService.ts @@ -7,13 +7,13 @@ import type { TopicFilter } from '../types/chat' import { modelServiceManager } from './modelServiceManager' -import { MCPClientService } from './MCPClientService' +import { mcpClientService } from './MCPClientService' class ChatService { private static instance: ChatService private topics: Map = new Map() private conversations: Map = new Map() - private mcpClient: MCPClientService = new MCPClientService() + private mcpClient = mcpClientService // 使用单例实例 static getInstance(): ChatService { if (!ChatService.instance) { @@ -593,8 +593,11 @@ class ChatService { if (mcpServerId) { console.log('🔧 [callModelStream] 获取 MCP 服务器工具:', mcpServerId) const mcpTools = this.mcpClient.getTools(mcpServerId) + console.log('🔧 [callModelStream] MCP 原始工具列表:', mcpTools) tools = this.convertToolsToOpenAIFormat(mcpTools) - console.log('🔧 [callModelStream] 转换后的工具:', tools.length, '个') + console.log('🔧 [callModelStream] 转换后的工具:', tools.length, '个', tools) + } else { + console.log('⚠️ [callModelStream] 未选择 MCP 服务器,不注入工具') } // 准备消息历史