UNPKG

@flexabrain/mcp-server

Version:

Advanced electrical schematic analysis MCP server with rail engineering expertise

186 lines 9.44 kB
#!/usr/bin/env node /** * FlexaBrain MCP Server - Main Entry Point * * Intelligent electrical schematic analysis for VS Code agent mode * Leverages rail electrical engineering expertise for superior component recognition, * safety analysis, and maintenance recommendations. */ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js'; import { MCP_TOOLS } from './types/mcp.js'; // Tool implementations import { analyzeElectricalSchematic as analyzeSchematicImpl } from './tools/analyze-schematic.js'; import { extractComponentInventory as extractInventoryImpl } from './tools/extract-component-inventory.js'; import { validateElectricalStandards as validateStandardsImpl } from './tools/validate-electrical-standards.js'; import { analyzePowerFlow as analyzePowerFlowImpl } from './tools/analyze-power-flow.js'; import { generateMaintenanceSchedule as generateMaintenanceImpl } from './tools/generate-maintenance-schedule.js'; import { checkSafetyCompliance as checkSafetyImpl } from './tools/check-safety-compliance.js'; import { transformSchematicImage as transformImageImpl } from './tools/transform-schematic-image.js'; import { batchAnalyzeSchematics as batchAnalyzeImpl } from './tools/batch-analyze-schematics.js'; // Wrapper functions to format responses for MCP const analyzeElectricalSchematic = async (args) => ({ content: [{ type: 'text', text: await analyzeSchematicImpl(args) }] }); const extractComponentInventory = async (args) => ({ content: [{ type: 'text', text: await extractInventoryImpl(args) }] }); const validateElectricalStandards = async (args) => ({ content: [{ type: 'text', text: await validateStandardsImpl(args) }] }); const analyzePowerFlow = async (args) => ({ content: [{ type: 'text', text: await analyzePowerFlowImpl(args) }] }); const generateMaintenanceSchedule = async (args) => ({ content: [{ type: 'text', text: await generateMaintenanceImpl(args) }] }); const checkSafetyCompliance = async (args) => ({ content: [{ type: 'text', text: await checkSafetyImpl(args) }] }); const transformSchematicImage = async (args) => ({ content: [{ type: 'text', text: await transformImageImpl(args) }] }); const batchAnalyzeSchematics = async (args) => ({ content: [{ type: 'text', text: await batchAnalyzeImpl(args) }] }); class FlexaBrainMCPServer { server; version = '1.0.0'; constructor() { this.server = new Server({ name: 'flexabrain', version: this.version, }); this.setupHandlers(); this.logServerInfo(); } logServerInfo() { console.error(`FlexaBrain MCP Server v${this.version}`); console.error('Intelligent Electrical Schematic Analysis'); console.error('Powered by Rail Electrical Engineering Expertise'); console.error('=========================================='); } setupHandlers() { // Handle tool listing requests this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: MCP_TOOLS.ANALYZE_ELECTRICAL_SCHEMATIC.name, description: MCP_TOOLS.ANALYZE_ELECTRICAL_SCHEMATIC.description, inputSchema: MCP_TOOLS.ANALYZE_ELECTRICAL_SCHEMATIC.inputSchema, }, { name: MCP_TOOLS.EXTRACT_COMPONENT_INVENTORY.name, description: MCP_TOOLS.EXTRACT_COMPONENT_INVENTORY.description, inputSchema: MCP_TOOLS.EXTRACT_COMPONENT_INVENTORY.inputSchema, }, { name: MCP_TOOLS.VALIDATE_ELECTRICAL_STANDARDS.name, description: MCP_TOOLS.VALIDATE_ELECTRICAL_STANDARDS.description, inputSchema: MCP_TOOLS.VALIDATE_ELECTRICAL_STANDARDS.inputSchema, }, { name: MCP_TOOLS.ANALYZE_POWER_FLOW.name, description: MCP_TOOLS.ANALYZE_POWER_FLOW.description, inputSchema: MCP_TOOLS.ANALYZE_POWER_FLOW.inputSchema, }, { name: MCP_TOOLS.GENERATE_MAINTENANCE_SCHEDULE.name, description: MCP_TOOLS.GENERATE_MAINTENANCE_SCHEDULE.description, inputSchema: MCP_TOOLS.GENERATE_MAINTENANCE_SCHEDULE.inputSchema, }, { name: MCP_TOOLS.CHECK_SAFETY_COMPLIANCE.name, description: MCP_TOOLS.CHECK_SAFETY_COMPLIANCE.description, inputSchema: MCP_TOOLS.CHECK_SAFETY_COMPLIANCE.inputSchema, }, { name: MCP_TOOLS.TRANSFORM_SCHEMATIC_IMAGE.name, description: MCP_TOOLS.TRANSFORM_SCHEMATIC_IMAGE.description, inputSchema: MCP_TOOLS.TRANSFORM_SCHEMATIC_IMAGE.inputSchema, }, { name: MCP_TOOLS.BATCH_ANALYZE_SCHEMATICS.name, description: MCP_TOOLS.BATCH_ANALYZE_SCHEMATICS.description, inputSchema: MCP_TOOLS.BATCH_ANALYZE_SCHEMATICS.inputSchema, }, ], }; }); // Handle tool execution requests this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { console.error(`FlexaBrain: Executing tool "${name}"`); switch (name) { case 'analyze_electrical_schematic': return await analyzeElectricalSchematic(args); case 'extract_component_inventory': return await extractComponentInventory(args); case 'validate_electrical_standards': return await validateElectricalStandards(args); case 'analyze_power_flow': return await analyzePowerFlow(args); case 'generate_maintenance_schedule': return await generateMaintenanceSchedule(args); case 'check_safety_compliance': return await checkSafetyCompliance(args); case 'transform_schematic_image': return await transformSchematicImage(args); case 'batch_analyze_schematics': return await batchAnalyzeSchematics(args); default: throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`); } } catch (error) { console.error(`FlexaBrain: Tool execution failed for "${name}":`, error); // Handle different types of errors appropriately if (error instanceof McpError) { throw error; } // Convert other errors to MCP errors if (error instanceof Error) { throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${error.message}`, error.stack); } throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${String(error)}`); } }); // Error handling this.server.onerror = (error) => { console.error('FlexaBrain MCP Server error:', error); }; } async start() { try { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('FlexaBrain MCP Server: Ready for electrical schematic analysis'); } catch (error) { console.error('FlexaBrain MCP Server: Failed to start:', error); process.exit(1); } } async stop() { try { await this.server.close(); console.error('FlexaBrain MCP Server: Stopped'); } catch (error) { console.error('FlexaBrain MCP Server: Error during shutdown:', error); } } } // Handle process signals for graceful shutdown const server = new FlexaBrainMCPServer(); process.on('SIGINT', async () => { console.error('FlexaBrain MCP Server: Received SIGINT, shutting down gracefully...'); await server.stop(); process.exit(0); }); process.on('SIGTERM', async () => { console.error('FlexaBrain MCP Server: Received SIGTERM, shutting down gracefully...'); await server.stop(); process.exit(0); }); process.on('uncaughtException', (error) => { console.error('FlexaBrain MCP Server: Uncaught exception:', error); process.exit(1); }); process.on('unhandledRejection', (reason, promise) => { console.error('FlexaBrain MCP Server: Unhandled rejection at:', promise, 'reason:', reason); process.exit(1); }); // Start the server server.start().catch((error) => { console.error('FlexaBrain MCP Server: Startup failed:', error); process.exit(1); }); //# sourceMappingURL=index.js.map