rohitg00/ai-engineering-from-scratch · error · RuntimeError

JSON-RPC result must be an object

Error message

JSON-RPC result must be an object

What it means

Error "JSON-RPC result must be an object" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/13-tools-and-protocols/08-building-an-mcp-client/code/main.py:115

    response: dict[str, Any],
    expected_id: int | str,
) -> tuple[str, dict[str, Any]]:
    response_id = response.get("id")
    if (
        response.get("jsonrpc") != "2.0"
        or type(response_id) not in (int, str)
        or type(response_id) is not type(expected_id)
        or response_id != expected_id
    ):
        raise RuntimeError("invalid JSON-RPC response envelope")
    has_result = "result" in response
    has_error = "error" in response
    if has_result == has_error:
        raise RuntimeError("JSON-RPC response must contain exactly one of result or error")
    if has_result:
        result = response["result"]
        if not isinstance(result, dict):
            raise RuntimeError("JSON-RPC result must be an object")
        return "result", result
    error = response["error"]
    if not isinstance(error, dict):
        raise RuntimeError("JSON-RPC error must be an object")
    code = error.get("code")
    if not isinstance(code, int) or isinstance(code, bool) or not isinstance(error.get("message"), str):
        raise RuntimeError("JSON-RPC error requires an integer code and string message")
    return "error", error


def validate_modern_request(message: dict[str, Any], supported: list[str]) -> None:
    if message.get("jsonrpc") != "2.0" or not isinstance(message.get("method"), str):
        raise RpcFault(-32600, "Invalid Request")
    params = message.get("params")
    if not isinstance(params, dict) or not isinstance(params.get("_meta"), dict):
        raise RpcFault(-32602, "Modern params._meta is required")
    meta = params["_meta"]
    requested = meta.get(VERSION_KEY)

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/13-tools-and-protocols/08-building-an-mcp-client/code/main.py:115 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/d40135fdca657976. Report an issue: GitHub.