closer-code 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/.closer-code.example.json +32 -0
  2. package/DUAL_OPTIMIZATION_COMPLETE.md +293 -0
  3. package/README.md +167 -557
  4. package/README_OPENAI.md +163 -0
  5. package/THINKING_THROTTLING_OPTIMIZATION.md +244 -0
  6. package/THROTTLING_1_5S_OPTIMIZATION.md +401 -0
  7. package/TOOLS_IMPROVEMENTS_SUMMARY.md +273 -0
  8. package/cloco.md +5 -1
  9. package/config.example.json +15 -94
  10. package/config.mcp.example.json +81 -0
  11. package/dist/bash-runner.js +5 -126
  12. package/dist/batch-cli.js +286 -20658
  13. package/dist/closer-cli.js +329 -21135
  14. package/dist/index.js +308 -31036
  15. package/docs/ANTHROPIC_TOOL_ERROR_HANDLING.md +220 -0
  16. package/docs/BUILD_COMMANDS.md +79 -0
  17. package/docs/CTRL_Z_SUPPORT.md +189 -0
  18. package/docs/DEEPSEEK_R1_INTEGRATION.md +427 -0
  19. package/docs/FIX_OPENAI_TOOL_ERROR_HANDLING.md +375 -0
  20. package/docs/FIX_OPENAI_TOOL_RESULT.md +198 -0
  21. package/docs/INPUT_ENHANCEMENTS.md +192 -0
  22. package/docs/MCP_IMPLEMENTATION_SUMMARY.md +428 -0
  23. package/docs/MCP_INTEGRATION.md +418 -0
  24. package/docs/MCP_QUICKSTART.md +299 -0
  25. package/docs/MCP_README.md +166 -0
  26. package/docs/MINIFY_BUILD.md +180 -0
  27. package/docs/MULTILINE_INPUT_FEATURE.md +119 -0
  28. package/docs/OPENAI_CLIENT.md +258 -0
  29. package/docs/PROJECT_LOCAL_CONFIG.md +471 -0
  30. package/docs/PROJECT_LOCAL_CONFIG_SUMMARY.md +407 -0
  31. package/docs/REFACTOR_CONVERSATION.md +306 -0
  32. package/docs/REGION_EDIT_DESIGN.md +475 -0
  33. package/docs/SIGNAL_HANDLING.md +171 -0
  34. package/docs/STREAM_UPDATE_THROTTLE.md +273 -0
  35. package/docs/TOOLS_REFACTOR_PLAN.md +520 -0
  36. package/ds_r1.md +249 -0
  37. package/examples/abort-fence-example.js +294 -0
  38. package/package.json +18 -4
  39. package/src/ai-client-legacy.js +6 -1
  40. package/src/ai-client-openai.js +672 -0
  41. package/src/ai-client.js +30 -13
  42. package/src/closer-cli.jsx +450 -162
  43. package/src/components/fullscreen-conversation.jsx +157 -0
  44. package/src/components/ink-text-input/index.jsx +324 -0
  45. package/src/components/multiline-text-input.jsx +614 -0
  46. package/src/components/progress-bar.jsx +135 -0
  47. package/src/components/tool-detail-view.jsx +82 -0
  48. package/src/components/tool-renderers/bash-renderer.jsx +197 -0
  49. package/src/components/tool-renderers/file-edit-renderer.jsx +247 -0
  50. package/src/components/tool-renderers/file-read-renderer.jsx +261 -0
  51. package/src/components/tool-renderers/file-write-renderer.jsx +222 -0
  52. package/src/components/tool-renderers/index.jsx +178 -0
  53. package/src/components/tool-renderers/list-renderer.jsx +274 -0
  54. package/src/components/tool-renderers/search-renderer.jsx +248 -0
  55. package/src/config.js +182 -20
  56. package/src/conversation/abort-fence.js +158 -0
  57. package/src/conversation/core.js +377 -0
  58. package/src/conversation/index.js +33 -0
  59. package/src/conversation/mcp-integration.js +96 -0
  60. package/src/conversation/plan-manager.js +295 -0
  61. package/src/conversation/stream-handler.js +154 -0
  62. package/src/conversation/tool-executor.js +264 -0
  63. package/src/conversation.js +23 -958
  64. package/src/hooks/use-throttled-state.js +158 -0
  65. package/src/input/enhanced-input.jsx +268 -0
  66. package/src/input/history.js +342 -0
  67. package/src/logger.js +20 -0
  68. package/src/mcp/client.js +275 -0
  69. package/src/mcp/tools-adapter.js +149 -0
  70. package/src/planner.js +18 -5
  71. package/src/prompt-builder.js +159 -0
  72. package/src/tools.js +457 -25
  73. package/src/utils/json-parser.js +231 -0
  74. package/src/utils/json-repair.js +146 -0
  75. package/src/utils/platform.js +259 -0
  76. package/test/test-ctrl-bf.js +121 -0
  77. package/test/test-deepseek-reasoning.js +118 -0
  78. package/test/test-history-navigation.js +80 -0
  79. package/test/test-input-fix.js +105 -0
  80. package/test/test-input-history.js +98 -0
  81. package/test/test-mcp.js +115 -0
  82. package/test/test-openai-client.js +152 -0
  83. package/test/test-openai-tool-result.js +199 -0
  84. package/test/test-project-config.js +106 -0
  85. package/test/test-shortcuts.js +79 -0
  86. package/test/test-stream-throttle.js +124 -0
  87. package/test/test-tool-error-handling.js +95 -0
  88. package/test/verify-input-fix.sh +35 -0
  89. package/test-abort-fence.js +263 -0
  90. package/test-abort-fix.js +54 -0
  91. package/test-abort-new-conversation.js +75 -0
  92. package/test-ctrl-z.js +54 -0
  93. package/test-file-read.js +105 -0
  94. package/test-tool-display.js +127 -0
  95. package/src/closer-cli.jsx.backup +0 -948
  96. package/test/workflows/longtalk/cloco.md +0 -19
  97. package/test/workflows/longtalk/emoji_500.txt +0 -63
  98. package/test/workflows/longtalk/emoji_list.txt +0 -20
  99. package/test-ctrl-c.jsx +0 -126
