-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
76 lines (66 loc) · 1.88 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { spawn } from "child_process";
// 启动服务器进程
const isBun = typeof process.versions.bun !== 'undefined';
const serverProcess = spawn(
isBun ? "bun" : "node",
isBun ? ["index.ts"] : ["--loader", "ts-node/esm", "index.ts"],
{stdio: ["pipe", "pipe", "inherit"]}
);
// 准备简单的JSON-RPC请求
const request = {
jsonrpc: "2.0",
id: 1,
method: "tools/list",
params: {
version: "1.0",
},
};
// 监听服务器输出
serverProcess.stdout.on("data", (data) => {
const output = data.toString().trim();
console.log("服务器响应:", data.toString());
const jsonMatch = output.match(/\{.*\}/);
if (!jsonMatch) return;
try {
const response = JSON.parse(output);
if (response.id === 1 && response.result && response.result.tools) {
console.log("发送搜索请求...");
// 修改搜索请求格式
const searchRequest = {
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
version: "0.1.0",
name: "bilibili-search",
arguments: {
keyword: "岛市老八",
limit: 10,
},
},
};
serverProcess.stdin.write(JSON.stringify(searchRequest) + "\n");
return;
}
// 如果是搜索响应,关闭服务器
if (response.id === 2) {
console.log("测试完成,关闭服务器...");
serverProcess.kill();
process.exit(0);
}
} catch (e) {
console.error("解析响应失败:", e);
}
// 如果不需要继续测试,关闭服务器
serverProcess.kill();
process.exit(0);
});
// 发送请求前打印请求内容
console.log("发送 MCP 请求:", JSON.stringify(request, null, 2));
serverProcess.stdin.write(JSON.stringify(request) + "\n");
// 设置超时
setTimeout(() => {
console.error("测试超时");
serverProcess.kill();
process.exit(1);
}, 10000); // 增加超时时间