fix: 修复 MCP 工具未注入到 AI 请求的问题
关键修复: - chatService 现在使用 mcpClientService 单例而不是创建新实例 - 这确保使用的是应用中实际连接的 MCP 服务器 问题原因: - chatService 之前创建了独立的 MCPClientService 实例 - 该实例没有任何已连接的 MCP 服务器 - 导致 getTools() 返回空数组 - AI 请求中没有包含工具定义 增强的调试日志: - MCPClientService.getTools 现在显示连接的服务器列表 - 显示 capabilities 和 tools 的详细信息 - chatService 显示 MCP 工具获取和转换过程 现在工具调用流程应该正常工作: 1. 用户选择 MCP 服务器 2. chatService 从 mcpClientService 获取工具列表 3. 工具被转换为 OpenAI 格式并注入请求 4. AI 可以识别并调用工具
This commit is contained in:
@@ -114,16 +114,6 @@
|
|||||||
</n-button>
|
</n-button>
|
||||||
</n-dropdown>
|
</n-dropdown>
|
||||||
|
|
||||||
<!-- 添加服务器 -->
|
|
||||||
<n-button size="small" text>
|
|
||||||
<template #icon>
|
|
||||||
<n-icon :component="PlusIcon" />
|
|
||||||
</template>
|
|
||||||
添加服务器...
|
|
||||||
</n-button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="toolbar-right">
|
|
||||||
<!-- 模型选择器 -->
|
<!-- 模型选择器 -->
|
||||||
<n-dropdown
|
<n-dropdown
|
||||||
trigger="click"
|
trigger="click"
|
||||||
@@ -138,8 +128,11 @@
|
|||||||
<n-icon :component="ChevronDownIcon" size="14" style="margin-left: 4px;" />
|
<n-icon :component="ChevronDownIcon" size="14" style="margin-left: 4px;" />
|
||||||
</n-button>
|
</n-button>
|
||||||
</n-dropdown>
|
</n-dropdown>
|
||||||
|
</div>
|
||||||
|
|
||||||
<span class="toolbar-divider">|</span>
|
<span class="toolbar-divider">|</span>
|
||||||
|
|
||||||
|
<div class="toolbar-right">
|
||||||
|
|
||||||
<!-- 快捷操作 -->
|
<!-- 快捷操作 -->
|
||||||
<n-button-group size="small">
|
<n-button-group size="small">
|
||||||
|
|||||||
@@ -307,12 +307,23 @@ export class MCPClientService {
|
|||||||
* 获取服务器的工具列表
|
* 获取服务器的工具列表
|
||||||
*/
|
*/
|
||||||
getTools(serverId: string): Tool[] {
|
getTools(serverId: string): Tool[] {
|
||||||
|
console.log('🔍 [MCPClientService.getTools] 请求获取工具:', serverId);
|
||||||
|
console.log('🔍 [MCPClientService.getTools] 当前连接的服务器:', Array.from(this.clients.keys()));
|
||||||
|
|
||||||
const serverInfo = this.clients.get(serverId);
|
const serverInfo = this.clients.get(serverId);
|
||||||
if (!serverInfo) {
|
if (!serverInfo) {
|
||||||
console.warn(`服务器 ${serverId} 未连接`);
|
console.warn(`❌ [MCPClientService.getTools] 服务器 ${serverId} 未连接`);
|
||||||
return [];
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ import type {
|
|||||||
TopicFilter
|
TopicFilter
|
||||||
} from '../types/chat'
|
} from '../types/chat'
|
||||||
import { modelServiceManager } from './modelServiceManager'
|
import { modelServiceManager } from './modelServiceManager'
|
||||||
import { MCPClientService } from './MCPClientService'
|
import { mcpClientService } from './MCPClientService'
|
||||||
|
|
||||||
class ChatService {
|
class ChatService {
|
||||||
private static instance: ChatService
|
private static instance: ChatService
|
||||||
private topics: Map<string, Topic> = new Map()
|
private topics: Map<string, Topic> = new Map()
|
||||||
private conversations: Map<string, Conversation> = new Map()
|
private conversations: Map<string, Conversation> = new Map()
|
||||||
private mcpClient: MCPClientService = new MCPClientService()
|
private mcpClient = mcpClientService // 使用单例实例
|
||||||
|
|
||||||
static getInstance(): ChatService {
|
static getInstance(): ChatService {
|
||||||
if (!ChatService.instance) {
|
if (!ChatService.instance) {
|
||||||
@@ -593,8 +593,11 @@ class ChatService {
|
|||||||
if (mcpServerId) {
|
if (mcpServerId) {
|
||||||
console.log('🔧 [callModelStream] 获取 MCP 服务器工具:', mcpServerId)
|
console.log('🔧 [callModelStream] 获取 MCP 服务器工具:', mcpServerId)
|
||||||
const mcpTools = this.mcpClient.getTools(mcpServerId)
|
const mcpTools = this.mcpClient.getTools(mcpServerId)
|
||||||
|
console.log('🔧 [callModelStream] MCP 原始工具列表:', mcpTools)
|
||||||
tools = this.convertToolsToOpenAIFormat(mcpTools)
|
tools = this.convertToolsToOpenAIFormat(mcpTools)
|
||||||
console.log('🔧 [callModelStream] 转换后的工具:', tools.length, '个')
|
console.log('🔧 [callModelStream] 转换后的工具:', tools.length, '个', tools)
|
||||||
|
} else {
|
||||||
|
console.log('⚠️ [callModelStream] 未选择 MCP 服务器,不注入工具')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 准备消息历史
|
// 准备消息历史
|
||||||
|
|||||||
Reference in New Issue
Block a user