#!/usr/bin/env npx tsx import fs from 'fs'; import path from 'path'; /** * Test script to identify the timing issue with MCP tool loading */ async function testMCPTimingIssue() { console.log('šŸ• Testing MCP Tool Loading Timing Issue\n'); const configPath = path.join(process.env.HOME!, '.config/hashgraph-online/mcp-servers.json'); const servers = JSON.parse(fs.readFileSync(configPath, 'utf8')); console.log('Current server statuses:'); servers.forEach((server: any) => { console.log(`- ${server.name}: status=${server.status}, tools=${server.tools?.length || 0}`); }); console.log('\nšŸ” Analysis:\n'); console.log('The issue is a race condition in mcp-service.ts:'); console.log('1. Server connects successfully (connectServer method)'); console.log('2. fetchAndSaveTools is called with a 5-second setTimeout'); console.log('3. Something disconnects the server before 5 seconds'); console.log('4. When fetchAndSaveTools runs, server is gone'); console.log('5. Warning: "Server was disconnected before tools could be fetched"'); console.log('\nšŸ› Likely Causes:'); console.log('1. Multiple connect attempts causing disconnection of existing connection'); console.log('2. Frontend rapidly calling loadServers/reloadServers'); console.log('3. The server process itself exiting after initial connection'); console.log('\nāœ… Proposed Fix:'); console.log('1. Reduce the setTimeout delay from 5000ms to 1000ms'); console.log('2. Add a flag to prevent duplicate connections'); console.log('3. Check if server is still connected before fetching tools'); console.log('4. Fetch tools immediately after successful handshake instead of setTimeout'); console.log('\nšŸ“ The fix needs to be in fetchAndSaveTools method:'); console.log(` private async fetchAndSaveTools(serverId: string): Promise { setTimeout(async () => { try { if (!this.servers.has(serverId)) { this.logger.warn(\`Server \${serverId} disconnected before tools fetch\`); return; } } catch (error) { } }, 1000); // Reduced from 5000ms } `); } testMCPTimingIssue().catch(console.error);