Files
map-client-vue/test-mcp-connection.js
2025-10-14 14:18:20 +08:00

102 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
// 测试 MCP 服务器连接
const serverUrl = 'http://0.0.0.0:3100/mcp';
const healthUrl = 'http://0.0.0.0:3100/health';
console.log('🧪 测试 MCP 服务器连接...\n');
// 测试健康检查
console.log(`📡 测试健康检查: ${healthUrl}`);
fetch(healthUrl, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(response => {
console.log(`✅ 健康检查响应: ${response.status} ${response.statusText}`);
return response.text();
})
.then(data => {
console.log('健康检查数据:', data);
})
.catch(error => {
console.error('❌ 健康检查失败:', error.message);
});
// 测试 MCP initialize 请求
console.log(`\n🔗 测试 MCP 连接: ${serverUrl}`);
const initRequest = {
jsonrpc: '2.0',
id: '1',
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {
roots: {
listChanged: true
},
sampling: {}
},
clientInfo: {
name: 'MCP-Vue-Client',
version: '1.0.0'
}
}
};
fetch(serverUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(initRequest)
})
.then(response => {
console.log(`✅ MCP 响应: ${response.status} ${response.statusText}`);
return response.json();
})
.then(data => {
console.log('MCP 初始化响应:', JSON.stringify(data, null, 2));
// 如果初始化成功,测试获取工具列表
if (data.result) {
console.log('\n🔧 测试工具列表...');
return fetch(serverUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: '2',
method: 'tools/list',
params: {}
})
});
}
})
.then(response => {
if (response) {
console.log(`✅ 工具列表响应: ${response.status} ${response.statusText}`);
return response.json();
}
})
.then(data => {
if (data) {
console.log('可用工具:', JSON.stringify(data, null, 2));
}
})
.catch(error => {
console.error('❌ MCP 连接失败:', error.message);
if (error.message.includes('Failed to fetch')) {
console.error('💡 可能的原因:');
console.error(' - CORS 未启用');
console.error(' - 服务器未运行');
console.error(' - 网络连接问题');
}
});
console.log('\n⏰ 等待测试完成...');