{"record":{"id":"fb7ab637e9034199","repo":"yikart/AiToEarn","slug":"32600","errorCode":"-32600","errorMessage":"Invalid Request: Only one initialization request is allowed","messagePattern":"Invalid Request: Only one initialization request is allowed","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"project/aitoearn-backend/libs/nest-mcp/src/services/mcp-streamable-http.service.ts","lineNumber":202,"sourceCode":"  }\n\n  /**\n   * Handle requests in stateful mode\n   */\n  async handleStatefulRequest(\n    req: HttpRequest,\n    res: HttpResponse,\n    body: unknown,\n  ): Promise<void> {\n    const sessionId = req.headers['mcp-session-id'] as string | undefined\n\n    this.logger.debug(`[${sessionId || 'New'}] Handling stateful MCP request`)\n\n    // Case 1: New initialization request\n    if (!sessionId && this.isInitializeRequest(body)) {\n      // Validate it's not a batch with multiple requests\n      if (Array.isArray(body) && body.length > 1) {\n        res.status(400).json({\n          jsonrpc: '2.0',\n          error: {\n            code: -32600,\n            message:\n              'Invalid Request: Only one initialization request is allowed',\n          },\n          id: null,\n        })\n        return\n      }\n\n      // Build capabilities\n      const capabilities = buildMcpCapabilities(\n        this.mcpModuleId,\n        this.toolRegistry,\n        this.options,\n      )\n","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/libs/nest-mcp/src/services/mcp-streamable-http.service.ts#L184-L220","documentation":"During stateful session setup, the server received an `initialize` request but the POST body was a JSON-RPC batch containing more than one request. The library rejects this with -32600 and HTTP 400 because batching multiple messages including initialize could create ambiguous session state; only a single initialize request is allowed per batch.","triggerScenarios":"POSTing a JSON array like `[ {initialize...}, {tools/list...} ]` (or any array with length > 1 where an element is an initialize request) to the MCP endpoint without a session ID.","commonSituations":"Clients implementing JSON-RPC batch support too aggressively, proxy/gateway code that coalesces requests into an array, or hand-rolled test scripts sending arrays of messages at once.","solutions":["Send the initialize request alone as a single JSON-RPC object, not inside an array.","Initialize first, capture the Mcp-Session-Id response header, then batch or pipeline subsequent calls on that session if needed.","If your client must batch, batch only post-initialization requests on an established session."],"exampleFix":"// before\nawait fetch(url, { method: 'POST', body: JSON.stringify([initReq, listToolsReq]) })\n// after\nconst res = await fetch(url, { method: 'POST', body: JSON.stringify(initReq) })\nconst sessionId = res.headers.get('mcp-session-id')\nawait fetch(url, { method: 'POST', headers: { 'mcp-session-id': sessionId }, body: JSON.stringify(listToolsReq) })","handlingStrategy":"validation","validationCode":"const isBatch = Array.isArray(body)\nif (isBatch && body.some((r: any) => r?.method === 'initialize') && body.length > 1) {\n  throw new Error('initialize must be sent alone, not in a batch')\n}","typeGuard":"function isSingleInitialize(body: unknown): body is { jsonrpc: '2.0'; method: 'initialize'; id: string | number } {\n  return !Array.isArray(body) && typeof body === 'object' && body !== null && (body as any).method === 'initialize'\n}","tryCatchPattern":"if (res.status === 400 && (await res.json())?.error?.code === -32600) {\n  // resend initialize as a single, non-batched request\n}","preventionTips":["Always send initialize as a single JSON-RPC object.","Only batch requests after initialization, on an established session.","Review proxy/gateway code that merges or coalesces client requests into arrays."],"tags":["json-rpc","initialization","batching","http-400"],"backgroundTag":"invalid-initialization-request","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}