{"record":{"id":"def72b022226e17c","repo":"rohitg00/ai-engineering-from-scratch","slug":"invalid-json-rpc-response-envelope","errorCode":null,"errorMessage":"invalid JSON-RPC response envelope","messagePattern":"invalid JSON-RPC response envelope","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/08-building-an-mcp-client/code/main.py","lineNumber":107,"sourceCode":"    }\n    if ttl_ms is not None:\n        result[\"ttlMs\"] = ttl_ms\n        result[\"cacheScope\"] = cache_scope\n    return result\n\n\ndef decode_rpc_response(\n    response: dict[str, Any],\n    expected_id: int | str,\n) -> tuple[str, dict[str, Any]]:\n    response_id = response.get(\"id\")\n    if (\n        response.get(\"jsonrpc\") != \"2.0\"\n        or type(response_id) not in (int, str)\n        or type(response_id) is not type(expected_id)\n        or response_id != expected_id\n    ):\n        raise RuntimeError(\"invalid JSON-RPC response envelope\")\n    has_result = \"result\" in response\n    has_error = \"error\" in response\n    if has_result == has_error:\n        raise RuntimeError(\"JSON-RPC response must contain exactly one of result or error\")\n    if has_result:\n        result = response[\"result\"]\n        if not isinstance(result, dict):\n            raise RuntimeError(\"JSON-RPC result must be an object\")\n        return \"result\", result\n    error = response[\"error\"]\n    if not isinstance(error, dict):\n        raise RuntimeError(\"JSON-RPC error must be an object\")\n    code = error.get(\"code\")\n    if not isinstance(code, int) or isinstance(code, bool) or not isinstance(error.get(\"message\"), str):\n        raise RuntimeError(\"JSON-RPC error requires an integer code and string message\")\n    return \"error\", error\n\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/13-tools-and-protocols/08-building-an-mcp-client/code/main.py#L89-L125","documentation":"Error \"invalid JSON-RPC response envelope\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/13-tools-and-protocols/08-building-an-mcp-client/code/main.py:107 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"}