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
151 lines • 6.81 kB
JavaScript
import { BaseToolHandler } from '../base-handler.js';
/**
* Handler for Next.js Edge Runtime analysis tool
*/
export class NextJSEdgeRuntimeHandler extends BaseToolHandler {
nextjsEngine;
constructor(nextjsEngine) {
super();
this.nextjsEngine = nextjsEngine;
}
tools = [
{
name: 'nextjs_edge_runtime',
description: 'Analyze Edge Runtime usage including routes, unsupported APIs, bundle size, and cold start performance.',
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');
}
if (toolName === 'nextjs_edge_runtime') {
return this.analyzeEdgeRuntime(args, session);
}
throw new Error(`Unknown Next.js edge runtime tool: ${toolName}`);
}
async analyzeEdgeRuntime(args, session) {
try {
const edgeInfo = await this.nextjsEngine.getEdgeRuntimeInfo();
let content = '⚡ **Next.js Edge Runtime Analysis**\n\n';
// Check if Edge Runtime is used
if (!edgeInfo.routes || edgeInfo.routes.length === 0) {
content += '❌ **No Edge Runtime routes detected**\n\n';
content += 'The Edge Runtime provides:\n';
content += '- ⚡ Faster cold starts\n';
content += '- 🌍 Global deployment at edge locations\n';
content += '- 💰 Lower costs for simple functions\n';
content += '- 🔒 Enhanced security with limited APIs\n\n';
content += '### How to Use Edge Runtime\n\n';
content += '```typescript\n';
content += '// App Router\n';
content += 'export const runtime = "edge"\n\n';
content += '// API Routes\n';
content += 'export const config = {\n';
content += ' runtime: "edge"\n';
content += '}\n';
content += '```\n';
return {
content: [{
type: 'text',
text: content.trim()
}]
};
}
// Edge routes found
content += `### 🌍 Edge Routes: ${edgeInfo.routes.length}\n\n`;
edgeInfo.routes.forEach((route) => {
content += `- ${route}\n`;
});
content += '\n';
// Bundle size
if (edgeInfo.bundleSize) {
content += `### 📦 Bundle Size: ${edgeInfo.bundleSize}\n\n`;
// Check if bundle is too large
const sizeMatch = edgeInfo.bundleSize.match(/(\d+)/);
const sizeInKB = sizeMatch ? parseInt(sizeMatch[1]) : 0;
if (sizeInKB > 128) {
content += '⚠️ **Large bundle size for Edge Runtime**\n';
content += 'Consider:\n';
content += '- Removing unnecessary dependencies\n';
content += '- Using dynamic imports\n';
content += '- Moving to Node.js runtime if needed\n\n';
}
}
// Cold start performance
if (edgeInfo.coldStart) {
content += `### 🚀 Cold Start: ${edgeInfo.coldStart}\n\n`;
const timeMatch = edgeInfo.coldStart.match(/(\d+)/);
const timeInMs = timeMatch ? parseInt(timeMatch[1]) : 0;
if (timeInMs > 100) {
content += '⚠️ **Slower than expected cold start**\n';
content += 'Edge Runtime should have <50ms cold starts.\n\n';
}
}
// Unsupported APIs
if (edgeInfo.unsupportedAPIs && edgeInfo.unsupportedAPIs.length > 0) {
content += '### ❌ Unsupported APIs Detected\n\n';
content += 'The following Node.js APIs are not available in Edge Runtime:\n';
edgeInfo.unsupportedAPIs.forEach((api) => {
content += `- **${api}**`;
// Provide alternatives
switch (api) {
case 'fs':
content += ' → Use fetch API or external storage';
break;
case 'path':
content += ' → Use URL API';
break;
case 'crypto':
content += ' → Use Web Crypto API';
break;
case 'child_process':
content += ' → Not available, use serverless functions';
break;
}
content += '\n';
});
content += '\n';
}
// Warnings
if (edgeInfo.warnings && edgeInfo.warnings.length > 0) {
content += '### ⚠️ Warnings\n\n';
edgeInfo.warnings.forEach((warning) => {
content += `- ${warning}\n`;
});
content += '\n';
}
// Best practices and recommendations
content += '### 💡 Edge Runtime Best Practices\n\n';
content += '1. **Keep bundles small** - Aim for <128KB\n';
content += '2. **Use Web APIs** - Avoid Node.js-specific APIs\n';
content += '3. **Optimize cold starts** - Minimize initialization code\n';
content += '4. **Consider middleware** - Great use case for Edge Runtime\n';
content += '5. **Monitor performance** - Track cold start times\n';
return {
content: [{
type: 'text',
text: content.trim()
}]
};
}
catch (error) {
throw new Error(`Failed to analyze Edge Runtime: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
//# sourceMappingURL=nextjs-edge-runtime-handler.js.map