Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | /** * V3 MCP HTTP Transport * * HTTP/REST transport for MCP communication: * - Express-based with optimized middleware * - WebSocket support for real-time notifications * - Connection pooling for client connections * - Security headers with helmet * * Performance Targets: * - Request handling: <20ms overhead * - WebSocket message: <5ms */ import { EventEmitter } from 'events'; import express, { Express, Request, Response, NextFunction } from 'express'; import { createServer, Server } from 'http'; import { WebSocketServer, WebSocket } from 'ws'; import cors from 'cors'; import helmet from 'helmet'; import { ITransport, TransportType, MCPRequest, MCPResponse, MCPNotification, RequestHandler, NotificationHandler, TransportHealthStatus, ILogger, AuthConfig, } from '../types.js'; /** * HTTP Transport Configuration */ export interface HttpTransportConfig { host: string; port: number; tlsEnabled?: boolean; tlsCert?: string; tlsKey?: string; corsEnabled?: boolean; corsOrigins?: string[]; auth?: AuthConfig; maxRequestSize?: string; requestTimeout?: number; } /** * HTTP Transport Implementation */ export class HttpTransport extends EventEmitter implements ITransport { public readonly type: TransportType = 'http'; private requestHandler?: RequestHandler; private notificationHandler?: NotificationHandler; private app: Express; private server?: Server; private wss?: WebSocketServer; private running = false; private activeConnections = new Set<WebSocket>(); // Statistics private messagesReceived = 0; private messagesSent = 0; private errors = 0; private httpRequests = 0; private wsMessages = 0; constructor( private readonly logger: ILogger, private readonly config: HttpTransportConfig ) { super(); this.app = express(); this.setupMiddleware(); this.setupRoutes(); } /** * Start the transport */ async start(): Promise<void> { if (this.running) { throw new Error('HTTP transport already running'); } this.logger.info('Starting HTTP transport', { host: this.config.host, port: this.config.port, }); // Create HTTP server this.server = createServer(this.app); // Create WebSocket server this.wss = new WebSocketServer({ server: this.server, path: '/ws', }); this.setupWebSocketHandlers(); // Start server await new Promise<void>((resolve, reject) => { this.server!.listen(this.config.port, this.config.host, () => { resolve(); }); this.server!.on('error', reject); }); this.running = true; this.logger.info('HTTP transport started', { url: `http://${this.config.host}:${this.config.port}`, }); } /** * Stop the transport */ async stop(): Promise<void> { if (!this.running) { return; } this.logger.info('Stopping HTTP transport'); this.running = false; // Close all WebSocket connections for (const ws of this.activeConnections) { try { ws.close(1000, 'Server shutting down'); } catch { // Ignore errors } } this.activeConnections.clear(); // Close WebSocket server if (this.wss) { this.wss.close(); this.wss = undefined; } // Close HTTP server if (this.server) { await new Promise<void>((resolve) => { this.server!.close(() => resolve()); }); this.server = undefined; } this.logger.info('HTTP transport stopped'); } /** * Register request handler */ onRequest(handler: RequestHandler): void { this.requestHandler = handler; } /** * Register notification handler */ onNotification(handler: NotificationHandler): void { this.notificationHandler = handler; } /** * Get health status */ async getHealthStatus(): Promise<TransportHealthStatus> { return { healthy: this.running, metrics: { messagesReceived: this.messagesReceived, messagesSent: this.messagesSent, errors: this.errors, httpRequests: this.httpRequests, wsMessages: this.wsMessages, activeConnections: this.activeConnections.size, }, }; } /** * Send notification to all connected WebSocket clients */ async sendNotification(notification: MCPNotification): Promise<void> { const message = JSON.stringify(notification); for (const ws of this.activeConnections) { try { if (ws.readyState === WebSocket.OPEN) { ws.send(message); this.messagesSent++; } } catch (error) { this.logger.error('Failed to send notification', { error }); this.errors++; } } } /** * Setup Express middleware */ private setupMiddleware(): void { // Security headers this.app.use(helmet({ contentSecurityPolicy: false, // Allow for flexibility })); // CORS - Secure defaults (no wildcard in production) if (this.config.corsEnabled !== false) { const allowedOrigins = this.config.corsOrigins; // SECURITY: Reject wildcard CORS in production unless explicitly configured if (!allowedOrigins || allowedOrigins.length === 0) { this.logger.warn('CORS: No origins configured, restricting to same-origin only'); } this.app.use(cors({ origin: (origin, callback) => { // Allow requests with no origin (same-origin, curl, etc.) if (!origin) { callback(null, true); return; } // Check against allowed origins if (allowedOrigins && allowedOrigins.length > 0) { if (allowedOrigins.includes(origin) || allowedOrigins.includes('*')) { callback(null, true); } else { callback(new Error(`CORS: Origin '${origin}' not allowed`)); } } else { // No origins configured - reject cross-origin requests callback(new Error('CORS: Cross-origin requests not allowed')); } }, credentials: true, maxAge: 86400, methods: ['GET', 'POST', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization', 'X-Request-ID'], })); } // Body parsing this.app.use(express.json({ limit: this.config.maxRequestSize || '10mb', })); // Request timeout if (this.config.requestTimeout) { this.app.use((req, res, next) => { res.setTimeout(this.config.requestTimeout!, () => { res.status(408).json({ jsonrpc: '2.0', id: null, error: { code: -32000, message: 'Request timeout' }, }); }); next(); }); } // Request logging this.app.use((req, res, next) => { const startTime = performance.now(); res.on('finish', () => { const duration = performance.now() - startTime; this.logger.debug('HTTP request', { method: req.method, path: req.path, status: res.statusCode, duration: `${duration.toFixed(2)}ms`, }); }); next(); }); } /** * Setup Express routes */ private setupRoutes(): void { // Health check this.app.get('/health', (req, res) => { res.json({ status: 'ok', timestamp: new Date().toISOString(), connections: this.activeConnections.size, }); }); // MCP JSON-RPC endpoint this.app.post('/rpc', async (req, res) => { await this.handleHttpRequest(req, res); }); // Alternative MCP endpoint this.app.post('/mcp', async (req, res) => { await this.handleHttpRequest(req, res); }); // Server info this.app.get('/info', (req, res) => { res.json({ name: 'Claude-Flow MCP Server V3', version: '3.0.0', transport: 'http', capabilities: { jsonrpc: true, websocket: true, }, }); }); // 404 handler this.app.use((req, res) => { res.status(404).json({ error: 'Not found', path: req.path, }); }); // Error handler this.app.use((err: Error, req: Request, res: Response, next: NextFunction) => { this.logger.error('Express error', { error: err }); this.errors++; res.status(500).json({ jsonrpc: '2.0', id: null, error: { code: -32603, message: 'Internal error' }, }); }); } /** * Setup WebSocket handlers */ private setupWebSocketHandlers(): void { if (!this.wss) return; this.wss.on('connection', (ws, req) => { this.activeConnections.add(ws); this.logger.info('WebSocket client connected', { total: this.activeConnections.size, }); ws.on('message', async (data) => { await this.handleWebSocketMessage(ws, data.toString()); }); ws.on('close', () => { this.activeConnections.delete(ws); this.logger.info('WebSocket client disconnected', { total: this.activeConnections.size, }); }); ws.on('error', (error) => { this.logger.error('WebSocket error', { error }); this.errors++; this.activeConnections.delete(ws); }); }); } /** * Handle HTTP request */ private async handleHttpRequest(req: Request, res: Response): Promise<void> { this.httpRequests++; this.messagesReceived++; // Validate authentication (ALWAYS check, not just when explicitly enabled) // SECURITY: Auth should be opt-out, not opt-in const requiresAuth = this.config.auth?.enabled !== false; // Default to requiring auth if (requiresAuth && this.config.auth) { const authResult = this.validateAuth(req); if (!authResult.valid) { this.logger.warn('Authentication failed', { ip: req.ip, path: req.path, error: authResult.error, }); res.status(401).json({ jsonrpc: '2.0', id: null, error: { code: -32001, message: 'Unauthorized' }, }); return; } } else if (requiresAuth && !this.config.auth) { // No auth configured but auth is required - warn and continue (development mode) this.logger.warn('No authentication configured - running in development mode'); } const message = req.body; // Validate JSON-RPC format if (message.jsonrpc !== '2.0') { res.status(400).json({ jsonrpc: '2.0', id: message.id || null, error: { code: -32600, message: 'Invalid JSON-RPC version' }, }); return; } if (!message.method) { res.status(400).json({ jsonrpc: '2.0', id: message.id || null, error: { code: -32600, message: 'Missing method' }, }); return; } // Handle notification vs request if (message.id === undefined) { // Notification if (this.notificationHandler) { await this.notificationHandler(message as MCPNotification); } res.status(204).end(); } else { // Request if (!this.requestHandler) { res.status(500).json({ jsonrpc: '2.0', id: message.id, error: { code: -32603, message: 'No request handler' }, }); return; } try { const response = await this.requestHandler(message as MCPRequest); res.json(response); this.messagesSent++; } catch (error) { this.errors++; res.status(500).json({ jsonrpc: '2.0', id: message.id, error: { code: -32603, message: error instanceof Error ? error.message : 'Internal error', }, }); } } } /** * Handle WebSocket message */ private async handleWebSocketMessage(ws: WebSocket, data: string): Promise<void> { this.wsMessages++; this.messagesReceived++; try { const message = JSON.parse(data); if (message.jsonrpc !== '2.0') { ws.send(JSON.stringify({ jsonrpc: '2.0', id: message.id || null, error: { code: -32600, message: 'Invalid JSON-RPC version' }, })); return; } if (message.id === undefined) { // Notification if (this.notificationHandler) { await this.notificationHandler(message as MCPNotification); } } else { // Request if (!this.requestHandler) { ws.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32603, message: 'No request handler' }, })); return; } const response = await this.requestHandler(message as MCPRequest); ws.send(JSON.stringify(response)); this.messagesSent++; } } catch (error) { this.errors++; this.logger.error('WebSocket message error', { error }); try { const parsed = JSON.parse(data); ws.send(JSON.stringify({ jsonrpc: '2.0', id: parsed.id || null, error: { code: -32700, message: 'Parse error' }, })); } catch { ws.send(JSON.stringify({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' }, })); } } } /** * Validate authentication */ private validateAuth(req: Request): { valid: boolean; error?: string } { const auth = req.headers.authorization; if (!auth) { return { valid: false, error: 'Authorization header required' }; } const tokenMatch = auth.match(/^Bearer\s+(.+)$/i); if (!tokenMatch) { return { valid: false, error: 'Invalid authorization format' }; } const token = tokenMatch[1]; if (this.config.auth?.tokens?.length) { if (!this.config.auth.tokens.includes(token)) { return { valid: false, error: 'Invalid token' }; } } return { valid: true }; } } /** * Create HTTP transport */ export function createHttpTransport( logger: ILogger, config: HttpTransportConfig ): HttpTransport { return new HttpTransport(logger, config); } |