xiaozhi-client 1.7.1 → 1.7.2
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.
- package/README.md +29 -397
- package/dist/WebServerStandalone.js +34 -13
- package/dist/WebServerStandalone.js.map +1 -1
- package/dist/cli.js +37 -16
- package/dist/cli.js.map +1 -1
- package/dist/mcpServerProxy.js +28 -7
- package/dist/mcpServerProxy.js.map +1 -1
- package/dist/package.json +10 -5
- package/docs/{Architecture.md → development/architecture.mdx} +4 -1
- package/docs/docs.json +51 -0
- package/docs/getting-started/install.mdx +191 -0
- package/docs/getting-started/intro.mdx +11 -0
- package/docs/getting-started/quickstart.mdx +108 -0
- package/docs/images/preview.png +0 -0
- package/docs/reference/command.mdx +15 -0
- package/docs/usage/add-mcp-server.mdx +6 -0
- package/docs/usage/as-a-mcp-server.mdx +6 -0
- package/docs/usage/docker.mdx +99 -0
- package/docs/usage/modelscope.mdx +6 -0
- package/docs/usage/web-control.mdx +6 -0
- package/package.json +10 -5
- package/web/dist/assets/{form-utils-DmILtYcF.js → form-utils-RpkiEEz8.js} +2 -2
- package/web/dist/assets/{form-utils-DmILtYcF.js.map → form-utils-RpkiEEz8.js.map} +1 -1
- package/web/dist/assets/index-BjG6tl-5.css +1 -0
- package/web/dist/assets/index-BvDVeF-B.js +31 -0
- package/web/dist/assets/index-BvDVeF-B.js.map +1 -0
- package/web/dist/assets/radix-ui-BA32w1ww.js +2 -0
- package/web/dist/assets/radix-ui-BA32w1ww.js.map +1 -0
- package/web/dist/assets/react-vendor-CoesXubw.js +234 -0
- package/web/dist/assets/react-vendor-CoesXubw.js.map +1 -0
- package/web/dist/assets/{utils-DYeV2b9J.js → utils-N_0RTAPv.js} +2 -2
- package/web/dist/assets/{utils-DYeV2b9J.js.map → utils-N_0RTAPv.js.map} +1 -1
- package/web/dist/index.html +6 -6
- package/docs/CLI.md +0 -928
- package/docs/examples-usage.md +0 -647
- package/web/dist/assets/index-CQVdYi7r.js +0 -31
- package/web/dist/assets/index-CQVdYi7r.js.map +0 -1
- package/web/dist/assets/index-Cz9EK1KU.css +0 -1
- package/web/dist/assets/radix-ui-DW48STyt.js +0 -2
- package/web/dist/assets/radix-ui-DW48STyt.js.map +0 -1
- package/web/dist/assets/react-vendor-CP-QpYlg.js +0 -189
- package/web/dist/assets/react-vendor-CP-QpYlg.js.map +0 -1
- /package/docs/{RELEASE_GUIDE.md → development/todo__release-guide.md} +0 -0
- /package/docs/{SettingManager.md → development/todo__setting-manager.md} +0 -0
package/docs/examples-usage.md
DELETED
|
@@ -1,647 +0,0 @@
|
|
|
1
|
-
# 示例文件使用说明
|
|
2
|
-
|
|
3
|
-
本文档详细介绍了 `src/services/` 目录中所有 `.example.ts` 文件的功能、使用方法和运行步骤。
|
|
4
|
-
|
|
5
|
-
## 概述
|
|
6
|
-
|
|
7
|
-
项目中包含以下示例文件:
|
|
8
|
-
|
|
9
|
-
1. **AdvancedFeatures.example.ts** - 高级功能演示(错误处理、性能监控、健康检查、配置热重载)
|
|
10
|
-
2. **MCPService.example.ts** - MCPService 基础使用示例
|
|
11
|
-
3. **MCPService.ping.example.ts** - MCP Service Ping 功能演示
|
|
12
|
-
4. **MCPServiceManager.example.ts** - MCPServiceManager 使用示例
|
|
13
|
-
|
|
14
|
-
## 环境要求
|
|
15
|
-
|
|
16
|
-
### 系统要求
|
|
17
|
-
|
|
18
|
-
- Node.js >= 18.0.0
|
|
19
|
-
- pnpm 包管理器(推荐)或 npm/yarn
|
|
20
|
-
|
|
21
|
-
### 项目依赖
|
|
22
|
-
|
|
23
|
-
确保已安装项目依赖:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
pnpm install
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### 必要的配置文件
|
|
30
|
-
|
|
31
|
-
示例运行需要以下配置文件:
|
|
32
|
-
|
|
33
|
-
- `xiaozhi.config.json` - 主配置文件
|
|
34
|
-
- `templates/hello-world/mcpServers/calculator.js` - 计算器 MCP 服务
|
|
35
|
-
- `templates/hello-world/mcpServers/datetime.js` - 日期时间 MCP 服务
|
|
36
|
-
|
|
37
|
-
## 示例文件详细说明
|
|
38
|
-
|
|
39
|
-
### 1. AdvancedFeatures.example.ts
|
|
40
|
-
|
|
41
|
-
**功能说明:**
|
|
42
|
-
演示 xiaozhi-client 的高级功能,包括:
|
|
43
|
-
- 错误处理和分类
|
|
44
|
-
- 性能监控和指标收集
|
|
45
|
-
- 健康检查和服务监控
|
|
46
|
-
- 配置文件热重载
|
|
47
|
-
|
|
48
|
-
**运行方法:**
|
|
49
|
-
|
|
50
|
-
```bash
|
|
51
|
-
# 方法一:使用 Node.js 直接运行
|
|
52
|
-
pnpm example src/services/AdvancedFeatures.example.ts
|
|
53
|
-
|
|
54
|
-
# 方法二:使用 tsx 运行(需要安装 tsx)
|
|
55
|
-
npx tsx src/services/AdvancedFeatures.example.ts
|
|
56
|
-
|
|
57
|
-
# 方法三:编译后运行
|
|
58
|
-
pnpm build
|
|
59
|
-
node dist/services/AdvancedFeatures.example.js
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
**可用的命令参数:**
|
|
63
|
-
```bash
|
|
64
|
-
# 运行完整演示(默认)
|
|
65
|
-
pnpm example src/services/AdvancedFeatures.example.ts
|
|
66
|
-
|
|
67
|
-
# 只演示错误处理
|
|
68
|
-
pnpm example src/services/AdvancedFeatures.example.ts error
|
|
69
|
-
|
|
70
|
-
# 只演示性能监控
|
|
71
|
-
pnpm example src/services/AdvancedFeatures.example.ts performance
|
|
72
|
-
|
|
73
|
-
# 只演示健康检查
|
|
74
|
-
pnpm example src/services/AdvancedFeatures.example.ts health
|
|
75
|
-
|
|
76
|
-
# 只演示配置热重载
|
|
77
|
-
pnpm example src/services/AdvancedFeatures.example.ts config
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
**预期输出:**
|
|
81
|
-
- 错误分类和处理策略信息
|
|
82
|
-
- 性能指标统计数据
|
|
83
|
-
- 服务健康状态报告
|
|
84
|
-
- 配置变更监听日志
|
|
85
|
-
|
|
86
|
-
**注意事项:**
|
|
87
|
-
- 配置热重载演示需要手动修改 `xiaozhi.config.json` 文件来触发变更事件
|
|
88
|
-
- 健康检查演示会启动实际的 MCP 服务,确保配置文件正确
|
|
89
|
-
|
|
90
|
-
### 2. MCPService.example.ts
|
|
91
|
-
|
|
92
|
-
**功能说明:**
|
|
93
|
-
演示 MCPService 类的基础使用,包括:
|
|
94
|
-
- 服务连接和断开
|
|
95
|
-
- 工具列表获取
|
|
96
|
-
- 工具调用
|
|
97
|
-
- 重连配置管理
|
|
98
|
-
|
|
99
|
-
**运行方法:**
|
|
100
|
-
|
|
101
|
-
```bash
|
|
102
|
-
# 使用 Node.js 运行
|
|
103
|
-
pnpm example src/services/MCPService.example.ts
|
|
104
|
-
|
|
105
|
-
# 使用 tsx 运行
|
|
106
|
-
npx tsx src/services/MCPService.example.ts
|
|
107
|
-
|
|
108
|
-
# 编译后运行
|
|
109
|
-
pnpm build
|
|
110
|
-
node dist/services/MCPService.example.js
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
**预期输出:**
|
|
114
|
-
- 服务连接状态信息
|
|
115
|
-
- 可用工具列表
|
|
116
|
-
- 工具调用结果(注意:当前示例代码对 calculator 工具缺少必需参数,会显示调用失败)
|
|
117
|
-
- 重连配置信息
|
|
118
|
-
|
|
119
|
-
**注意:** 当前示例代码中的工具调用逻辑只处理了 `add` 和 `multiply` 工具,但实际的计算器服务提供的是 `calculator` 工具,需要 `javascript_expression` 参数。如果要修复这个问题,可以在示例代码中添加:
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
if (firstTool.name === "calculator") {
|
|
123
|
-
args = { javascript_expression: "5 + 3" };
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
**依赖的 MCP 服务:**
|
|
128
|
-
- 计算器服务:`templates/hello-world/mcpServers/calculator.js`
|
|
129
|
-
|
|
130
|
-
### 3. MCPService.ping.example.ts
|
|
131
|
-
|
|
132
|
-
**功能说明:**
|
|
133
|
-
演示 MCP Service 的 Ping 监控功能,包括:
|
|
134
|
-
- Ping 配置管理
|
|
135
|
-
- 连接状态监控
|
|
136
|
-
- 故障检测和恢复
|
|
137
|
-
- 运行时配置更新
|
|
138
|
-
|
|
139
|
-
**运行方法:**
|
|
140
|
-
|
|
141
|
-
```bash
|
|
142
|
-
# 使用 Node.js 运行
|
|
143
|
-
pnpm example src/services/MCPService.ping.example.ts
|
|
144
|
-
|
|
145
|
-
# 使用 tsx 运行
|
|
146
|
-
npx tsx src/services/MCPService.ping.example.ts
|
|
147
|
-
|
|
148
|
-
# 编译后运行
|
|
149
|
-
pnpm build
|
|
150
|
-
node dist/services/MCPService.ping.example.js
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
**预期输出:**
|
|
154
|
-
- Ping 配置信息
|
|
155
|
-
- 连接状态和 Ping 统计
|
|
156
|
-
- 配置动态更新演示
|
|
157
|
-
- 默认行为对比演示
|
|
158
|
-
|
|
159
|
-
**依赖的 MCP 服务:**
|
|
160
|
-
- 日期时间服务:`templates/hello-world/mcpServers/datetime.js`
|
|
161
|
-
|
|
162
|
-
### 4. MCPServiceManager.example.ts
|
|
163
|
-
|
|
164
|
-
**功能说明:**
|
|
165
|
-
演示 MCPServiceManager 的服务管理功能,包括:
|
|
166
|
-
- 多服务管理
|
|
167
|
-
- 服务启动和停止
|
|
168
|
-
- 工具聚合和调用
|
|
169
|
-
- 配置动态管理
|
|
170
|
-
|
|
171
|
-
**运行方法:**
|
|
172
|
-
|
|
173
|
-
```bash
|
|
174
|
-
# 使用 Node.js 运行
|
|
175
|
-
pnpm example src/services/MCPServiceManager.example.ts
|
|
176
|
-
|
|
177
|
-
# 使用 tsx 运行
|
|
178
|
-
npx tsx src/services/MCPServiceManager.example.ts
|
|
179
|
-
|
|
180
|
-
# 编译后运行
|
|
181
|
-
pnpm build
|
|
182
|
-
node dist/services/MCPServiceManager.example.js
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
**预期输出:**
|
|
186
|
-
- 服务管理器状态
|
|
187
|
-
- 所有可用工具列表
|
|
188
|
-
- 工具调用演示
|
|
189
|
-
- 单个服务管理操作
|
|
190
|
-
- 配置管理演示
|
|
191
|
-
|
|
192
|
-
**依赖的 MCP 服务:**
|
|
193
|
-
- 计算器服务和日期时间服务(通过默认配置加载)
|
|
194
|
-
|
|
195
|
-
## 运行环境配置
|
|
196
|
-
|
|
197
|
-
### 配置文件设置
|
|
198
|
-
|
|
199
|
-
确保 `xiaozhi.config.json` 文件存在并包含正确的配置:
|
|
200
|
-
|
|
201
|
-
```json
|
|
202
|
-
{
|
|
203
|
-
"mcpEndpoint": "<请填写你的接入点地址(获取地址在 xiaozhi.me)>",
|
|
204
|
-
"mcpServers": {
|
|
205
|
-
"calculator": {
|
|
206
|
-
"command": "node",
|
|
207
|
-
"args": ["./templates/hello-world/mcpServers/calculator.js"]
|
|
208
|
-
},
|
|
209
|
-
"datetime": {
|
|
210
|
-
"command": "node",
|
|
211
|
-
"args": ["./templates/hello-world/mcpServers/datetime.js"]
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
"connection": {
|
|
215
|
-
"heartbeatInterval": 30000,
|
|
216
|
-
"heartbeatTimeout": 10000,
|
|
217
|
-
"reconnectInterval": 5000
|
|
218
|
-
},
|
|
219
|
-
"webUI": {
|
|
220
|
-
"port": 9999
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
### 环境变量
|
|
226
|
-
|
|
227
|
-
可选的环境变量:
|
|
228
|
-
- `XIAOZHI_CONFIG_DIR` - 指定配置文件目录
|
|
229
|
-
- `NODE_ENV` - 设置运行环境(development/production)
|
|
230
|
-
|
|
231
|
-
## 常见问题和故障排除
|
|
232
|
-
|
|
233
|
-
### 1. 模块导入错误
|
|
234
|
-
|
|
235
|
-
**问题:** `Cannot find module` 或 `ERR_MODULE_NOT_FOUND`
|
|
236
|
-
|
|
237
|
-
**解决方案:**
|
|
238
|
-
```bash
|
|
239
|
-
# 确保依赖已安装
|
|
240
|
-
pnpm install
|
|
241
|
-
|
|
242
|
-
# 检查 Node.js 版本
|
|
243
|
-
node --version # 应该 >= 18.0.0
|
|
244
|
-
|
|
245
|
-
# 使用正确的加载器
|
|
246
|
-
pnpm example src/services/AdvancedFeatures.example.ts
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
### 2. MCP 服务连接失败
|
|
250
|
-
|
|
251
|
-
**问题:** `Connection refused` 或 `ENOENT` (文件不存在错误)
|
|
252
|
-
|
|
253
|
-
**解决方案:**
|
|
254
|
-
```bash
|
|
255
|
-
# 检查 MCP 服务文件是否存在
|
|
256
|
-
ls -la templates/hello-world/mcpServers/
|
|
257
|
-
|
|
258
|
-
# 确保 calculator.js 和 datetime.js 可执行
|
|
259
|
-
node templates/hello-world/mcpServers/calculator.js
|
|
260
|
-
|
|
261
|
-
# 检查配置文件路径
|
|
262
|
-
cat xiaozhi.config.json
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
### 3. 权限错误
|
|
266
|
-
|
|
267
|
-
**问题:** `EACCES` (权限被拒绝错误) 或权限被拒绝
|
|
268
|
-
|
|
269
|
-
**解决方案:**
|
|
270
|
-
```bash
|
|
271
|
-
# 检查文件权限
|
|
272
|
-
chmod +x templates/hello-world/mcpServers/*.js
|
|
273
|
-
|
|
274
|
-
# 确保当前用户有读写权限
|
|
275
|
-
ls -la xiaozhi.config.json
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
### 4. TypeScript 编译错误
|
|
279
|
-
|
|
280
|
-
**问题:** TypeScript 类型错误
|
|
281
|
-
|
|
282
|
-
**解决方案:**
|
|
283
|
-
```bash
|
|
284
|
-
# 检查类型
|
|
285
|
-
pnpm type:check
|
|
286
|
-
|
|
287
|
-
# 安装类型定义
|
|
288
|
-
pnpm install --save-dev @types/node
|
|
289
|
-
|
|
290
|
-
# 使用 tsx 替代 ts-node
|
|
291
|
-
npx tsx src/services/AdvancedFeatures.example.ts
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
## 开发和调试
|
|
295
|
-
|
|
296
|
-
### 启用详细日志
|
|
297
|
-
|
|
298
|
-
在示例文件中,可以通过修改日志级别来获取更多调试信息:
|
|
299
|
-
|
|
300
|
-
```typescript
|
|
301
|
-
// 在示例文件顶部添加
|
|
302
|
-
process.env.DEBUG = "xiaozhi:*";
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
### 使用调试器
|
|
306
|
-
|
|
307
|
-
```bash
|
|
308
|
-
# 使用 Node.js 调试器
|
|
309
|
-
node --inspect --loader ts-node/esm src/services/AdvancedFeatures.example.ts
|
|
310
|
-
|
|
311
|
-
# 使用 VS Code 调试
|
|
312
|
-
# 在 .vscode/launch.json 中添加配置
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
### 测试运行
|
|
316
|
-
|
|
317
|
-
```bash
|
|
318
|
-
# 运行相关测试
|
|
319
|
-
pnpm test src/services/__tests__/
|
|
320
|
-
|
|
321
|
-
# 运行特定测试文件
|
|
322
|
-
pnpm test src/services/__tests__/MCPService.test.ts
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
## 最佳实践
|
|
326
|
-
|
|
327
|
-
### 1. 示例代码使用场景
|
|
328
|
-
|
|
329
|
-
- **学习和理解:** 通过运行示例了解各个组件的功能
|
|
330
|
-
- **集成参考:** 将示例代码作为集成到自己项目的参考
|
|
331
|
-
- **功能测试:** 验证 MCP 服务的正确性和性能
|
|
332
|
-
- **故障排除:** 使用示例诊断配置和连接问题
|
|
333
|
-
|
|
334
|
-
### 2. 生产环境注意事项
|
|
335
|
-
|
|
336
|
-
- 不要在生产环境直接运行示例文件
|
|
337
|
-
- 示例中的配置仅供参考,需要根据实际需求调整
|
|
338
|
-
- 注意安全性,特别是在使用 `eval()` 等功能时
|
|
339
|
-
- 建议使用环境变量管理敏感配置
|
|
340
|
-
|
|
341
|
-
### 3. 自定义扩展
|
|
342
|
-
|
|
343
|
-
示例文件可以作为基础进行扩展:
|
|
344
|
-
|
|
345
|
-
```typescript
|
|
346
|
-
// 基于示例创建自定义服务
|
|
347
|
-
import { AdvancedFeaturesDemo } from './AdvancedFeatures.example.js';
|
|
348
|
-
|
|
349
|
-
class MyCustomDemo extends AdvancedFeaturesDemo {
|
|
350
|
-
// 添加自定义功能
|
|
351
|
-
}
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
## 快速开始指南
|
|
355
|
-
|
|
356
|
-
### 第一次运行示例
|
|
357
|
-
|
|
358
|
-
1. **克隆或下载项目**
|
|
359
|
-
|
|
360
|
-
```bash
|
|
361
|
-
git clone <repository-url>
|
|
362
|
-
cd xiaozhi-client
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
2. **安装依赖**
|
|
366
|
-
|
|
367
|
-
```bash
|
|
368
|
-
pnpm install
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
3. **配置文件准备**
|
|
372
|
-
|
|
373
|
-
```bash
|
|
374
|
-
# 复制示例配置文件
|
|
375
|
-
cp xiaozhi.config.json.example xiaozhi.config.json
|
|
376
|
-
|
|
377
|
-
# 编辑配置文件,填入你的接入点地址
|
|
378
|
-
nano xiaozhi.config.json
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
4. **运行第一个示例**
|
|
382
|
-
|
|
383
|
-
```bash
|
|
384
|
-
# 运行 MCPService 基础示例
|
|
385
|
-
npx tsx src/services/MCPService.example.ts
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### 推荐的学习顺序
|
|
389
|
-
|
|
390
|
-
1. **MCPService.example.ts** - 了解基础服务操作
|
|
391
|
-
2. **MCPService.ping.example.ts** - 学习监控功能
|
|
392
|
-
3. **MCPServiceManager.example.ts** - 掌握多服务管理
|
|
393
|
-
4. **AdvancedFeatures.example.ts** - 探索高级功能
|
|
394
|
-
|
|
395
|
-
## 示例输出详解
|
|
396
|
-
|
|
397
|
-
### MCPService.example.ts 典型输出
|
|
398
|
-
|
|
399
|
-
```text
|
|
400
|
-
🚀 MCPService 使用示例
|
|
401
|
-
📡 正在连接到 MCP 服务...
|
|
402
|
-
✅ 连接成功!
|
|
403
|
-
📊 服务状态: {
|
|
404
|
-
name: 'calculator',
|
|
405
|
-
connected: true,
|
|
406
|
-
initialized: true,
|
|
407
|
-
toolCount: 1,
|
|
408
|
-
connectionState: 'connected'
|
|
409
|
-
}
|
|
410
|
-
🛠️ 可用工具: [
|
|
411
|
-
{
|
|
412
|
-
name: 'calculator',
|
|
413
|
-
description: 'For mathematical calculation, always use this tool to calculate the result of a JavaScript expression. Math object and basic operations are available.'
|
|
414
|
-
}
|
|
415
|
-
]
|
|
416
|
-
🔧 调用工具: calculator
|
|
417
|
-
📋 工具调用结果: {
|
|
418
|
-
content: [
|
|
419
|
-
{
|
|
420
|
-
type: 'text',
|
|
421
|
-
text: '{"success":true,"result":8}'
|
|
422
|
-
}
|
|
423
|
-
]
|
|
424
|
-
}
|
|
425
|
-
⚙️ 当前重连配置: {
|
|
426
|
-
enabled: true,
|
|
427
|
-
maxAttempts: 5,
|
|
428
|
-
initialInterval: 2000,
|
|
429
|
-
maxInterval: 30000,
|
|
430
|
-
backoffMultiplier: 2
|
|
431
|
-
}
|
|
432
|
-
🔄 更新后的重连配置: {
|
|
433
|
-
enabled: true,
|
|
434
|
-
maxAttempts: 8,
|
|
435
|
-
initialInterval: 1500,
|
|
436
|
-
maxInterval: 30000,
|
|
437
|
-
backoffMultiplier: 2
|
|
438
|
-
}
|
|
439
|
-
🔌 断开连接...
|
|
440
|
-
👋 示例结束
|
|
441
|
-
```
|
|
442
|
-
|
|
443
|
-
### AdvancedFeatures.example.ts 典型输出
|
|
444
|
-
|
|
445
|
-
```text
|
|
446
|
-
🚀 开始高级功能完整演示
|
|
447
|
-
=== 错误处理功能演示 ===
|
|
448
|
-
错误: Connection refused
|
|
449
|
-
分类: CONNECTION_ERROR (CONN_001)
|
|
450
|
-
可恢复: 是
|
|
451
|
-
恢复策略: RETRY_WITH_BACKOFF
|
|
452
|
-
用户消息: 连接服务失败,正在尝试重新连接...
|
|
453
|
-
需要告警: 否
|
|
454
|
-
---
|
|
455
|
-
=== 性能监控功能演示 ===
|
|
456
|
-
服务: demo-service
|
|
457
|
-
总操作数: 5
|
|
458
|
-
成功操作: 4
|
|
459
|
-
失败操作: 1
|
|
460
|
-
成功率: 80.0%
|
|
461
|
-
错误率: 20.0%
|
|
462
|
-
平均工具调用延迟: 180ms
|
|
463
|
-
运行时间: 0.1s
|
|
464
|
-
性能报告:
|
|
465
|
-
总服务数: 1
|
|
466
|
-
总操作数: 5
|
|
467
|
-
平均成功率: 80.0%
|
|
468
|
-
平均错误率: 20.0%
|
|
469
|
-
=== 健康检查功能演示 ===
|
|
470
|
-
健康检查结果:
|
|
471
|
-
服务: test-stdio-service
|
|
472
|
-
健康状态: 健康
|
|
473
|
-
连接稳定: 是
|
|
474
|
-
响应时间: 45ms
|
|
475
|
-
错误率: 0.0%
|
|
476
|
-
运行时间: 2.3s
|
|
477
|
-
---
|
|
478
|
-
✅ 高级功能演示完成
|
|
479
|
-
要演示配置热重载功能,请运行: npm run demo:config-watch
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
## 高级用法和扩展
|
|
483
|
-
|
|
484
|
-
### 自定义 MCP 服务
|
|
485
|
-
|
|
486
|
-
你可以创建自己的 MCP 服务来配合示例使用:
|
|
487
|
-
|
|
488
|
-
```javascript
|
|
489
|
-
// myCustomService.js
|
|
490
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
491
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
492
|
-
import { z } from "zod";
|
|
493
|
-
|
|
494
|
-
const server = new McpServer({
|
|
495
|
-
name: "MyCustomService",
|
|
496
|
-
version: "1.0.0",
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
server.tool(
|
|
500
|
-
"greet",
|
|
501
|
-
"Greet a person with a custom message",
|
|
502
|
-
{
|
|
503
|
-
name: z.string().describe("Name of the person to greet"),
|
|
504
|
-
message: z.string().optional().describe("Custom greeting message"),
|
|
505
|
-
},
|
|
506
|
-
async ({ name, message = "Hello" }) => {
|
|
507
|
-
return {
|
|
508
|
-
content: [
|
|
509
|
-
{
|
|
510
|
-
type: "text",
|
|
511
|
-
text: `${message}, ${name}!`,
|
|
512
|
-
},
|
|
513
|
-
],
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
);
|
|
517
|
-
|
|
518
|
-
const transport = new StdioServerTransport();
|
|
519
|
-
await server.connect(transport);
|
|
520
|
-
```
|
|
521
|
-
|
|
522
|
-
然后在配置文件中添加:
|
|
523
|
-
|
|
524
|
-
```json
|
|
525
|
-
{
|
|
526
|
-
"mcpServers": {
|
|
527
|
-
"my-custom-service": {
|
|
528
|
-
"command": "node",
|
|
529
|
-
"args": ["./myCustomService.js"]
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
```
|
|
534
|
-
|
|
535
|
-
### 集成到现有项目
|
|
536
|
-
|
|
537
|
-
```typescript
|
|
538
|
-
// 在你的项目中使用示例代码
|
|
539
|
-
import { MCPService, MCPTransportType } from './src/services/MCPService.js';
|
|
540
|
-
|
|
541
|
-
async function integrateWithMyApp() {
|
|
542
|
-
const service = new MCPService({
|
|
543
|
-
name: "my-app-service",
|
|
544
|
-
type: MCPTransportType.STDIO,
|
|
545
|
-
command: "node",
|
|
546
|
-
args: ["./my-mcp-server.js"],
|
|
547
|
-
ping: {
|
|
548
|
-
enabled: true,
|
|
549
|
-
interval: 30000,
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
await service.connect();
|
|
554
|
-
|
|
555
|
-
// 使用服务...
|
|
556
|
-
const tools = service.getTools();
|
|
557
|
-
const result = await service.callTool("my-tool", { param: "value" });
|
|
558
|
-
|
|
559
|
-
await service.disconnect();
|
|
560
|
-
}
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
### 性能优化建议
|
|
564
|
-
|
|
565
|
-
1. **连接池管理**
|
|
566
|
-
```typescript
|
|
567
|
-
// 使用连接池避免频繁连接断开
|
|
568
|
-
const servicePool = new Map<string, MCPService>();
|
|
569
|
-
|
|
570
|
-
async function getOrCreateService(config: MCPServiceConfig): Promise<MCPService> {
|
|
571
|
-
if (!servicePool.has(config.name)) {
|
|
572
|
-
const service = new MCPService(config);
|
|
573
|
-
await service.connect();
|
|
574
|
-
servicePool.set(config.name, service);
|
|
575
|
-
}
|
|
576
|
-
return servicePool.get(config.name)!;
|
|
577
|
-
}
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
2. **错误处理策略**
|
|
581
|
-
```typescript
|
|
582
|
-
// 实现智能重试机制
|
|
583
|
-
async function callToolWithRetry(service: MCPService, toolName: string, args: any, maxRetries = 3) {
|
|
584
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
585
|
-
try {
|
|
586
|
-
return await service.callTool(toolName, args);
|
|
587
|
-
} catch (error) {
|
|
588
|
-
if (i === maxRetries - 1) throw error;
|
|
589
|
-
await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, i)));
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
## 故障排除进阶
|
|
596
|
-
|
|
597
|
-
### 日志分析
|
|
598
|
-
|
|
599
|
-
启用详细日志来诊断问题:
|
|
600
|
-
|
|
601
|
-
```bash
|
|
602
|
-
# 设置环境变量启用调试日志
|
|
603
|
-
export DEBUG=xiaozhi:*,mcp:*
|
|
604
|
-
npx tsx src/services/AdvancedFeatures.example.ts
|
|
605
|
-
```
|
|
606
|
-
|
|
607
|
-
### 网络问题诊断
|
|
608
|
-
|
|
609
|
-
```bash
|
|
610
|
-
# 检查端口占用
|
|
611
|
-
lsof -i :9999
|
|
612
|
-
|
|
613
|
-
# 测试网络连接
|
|
614
|
-
telnet localhost 9999
|
|
615
|
-
|
|
616
|
-
# 检查防火墙设置
|
|
617
|
-
sudo ufw status
|
|
618
|
-
```
|
|
619
|
-
|
|
620
|
-
### 内存和性能监控
|
|
621
|
-
|
|
622
|
-
```bash
|
|
623
|
-
# 使用 Node.js 内置性能监控
|
|
624
|
-
node --inspect --loader ts-node/esm src/services/AdvancedFeatures.example.ts
|
|
625
|
-
|
|
626
|
-
# 使用 clinic.js 进行性能分析
|
|
627
|
-
npx clinic doctor -- pnpm example src/services/AdvancedFeatures.example.ts
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
## 相关文档
|
|
631
|
-
|
|
632
|
-
- [CLI 使用说明](./CLI.md)
|
|
633
|
-
- [架构文档](./Architecture.md)
|
|
634
|
-
- [配置管理](./SettingManager.md)
|
|
635
|
-
- [项目 README](../README.md)
|
|
636
|
-
|
|
637
|
-
## 贡献和反馈
|
|
638
|
-
|
|
639
|
-
如果你在使用示例过程中遇到问题或有改进建议,欢迎:
|
|
640
|
-
|
|
641
|
-
1. 提交 Issue 报告问题
|
|
642
|
-
2. 提交 Pull Request 改进文档
|
|
643
|
-
3. 在社区讨论中分享使用经验
|
|
644
|
-
|
|
645
|
-
---
|
|
646
|
-
|
|
647
|
-
*最后更新时间:2024年12月*
|