-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mcp-server.js
More file actions
141 lines (116 loc) · 3.66 KB
/
test-mcp-server.js
File metadata and controls
141 lines (116 loc) · 3.66 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
/**
* MCP服务器连接测试脚本
* 用于验证coderocket-mcp服务器是否正常响应
*/
import { spawn } from 'child_process';
import { resolve } from 'path';
const DEBUG = true;
function log(message, data) {
console.log(`[TEST] ${new Date().toISOString()} - ${message}`);
if (data) {
console.log(`[TEST] Data:`, JSON.stringify(data, null, 2));
}
}
function testMCPServer(command, args = []) {
return new Promise((resolve, reject) => {
log(`🚀 启动MCP服务器测试`, { command, args });
const server = spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
DEBUG: 'true',
NODE_ENV: 'development'
}
});
let stdout = '';
let stderr = '';
let hasResponded = false;
// 设置超时
const timeout = setTimeout(() => {
if (!hasResponded) {
log('⏰ 服务器启动超时');
server.kill();
reject(new Error('服务器启动超时'));
}
}, 10000);
server.stdout.on('data', (data) => {
stdout += data.toString();
log('📤 服务器stdout输出', data.toString());
});
server.stderr.on('data', (data) => {
stderr += data.toString();
log('📤 服务器stderr输出', data.toString());
// 检查是否有调试日志表明服务器启动成功
if (data.toString().includes('MCP 服务器启动成功')) {
hasResponded = true;
clearTimeout(timeout);
// 发送工具列表请求
log('📋 发送工具列表请求');
const listToolsRequest = {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
};
server.stdin.write(JSON.stringify(listToolsRequest) + '\n');
// 等待响应
setTimeout(() => {
server.kill();
resolve({ stdout, stderr });
}, 3000);
}
});
server.on('error', (error) => {
clearTimeout(timeout);
log('❌ 服务器启动错误', error.message);
reject(error);
});
server.on('exit', (code, signal) => {
clearTimeout(timeout);
log('🔚 服务器退出', { code, signal });
if (!hasResponded) {
reject(new Error(`服务器异常退出: code=${code}, signal=${signal}`));
}
});
});
}
async function runTests() {
console.log('🧪 开始MCP服务器连接测试\n');
const tests = [
{
name: 'test-coderocket-mcp (本地开发版)',
command: 'node',
args: [resolve(process.cwd(), 'dist/index.js')]
},
{
name: 'coderocket-mcp (npm全局版)',
command: 'coderocket-mcp',
args: []
},
{
name: 'coderocket-mcp (npx版)',
command: 'npx',
args: ['-y', '@yeepay/coderocket-mcp@1.4.1']
}
];
for (const test of tests) {
try {
console.log(`\n🔍 测试: ${test.name}`);
console.log(`📋 命令: ${test.command} ${test.args.join(' ')}`);
const result = await testMCPServer(test.command, test.args);
console.log(`✅ ${test.name} - 测试通过`);
console.log('📤 输出摘要:');
console.log(' stdout行数:', result.stdout.split('\n').length);
console.log(' stderr行数:', result.stderr.split('\n').length);
// 检查是否有工具列表响应
if (result.stdout.includes('tools') || result.stderr.includes('tools')) {
console.log(' 🎯 检测到工具列表响应');
}
} catch (error) {
console.log(`❌ ${test.name} - 测试失败: ${error.message}`);
}
}
console.log('\n🏁 测试完成');
}
runTests().catch(console.error);