All files / src app.controller.ts

100% Statements 72/72
100% Branches 44/44
100% Functions 5/5
100% Lines 70/70

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    5x                     5x 5x     5x 33x     33x 33x       5x 3x       5x         22x 22x   22x 2x       20x       5x 5x 1x     4x 4x 1x       18x 18x   2x               2x 1x           5x         41x 41x   41x 1x 1x       40x       3x 3x 1x 1x     2x 2x 1x 1x       38x 38x   2x               2x 1x           5x         30x 30x   30x 2x       28x       3x 3x 1x     2x 2x 1x         26x 26x   22x 6x     16x 15x   1x     4x 4x           2x               2x 1x          
import { Request, Response } from 'express';
 
import {
  All,
  Controller,
  Get,
  Logger,
  Param,
  Post,
  Req,
  Res
} from '@nestjs/common';
 
import { ConfigService } from './config/config.service';
import { MCPServerService } from './mcp/mcp-server.service';
 
@Controller()
export class AppController {
  private readonly logger = new Logger(AppController.name);
 
  constructor(
    private configService: ConfigService,
    private mcpServerService: MCPServerService
  ) {}
 
  @Get()
  healthCheck() {
    return { status: 'ok' };
  }
 
  @Get(':clientName/sse')
  async handleSSE(
    @Param('clientName') clientName: string,
    @Req() req: Request,
    @Res() res: Response
  ) {
    const config = this.configService.getConfig();
    const clientConfig = config.mcpServers[clientName];
 
    if (!clientConfig) {
      return res.status(404).json({ error: 'Client not found' });
    }
 
    // Check authentication if configured
    if (
      clientConfig.options?.authTokens &&
      clientConfig.options.authTokens.length > 0
    ) {
      const authHeader = req.headers.authorization;
      if (!authHeader) {
        return res.status(401).json({ error: 'Unauthorized' });
      }
 
      const token = authHeader.replace(/^Bearer /, '').trim();
      if (!clientConfig.options.authTokens.includes(token)) {
        return res.status(401).json({ error: 'Unauthorized' });
      }
    }
 
    try {
      await this.mcpServerService.handleSSERequest(clientName, req, res);
    } catch (error) {
      this.logger.error(
        [
          'Error handling SSE for ',
          clientName,
          ': ',
          String(error)
        ].join('')
      );
      if (!res.headersSent) {
        res.status(500).json({ error: 'Internal server error' });
      }
    }
  }
 
  @Post(':clientName/message')
  async handlePostMessage(
    @Param('clientName') clientName: string,
    @Req() req: Request,
    @Res() res: Response
  ): Promise<void> {
    const config = this.configService.getConfig();
    const clientConfig = config.mcpServers[clientName];
 
    if (!clientConfig) {
      res.status(404).json({ error: 'Client not found' });
      return;
    }
 
    // Check authentication if configured
    if (
      clientConfig.options?.authTokens &&
      clientConfig.options.authTokens.length > 0
    ) {
      const authHeader = req.headers.authorization;
      if (!authHeader) {
        res.status(401).json({ error: 'Unauthorized' });
        return;
      }
 
      const token = authHeader.replace(/^Bearer /, '').trim();
      if (!clientConfig.options.authTokens.includes(token)) {
        res.status(401).json({ error: 'Unauthorized' });
        return;
      }
    }
 
    try {
      await this.mcpServerService.handlePostMessage(clientName, req, res);
    } catch (error) {
      this.logger.error(
        [
          'Error handling POST message for ',
          clientName,
          ': ',
          String(error)
        ].join('')
      );
      if (!res.headersSent) {
        res.status(500).json({ error: 'Internal server error' });
      }
    }
  }
 
  @All(':clientName')
  async handleStreamableHTTP(
    @Param('clientName') clientName: string,
    @Req() req: Request,
    @Res() res: Response
  ) {
    const config = this.configService.getConfig();
    const clientConfig = config.mcpServers[clientName];
 
    if (!clientConfig) {
      return res.status(404).json({ error: 'Client not found' });
    }
 
    // Check authentication if configured
    if (
      clientConfig.options?.authTokens &&
      clientConfig.options.authTokens.length > 0
    ) {
      const authHeader = req.headers.authorization;
      if (!authHeader) {
        return res.status(401).json({ error: 'Unauthorized' });
      }
 
      const token = authHeader.replace(/^Bearer /, '').trim();
      if (!clientConfig.options.authTokens.includes(token)) {
        return res.status(401).json({ error: 'Unauthorized' });
      }
    }
 
    // Only handle streamable-http if server type is streamable-http
    const serverType = config.mcpProxy.type || 'sse';
    if (serverType !== 'streamable-http') {
      // For SSE, use the SSE endpoint
      if (req.method === 'GET') {
        return this.handleSSE(clientName, req, res);
      }
      // For POST to SSE, use handlePostMessage
      if (req.method === 'POST') {
        return this.handlePostMessage(clientName, req, res);
      }
      return res.status(404).json({ error: 'Endpoint not found' });
    }
 
    try {
      await this.mcpServerService.handleStreamableHTTPRequest(
        clientName,
        req,
        res
      );
    } catch (error) {
      this.logger.error(
        [
          'Error handling Streamable HTTP for ',
          clientName,
          ': ',
          String(error)
        ].join('')
      );
      if (!res.headersSent) {
        res.status(500).json({ error: 'Internal server error' });
      }
    }
  }
}