#!/usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
class LogAnalyzer {
private logFile: string;
constructor() {
this.logFile = path.join(process.cwd(), 'mcp-server.log');
}
analyzeLogs() {
console.log('๐ ANALYZING MCP SERVER LOGS');
console.log('============================');
if (!fs.existsSync(this.logFile)) {
console.log('โ No log file found at:', this.logFile);
console.log(' The server may not have run yet with enhanced logging.');
return;
}
try {
const logContent = fs.readFileSync(this.logFile, 'utf-8');
const lines = logContent.split('\n').filter(line => line.trim());
console.log(`๐ Log file: ${this.logFile}`);
console.log(`๐ Total log lines: ${lines.length}`);
console.log();
// Analyze different types of log entries
const stats = {
INFO: 0,
WARN: 0,
ERROR: 0,
DEBUG: 0,
REQUEST: 0,
RESPONSE: 0,
AUTH: 0
};
const authAttempts: string[] = [];
const errors: string[] = [];
const requests: string[] = [];
const responses: string[] = [];
lines.forEach(line => {
// Count by log level
Object.keys(stats).forEach(level => {
if (line.includes(`] ${level}:`)) {
(stats as any)[level]++;
}
});
// Collect authentication attempts
if (line.includes('AUTH:')) {
authAttempts.push(line);
}
// Collect errors
if (line.includes('ERROR:')) {
errors.push(line);
}
// Collect requests
if (line.includes('REQUEST:')) {
requests.push(line);
}
// Collect responses
if (line.includes('RESPONSE:')) {
responses.push(line);
}
});
console.log('๐ LOG STATISTICS:');
console.log('==================');
Object.entries(stats).forEach(([level, count]) => {
console.log(`${level.padEnd(10)}: ${count}`);
});
console.log();
console.log('๐ AUTHENTICATION ANALYSIS:');
console.log('===========================');
if (authAttempts.length === 0) {
console.log('โน๏ธ No authentication attempts found in logs');
} else {
console.log(`Total auth events: ${authAttempts.length}`);
authAttempts.slice(-10).forEach(line => {
const timestamp = line.match(/\[(.*?)\]/)?.[1];
const message = line.split('AUTH: ')[1];
console.log(` ${timestamp}: ${message}`);
});
if (authAttempts.length > 10) {
console.log(` ... and ${authAttempts.length - 10} more auth events`);
}
}
console.log();
console.log('โ ERROR ANALYSIS:');
console.log('==================');
if (errors.length === 0) {
console.log('โ
No errors found in logs');
} else {
console.log(`Total errors: ${errors.length}`);
console.log('Recent errors:');
errors.slice(-5).forEach(line => {
const timestamp = line.match(/\[(.*?)\]/)?.[1];
const message = line.split('ERROR: ')[1];
console.log(` ${timestamp}: ${message}`);
});
if (errors.length > 5) {
console.log(` ... and ${errors.length - 5} more errors`);
}
}
console.log();
console.log('๐ฅ REQUEST/RESPONSE ANALYSIS:');
console.log('============================');
console.log(`Total requests: ${requests.length}`);
console.log(`Total responses: ${responses.length}`);
if (requests.length > 0) {
console.log('\nRecent requests:');
requests.slice(-5).forEach(line => {
const timestamp = line.match(/\[(.*?)\]/)?.[1];
const message = line.split('REQUEST: ')[1];
console.log(` ${timestamp}: ${message}`);
});
}
if (responses.length > 0) {
console.log('\nRecent responses:');
responses.slice(-5).forEach(line => {
const timestamp = line.match(/\[(.*?)\]/)?.[1];
const message = line.split('RESPONSE: ')[1];
console.log(` ${timestamp}: ${message}`);
});
}
console.log();
console.log('๐ง DEBUGGING RECOMMENDATIONS:');
console.log('=============================');
if (errors.length > 0) {
console.log('โ Issues found:');
console.log(' - Check error messages above for authentication failures');
console.log(' - Verify credentials are correct');
console.log(' - Ensure API endpoints are accessible');
}
if (authAttempts.length === 0) {
console.log('โ ๏ธ No authentication attempts:');
console.log(' - The MCP server may not be receiving auth requests');
console.log(' - Check Claude Desktop configuration');
console.log(' - Verify server is starting correctly');
}
if (requests.length === 0) {
console.log('โ ๏ธ No requests processed:');
console.log(' - The MCP server may not be communicating with Claude Desktop');
console.log(' - Check stdio transport setup');
console.log(' - Verify Claude Desktop can launch the server');
}
if (stats.ERROR === 0 && authAttempts.length > 0 && requests.length > 0) {
console.log('โ
Server appears to be working correctly');
console.log(' - Authentication events logged');
console.log(' - Requests/responses processed');
console.log(' - No errors detected');
}
console.log();
console.log('๐ NEXT STEPS:');
console.log('==============');
console.log('1. Use the enhanced logging server: node dist/index-enhanced.js');
console.log('2. Configure Claude Desktop to use the enhanced server');
console.log('3. Try authentication and check this log for detailed info');
console.log('4. If issues persist, check the full log file:', this.logFile);
} catch (error) {
console.error('โ Error analyzing logs:', error);
}
}
clearLogs() {
if (fs.existsSync(this.logFile)) {
fs.writeFileSync(this.logFile, '');
console.log('๐๏ธ Log file cleared');
} else {
console.log('โน๏ธ No log file to clear');
}
}
tailLogs(lines: number = 20) {
if (!fs.existsSync(this.logFile)) {
console.log('โ No log file found');
return;
}
const content = fs.readFileSync(this.logFile, 'utf-8');
const logLines = content.split('\n').filter(line => line.trim());
const recentLines = logLines.slice(-lines);
console.log(`๐ Last ${recentLines.length} log entries:`);
console.log('================================');
recentLines.forEach(line => {
console.log(line);
});
}
}
// CLI interface
const analyzer = new LogAnalyzer();
const command = process.argv[2];
switch (command) {
case 'clear':
analyzer.clearLogs();
break;
case 'tail':
const lines = parseInt(process.argv[3] || '20');
analyzer.tailLogs(lines);
break;
case 'analyze':
default:
analyzer.analyzeLogs();
break;
}
export { LogAnalyzer };