fix: Resolve MCP installation issues and enhance path resolution

- Fixed API endpoint mismatches in mcp-manager.js to ensure global install/update buttons function correctly.
- Corrected undefined function references in mcp-manager.js for project installation.
- Refactored event handling to eliminate global scope pollution in mcp-manager.js.
- Added comprehensive debugging guide for MCP installation issues.
- Implemented a session path resolver to infer content types from filenames and paths, improving usability.
- Introduced tests for embeddings improvements in init and status commands to verify functionality.
This commit is contained in:
catlog22
2025-12-17 22:05:16 +08:00
parent 8b927f302c
commit b22839c99f
24 changed files with 2043 additions and 192 deletions

248
MCP_FINAL_FIX.md Normal file
View File

@@ -0,0 +1,248 @@
# CCW MCP 工具安装问题 - 最终修复报告
## 🎯 通过 Gemini AI 发现的关键问题
### 问题 4: 事件监听器注册失败 ⚠️ **根本原因** ✅ 已修复
**文件**: `views/mcp-manager.js` Line 1407
**严重性**: Critical - 导致所有按钮完全无响应
**原因**: 使用 `querySelector` 只返回第一个匹配元素,如果 DOM 中有多个元素或选择器匹配错误,事件监听器会附加到错误的元素上
**症状**: 按钮点击无任何反应,控制台无错误,调试困难
**修复前**:
```javascript
const btn = document.querySelector(`button[data-action="${action}"]`);
if (btn) {
btn.addEventListener('click', async (e) => {
e.preventDefault();
await handler();
});
}
```
**修复后**:
```javascript
const btns = document.querySelectorAll(`button[data-action="${action}"]`);
if (btns.length > 0) {
console.log(`[MCP] Attaching listener to ${action} (${btns.length} button(s) found)`);
btns.forEach(btn => {
btn.addEventListener('click', async (e) => {
e.preventDefault();
console.log(`[MCP] Button clicked: ${action}`);
try {
await handler();
} catch (err) {
console.error(`[MCP] Error executing handler for ${action}:`, err);
if (typeof showRefreshToast === 'function') {
showRefreshToast(`Action failed: ${err.message}`, 'error');
}
}
});
});
} else {
console.warn(`[MCP] No buttons found for action: ${action}`);
}
```
**修复优势**:
1. ✅ 使用 `querySelectorAll` 确保所有匹配按钮都绑定事件
2. ✅ 添加详细的控制台日志,便于调试
3. ✅ 添加错误捕获和用户友好的错误提示
4. ✅ 添加警告日志,发现选择器不匹配问题
---
### 问题 5: MCP 计数显示错误 ✅ 已修复
**文件**: `components/mcp-manager.js` Line 398
**严重性**: Medium - 显示 2/2 但实际只有 1 个服务器
**原因**: 路径格式不一致 - `loadMcpConfig` 使用 `\\``updateMcpBadge` 使用 `/`
**修复前**:
```javascript
function updateMcpBadge() {
const badge = document.getElementById('badgeMcpServers');
if (badge) {
const currentPath = projectPath; // Keep original format (forward slash)
const projectData = mcpAllProjects[currentPath];
// ...
}
}
```
**修复后**:
```javascript
function updateMcpBadge() {
const badge = document.getElementById('badgeMcpServers');
if (badge) {
// IMPORTANT: Use same path normalization as loadMcpConfig
const currentPath = projectPath.replace(/\//g, '\\');
const projectData = mcpAllProjects[currentPath];
// ...
console.log('[MCP Badge]', { currentPath, totalServers, enabledServers, disabledServers });
badge.textContent = `${enabledServers}/${totalServers}`;
}
}
```
---
## 📊 完整问题清单
| # | 问题 | 文件 | 严重性 | 状态 |
|---|------|------|--------|------|
| 1 | API 端点不匹配 | components/mcp-manager.js | Critical | ✅ 已修复 |
| 2 | 未定义函数引用 | views/mcp-manager.js | High | ✅ 已修复 |
| 3 | 全局作用域污染 | views/mcp-manager.js | Medium | ✅ 已修复 |
| 4 | **querySelector 单选器失效** | views/mcp-manager.js | **Critical** | ✅ 已修复 |
| 5 | MCP 计数路径不一致 | components/mcp-manager.js | Medium | ✅ 已修复 |
---
## 🔍 Gemini AI 分析价值
Gemini AI 深度分析提供了关键洞察:
1. **执行流程追踪**: 确认 `attachMcpEventListeners()` 在 DOM 更新后立即调用
2. **选择器匹配验证**: 发现 `data-action` 属性完全匹配
3. **根本原因识别**: 定位到 `querySelector` vs `querySelectorAll` 的关键差异
4. **静默失败诊断**: 指出缺少日志导致调试困难
5. **修复建议**: 提供完整的代码修复方案,包括错误处理
---
## 🧪 测试验证步骤
### 1. 重启服务
```bash
cd D:\Claude_dms3\ccw
npm run dev
```
### 2. 打开浏览器控制台
- 访问 `http://localhost:3456`
-`F12` 打开控制台
- **现在应该能看到调试日志**:
```
[MCP] Attaching listener to install-ccw-workspace (1 button(s) found)
[MCP] Attaching listener to install-ccw-global (1 button(s) found)
[MCP] Attaching listener to update-ccw-workspace (1 button(s) found)
[MCP] Attaching listener to update-ccw-global (1 button(s) found)
[MCP] Attaching listener to install-ccw-codex (1 button(s) found)
```
### 3. 点击按钮测试
点击任何 CCW Tools 按钮,应该看到:
```
[MCP] Button clicked: install-ccw-workspace
Installing CCW Tools MCP to workspace...
```
如果失败,会看到:
```
[MCP] Error executing handler for install-ccw-workspace: <错误详情>
```
### 4. 验证 MCP 计数
- 导航到 MCP Manager
- 检查左侧导航栏的 MCP 徽章
- **现在应该显示正确的计数** (1/1 而不是 2/2)
- 控制台会显示:
```
[MCP Badge] { currentPath: 'D:\\dongdiankaifa9', totalServers: 1, enabledServers: 1, disabledServers: [] }
```
---
## ✅ 预期结果
### 所有按钮现在应该正常工作
#### 1. 工作空间安装/更新
- **位置**: CCW Tools MCP 卡片
- **按钮**: "安装到工作空间" / "在工作空间更新"
- **预期**: 点击后显示加载提示,成功后显示成功消息
#### 2. 全局安装/更新
- **位置**: CCW Tools MCP 卡片
- **按钮**: "Install to global" / "Update Globally"
- **预期**: 更新 `~/.claude.json`
#### 3. Codex 安装
- **位置**: Codex 模式的 CCW Tools MCP 卡片
- **按钮**: "Install" / "Update"
- **预期**: 更新 `~/.codex/config.toml`
#### 4. 从其他项目安装
- **位置**: "Available from Other Projects" 部分
- **预期**: 弹出配置类型选择对话框
#### 5. MCP 计数显示
- **位置**: 左侧导航栏 MCP Manager 徽章
- **预期**: 显示正确的启用/总计数量 (例如 1/1)
---
## 🐛 调试指南
### 如果按钮仍然无响应
#### 检查控制台日志
如果看不到 `[MCP] Attaching listener` 日志:
- ❌ `attachMcpEventListeners()` 函数没有被调用
- 解决方法: 检查 `renderMcpManager()` 是否正确调用了该函数
#### 如果看到 "No buttons found"
```
[MCP] No buttons found for action: install-ccw-workspace
```
- ❌ `data-action` 属性不匹配或按钮未渲染
- 解决方法: 检查 HTML 生成逻辑,确保 `data-action` 属性正确设置
#### 如果看到点击日志但无反应
```
[MCP] Button clicked: install-ccw-workspace
```
但之后无进一步输出:
- ❌ 函数执行被阻塞或抛出未捕获的错误
- 解决方法: 检查 `installCcwToolsMcp()` 函数实现
#### 如果 MCP 计数仍然错误
控制台应该显示:
```
[MCP Badge] { currentPath: '...', totalServers: 1, enabledServers: 1, disabledServers: [] }
```
- 如果 `currentPath` 格式错误 (例如包含 `/` 而不是 `\\`),说明路径规范化失败
- 如果 `totalServers` 不正确,检查 `mcpAllProjects` 数据结构
---
## 📋 修复文件清单
```
D:\Claude_dms3\ccw\src\templates\dashboard-js\components\mcp-manager.js
- Line 398: updateMcpBadge 路径规范化修复
- Line 404: 添加调试日志
D:\Claude_dms3\ccw\src\templates\dashboard-js\views\mcp-manager.js
- Line 1407-1420: querySelector → querySelectorAll + 完整错误处理
```
---
## 🎯 关键收获
1. **工具链选择**: Gemini AI 在执行流程追踪和根本原因分析上表现出色
2. **调试友好**: 添加详细日志是诊断"静默失败"的关键
3. **健壮性设计**: `querySelectorAll` + 错误捕获 > 简单的 `querySelector`
4. **路径规范化**: 在 Windows 系统上,路径格式必须一致(统一使用 `\\`
---
## 🚀 下一步
重启服务后测试所有按钮功能。如果仍有问题,请提供:
1. 完整的浏览器控制台输出(包括 `[MCP]` 前缀的所有日志)
2. Network 标签中的 API 请求详情
3. 具体点击的按钮和操作步骤
所有修复已合并到代码库,重新编译完成。