#!/usr/bin/env tsx import { MCPService } from '../src/main/services/mcp-service'; import * as path from 'path'; import * as os from 'os'; import * as fs from 'fs'; /** * Script to verify all MCP service fixes are working */ async function verifyMCPFixes(): Promise { console.log('šŸ” Verifying MCP Service Fixes...\n'); const mcpService = MCPService.getInstance(); let allTestsPassed = true; try { console.log('1ļøāƒ£ Testing Config Path Fix...'); const configPath = (mcpService as any).configPath; const expectedPath = path.join(os.homedir(), '.config', 'hashgraph-online', 'mcp-servers.json'); const wrongPath = path.join(os.homedir(), 'mcp-servers.json'); if (configPath === expectedPath) { console.log(' āœ… Config path is correct:', configPath); } else { console.log(' āŒ Config path is wrong:', configPath); console.log(' Expected:', expectedPath); allTestsPassed = false; } if (fs.existsSync(wrongPath)) { console.log(' āš ļø Old config file still exists at wrong location:', wrongPath); console.log(' Please run: rm', `"${wrongPath}"`); } else { console.log(' āœ… No config file at wrong location'); } console.log('\n2ļøāƒ£ Testing JSON Format Fix...'); const configs = await mcpService.loadServers(); if (fs.existsSync(configPath)) { const content = fs.readFileSync(configPath, 'utf8'); if (content.endsWith('\n')) { console.log(' āœ… JSON file ends with newline'); } else { console.log(' āŒ JSON file does not end with newline'); allTestsPassed = false; } try { JSON.parse(content.trim()); console.log(' āœ… JSON is valid'); } catch (error) { console.log(' āŒ JSON is invalid:', error); allTestsPassed = false; } } else { console.log(' āŒ Config file does not exist'); allTestsPassed = false; } console.log('\n3ļøāƒ£ Testing Server Configurations...'); console.log(` šŸ“Š Found ${configs.length} server configurations:`); configs.forEach((config, index) => { console.log(` ${index + 1}. ${config.name} (${config.type}) - ${config.status}`); }); const filesystemServer = configs.find(c => c.type === 'filesystem'); if (filesystemServer) { console.log(' āœ… Filesystem server found'); } else { console.log(' āŒ No filesystem server found'); allTestsPassed = false; } console.log('\n4ļøāƒ£ Testing Connection and Tools Loading...'); if (filesystemServer) { console.log(` šŸ”Œ Testing connection to ${filesystemServer.name}...`); await mcpService.disconnectServer(filesystemServer.id); const connectionResult = await mcpService.testConnection(filesystemServer); if (connectionResult.success) { console.log(' āœ… Connection test successful'); console.log(` šŸ› ļø Found ${connectionResult.tools?.length || 0} tools in test`); console.log(' šŸ”— Attempting actual connection...'); const connectResult = await mcpService.connectServer(filesystemServer.id); if (connectResult.success) { console.log(' āœ… Connection successful'); console.log(' ā³ Waiting for tools to be fetched...'); await new Promise(resolve => setTimeout(resolve, 8000)); const updatedConfigs = await mcpService.loadServers(); const updatedServer = updatedConfigs.find(c => c.id === filesystemServer.id); if (updatedServer) { console.log(` šŸ“Š Server status: ${updatedServer.status}`); console.log(` šŸ› ļø Tools loaded: ${updatedServer.tools?.length || 0}`); if (updatedServer.status === 'ready' && (updatedServer.tools?.length || 0) > 0) { console.log(' āœ… Tools loading is working correctly'); console.log(' šŸ·ļø Available tools:', updatedServer.tools?.slice(0, 3).map(t => t.name).join(', ') + '...'); } else if (updatedServer.status === 'connected') { console.log(' āš ļø Server connected but tools not yet loaded (this is normal)'); } else { console.log(' āŒ Tools loading may have issues'); allTestsPassed = false; } } else { console.log(' āŒ Could not find updated server config'); allTestsPassed = false; } } else { console.log(' āŒ Connection failed:', connectResult.error); allTestsPassed = false; } } else { console.log(' āŒ Connection test failed:', connectionResult.error); allTestsPassed = false; } await mcpService.disconnectServer(filesystemServer.id); } else { console.log(' āš ļø Skipping connection test - no filesystem server available'); } console.log('\n5ļøāƒ£ Testing Error Handling...'); try { const invalidResult = await mcpService.testConnection({ id: 'invalid-test', name: 'Invalid Test', type: 'custom', status: 'disconnected', enabled: true, config: { type: 'custom', command: 'nonexistent-command', args: [] }, tools: [], createdAt: new Date(), updatedAt: new Date(), }); if (!invalidResult.success) { console.log(' āœ… Error handling works correctly for invalid configs'); } else { console.log(' āš ļø Invalid config test unexpectedly succeeded'); } } catch (error) { console.log(' āœ… Error handling works correctly:', (error as Error).message); } console.log('\nšŸ“‹ Summary:'); if (allTestsPassed) { console.log(' šŸŽ‰ All MCP service fixes are working correctly!'); console.log(' šŸš€ The desktop app should now have resolved:'); console.log(' - JSON parsing errors'); console.log(' - Config file location issues'); console.log(' - Tools loading problems'); console.log(' - Excessive load/save cycles'); } else { console.log(' āŒ Some issues were found that need attention'); } } catch (error) { console.error('āŒ Verification failed with error:', error); } finally { await mcpService.disconnectAll(); } } verifyMCPFixes().catch(console.error);