# MCP Module - Quick Reference & Testing Guide ## ๐Ÿ“‹ Module at a Glance | Aspect | Details | |--------|---------| | **Location** | `apps/api/src/modules/mcp/` | | **Total Files** | 4 source files | | **Test Files** | 1 test file (174 lines) | | **Architecture** | Presentation layer only | | **Testing Framework** | Vitest | | **Test Coverage** | Controller well tested โœ… | ## ๐Ÿ“ Complete File Listing ``` MCP Module Files: โœ… index.ts (1 line) โœ… mcp.module.ts (22 lines) โœ… presentation/mcp-transport.controller.ts (102 lines) โœ… presentation/__tests__/mcp-transport.controller.spec.ts (174 lines) ``` ## ๐Ÿ—๏ธ Architecture ### Current (Simplified - Presentation Only) ``` Presentation Layer โœ… โ”œโ”€โ”€ McpTransportController โ”‚ โ”œโ”€โ”€ GET /mcp/servers โ”‚ โ”œโ”€โ”€ GET /mcp/:serverName/sse โ”‚ โ””โ”€โ”€ POST /mcp/:serverName/messages โ””โ”€โ”€ Tests (174 lines) McpIntegrationModule (22 lines) โ”œโ”€โ”€ Module config โ”œโ”€โ”€ Service setup โ””โ”€โ”€ Module lifecycle ``` ### Missing Layers (Not Implemented) ``` โŒ Domain Layer (no entities, events, business logic) โŒ Application Layer (no handlers, commands, queries) โŒ Infrastructure Layer (no repositories, adapters) ``` ## ๐Ÿงช Testing Overview ### Test File: mcp-transport.controller.spec.ts (174 lines) - **Total Tests**: 11 - **Test Suites**: 4 describe blocks - **Coverage**: 100% of controller endpoints ### Test Breakdown ``` 1. Security Decorators (4 tests) โ”œโ”€โ”€ JwtAuthGuard applied โ”œโ”€โ”€ listServers throttle (30 req/60s) โ”œโ”€โ”€ handleSse throttle (5 req/60s) โšก stricter โ””โ”€โ”€ handleMessage throttle (30 req/60s) 2. listServers (2 tests) โ”œโ”€โ”€ Returns server list โ””โ”€โ”€ Handles empty list 3. handleSse (3 tests) โ”œโ”€โ”€ Throws NOT_FOUND for missing server โ”œโ”€โ”€ Creates transport & connects โ””โ”€โ”€ Cleans up on connection close 4. handleMessage (2 tests) โ”œโ”€โ”€ Throws BAD_REQUEST for missing sessionId โ””โ”€โ”€ Throws NOT_FOUND for expired session ``` ## ๐ŸŽฏ Key Classes ### McpIntegrationModule ```typescript class McpIntegrationModule implements OnModuleInit { constructor( typesenseClient: TypesenseClientService, mcpRegistry: McpRegistryService, logger: LoggerService, ) {} async onModuleInit(): Promise { // Set typesense client, re-initialize servers, log results } } ``` ### McpTransportController ```typescript @Controller('mcp') @UseGuards(JwtAuthGuard) class McpTransportController { listServers(): { servers: string[] } async handleSse(serverName, user, req, res): Promise async handleMessage(serverName, user, req, res): Promise } ``` ## ๐Ÿ”ง Testing Patterns ### Module Mocking ```typescript vi.mock('@goodgo/mcp-servers', () => ({ SSEServerTransport: class MockSSE { sessionId = 'mock-session-id'; handlePostMessage = vi.fn().mockResolvedValue(undefined); }, })); ``` ### Service Mocking ```typescript mockRegistry = { getServerNames: vi.fn(), getServer: vi.fn(), }; ``` ### Decorator Verification ```typescript const guards = Reflect.getMetadata('__guards__', McpTransportController); const throttleLimit = Reflect.getMetadata('THROTTLER:LIMITdefault', method); ``` ## ๐Ÿ“š Testing Examples from Other Modules ### Auth Module Pattern (Simple) - Minimal dependencies - Direct handler instantiation - Focus on behavior verification ### Payments Module Pattern (Complex) - Multiple dependency mocks - Business rule testing - Event bus verification ### Domain Entity Pattern - Explicit vitest imports - State transition testing - Domain event verification - Error handling with Result types ### Infrastructure Pattern - External service simulation - Security testing (crypto) - Complex test data builders ## ๐Ÿš€ Running Tests ```bash # Run all MCP tests pnpm test -- src/modules/mcp # Run with watch pnpm test -- --watch src/modules/mcp # Run with coverage pnpm test -- --coverage src/modules/mcp # Run specific test pnpm test -- src/modules/mcp/presentation/__tests__/mcp-transport.controller.spec.ts ``` ## ๐Ÿ’ก Recommendations ### High Priority - โœ… Controller is well tested - maintain this - ๐Ÿ“ Add tests for McpIntegrationModule.onModuleInit() - ๐Ÿ“ Test module dependency injection ### Lower Priority - ๐Ÿ—๏ธ If domain logic added, follow payments patterns - ๐Ÿ—๏ธ If handlers added, use CQRS patterns - ๐Ÿ—๏ธ Add integration tests for SSE lifecycle