All files / src/middleware mcp-json-body.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 3/3
100% Lines 9/9

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 2823x     23x 122x 122x 27x 27x           6x               6x   21x        
import express, { type RequestHandler } from 'express';
 
/** Parse admitted MCP messages once and report size errors in the JSON-RPC envelope. */
export function mcpJsonBodyParser(limit: number): RequestHandler {
  const parse = express.json({ limit });
  return (req, res, next) => {
    parse(req, res, (error: unknown) => {
      if (
        error &&
        typeof error === 'object' &&
        'type' in error &&
        error.type === 'entity.too.large'
      ) {
        res.status(413).json({
          jsonrpc: '2.0',
          id: null,
          error: {
            code: -32600,
            message: `JSON request exceeds ${limit} bytes; configure MCP_REQUEST_BODY_LIMIT to raise the limit.`,
          },
        });
        return;
      }
      next(error);
    });
  };
}