{"record":{"id":"948e2013f48d0c2d","repo":"yikart/AiToEarn","slug":"32603","errorCode":"-32603","errorMessage":"Internal server error","messagePattern":"Internal server error","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"project/aitoearn-backend/libs/nest-mcp/src/services/mcp-streamable-http.service.ts","lineNumber":118,"sourceCode":"\n    this.logger.debug(\n      `[${sessionId || 'No-Session'}] Received MCP request: ${JSON.stringify(body)}`,\n    )\n\n    try {\n      if (this.isStatelessMode) {\n        return this.handleStatelessRequest(adaptedReq, adaptedRes, body)\n      }\n      else {\n        return this.handleStatefulRequest(adaptedReq, adaptedRes, body)\n      }\n    }\n    catch (error) {\n      this.logger.error(\n        `[${sessionId || 'No-Session'}] Error handling MCP request: ${error}`,\n      )\n      if (!adaptedRes.headersSent) {\n        adaptedRes.status(500).json({\n          jsonrpc: '2.0',\n          error: {\n            code: -32603,\n            message: 'Internal server error',\n          },\n          id: null,\n        })\n      }\n    }\n  }\n\n  /**\n   * Handle requests in stateless mode\n   */\n  async handleStatelessRequest(\n    req: any,\n    res: HttpResponse,\n    body: unknown,","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/libs/nest-mcp/src/services/mcp-streamable-http.service.ts#L100-L136","documentation":"The server's handlePostRequest caught an unexpected exception while dispatching an MCP JSON-RPC POST and returned a generic JSON-RPC -32603 internal error with HTTP 500. This is the library's catch-all: the actual cause is only visible in the server log line `[<sessionId>] Error handling MCP request: <error>`. It means request parsing, transport creation, or protocol handling threw before a JSON-RPC response could be produced.","triggerScenarios":"Any uncaught exception inside handlePostRequest: malformed body that survives earlier checks, HttpAdapterFactory.getAdapter failing for an unsupported req/res shape, transport.handleRequest throwing, or a tool/resource handler inside the MCP server throwing synchronously during dispatch.","commonSituations":"Custom MCP tools crashing on bad input, incompatible Express/Fastify req/res objects passed in from the host framework, JSON body parser middleware missing so `body` is a raw stream, or a bug introduced after a library upgrade.","solutions":["Read the server log line starting with `[<sessionId>] Error handling MCP request:` to get the real underlying error.","Confirm the request has a parsed JSON body (express.json()/Nest body-parser) before it reaches the MCP controller.","Verify you are passing framework-native req/res objects that HttpAdapterFactory supports (Express or Fastify), not wrapped or proxied objects.","Fix or add error handling in the specific MCP tool/handler that threw.","Pin/upgrade the nest-mcp library version if the crash originates inside library code."],"exampleFix":"// before\napp.use('/mcp', mcpController.handlePostRequest)\n// after\napp.use(express.json())\napp.use('/mcp', mcpController.handlePostRequest)","handlingStrategy":"try-catch","validationCode":"if (typeof req.body !== 'object' || req.body === null) throw new Error('MCP POST requires a parsed JSON body')","typeGuard":"function isJsonRpcRequest(v: unknown): v is { jsonrpc: '2.0'; id: string | number | null; method: string } {\n  return typeof v === 'object' && v !== null && (v as any).jsonrpc === '2.0' && typeof (v as any).method === 'string'\n}","tryCatchPattern":"try {\n  await mcpService.handlePostRequest(req, res)\n} catch (err) {\n  logger.error('MCP POST failed', err)\n  if (!res.headersSent) res.status(500).json({ jsonrpc: '2.0', error: { code: -32603, message: 'Internal server error' }, id: null })\n}","preventionTips":["Always enable JSON body parsing middleware before the MCP route.","Log the full underlying error server-side; -32603 is intentionally generic to clients.","Add integration tests that exercise every registered tool to catch handler crashes early.","Pin library versions and read changelogs before upgrading the MCP transport layer."],"tags":["json-rpc","internal-error","http-500","mcp"],"backgroundTag":"json-rpc-internal-error","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}