{"record":{"id":"5bb7318b40dc5bfe","repo":"rohitg00/ai-engineering-from-scratch","slug":"response-id-does-not-exactly-match-request-id","errorCode":null,"errorMessage":"response id does not exactly match request id","messagePattern":"response id does not exactly match request id","errorType":"validation","errorClass":"ProtocolViolation","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py","lineNumber":393,"sourceCode":"    name: str\n    passed: bool\n    detail: str\n    evidence_digest: str\n\n\ndef validate_response(\n    request: dict[str, Any],\n    status: int,\n    response: dict[str, Any],\n    era: str,\n    capabilities: set[str],\n) -> None:\n    if not isinstance(response, dict) or response.get(\"jsonrpc\") != \"2.0\":\n        raise ProtocolViolation(\"invalid JSON-RPC response envelope\")\n    request_id = request.get(\"id\")\n    response_id = response.get(\"id\")\n    if type(response_id) is not type(request_id) or response_id != request_id:\n        raise ProtocolViolation(\"response id does not exactly match request id\")\n    has_result = \"result\" in response\n    has_error = \"error\" in response\n    if has_result == has_error:\n        raise ProtocolViolation(\"response must contain exactly one of result or error\")\n    if has_result:\n        if status != 200:\n            raise ProtocolViolation(\"successful JSON-RPC result requires HTTP 200\")\n        validate_result(response[\"result\"], era, capabilities)\n        validate_method_result(request[\"method\"], response[\"result\"])\n        return\n\n    error = response[\"error\"]\n    if not isinstance(error, dict):\n        raise ProtocolViolation(\"JSON-RPC error must be an object\")\n    code = error.get(\"code\")\n    if isinstance(code, bool) or not isinstance(code, int) or not isinstance(error.get(\"message\"), str):\n        raise ProtocolViolation(\"JSON-RPC error requires integer code and string message\")\n    expected_status = {-32020: 400, -32021: 400, -32022: 400, -32601: 404}.get(code)","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py#L375-L411","documentation":"The JSON-RPC id correlation check in validate_response. It compares request and response ids with both an exact type check and an equality check, so a response id of True matches a request id of 1 only by value in Python but is rejected because type(response_id) is not type(request_id). Fires when the server echoes the wrong id, coerces it to a string, or answers a different request.","triggerScenarios":"Thrown at phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py:393 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Echo the request id verbatim in the response, preserving its type (int, string, or null)","Ensure the server does not stringify or renumber ids when routing batch replies","Match the response to the correct in-flight request before validating","In tests, build the expected response from the same id object used in the request"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}