{"record":{"id":"503d4198f2ae670f","repo":"different-ai/openwork","slug":"request-id-mismatch","errorCode":"request_id_mismatch","errorMessage":"request_id_mismatch","messagePattern":"request_id_mismatch","errorType":"error_code","errorClass":"SafeProbeFailure","httpStatus":null,"severity":"error","filePath":"apps/server/src/agent-context-cloud-probe.ts","lineNumber":532,"sourceCode":"    if (line.startsWith(\":\")) continue;\n    const colon = line.indexOf(\":\");\n    const field = colon < 0 ? line : line.slice(0, colon);\n    const rawValue = colon < 0 ? \"\" : line.slice(colon + 1);\n    const value = rawValue.startsWith(\" \") ? rawValue.slice(1) : rawValue;\n    if (field === \"event\") event = value;\n    if (field === \"data\") data.push(value);\n  }\n  dispatch();\n  if (messages.length !== 1) throw new SafeProbeFailure(\"invalid_json\");\n  return messages[0];\n}\n\nfunction parseJsonRpcResult(payload: unknown, requestId: string): Record<string, unknown> {\n  if (!isRecord(payload) || payload.jsonrpc !== \"2.0\") {\n    throw new SafeProbeFailure(\"invalid_jsonrpc_envelope\");\n  }\n  if (Object.hasOwn(payload, \"error\")) throw new SafeProbeFailure(\"jsonrpc_error\");\n  if (payload.id !== requestId) throw new SafeProbeFailure(\"request_id_mismatch\");\n  if (!isRecord(payload.result)) throw new SafeProbeFailure(\"invalid_jsonrpc_envelope\");\n  return payload.result;\n}\n\nfunction requireSupportedProtocolVersion(initializeResult: Record<string, unknown>): void {\n  const version = initializeResult.protocolVersion;\n  if (typeof version !== \"string\" || version.length === 0 || version.length > MAX_PROTOCOL_HEADER_LENGTH) {\n    throw new SafeProbeFailure(\"invalid_jsonrpc_envelope\");\n  }\n  if (version !== MCP_PROTOCOL_VERSION) throw new SafeProbeFailure(\"unsupported_protocol_version\");\n}\n\nfunction validateCatalog(rpcResult: Record<string, unknown>): { toolIds: string[]; totalToolCount: number } {\n  if (rpcResult.nextCursor !== undefined && rpcResult.nextCursor !== null) {\n    throw new SafeProbeFailure(\"pagination_unsupported\");\n  }\n  if (!Array.isArray(rpcResult.tools) || rpcResult.tools.length > MAX_TOOL_COUNT) {\n    throw new SafeProbeFailure(\"invalid_catalog\");","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/agent-context-cloud-probe.ts#L514-L550","documentation":"parseJsonRpcResult throws SafeProbeFailure(\"request_id_mismatch\") when the response's 'id' does not strictly equal the request id the probe generated. JSON-RPC correlation requires id echo; any mismatch indicates a response for a different request or a tampered/buggy server.","triggerScenarios":"Server responds with id: null, omits id, returns a string id where a number was sent (or vice versa), or echoes a stale id.","commonSituations":"Server implementation reuses or regenerates ids; a multiplexing proxy returns the wrong pending response; server coerces numeric ids to strings; concurrent requests interleaved incorrectly.","solutions":["Verify the server echoes the exact 'id' value from the request (same type and value)","Test the endpoint with a single sequential request to rule out interleaving bugs","Update the server's JSON-RPC implementation to a spec-compliant version","Check for proxies that cache or reroute responses between concurrent clients"],"exampleFix":"// before (server)\nres.write(JSON.stringify({jsonrpc:\"2.0\",id:\"req-1\",result})) // request id was 1 (number)\n// after\nres.write(JSON.stringify({jsonrpc:\"2.0\",id:requestId,result}))","handlingStrategy":"validation","validationCode":"const echoed = (body as Record<string, unknown> | null)?.id;\nif (echoed !== requestId) throw new Error(`response id ${JSON.stringify(echoed)} does not match request id ${JSON.stringify(requestId)}`);","typeGuard":"function hasMatchingId(payload: unknown, requestId: unknown): payload is { id: unknown } & Record<string, unknown> {\n  return typeof payload === \"object\" && payload !== null && (payload as Record<string, unknown>).id === requestId;\n}","tryCatchPattern":"try {\n  const result = parseJsonRpcResult(payload, requestId);\n} catch (e) {\n  if (e instanceof SafeProbeFailure && e.code === \"request_id_mismatch\") {\n    // log both ids and body; treat server as non-compliant or serialized behind a misbehaving proxy\n  } else throw e;\n}","preventionTips":["Use unique, monotonically generated numeric request ids per call","Avoid proxies that multiplex/cache JSON-RPC responses","Add a server contract test asserting exact id echo including type"],"tags":["json-rpc","mcp","protocol"],"backgroundTag":"json-rpc-request-id-mismatch","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}