@@ -0,0 +1,418 @@
1
+ # MCP 集成指南
2
+
3
+ ## 概述
4
+
5
+ Closer Code 现在支持作为 **MCP Client** 连接到外部的 MCP Servers,从而扩展 AI 助理的能力。
6
+
7
+ ### 什么是 MCP?
8
+
9
+ MCP (Model Context Protocol) 是一个开放协议,允许 AI 应用与外部数据源和工具进行标准化通信。
10
+
11
+ ### 支持的 MCP Servers
12
+
13
+ 你可以连接到任何符合 MCP 标准的服务器,包括:
14
+
15
+ - **官方 MCP Servers**:
16
+ - `@modelcontextprotocol/server-filesystem` - 文件系统访问
17
+ - `@modelcontextprotocol/server-git` - Git 操作
18
+ - `@modelcontextprotocol/server-postgres` - PostgreSQL 数据库
19
+ - `@modelcontextprotocol/server-sqlite` - SQLite 数据库
20
+ - `@modelcontextprotocol/server-brave-search` - Brave 搜索
21
+ - `@modelcontextprotocol/server-github` - GitHub API
22
+
23
+ - **自定义 MCP Servers**:
24
+ - 任何你自己开发的 MCP Server
25
+ - 第三方 MCP Servers
26
+
27
+ ## 配置
28
+
29
+ ### 1. 启用 MCP
30
+
31
+ 在配置文件 `~/.closer-code/config.json` 中添加 MCP 配置:
32
+
33
+ ```json
34
+ {
35
+ "mcp": {
36
+ "enabled": true,
37
+ "servers": {
38
+ "filesystem": {
39
+ "enabled": true,
40
+ "command": "npx",
41
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/allowed/path"],
42
+ "env": {}
43
+ }
44
+ }
45
+ }
46
+ }
47
+ ```
48
+
49
+ ### 2. 配置示例
50
+
51
+ #### 文件系统 MCP Server
52
+
53
+ ```json
54
+ {
55
+ "mcp": {
56
+ "enabled": true,
57
+ "servers": {
58
+ "filesystem": {
59
+ "enabled": true,
60
+ "command": "npx",
61
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/projects"],
62
+ "env": {}
63
+ }
64
+ }
65
+ }
66
+ }
67
+ ```
68
+
69
+ #### Git MCP Server
70
+
71
+ ```json
72
+ {
73
+ "mcp": {
74
+ "enabled": true,
75
+ "servers": {
76
+ "git": {
77
+ "enabled": true,
78
+ "command": "npx",
79
+ "args": ["-y", "@modelcontextprotocol/server-git"],
80
+ "env": {}
81
+ }
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ #### PostgreSQL MCP Server
88
+
89
+ ```json
90
+ {
91
+ "mcp": {
92
+ "enabled": true,
93
+ "servers": {
94
+ "postgres": {
95
+ "enabled": true,
96
+ "command": "npx",
97
+ "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:password@localhost:5432/mydb"],
98
+ "env": {}
99
+ }
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ #### 自定义 MCP Server
106
+
107
+ ```json
108
+ {
109
+ "mcp": {
110
+ "enabled": true,
111
+ "servers": {
112
+ "my-custom-server": {
113
+ "enabled": true,
114
+ "command": "node",
115
+ "args": ["/path/to/custom-mcp-server.js"],
116
+ "env": {
117
+ "API_KEY": "your-api-key",
118
+ "API_ENDPOINT": "https://api.example.com"
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ ### 3. 完整配置示例
127
+
128
+ 参考 `config.mcp.example.json` 文件获取完整的配置示例。
129
+
130
+ ## 使用方法
131
+
132
+ ### 启动 Closer Code
133
+
134
+ ```bash
135
+ # 交互模式
136
+ cloco
137
+
138
+ # 批处理模式
139
+ cloco -b "查询数据库中的用户表"
140
+ ```
141
+
142
+ ### 使用 MCP 工具
143
+
144
+ 启动后,AI 会自动加载所有启用的 MCP 工具。你可以直接使用:
145
+
146
+ ```
147
+ ❯ 查询 PostgreSQL 数据库中的所有用户
148
+ ❯ 读取 /home/user/projects/config.json 文件
149
+ ❯ 获取 GitHub 仓库的 issue 列表
150
+ ❯ 使用 Brave 搜索最新新闻
151
+ ```
152
+
153
+ ### 查看可用的 MCP 工具
154
+
155
+ 启动时会显示加载的 MCP 工具:
156
+
157
+ ```
158
+ [MCP] Initializing MCP Client...
159
+ [MCP] ✓ Loaded 15 tools from MCP Servers
160
+ [MCP] Available MCP tools:
161
+ - filesystem_read_file [from filesystem]
162
+ - filesystem_write_file [from filesystem]
163
+ - filesystem_list_directory [from filesystem]
164
+ - git_clone [from git]
165
+ - git_commit [from git]
166
+ - postgres_query [from postgres]
167
+ ...
168
+ ```
169
+
170
+ ## 工具命名规则
171
+
172
+ MCP 工具在 Closer Code 中使用以下命名格式:
173
+
174
+ ```
175
+ {serverName}_{toolName}
176
+ ```
177
+
178
+ 例如:
179
+ - `filesystem_read_file`
180
+ - `git_clone`
181
+ - `postgres_query`
182
+
183
+ ## 开发自定义 MCP Server
184
+
185
+ ### 基本结构
186
+
187
+ ```javascript
188
+ // custom-mcp-server.js
189
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
190
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
191
+
192
+ const server = new Server(
193
+ { name: 'my-custom-server', version: '1.0.0' },
194
+ { capabilities: { tools: {} } }
195
+ );
196
+
197
+ // 注册工具
198
+ server.setRequestHandler('tools/list', async () => ({
199
+ tools: [
200
+ {
201
+ name: 'my_tool',
202
+ description: 'My custom tool',
203
+ inputSchema: {
204
+ type: 'object',
205
+ properties: {
206
+ param1: { type: 'string', description: 'Parameter 1' }
207
+ },
208
+ required: ['param1']
209
+ }
210
+ }
211
+ ]
212
+ }));
213
+
214
+ // 处理工具调用
215
+ server.setRequestHandler('tools/call', async (request) => {
216
+ const { name, arguments: args } = request.params;
217
+
218
+ if (name === 'my_tool') {
219
+ // 执行你的逻辑
220
+ const result = doSomething(args.param1);
221
+
222
+ return {
223
+ content: [{ type: 'text', text: JSON.stringify(result) }]
224
+ };
225
+ }
226
+
227
+ throw new Error(`Tool ${name} not found`);
228
+ });
229
+
230
+ // 启动服务器
231
+ const transport = new StdioServerTransport();
232
+ await server.connect(transport);
233
+ ```
234
+
235
+ ### 在 Closer Code 中使用
236
+
237
+ ```json
238
+ {
239
+ "mcp": {
240
+ "enabled": true,
241
+ "servers": {
242
+ "my-custom": {
243
+ "enabled": true,
244
+ "command": "node",
245
+ "args": ["/path/to/custom-mcp-server.js"],
246
+ "env": {}
247
+ }
248
+ }
249
+ }
250
+ }
251
+ ```
252
+
253
+ ## 故障排查
254
+
255
+ ### MCP Server 连接失败
256
+
257
+ ```
258
+ [MCP] ✗ 连接 MCP Server 失败: postgres - Error: Connection refused
259
+ ```
260
+
261
+ **解决方案**:
262
+ 1. 检查 MCP Server 是否正确安装
263
+ 2. 验证命令和参数是否正确
264
+ 3. 查看环境变量配置
265
+
266
+ ### 工具调用失败
267
+
268
+ ```
269
+ [MCP] MCP 工具调用失败: postgres_query - relation "users" does not exist
270
+ ```
271
+
272
+ **解决方案**:
273
+ 1. 检查工具参数是否正确
274
+ 2. 验证 MCP Server 的配置
275
+ 3. 查看 MCP Server 的日志
276
+
277
+ ### 性能问题
278
+
279
+ 如果连接多个 MCP Servers 导致性能问题:
280
+
281
+ 1. **禁用不需要的 Servers**:
282
+ ```json
283
+ {
284
+ "mcp": {
285
+ "servers": {
286
+ "unneeded-server": {
287
+ "enabled": false
288
+ }
289
+ }
290
+ }
291
+ }
292
+ ```
293
+
294
+ 2. **优化 Server 配置**:
295
+ - 减少 Server 数量
296
+ - 使用本地 Server 而非远程
297
+
298
+ ## 最佳实践
299
+
300
+ ### 1. 安全性
301
+
302
+ - ⚠️ **谨慎授予权限**: 只授予必要的文件系统路径
303
+ - 🔒 **保护敏感信息**: 使用环境变量存储 API Keys
304
+ - 🚫 **禁用未使用的 Servers**: 减少攻击面
305
+
306
+ ### 2. 性能优化
307
+
308
+ - ✅ **按需启用**: 只启用需要的 MCP Servers
309
+ - ✅ **本地优先**: 优先使用本地 MCP Servers
310
+ - ✅ **缓存结果**: 对于重复查询,考虑缓存
311
+
312
+ ### 3. 工具命名
313
+
314
+ - 📝 **描述性名称**: 使用清晰的工具名称
315
+ - 🏷️ **前缀规范**: 使用 serverName 作为前缀避免冲突
316
+
317
+ ## 示例场景
318
+
319
+ ### 场景 1:数据库查询助手
320
+
321
+ ```json
322
+ {
323
+ "mcp": {
324
+ "enabled": true,
325
+ "servers": {
326
+ "postgres": {
327
+ "enabled": true,
328
+ "command": "npx",
329
+ "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost:5432/mydb"]
330
+ }
331
+ }
332
+ }
333
+ }
334
+ ```
335
+
336
+ **使用**:
337
+ ```
338
+ ❯ 查询所有订单状态为 "pending" 的记录
339
+ ❯ 统计每个用户的订单数量
340
+ ❯ 查找最近 7 天的销售额
341
+ ```
342
+
343
+ ### 场景 2:GitHub 代码审查助手
344
+
345
+ ```json
346
+ {
347
+ "mcp": {
348
+ "enabled": true,
349
+ "servers": {
350
+ "github": {
351
+ "enabled": true,
352
+ "command": "npx",
353
+ "args": ["-y", "@modelcontextprotocol/server-github"],
354
+ "env": {
355
+ "GITHUB_TOKEN": "your-github-token"
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+ ```
362
+
363
+ **使用**:
364
+ ```
365
+ ❯ 获取我的所有 open pull requests
366
+ ❯ 查看 repo 中的 issue 列表
367
+ ❞ 检查某个 PR 的代码变更
368
+ ```
369
+
370
+ ### 场景 3:多数据源分析
371
+
372
+ ```json
373
+ {
374
+ "mcp": {
375
+ "enabled": true,
376
+ "servers": {
377
+ "postgres": {
378
+ "enabled": true,
379
+ "command": "npx",
380
+ "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://..."]
381
+ },
382
+ "filesystem": {
383
+ "enabled": true,
384
+ "command": "npx",
385
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/data"]
386
+ },
387
+ "brave-search": {
388
+ "enabled": true,
389
+ "command": "npx",
390
+ "args": ["-y", "@modelcontextprotocol/server-brave-search"],
391
+ "env": {
392
+ "BRAVE_API_KEY": "your-api-key"
393
+ }
394
+ }
395
+ }
396
+ }
397
+ }
398
+ ```
399
+
400
+ **使用**:
401
+ ```
402
+ ❯ 从数据库读取用户数据,从文件系统读取日志,然后分析异常
403
+ ❞ 搜索最新的技术文档,然后结合项目代码给出建议
404
+ ```
405
+
406
+ ## 相关资源
407
+
408
+ - [MCP 官方文档](https://modelcontextprotocol.io/)
409
+ - [MCP SDK GitHub](https://github.com/modelcontextprotocol/typescript-sdk)
410
+ - [官方 MCP Servers](https://github.com/modelcontextprotocol/servers)
411
+
412
+ ## 贡献
413
+
414
+ 如果你开发了自定义的 MCP Server 并希望与社区分享,欢迎提交 PR!
415
+
416
+ ## 许可证
417
+
418
+ MIT License
@@ -0,0 +1,299 @@
1
+ # MCP 快速开始指南
2
+
3
+ ## 5 分钟快速上手
4
+
5
+ ### 步骤 1: 安装依赖(已完成)
6
+
7
+ ```bash
8
+ npm install @modelcontextprotocol/sdk
9
+ ```
10
+
11
+ ### 步骤 2: 配置 MCP Server
12
+
13
+ 编辑 `~/.closer-code/config.json`,添加 MCP 配置:
14
+
15
+ ```json
16
+ {
17
+ "mcp": {
18
+ "enabled": true,
19
+ "servers": {
20
+ "filesystem": {
21
+ "enabled": true,
22
+ "command": "npx",
23
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/projects"],
24
+ "env": {}
25
+ }
26
+ }
27
+ }
28
+ }
29
+ ```
30
+
31
+ ### 步骤 3: 启动 Closer Code
32
+
33
+ ```bash
34
+ cloco
35
+ ```
36
+
37
+ 你会看到:
38
+
39
+ ```
40
+ [MCP] Initializing MCP Client...
41
+ [MCP] ✓ Loaded 3 tools from MCP Servers
42
+ [MCP] Available MCP tools:
43
+ - filesystem_read_file [from filesystem]
44
+ - filesystem_write_file [from filesystem]
45
+ - filesystem_list_directory [from filesystem]
46
+ ```
47
+
48
+ ### 步骤 4: 使用 MCP 工具
49
+
50
+ ```
51
+ ❯ 读取 /home/user/projects/package.json
52
+ ❯ 列出 /home/user/projects/src 目录的文件
53
+ ```
54
+
55
+ ## 常见配置示例
56
+
57
+ ### Git 操作
58
+
59
+ ```json
60
+ {
61
+ "mcp": {
62
+ "enabled": true,
63
+ "servers": {
64
+ "git": {
65
+ "enabled": true,
66
+ "command": "npx",
67
+ "args": ["-y", "@modelcontextprotocol/server-git"],
68
+ "env": {}
69
+ }
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ **使用**:
76
+ ```
77
+ ❯ 查看当前分支的提交历史
78
+ ❞ 创建新分支 feature/new-feature
79
+ ```
80
+
81
+ ### PostgreSQL 数据库
82
+
83
+ ```json
84
+ {
85
+ "mcp": {
86
+ "enabled": true,
87
+ "servers": {
88
+ "postgres": {
89
+ "enabled": true,
90
+ "command": "npx",
91
+ "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:password@localhost:5432/mydb"],
92
+ "env": {}
93
+ }
94
+ }
95
+ }
96
+ }
97
+ ```
98
+
99
+ **使用**:
100
+ ```
101
+ ❯ 查询 users 表中的所有记录
102
+ ❞ 统计每个用户的订单数量
103
+ ```
104
+
105
+ ### Brave 搜索
106
+
107
+ ```json
108
+ {
109
+ "mcp": {
110
+ "enabled": true,
111
+ "servers": {
112
+ "brave-search": {
113
+ "enabled": true,
114
+ "command": "npx",
115
+ "args": ["-y", "@modelcontextprotocol/server-brave-search"],
116
+ "env": {
117
+ "BRAVE_API_KEY": "your-brave-api-key"
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ ```
124
+
125
+ **使用**:
126
+ ```
127
+ ❯ 搜索最新的 Node.js 性能优化技巧
128
+ ❞ 查找 MCP 协议的最佳实践
129
+ ```
130
+
131
+ ### GitHub API
132
+
133
+ ```json
134
+ {
135
+ "mcp": {
136
+ "enabled": true,
137
+ "servers": {
138
+ "github": {
139
+ "enabled": true,
140
+ "command": "npx",
141
+ "args": ["-y", "@modelcontextprotocol/server-github"],
142
+ "env": {
143
+ "GITHUB_TOKEN": "your-github-token"
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
151
+ **使用**:
152
+ ```
153
+ ❯ 获取我的所有 open pull requests
154
+ ❞ 查看 repo 的 issue 列表
155
+ ```
156
+
157
+ ## 测试配置
158
+
159
+ 运行测试验证配置:
160
+
161
+ ```bash
162
+ npm run test:mcp
163
+ ```
164
+
165
+ 输出示例:
166
+
167
+ ```
168
+ === MCP Client 测试 ===
169
+
170
+ 📋 配置的 MCP Servers:
171
+ ✅ 启用 filesystem
172
+ 命令: npx
173
+ 参数: -y @modelcontextprotocol/server-filesystem /home/user/projects
174
+
175
+ 🔗 正在连接到 MCP Servers...
176
+
177
+ ✓ 已连接到 MCP Server: filesystem (3 个工具)
178
+ [MCP] MCP Servers 连接完成: 1 个服务器成功连接
179
+
180
+ 📊 连接状态:
181
+ 已连接服务器: filesystem
182
+ 总工具数: 3
183
+
184
+ 🛠️ 可用的 MCP 工具 (3 个):
185
+
186
+ • filesystem_read_file
187
+ 来源: filesystem
188
+ 描述: Read the contents of a file
189
+
190
+ • filesystem_write_file
191
+ 来源: filesystem
192
+ 描述: Write content to a file
193
+
194
+ • filesystem_list_directory
195
+ 来源: filesystem
196
+ 描述: List files in a directory
197
+ ```
198
+
199
+ ## 故障排查
200
+
201
+ ### 问题: MCP Server 连接失败
202
+
203
+ **错误信息**:
204
+ ```
205
+ [MCP] ✗ 连接 MCP Server 失败: filesystem - Error: Command failed: npx
206
+ ```
207
+
208
+ **解决方案**:
209
+ 1. 确保已安装 Node.js 和 npm
210
+ 2. 检查网络连接(首次运行需要下载包)
211
+ 3. 尝试手动运行命令测试:
212
+ ```bash
213
+ npx -y @modelcontextprotocol/server-filesystem /path
214
+ ```
215
+
216
+ ### 问题: 工具调用失败
217
+
218
+ **错误信息**:
219
+ ```
220
+ [MCP] MCP 工具调用失败: filesystem_read_file - Permission denied
221
+ ```
222
+
223
+ **解决方案**:
224
+ 1. 检查文件路径权限
225
+ 2. 确保配置的路径正确
226
+ 3. 使用绝对路径而非相对路径
227
+
228
+ ### 问题: 找不到工具
229
+
230
+ **错误信息**:
231
+ ```
232
+ Tool filesystem_read_file not found
233
+ ```
234
+
235
+ **解决方案**:
236
+ 1. 检查 MCP Server 是否成功连接
237
+ 2. 查看启动时的工具列表
238
+ 3. 运行 `npm run test:mcp` 诊断
239
+
240
+ ## 最佳实践
241
+
242
+ ### 1. 路径配置
243
+
244
+ 使用绝对路径:
245
+ ```json
246
+ {
247
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/projects"]
248
+ }
249
+ ```
250
+
251
+ 而非相对路径:
252
+ ```json
253
+ {
254
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "./projects"] // ❌ 不推荐
255
+ }
256
+ ```
257
+
258
+ ### 2. 环境变量
259
+
260
+ 敏感信息使用环境变量:
261
+ ```json
262
+ {
263
+ "env": {
264
+ "DATABASE_URL": "postgresql://user:pass@localhost/db"
265
+ }
266
+ }
267
+ ```
268
+
269
+ 或在 shell 中导出:
270
+ ```bash
271
+ export DATABASE_URL="postgresql://user:pass@localhost/db"
272
+ ```
273
+
274
+ ### 3. 按需启用
275
+
276
+ 只启用需要的 Servers:
277
+ ```json
278
+ {
279
+ "mcp": {
280
+ "servers": {
281
+ "postgres": {
282
+ "enabled": false // 暂时禁用
283
+ }
284
+ }
285
+ }
286
+ }
287
+ ```
288
+
289
+ ## 下一步
290
+
291
+ - 📖 阅读完整文档:[MCP 集成指南](./MCP_INTEGRATION.md)
292
+ - 🛠️ 开发自定义 MCP Server
293
+ - 🤝 分享你的 MCP Servers
294
+
295
+ ## 获取帮助
296
+
297
+ - 📚 [MCP 官方文档](https://modelcontextprotocol.io/)
298
+ - 💬 [GitHub Issues](https://github.com/modelcontextprotocol/typescript-sdk/issues)
299
+ - 📖 [示例配置](../config.mcp.example.json)