{"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":"The result/error XOR rule of JSON-RPC 2.0, enforced in validate_response: has_result == has_error means the response contained both members or neither. A response with both is ambiguous and one with neither is a meaningless envelope, so the conformance check rejects it before looking at HTTP status or payload shape.","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":["Return exactly one of \"result\" or \"error\" per response, never both, never neither","For notifications the server must return 202 with an empty body, not an empty JSON object","Audit response builders for code paths that attach a default empty result alongside an error","Add a regression transcript covering the both-fields case"],"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"}