#!/usr/bin/env node
const { spawn } = require('child_process');
async function testMCPComprehensive() {
console.log('š Comprehensive MCP Server Test');
console.log('================================\n');
return new Promise((resolve, reject) => {
const serverProcess = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env, NODE_ENV: 'production' }
});
let messageId = 1;
let testResults = {
initialization: false,
toolsList: false,
detectErrors: false,
analyzeError: false,
protocolClean: true,
stderrEmpty: true
};
let stderrData = '';
function sendMessage(method, params = {}) {
const message = {
jsonrpc: '2.0',
id: messageId++,
method,
params
};
const messageStr = JSON.stringify(message) + '\n';
serverProcess.stdin.write(messageStr);
return messageId - 1;
}
serverProcess.stdout.on('data', (data) => {
const responses = data.toString().trim().split('\n');
responses.forEach(response => {
if (response.trim()) {
try {
const parsed = JSON.parse(response);
if (parsed.id === 1) {
// Initialization response
if (parsed.result && parsed.result.protocolVersion) {
testResults.initialization = true;
console.log('ā
Initialization successful');
setTimeout(() => sendMessage('tools/list'), 100);
}
} else if (parsed.id === 2) {
// Tools list response
if (parsed.result && parsed.result.tools && parsed.result.tools.length > 0) {
testResults.toolsList = true;
console.log(`ā
Tools list received (${parsed.result.tools.length} tools)`);
// Test detect-errors tool
setTimeout(() => {
sendMessage('tools/call', {
name: 'detect-errors',
arguments: {
source: 'all',
includeWarnings: false
}
});
}, 100);
}
} else if (parsed.id === 3) {
// Detect-errors response
if (parsed.result) {
testResults.detectErrors = true;
console.log('ā
detect-errors tool executed successfully');
// Test analyze-error tool with a dummy error ID
setTimeout(() => {
sendMessage('tools/call', {
name: 'analyze-error',
arguments: {
errorId: 'test-error-id',
includeContext: true
}
});
}, 100);
}
} else if (parsed.id === 4) {
// Analyze-error response
if (parsed.result || parsed.error) {
testResults.analyzeError = true;
console.log('ā
analyze-error tool executed successfully');
// All tests complete
setTimeout(() => {
finishTests();
}, 100);
}
}
} catch (error) {
console.error('ā Failed to parse JSON response:', response);
testResults.protocolClean = false;
}
}
});
});
serverProcess.stderr.on('data', (data) => {
stderrData += data.toString();
testResults.stderrEmpty = false;
console.log('š„ STDERR (unexpected):', data.toString());
});
serverProcess.on('close', (code) => {
console.log(`\nš Server process exited with code ${code}`);
// Final test results
console.log('\nš Test Results Summary:');
console.log('========================');
console.log(`ā
Initialization: ${testResults.initialization ? 'PASS' : 'FAIL'}`);
console.log(`ā
Tools List: ${testResults.toolsList ? 'PASS' : 'FAIL'}`);
console.log(`ā
Detect Errors Tool: ${testResults.detectErrors ? 'PASS' : 'FAIL'}`);
console.log(`ā
Analyze Error Tool: ${testResults.analyzeError ? 'PASS' : 'FAIL'}`);
console.log(`ā
Protocol Clean: ${testResults.protocolClean ? 'PASS' : 'FAIL'}`);
console.log(`ā
STDERR Empty: ${testResults.stderrEmpty ? 'PASS' : 'FAIL'}`);
const allPassed = Object.values(testResults).every(result => result === true);
if (allPassed) {
console.log('\nš ALL TESTS PASSED! MCP server is fully functional.');
console.log('ā
MCP protocol communication is working correctly');
console.log('ā
No logging interference detected');
console.log('ā
All tools are accessible and functional');
resolve(true);
} else {
console.log('\nā Some tests failed. See results above.');
resolve(false);
}
});
serverProcess.on('error', (error) => {
console.error('ā Server process error:', error);
reject(error);
});
function finishTests() {
console.log('\nš All tests completed, closing server...');
serverProcess.kill('SIGTERM');
}
// Start the test sequence
console.log('š Starting comprehensive test...');
sendMessage('initialize', {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: {
name: 'comprehensive-test-client',
version: '1.0.0'
}
});
// Safety timeout
setTimeout(() => {
console.log('\nā° Test timeout - some tests may not have completed');
finishTests();
}, 15000);
});
}
testMCPComprehensive()
.then(success => {
process.exit(success ? 0 : 1);
})
.catch(error => {
console.error('Test failed with error:', error);
process.exit(1);
});