final-mcp-test.js•3.63 kB
// Final MCP Test with proper headers and session handling
import axios from 'axios';
const MCP_SERVER_URL = 'http://localhost:5002/mcp';
async function testMCP() {
try {
// 1. Initialize the session with proper Accept header
console.log('1. Initializing MCP session...');
const initResponse = await axios({
method: 'post',
url: MCP_SERVER_URL,
data: {
jsonrpc: '2.0',
method: 'initialize',
params: {
protocolVersion: '1.0',
clientInfo: { name: 'final-test', version: '1.0.0' },
capabilities: {}
},
id: 1
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
},
responseType: 'text',
// Disable automatic redirects
maxRedirects: 0,
// Don't throw on non-2xx status codes
validateStatus: () => true
});
console.log('Initialization status:', initResponse.status);
console.log('Response headers:', JSON.stringify(initResponse.headers, null, 2));
// 2. Extract session ID from response headers (case-insensitive)
const sessionHeader = Object.entries(initResponse.headers).find(
([key]) => key.toLowerCase() === 'mcp-session-id'
);
if (!sessionHeader || !sessionHeader[1]) {
console.error('No session ID in response headers');
console.log('Raw response data:', initResponse.data);
return;
}
const sessionId = sessionHeader[1];
console.log('Session ID:', sessionId);
// 3. Make a request with the session ID
console.log('\n2. Making request with session ID...');
const toolsResponse = await axios({
method: 'post',
url: MCP_SERVER_URL,
data: {
jsonrpc: '2.0',
method: 'mcp.list_tools',
params: {},
id: 2
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'mcp-session-id': sessionId,
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
},
responseType: 'text',
maxRedirects: 0,
validateStatus: () => true
});
console.log('Tools response status:', toolsResponse.status);
console.log('Response headers:', JSON.stringify(toolsResponse.headers, null, 2));
console.log('Response data:', toolsResponse.data);
// 4. Try calling a specific tool
console.log('\n3. Testing spiderfoot_ping...');
const pingResponse = await axios({
method: 'post',
url: MCP_SERVER_URL,
data: {
jsonrpc: '2.0',
method: 'spiderfoot_ping',
params: {},
id: 3
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'mcp-session-id': sessionId,
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
},
responseType: 'text',
maxRedirects: 0,
validateStatus: () => true
});
console.log('Ping response status:', pingResponse.status);
console.log('Response headers:', JSON.stringify(pingResponse.headers, null, 2));
console.log('Response data:', pingResponse.data);
} catch (error) {
console.error('Test failed:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response headers:', error.response.headers);
console.error('Response data:', error.response.data);
}
}
}
testMCP();