test-mcp-protocol.mjsā¢2.55 kB
#!/usr/bin/env node
import { spawn } from 'child_process';
console.log('š§ === TESTING MCP PROTOCOL HANDSHAKE ===\n');
// First test: initialize protocol
const initPayload = {
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: {
name: "test-client",
version: "1.0.0"
}
}
};
console.log('š Testing MCP initialize handshake...');
console.log('šØ Init payload:', JSON.stringify(initPayload, null, 2));
const serverProcess = spawn('node', ['build/index.js'], {
stdio: ['pipe', 'pipe', 'pipe']
});
let responseData = '';
let serverReady = false;
serverProcess.stdout.on('data', (data) => {
const chunk = data.toString();
responseData += chunk;
console.log('š¤ Response chunk:', chunk.trim());
});
serverProcess.stderr.on('data', (data) => {
const logMessage = data.toString();
console.log('š¢ Server log:', logMessage.trim());
// Wait for server ready message
if (logMessage.includes('š') && logMessage.includes('running on stdio')) {
serverReady = true;
console.log('\nā
Server ready, sending initialize...');
setTimeout(() => {
const payload = JSON.stringify(initPayload) + '\n';
console.log('šØ Sending payload of length:', payload.length);
serverProcess.stdin.write(payload);
// Wait for response
setTimeout(() => {
console.log('\nš === RESPONSE ANALYSIS ===');
console.log('š¤ Total response length:', responseData.length);
if (responseData.trim()) {
console.log('š¤ Raw response:\n' + responseData);
// Try parsing each line as JSON
const lines = responseData.split('\n').filter(line => line.trim());
console.log('š Response lines count:', lines.length);
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
try {
const parsed = JSON.parse(line);
console.log(`ā
Line ${i + 1} is valid JSON:`, JSON.stringify(parsed, null, 2));
} catch (e) {
console.log(`ā Line ${i + 1} is not JSON:`, line);
}
}
}
} else {
console.log('ā No response received on stdout');
}
serverProcess.kill();
}, 3000);
}, 1000);
}
});
serverProcess.on('exit', (code) => {
console.log('\nš Test completed with exit code:', code);
});