{"record":{"id":"c1f17352138d3c96","repo":"rohitg00/ai-engineering-from-scratch","slug":"response-must-contain-exactly-one-of-result-or-err","errorCode":null,"errorMessage":"response must contain exactly one of result or error","messagePattern":"response must contain exactly one of result or error","errorType":"validation","errorClass":"ProtocolViolation","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py","lineNumber":397,"sourceCode":"\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)\n    if expected_status is not None and status != expected_status:\n        raise ProtocolViolation(f\"JSON-RPC error {code} requires HTTP {expected_status}\")\n\n","sourceCodeStart":379,"sourceCodeEnd":415,"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#L379-L415","documentation":"Error \"response must contain exactly one of result or error\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py:397 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}