test-updated-network.mjsā¢3.08 kB
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log('=== TEST ANALISI RETE AGGIORNATA ===\n');
// Start the MCP server
const serverPath = join(__dirname, 'build', 'index.js');
console.log(`Starting MCP server: ${serverPath}`);
const serverProcess = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env }
});
let serverReady = false;
let responseBuffer = '';
// Handle server startup
serverProcess.stderr.on('data', (data) => {
const message = data.toString();
console.log(`[Server]: ${message.trim()}`);
if (message.includes('MCP server running on stdio')) {
serverReady = true;
console.log('ā
Server ready, starting network analysis test...\n');
testNetworkAnalysis();
}
});
// Handle responses
serverProcess.stdout.on('data', (data) => {
responseBuffer += data.toString();
// Try to parse complete JSON responses
const lines = responseBuffer.split('\n');
responseBuffer = lines.pop() || ''; // Keep incomplete line in buffer
for (const line of lines) {
if (line.trim()) {
try {
const response = JSON.parse(line);
console.log('š MCP Response:', JSON.stringify(response, null, 2));
if (response.result?.content?.[0]?.text) {
console.log('\nšÆ RISULTATO ANALISI:');
console.log(response.result.content[0].text);
console.log('\n' + '='.repeat(50));
}
// Close after getting result
if (response.result) {
setTimeout(() => {
serverProcess.kill();
process.exit(0);
}, 1000);
}
} catch (e) {
console.log(`[Raw Output]: ${line}`);
}
}
}
});
function testNetworkAnalysis() {
if (!serverReady) {
console.log('ā Server not ready yet');
return;
}
const request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "visum_network_analysis",
arguments: {
analysisType: "detailed"
}
}
};
console.log('š Sending network analysis request...');
console.log('Request:', JSON.stringify(request, null, 2));
serverProcess.stdin.write(JSON.stringify(request) + '\n');
}
// Handle process events
serverProcess.on('close', (code) => {
console.log(`\nš Test completato (exit code: ${code})`);
});
serverProcess.on('error', (error) => {
console.error('ā Server error:', error);
});
// Send initialization
setTimeout(() => {
if (serverReady) return;
const initRequest = {
jsonrpc: "2.0",
id: 0,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0.0" }
}
};
console.log('š” Sending initialization...');
serverProcess.stdin.write(JSON.stringify(initRequest) + '\n');
}, 100);
console.log('ā³ Waiting for server to start...');