analyze-index-main.js•2.05 kB
#!/usr/bin/env node
/**
* Research script to analyze the main() function structure
* and plan the refactoring approach
*/
console.log('=== Index.ts main() Function Analysis ===\n');
console.log('Current Structure (73 lines):');
console.log('1. Lines 266-269: Initialize debug and rate limiter');
console.log('2. Lines 271-282: Create server with config');
console.log('3. Lines 284-289: Register tools/list handler');
console.log('4. Lines 291-354: Register tools/call handler (64 lines!)');
console.log(' - Lines 293-309: Rate limit checking');
console.log(' - Lines 311-325: Tool validation and lookup');
console.log(' - Lines 327-335: Tool execution and result');
console.log(' - Lines 336-353: Error handling');
console.log('5. Lines 356-361: Create transport and connect');
console.log('\nRefactoring Strategy:');
console.log('1. Extract createServer() - server initialization');
console.log('2. Extract registerHandlers() - handler registration');
console.log('3. Extract handleToolCall() - the big tools/call handler');
console.log('4. Extract handleRateLimit() - rate limit logic');
console.log('5. Extract executeToolFunction() - tool execution');
console.log('6. Extract formatToolError() - error formatting');
console.log('\nExpected main() after refactoring (~15 lines):');
console.log(`
async function main(): Promise<void> {
debug.server('Starting MCP Time Server...');
const rateLimiter = new SlidingWindowRateLimiter();
const server = createServer();
registerHandlers(server, rateLimiter);
const transport = new StdioServerTransport();
await server.connect(transport);
debug.server('MCP Time Server connected to stdio transport');
console.error('MCP Time Server Node running on stdio');
}
`);
console.log('\nBenefits:');
console.log('- Each function has single responsibility');
console.log('- Easier to test individual parts');
console.log('- Better code organization');
console.log('- Debug can be added to each extracted function');
console.log('- main() becomes just orchestration');