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

invalid JSON-RPC request

Error message

invalid JSON-RPC request

What it means

Error "invalid JSON-RPC request" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py:184

    ends = value.endswith("?=")
    if starts or ends:
        if not (starts and ends):
            raise ProtocolViolation("malformed Base64 MCP header sentinel", -32020)
        encoded = value[len("=?base64?") : -2]
        try:
            return base64.b64decode(encoded, validate=True).decode("utf-8")
        except (ValueError, UnicodeDecodeError) as error:
            raise ProtocolViolation("malformed Base64 MCP header value", -32020) from error
    if value != value.strip() or any(
        ord(character) < 0x20 or ord(character) > 0x7E for character in value
    ):
        raise ProtocolViolation("unsafe MCP header value must use the Base64 sentinel", -32020)
    return value


def validate_request(headers: dict[str, str], body: dict[str, Any], era: str) -> None:
    if body.get("jsonrpc") != "2.0" or not isinstance(body.get("method"), str):
        raise ProtocolViolation("invalid JSON-RPC request")
    params = body.get("params", {})
    if not isinstance(params, dict):
        raise ProtocolViolation("params must be an object", -32602)
    wire_headers = normalized_headers(headers)
    if era == "legacy":
        if body["method"] != "initialize" and wire_headers.get("mcp-protocol-version") == MODERN_VERSION:
            raise ProtocolViolation("modern metadata reached the legacy branch", -32020)
        return
    if era != "modern":
        raise ValueError(f"unknown era: {era}")

    metadata = params.get("_meta")
    if not isinstance(metadata, dict):
        raise ProtocolViolation("modern request is missing params._meta", -32602)
    protocol_version = metadata.get(PROTOCOL_VERSION_KEY)
    capabilities = metadata.get(CLIENT_CAPABILITIES_KEY)
    if not isinstance(protocol_version, str):
        raise ProtocolViolation(f"modern request is missing {PROTOCOL_VERSION_KEY}", -32602)

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py:184 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


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