#!/usr/bin/env npx tsx import fs from 'fs'; import path from 'path'; import { spawn } from 'child_process'; /** * Diagnose why MCP tools aren't loading despite successful connections */ async function diagnoseMCPTools() { console.log('๐Ÿ” MCP Tools Loading Diagnostic\n'); const configPath = path.join(process.env.HOME!, '.config/hashgraph-online/mcp-servers.json'); console.log(`๐Ÿ“ Config Path: ${configPath}\n`); if (!fs.existsSync(configPath)) { console.log('โŒ Config file not found!\n'); return; } const configContent = fs.readFileSync(configPath, 'utf8'); let servers: any[]; try { servers = JSON.parse(configContent); console.log(`โœ… Config parsed successfully - ${servers.length} servers\n`); } catch (error) { console.log(`โŒ Failed to parse config: ${error}\n`); console.log('Raw content (first 500 chars):'); console.log(configContent.substring(0, 500)); return; } console.log('๐Ÿ“Š Server Tool Status:\n'); for (const server of servers) { console.log(`๐Ÿ“Œ ${server.name} (${server.type})`); console.log(` ID: ${server.id}`); console.log(` Status: ${server.status}`); console.log(` Enabled: ${server.enabled}`); console.log(` Tools: ${server.tools?.length || 0}`); if (server.tools && server.tools.length > 0) { console.log(` Tool Names: ${server.tools.map((t: any) => t.name).join(', ')}`); } console.log(''); } console.log('๐Ÿงช Testing Direct MCP Server Connections:\n'); const wikipediaServer = servers.find(s => s.name === 'Wikipedia'); if (wikipediaServer && wikipediaServer.config.type === 'custom') { console.log('Testing Wikipedia MCP server...'); await testMCPServer('Wikipedia', 'npx', ['-y', 'wikipedia-mcp']); } const filesystemServer = servers.find(s => s.type === 'filesystem'); if (filesystemServer) { console.log('Testing Filesystem MCP server...'); await testMCPServer('Filesystem', 'npx', [ '-y', '@modelcontextprotocol/server-filesystem', filesystemServer.config.rootPath || '/Users/michaelkantor' ]); } } /** * Test a single MCP server connection and tool listing */ function testMCPServer(name: string, command: string, args: string[]): Promise { return new Promise((resolve) => { console.log(`\n๐Ÿ”Œ Testing ${name}:`); console.log(` Command: ${command} ${args.join(' ')}`); const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'pipe'], env: { ...process.env } }); let stdout = ''; let stderr = ''; let toolsFound = false; const initRequest = { jsonrpc: '2.0', method: 'initialize', params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'mcp-diagnostic', version: '1.0.0' } }, id: 1 }; const toolsRequest = { jsonrpc: '2.0', method: 'tools/list', params: {}, id: 2 }; child.stdout?.on('data', (data) => { stdout += data.toString(); const lines = data.toString().split('\n'); for (const line of lines) { if (line.trim()) { try { const parsed = JSON.parse(line); if (parsed.id === 1 && parsed.result) { console.log(' โœ… Server initialized successfully'); child.stdin?.write(JSON.stringify(toolsRequest) + '\n'); } else if (parsed.id === 2 && parsed.result?.tools) { console.log(` โœ… Tools found: ${parsed.result.tools.length}`); parsed.result.tools.slice(0, 3).forEach((tool: any) => { console.log(` - ${tool.name}: ${tool.description || 'No description'}`); }); if (parsed.result.tools.length > 3) { console.log(` ... and ${parsed.result.tools.length - 3} more`); } toolsFound = true; child.kill(); } } catch (e) { } } } }); child.stderr?.on('data', (data) => { stderr += data.toString(); }); setTimeout(() => { child.stdin?.write(JSON.stringify(initRequest) + '\n'); }, 100); setTimeout(() => { if (!toolsFound) { console.log(' โš ๏ธ Timeout - no tools response received'); if (stderr) { console.log(' Stderr:', stderr.substring(0, 200)); } } child.kill(); resolve(); }, 5000); child.on('exit', () => { resolve(); }); }); } diagnoseMCPTools().catch(console.error);