first commit
This commit is contained in:
75
web/test-mcp-fixed.js
Normal file
75
web/test-mcp-fixed.js
Normal file
@@ -0,0 +1,75 @@
|
||||
// 测试修复后的 MCP 连接 (CommonJS)
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
async function testMCPConnection() {
|
||||
try {
|
||||
console.log('🧪 测试 MCP 连接 (修复版)...');
|
||||
|
||||
// 测试健康检查
|
||||
const healthResponse = await fetch('http://127.0.0.1:3100/health');
|
||||
console.log('✅ 健康检查:', healthResponse.status, healthResponse.statusText);
|
||||
|
||||
// 测试 MCP initialize 请求 (修复 Accept 头)
|
||||
const initResponse = await fetch('http://127.0.0.1:3100/mcp', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json, text/event-stream' // 修复的关键行
|
||||
},
|
||||
body: JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
id: uuidv4(),
|
||||
method: 'initialize',
|
||||
params: {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {
|
||||
roots: {
|
||||
listChanged: true
|
||||
},
|
||||
sampling: {}
|
||||
},
|
||||
clientInfo: {
|
||||
name: "xiaozhi-client",
|
||||
version: "1.0.0"
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
console.log('🔧 MCP Initialize:', initResponse.status, initResponse.statusText);
|
||||
|
||||
if (initResponse.ok) {
|
||||
const result = await initResponse.json();
|
||||
console.log('✅ MCP Initialize 成功:', JSON.stringify(result, null, 2));
|
||||
|
||||
// 如果初始化成功,测试获取工具列表
|
||||
const toolsResponse = await fetch('http://127.0.0.1:3100/mcp', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json, text/event-stream'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
id: uuidv4(),
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
})
|
||||
});
|
||||
|
||||
if (toolsResponse.ok) {
|
||||
const toolsResult = await toolsResponse.json();
|
||||
console.log('🔧 工具列表:', JSON.stringify(toolsResult, null, 2));
|
||||
}
|
||||
|
||||
} else {
|
||||
const error = await initResponse.text();
|
||||
console.log('❌ MCP Initialize 失败:', error);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 连接错误:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
testMCPConnection();
|
||||
Reference in New Issue
Block a user