ai-debug-local-mcp
Version:
🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh
164 lines • 7.45 kB
JavaScript
import { BaseToolHandler } from '../base-handler.js';
/**
* Handler for Next.js route debugging tool
*/
export class NextJSRouteDebugHandler extends BaseToolHandler {
nextjsEngine;
constructor(nextjsEngine) {
super();
this.nextjsEngine = nextjsEngine;
}
tools = [
{
name: 'nextjs_debug_route',
description: 'Complete analysis of current route including rendering, data fetching, performance, and issues.',
inputSchema: {
type: 'object',
properties: {
sessionId: {
type: 'string',
description: 'Debug session ID'
}
},
required: ['sessionId']
}
}
];
async handle(toolName, args, sessions) {
const session = sessions.get(args.sessionId);
if (!session) {
throw new Error(`Debug session ${args.sessionId} not found`);
}
// Validate this is a Next.js app
if (session.framework !== 'nextjs' && session.framework !== 'next' && !(session.framework === 'react' && session.isNextJS)) {
throw new Error('This tool only works with Next.js applications');
}
switch (toolName) {
case 'nextjs_debug_route':
return this.debugRoute(args, session);
default:
throw new Error(`Unknown Next.js route debug tool: ${toolName}`);
}
}
async debugRoute(args, session) {
try {
const debugInfo = await this.nextjsEngine.debugRoute();
let content = '🔍 **Next.js Route Debug**\n\n';
// Route Information
content += `### 📍 Route Information\n\n`;
content += `- **Route:** ${debugInfo.currentRoute}\n`;
content += `- **Type:** ${debugInfo.routeType}\n`;
content += `- **Rendering:** ${debugInfo.renderingMethod}\n`;
content += `- **Cache Status:** ${debugInfo.cacheStatus}\n\n`;
// Data Fetching
if (debugInfo.dataFetching && debugInfo.dataFetching.length > 0) {
content += '### 📊 Data Fetching\n\n';
debugInfo.dataFetching.forEach((fetchMethod) => {
content += `- **${fetchMethod}**\n`;
});
content += '\n';
}
// Performance Metrics
if (debugInfo.performance) {
content += '### ⚡ Performance Metrics\n\n';
const perf = debugInfo.performance;
content += `- **Render Time:** ${perf.renderTime}ms`;
if (perf.renderTime > 1000) {
content += ' ⚠️ (Slow)';
}
content += '\n';
content += `- **Hydration Time:** ${perf.hydrationTime}ms`;
if (perf.hydrationTime > 300) {
content += ' ⚠️ (Slow)';
}
content += '\n';
content += `- **Total Time:** ${perf.totalTime}ms`;
if (perf.totalTime > 1500) {
content += ' ⚠️ (Slow)';
}
content += '\n\n';
}
// Issues Detected
if (debugInfo.issues && debugInfo.issues.length > 0) {
content += '### ⚠️ Issues Detected\n\n';
debugInfo.issues.forEach((issue) => {
content += `- ${issue}\n`;
});
content += '\n';
}
else {
content += '### ✅ No issues detected\n\n';
content += 'This route appears to be well optimized!\n\n';
}
// Recommendations based on issues
if (debugInfo.issues && debugInfo.issues.length > 0) {
content += '### 💡 Recommendations\n\n';
if (debugInfo.issues.some((i) => i.includes('Sequential data fetching'))) {
content += '**Fix Sequential Fetching:**\n';
content += '```typescript\n';
content += '// ❌ Sequential\n';
content += 'const user = await getUser()\n';
content += 'const posts = await getPosts(user.id)\n\n';
content += '// ✅ Parallel\n';
content += 'const [user, posts] = await Promise.all([\n';
content += ' getUser(),\n';
content += ' getPosts(userId)\n';
content += '])\n';
content += '```\n\n';
}
if (debugInfo.issues.some((i) => i.includes('Deep layout nesting'))) {
content += '**Reduce Layout Nesting:**\n';
content += '- Combine related layouts\n';
content += '- Use route groups for organization\n';
content += '- Avoid unnecessary wrappers\n\n';
}
if (debugInfo.issues.some((i) => i.includes('Slow TTFB'))) {
content += '**Improve TTFB:**\n';
content += '- Cache database queries\n';
content += '- Optimize server middleware\n';
content += '- Use static generation when possible\n';
content += '- Consider Edge Runtime\n\n';
}
}
// Router-specific tips
const isAppRouter = debugInfo.renderingMethod === 'SSR' || debugInfo.renderingMethod === 'SSG';
if (isAppRouter) {
content += '### 🎯 App Router Tips\n\n';
content += '1. **Use Server Components** by default\n';
content += '2. **Add "use client"** only when needed\n';
content += '3. **Leverage streaming** with Suspense\n';
content += '4. **Cache data fetching** appropriately\n';
content += '5. **Use parallel routes** for complex layouts\n\n';
}
else {
content += '### 🎯 Pages Router Tips\n\n';
content += '1. **Consider migrating** to App Router\n';
content += '2. **Use getStaticProps** for static data\n';
content += '3. **Implement ISR** for dynamic content\n';
content += '4. **Optimize getServerSideProps** calls\n';
content += '5. **Use dynamic imports** for code splitting\n\n';
}
// Debug Commands
content += '### 🛠️ Debug Commands\n\n';
content += '```bash\n';
content += '# Build analysis\n';
content += 'ANALYZE=true npm run build\n\n';
content += '# Route tracing\n';
content += 'DEBUG=next:* npm run dev\n\n';
content += '# Performance profiling\n';
content += 'npm run build && npm run start\n';
content += '```\n';
return {
content: [{
type: 'text',
text: content.trim()
}],
debugInfo
};
}
catch (error) {
throw new Error(`Failed to debug route: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
//# sourceMappingURL=nextjs-route-debug-handler.js.map