-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-mcp-protocol.js
More file actions
184 lines (158 loc) · 5.3 KB
/
Copy pathdebug-mcp-protocol.js
File metadata and controls
184 lines (158 loc) · 5.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
/**
* MCP协议调试脚本
* 模拟RooCode客户端与coderocket-mcp服务器的完整交互
*/
import { spawn } from 'child_process';
function testMCPProtocol(command, args = []) {
return new Promise((resolve, reject) => {
console.log(`🚀 启动MCP服务器: ${command} ${args.join(' ')}`);
const server = spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
DEBUG: 'true',
NODE_ENV: 'development'
}
});
let responses = [];
let buffer = '';
// 设置超时
const timeout = setTimeout(() => {
console.log('⏰ 测试超时');
server.kill();
resolve(responses);
}, 15000);
server.stdout.on('data', (data) => {
buffer += data.toString();
// 尝试解析JSON响应
const lines = buffer.split('\n');
buffer = lines.pop() || ''; // 保留不完整的行
for (const line of lines) {
if (line.trim()) {
try {
const response = JSON.parse(line);
console.log('📨 收到响应:', JSON.stringify(response, null, 2));
responses.push(response);
} catch (e) {
console.log('📤 非JSON输出:', line);
}
}
}
});
server.stderr.on('data', (data) => {
const output = data.toString();
if (output.includes('MCP 服务器启动成功')) {
console.log('✅ 服务器启动成功,开始协议测试');
// 1. 发送初始化请求
setTimeout(() => {
console.log('\n🔧 发送初始化请求');
const initRequest = {
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {
roots: {
listChanged: true
},
sampling: {}
},
clientInfo: {
name: 'test-client',
version: '1.0.0'
}
}
};
server.stdin.write(JSON.stringify(initRequest) + '\n');
}, 500);
// 2. 发送工具列表请求
setTimeout(() => {
console.log('\n📋 发送工具列表请求');
const listToolsRequest = {
jsonrpc: '2.0',
id: 2,
method: 'tools/list',
params: {}
};
server.stdin.write(JSON.stringify(listToolsRequest) + '\n');
}, 1000);
// 3. 发送工具调用请求
setTimeout(() => {
console.log('\n🔧 发送工具调用请求');
const callToolRequest = {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'get_ai_service_status',
arguments: {}
}
};
server.stdin.write(JSON.stringify(callToolRequest) + '\n');
}, 1500);
// 4. 结束测试
setTimeout(() => {
clearTimeout(timeout);
server.kill();
resolve(responses);
}, 3000);
}
});
server.on('error', (error) => {
clearTimeout(timeout);
console.error('❌ 服务器错误:', error.message);
reject(error);
});
server.on('exit', (code, signal) => {
clearTimeout(timeout);
console.log(`🔚 服务器退出: code=${code}, signal=${signal}`);
});
});
}
async function runProtocolTest() {
console.log('🧪 开始MCP协议调试测试\n');
try {
const responses = await testMCPProtocol('node', ['dist/index.js']);
console.log('\n📊 测试结果分析:');
console.log(`总响应数: ${responses.length}`);
const initResponse = responses.find(r => r.id === 1);
const toolsResponse = responses.find(r => r.id === 2);
const callResponse = responses.find(r => r.id === 3);
if (initResponse) {
console.log('✅ 初始化响应: 正常');
console.log(' 服务器信息:', initResponse.result?.serverInfo);
} else {
console.log('❌ 初始化响应: 缺失');
}
if (toolsResponse) {
console.log('✅ 工具列表响应: 正常');
console.log(' 工具数量:', toolsResponse.result?.tools?.length || 0);
if (toolsResponse.result?.tools) {
console.log(' 工具名称:', toolsResponse.result.tools.map(t => t.name));
}
} else {
console.log('❌ 工具列表响应: 缺失');
}
if (callResponse) {
console.log('✅ 工具调用响应: 正常');
} else {
console.log('❌ 工具调用响应: 缺失');
}
console.log('\n🎯 诊断建议:');
if (responses.length === 0) {
console.log('- 服务器可能没有正确响应JSON-RPC请求');
console.log('- 检查MCP协议实现是否正确');
} else if (!toolsResponse || !toolsResponse.result?.tools?.length) {
console.log('- 工具列表为空或响应格式错误');
console.log('- 检查toolDefinitions和工具注册逻辑');
} else {
console.log('- MCP协议工作正常');
console.log('- 问题可能在RooCode客户端配置或连接');
}
} catch (error) {
console.error('❌ 协议测试失败:', error.message);
}
}
runProtocolTest().catch(console.error);