UNPKG

@opichi/smartcode

Version:

Universal code intelligence MCP server - analyze any codebase with TypeScript excellence and multi-language support

127 lines 4.64 kB
#!/usr/bin/env node /** * Opichi Smartcode - Universal Code Intelligence MCP Server * Analyze any codebase with TypeScript excellence and multi-language support */ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js'; import * as fs from 'fs'; import * as path from 'path'; import { codeIntelligenceTools, codeIntelligenceHandlers } from './tools/code-intelligence.js'; // Debug logging function function debugLog(message, data) { if (!process.env.MCP_DEBUG) return; const timestamp = new Date().toISOString(); const logEntry = `[${timestamp}] ${message}${data ? ': ' + JSON.stringify(data, null, 2) : ''}\n`; try { fs.appendFileSync(path.join(process.cwd(), '.mcp-debug.log'), logEntry); } catch (error) { // Fallback to stderr if file logging fails console.error(`[${timestamp}] ${message}`, data || ''); } } /** * Create and configure the MCP server */ function createServer() { const server = new Server({ name: 'opichi-smartcode', version: '1.2.10', }, { capabilities: { tools: {}, }, }); // List available tools server.setRequestHandler(ListToolsRequestSchema, async (request) => { debugLog('ListTools request received', { request }); const response = { tools: codeIntelligenceTools }; debugLog('ListTools response prepared', { toolCount: codeIntelligenceTools.length }); return response; }); // Handle tool calls server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; debugLog('Tool call received', { name, args }); try { // Route to appropriate handler const handler = codeIntelligenceHandlers[name]; if (!handler) { debugLog('Unknown tool requested', { name }); return { content: [{ type: 'text', text: `Unknown tool: ${name}` }] }; } debugLog('Starting tool execution', { name }); const startTime = Date.now(); const result = await handler(args || {}); const duration = Date.now() - startTime; debugLog('Tool execution completed', { name, duration }); return result; } catch (error) { debugLog('Tool execution error', { name, error: error instanceof Error ? error.message : String(error) }); return { content: [{ type: 'text', text: `Error executing tool ${name}: ${error instanceof Error ? error.message : String(error)}` }] }; } }); return server; } /** * Start the MCP server */ async function main() { debugLog('MCP Server starting up'); debugLog('Process info', { pid: process.pid, cwd: process.cwd(), nodeVersion: process.version, platform: process.platform, arch: process.arch }); const server = createServer(); debugLog('Server instance created'); const transport = new StdioServerTransport(); debugLog('Transport created'); // Log startup to stderr (not stdout which is used for MCP communication) console.error('🚀 Code Intelligence MCP Server v1.2.10 starting...'); console.error('💡 Lightweight LSP-based analysis with instant project indexing ready!'); debugLog('Starting server connection'); try { await server.connect(transport); console.error('✅ Server connected and ready for requests'); debugLog('Server connected successfully'); } catch (error) { console.error('❌ Failed to start server:', error); debugLog('Server connection failed', { error: error instanceof Error ? error.message : String(error) }); process.exit(1); } } // Handle graceful shutdown process.on('SIGINT', () => { console.error('🛑 Received SIGINT, shutting down gracefully...'); process.exit(0); }); process.on('SIGTERM', () => { console.error('🛑 Received SIGTERM, shutting down gracefully...'); process.exit(0); }); // Start the server main().catch((error) => { console.error('💥 Fatal error:', error); process.exit(1); }); //# sourceMappingURL=index.js.map