test-full-flow.js•4.98 kB
// Test the full MCP flow: initialize, get session ID, use it in subsequent requests
import http from 'http';
// Step 1: Initialize the session
async function initializeSession() {
const postData = JSON.stringify({
jsonrpc: '2.0',
method: 'initialize',
params: {
protocolVersion: '1.0',
clientInfo: { name: 'test-client', version: '1.0.0' },
capabilities: {}
},
id: 1
});
const options = {
hostname: 'localhost',
port: 5002,
path: '/mcp',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'Content-Length': Buffer.byteLength(postData),
'Connection': 'close'
}
};
console.log('1. Initializing session...');
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let data = '';
// Get the session ID from response headers (case-insensitive)
const sessionHeader = Object.entries(res.headers).find(
([key]) => key.toLowerCase() === 'mcp-session-id'
);
const sessionId = sessionHeader ? sessionHeader[1] : null;
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 400) {
reject({
statusCode: res.statusCode,
error: data,
sessionId: null
});
} else {
try {
const json = JSON.parse(data);
resolve({
statusCode: res.statusCode,
data: json,
sessionId: sessionId
});
} catch (e) {
// Handle SSE format
const lines = data.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const json = JSON.parse(line.substring(5).trim());
resolve({
statusCode: res.statusCode,
data: json,
sessionId: sessionId
});
return;
} catch (e) {
// Ignore parsing errors
}
}
}
// If we get here, we couldn't parse the response
resolve({
statusCode: res.statusCode,
data: data,
sessionId: sessionId
});
}
}
});
});
req.on('error', (error) => {
reject({
error: error.message,
sessionId: null
});
});
req.write(postData);
req.end();
});
}
// Step 2: Make a request with the session ID
async function makeRequest(sessionId, method, params = {}) {
const postData = JSON.stringify({
jsonrpc: '2.0',
method: method,
params: params,
id: Math.floor(Math.random() * 1000) // Random ID for each request
});
const options = {
hostname: 'localhost',
port: 5002,
path: '/mcp',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'Content-Length': Buffer.byteLength(postData),
'mcp-session-id': sessionId,
'Connection': 'close'
}
};
console.log(`\n2. Making request: ${method}`);
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const json = JSON.parse(data);
resolve({
statusCode: res.statusCode,
data: json,
headers: res.headers
});
} catch (e) {
// Handle non-JSON responses
resolve({
statusCode: res.statusCode,
data: data,
headers: res.headers
});
}
});
});
req.on('error', (error) => {
reject({
error: error.message
});
});
req.write(postData);
req.end();
});
}
// Run the full test
async function runTest() {
try {
// Step 1: Initialize and get session ID
const initResult = await initializeSession();
console.log('Initialization result:', JSON.stringify(initResult, null, 2));
if (!initResult.sessionId) {
console.error('No session ID received during initialization');
return;
}
// Step 2: Use the session ID to list tools
const toolsResult = await makeRequest(initResult.sessionId, 'mcp.list_tools');
console.log('List tools result:', JSON.stringify(toolsResult, null, 2));
// Step 3: Test ping
const pingResult = await makeRequest(initResult.sessionId, 'spiderfoot_ping');
console.log('Ping result:', JSON.stringify(pingResult, null, 2));
} catch (error) {
console.error('Test failed:', error);
}
}
// Run the test
runTest();