import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema, InitializeRequestSchema, ErrorCode, McpError, } from '@modelcontextprotocol/sdk/types.js'; // Import tools import { specProjectTool } from './tools/spec-project.js'; import { specArchiveTool } from './tools/spec-archive.js'; import { specTasksTool } from './tools/spec-tasks.js'; import { specRequirementsTool } from './tools/spec-requirements.js'; import { specDesignTool } from './tools/spec-design.js'; import { specValidateTool } from './tools/spec-validate.js'; // Import utilities import { getEnvironmentConfig } from './config.js'; // Available tools const TOOLS = [ specProjectTool, specArchiveTool, specTasksTool, specRequirementsTool, specDesignTool, specValidateTool ]; export class SpecMCPServer { private server: Server; private config: any; constructor() { // Get environment-specific configuration const envConfig = getEnvironmentConfig(); this.config = envConfig.config; this.server = new Server( { name: this.config.name, version: this.config.version, }, { capabilities: { tools: {}, }, } ); this.setupHandlers(); this.setupErrorHandling(); } private setupHandlers() { // Initialize handler - CRITICAL for MCP protocol this.server.setRequestHandler(InitializeRequestSchema, async (request) => { return { protocolVersion: '2024-11-05', // Use stable protocol version capabilities: { tools: {}, }, serverInfo: { name: this.config.name, version: this.config.version, }, }; }); // List tools handler this.server.setRequestHandler(ListToolsRequestSchema, async () => { try { const toolsList = TOOLS.map(tool => ({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema, })); return { tools: toolsList }; } catch (error: any) { return { tools: [] }; } }); // Call tool handler with improved error handling this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { const tool = TOOLS.find(t => t.name === name); if (!tool) { return { content: [{ type: "text", text: `❌ Unknown tool: ${name}` }] }; } const result = await tool.handler(args); return result; } catch (error: any) { return { content: [{ type: "text", text: `❌ Error in tool ${name}: ${error.message}` }] }; } }); } private setupErrorHandling() { // Handle uncaught errors process.on('uncaughtException', (error) => { console.error('Uncaught exception:', error); }); process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled rejection at:', promise, 'reason:', reason); }); } async run() { try { // Create stdio transport with proper configuration const transport = new StdioServerTransport(); // Connect to transport await this.server.connect(transport); // Keep process alive without heartbeat interference process.stdin.resume(); // Server is ready - no logging needed for production } catch (error: any) { console.error('Failed to start server:', error); process.exit(1); } } }