(request: NextRequest)
| 55 | } |
| 56 | |
| 57 | export async function readMcpJsonBodyWithLimit(request: NextRequest): Promise<unknown> { |
| 58 | const cached = parsedBodies.get(request) |
| 59 | if (cached !== undefined) return cached |
| 60 | |
| 61 | try { |
| 62 | assertContentLengthWithinLimit( |
| 63 | request.headers, |
| 64 | MAX_MCP_MANAGEMENT_BODY_BYTES, |
| 65 | 'MCP management request body' |
| 66 | ) |
| 67 | const buffer = await readStreamToBufferWithLimit(request.body, { |
| 68 | maxBytes: MAX_MCP_MANAGEMENT_BODY_BYTES, |
| 69 | label: 'MCP management request body', |
| 70 | signal: request.signal, |
| 71 | }) |
| 72 | const body = buffer.byteLength > 0 ? JSON.parse(buffer.toString('utf-8')) : {} |
| 73 | parsedBodies.set(request, body) |
| 74 | return body |
| 75 | } catch (error) { |
| 76 | if (request.signal.aborted) { |
| 77 | throw new McpBodyReadError('aborted', error) |
| 78 | } |
| 79 | if (isPayloadSizeLimitError(error)) { |
| 80 | throw new McpBodyReadError('payload_too_large', error) |
| 81 | } |
| 82 | if (error instanceof SyntaxError) { |
| 83 | throw new McpBodyReadError('invalid_json', error) |
| 84 | } |
| 85 | throw error |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export function mcpBodyReadErrorResponse( |
| 90 | error: unknown, |
no test coverage detected