Skip to main content
Glama

MCP Advisor

MIT License
88
64
  • Apple
  • Linux
offline-search-data-loading-fix.md5.63 kB
# 离线搜索数据加载修复方案 ## 问题背景 MCPAdvisor 的离线搜索功能在端到端测试中出现了数据加载问题,导致召回结果为空或过滤后无结果。虽然单元测试通过,但实际使用体验不符合预期。主要问题在于离线测试环境下无法正确加载本地兜底 JSON 数据文件(`mcp_server_list.json`)。 ## 问题分析 1. **路径解析不一致**:测试环境下 `getDataDirPath` 和 `getMcpServerListPath` 函数解析的路径与实际数据文件位置不一致。 2. **缺乏兜底机制**:当指定路径找不到数据文件时,没有尝试其他可能的路径。 3. **测试覆盖不完整**:测试用例无法真正测试"文件不存在"的场景,因为路径兜底逻辑总能找到数据文件。 ## 解决方案 ### 1. 增强路径解析逻辑 修改 `pathUtils.ts` 中的路径解析函数,增加多级兜底路径尝试: - 尝试多个可能的项目路径(当前目录及其父目录) - 增加详细的日志记录,便于调试路径解析过程 - 支持从多个位置加载数据文件,提高鲁棒性 ```typescript export function getDataDirPath(): string { // 尝试多个可能的项目路径 const candidatePaths = [ process.cwd(), path.resolve(process.cwd(), '..'), path.resolve(process.cwd(), '../..'), path.resolve(process.cwd(), '../../..') ]; // 遍历尝试每个路径 for (const basePath of candidatePaths) { const dataDir = path.join(basePath, 'data'); if (fs.existsSync(dataDir)) { logger.debug(`找到数据目录: ${dataDir}`); return dataDir; } } // 兜底使用当前工作目录下的 data 文件夹 const fallbackPath = path.join(process.cwd(), 'data'); logger.debug(`未找到数据目录,使用兜底路径: ${fallbackPath}`); return fallbackPath; } ``` ### 2. 改进 OfflineDataLoader 类 修改 `OfflineDataLoader` 类,增加对测试场景的支持: - 添加 `disableFallbackPaths` 选项,允许测试精确控制路径解析行为 - 优化 `loadFallbackData` 方法,支持测试"文件不存在"的场景 - 增加详细日志,记录数据加载过程 ```typescript export class OfflineDataLoader { private readonly filePath: string; private readonly disableFallbackPaths: boolean; constructor(options?: { filePath?: string; disableFallbackPaths?: boolean; }) { this.filePath = options?.filePath || getMcpServerListPath(); this.disableFallbackPaths = options?.disableFallbackPaths || false; } async loadFallbackData(): Promise<MCPServerInfo[]> { if (this.disableFallbackPaths) { logger.info('禁用兜底路径,直接返回空数组'); return []; } try { // 尝试加载数据文件 const data = await fs.promises.readFile(this.filePath, 'utf8'); const servers = JSON.parse(data) as MCPServerInfo[]; logger.debug(`加载了 ${servers.length} 个原始服务器数据`); return servers; } catch (error) { logger.error(`加载兜底数据失败: ${formatError(error)}`); return []; } } } ``` ### 3. 修复测试用例 更新 `offlineDataLoader.test.ts` 中的测试用例,使用新增的 `disableFallbackPaths` 选项: ```typescript it('when data file doesn\'t exist should return empty array', async () => { // 使用 disableFallbackPaths 选项禁用兜底路径 const loader = new OfflineDataLoader({ filePath: '/non/existent/path.json', disableFallbackPaths: true }); const result = await loader.loadFallbackData(); expect(result).toEqual([]); }); ``` ### 4. 验证脚本 创建独立验证脚本 `verify-offline-data.ts`,用于测试离线数据加载和搜索功能: ```typescript // 验证离线数据加载 const dataLoader = new OfflineDataLoader(); const servers = await dataLoader.loadFallbackData(); console.log(`加载了 ${servers.length} 个服务器数据`); // 验证向量搜索引擎 const vectorEngine = new EnhancedMemoryVectorEngine(); await vectorEngine.addServers(servers); // 生成随机向量进行测试 const mockVector = Array(1536).fill(0).map(() => Math.random()); const results = await vectorEngine.search({ vector: mockVector, textQuery: "小红书", limit: 10, minSimilarity: 0.1 }); console.log(`搜索结果: ${results.length} 个匹配项`); ``` ## 测试验证 1. **单元测试**:所有测试用例通过,包括之前失败的"文件不存在"场景测试。 2. **离线搜索测试**:验证脚本确认离线数据可以正确加载,并且搜索功能能返回预期结果。 3. **端到端测试**:启动服务后,通过 REST API 验证搜索功能可以正确返回结果,特别是对"小红书"相关查询的处理。 ## 技术收益 1. **提高鲁棒性**:通过多级路径兜底机制,确保在不同环境下都能正确加载数据。 2. **增强可测试性**:通过 `disableFallbackPaths` 选项,支持精确测试各种边缘场景。 3. **改进调试体验**:详细的日志记录帮助快速定位问题。 4. **提升搜索质量**:确保离线搜索能够返回有意义的结果,提高用户体验。 ## 后续优化方向 1. **路径缓存**:考虑缓存已解析的路径,避免重复检查文件系统。 2. **配置化**:将路径解析逻辑配置化,支持通过环境变量或配置文件指定数据路径。 3. **数据预加载**:考虑在服务启动时预加载并验证数据文件,提前发现问题。 4. **监控指标**:添加数据加载成功率、搜索结果数量等监控指标,便于观察系统健康状况。

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/istarwyh/mcpadvisor'

If you have feedback or need assistance with the MCP directory API, please join our Discord server