claude-emulator.mjsβ’8.41 kB
#!/usr/bin/env node
// π€ EMULATORE CLAUDE - Test MCP Visum Tools
import { spawn } from 'child_process';
import readline from 'readline';
console.log('π€ EMULATORE CLAUDE PER TEST MCP VISUM');
console.log('β'.repeat(50));
console.log('π¬ Scrivi i comandi come se fossi Claude');
console.log('π§ I tool Visum verranno testati direttamente via MCP');
console.log('β Premi CTRL+C per uscire');
console.log('β'.repeat(50));
class ClaudeEmulator {
constructor() {
this.mcpServer = null;
this.requestId = 1;
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'π€ Claude> '
});
this.commands = new Map([
['check-visum', { tool: 'check_visum', args: {} }],
['status-visum', { tool: 'get_visum_status', args: {} }],
['launch-visum', { tool: 'visum_launch', requiresArgs: true, argPrompt: 'Percorso progetto (.ver):' }],
['analyze-network', { tool: 'visum_network_analysis', requiresArgs: true, argPrompt: 'Tipo analisi (basic/detailed/topology/performance):' }],
['export-network', { tool: 'visum_export_network', requiresArgs: true, argPrompt: 'Elementi da esportare (nodes,links,zones):' }],
['connectivity-stats', { tool: 'visum_connectivity_stats', args: {} }],
['python-script', { tool: 'visum_python_analysis', requiresArgs: true, argPrompt: 'Script Python:' }],
['val-script', { tool: 'visum_val_script', requiresArgs: true, argPrompt: 'Script VAL:' }]
]);
}
async startMCPServer() {
console.log('\nπ Avvio server MCP...');
this.mcpServer = spawn('node', ['build/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd()
});
this.mcpServer.stderr.on('data', (data) => {
const message = data.toString().trim();
if (message.includes('running')) {
console.log('β
', message);
this.initializeMCP();
}
});
this.mcpServer.stdout.on('data', (data) => {
const output = data.toString().trim();
if (output) {
try {
const response = JSON.parse(output);
this.handleMCPResponse(response);
} catch (e) {
console.log('π¨ Raw output:', output);
}
}
});
this.mcpServer.on('error', (error) => {
console.error('β Errore server MCP:', error.message);
});
}
async initializeMCP() {
console.log('π§ Inizializzazione MCP...');
const initRequest = {
jsonrpc: "2.0",
id: this.requestId++,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "claude-emulator", version: "1.0.0" }
}
};
this.sendMCPRequest(initRequest);
setTimeout(() => {
console.log('\nβ
Claude Emulator pronto!');
this.showHelp();
this.rl.prompt();
}, 1000);
}
sendMCPRequest(request) {
if (this.mcpServer && this.mcpServer.stdin.writable) {
this.mcpServer.stdin.write(JSON.stringify(request) + '\n');
}
}
handleMCPResponse(response) {
console.log('\nπ¨ RISPOSTA MCP:');
console.log('β'.repeat(20));
if (response.result && response.result.content) {
// Risposta tool
response.result.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text);
}
});
} else if (response.result && response.result.tools) {
// Lista tool
console.log(`π οΈ ${response.result.tools.length} tool disponibili`);
const visumTools = response.result.tools.filter(t => t.name.startsWith('visum_'));
console.log(`π§ ${visumTools.length} tool Visum trovati`);
} else if (response.error) {
// Errore
console.log('β ERRORE:', response.error.message);
if (response.error.data) {
console.log('π Dettagli:', response.error.data);
}
} else {
// Altra risposta
console.log('π Risposta:', JSON.stringify(response, null, 2));
}
console.log('β'.repeat(20));
this.rl.prompt();
}
showHelp() {
console.log('\nπ COMANDI DISPONIBILI:');
console.log('β'.repeat(25));
console.log('π§ check-visum - Verifica installazione Visum');
console.log('π status-visum - Status connessione Visum');
console.log('π launch-visum - Lancia Visum con progetto');
console.log('π analyze-network - Analisi rete dettagliata');
console.log('π€ export-network - Export elementi rete');
console.log('π connectivity-stats - Statistiche connettivitΓ ');
console.log('π python-script - Esegui script Python');
console.log('π val-script - Esegui script VAL');
console.log('β help - Mostra questo aiuto');
console.log('πͺ exit - Esci dall\'emulatore');
}
async handleCommand(input) {
const command = input.trim().toLowerCase();
if (command === 'help' || command === '?') {
this.showHelp();
return;
}
if (command === 'exit' || command === 'quit') {
console.log('π Chiusura emulatore Claude...');
if (this.mcpServer) {
this.mcpServer.kill();
}
process.exit(0);
}
if (this.commands.has(command)) {
const cmd = this.commands.get(command);
await this.executeToolCommand(cmd, command);
} else {
console.log(`β Comando non riconosciuto: ${command}`);
console.log('π‘ Usa "help" per vedere i comandi disponibili');
}
}
async executeToolCommand(cmd, commandName) {
console.log(`\nπ§ Esecuzione: ${cmd.tool}`);
let args = cmd.args || {};
// Se servono argomenti, chiedili
if (cmd.requiresArgs) {
args = await this.getCommandArgs(cmd, commandName);
}
const toolRequest = {
jsonrpc: "2.0",
id: this.requestId++,
method: "tools/call",
params: {
name: cmd.tool,
arguments: args
}
};
console.log('π€ Invio richiesta MCP...');
this.sendMCPRequest(toolRequest);
}
async getCommandArgs(cmd, commandName) {
return new Promise((resolve) => {
if (commandName === 'launch-visum') {
this.rl.question('π Percorso progetto Visum (.ver): ', (projectPath) => {
resolve({
projectPath: projectPath.trim(),
visible: true
});
});
} else if (commandName === 'analyze-network') {
this.rl.question('π Tipo analisi (basic/detailed/topology/performance): ', (analysisType) => {
resolve({
analysisType: analysisType.trim() || 'detailed',
exportPath: 'C:\\temp\\mcp_analysis'
});
});
} else if (commandName === 'export-network') {
this.rl.question('π€ Elementi (nodes,links,zones): ', (elements) => {
const elementList = elements.trim().split(',').map(e => e.trim()).filter(e => e);
resolve({
elements: elementList.length > 0 ? elementList : ['nodes', 'links'],
outputDir: 'C:\\temp\\mcp_export',
method: 'python'
});
});
} else if (commandName === 'python-script') {
console.log('π Scrivi script Python (termina con una riga vuota):');
let script = '';
const collectScript = () => {
this.rl.question('>>> ', (line) => {
if (line.trim() === '') {
resolve({ script });
} else {
script += line + '\\n';
collectScript();
}
});
};
collectScript();
} else if (commandName === 'val-script') {
this.rl.question('π Script VAL: ', (script) => {
resolve({ script: script.trim() });
});
} else {
resolve({});
}
});
}
start() {
this.startMCPServer();
this.rl.on('line', (input) => {
this.handleCommand(input);
});
this.rl.on('close', () => {
console.log('\\nπ Arrivederci!');
if (this.mcpServer) {
this.mcpServer.kill();
}
process.exit(0);
});
}
}
// Avvia emulatore
console.log('π― Avvio Claude Emulator per test Visum...');
const emulator = new ClaudeEmulator();
emulator.start();