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

-32020

-32020

Error message

Malformed Base64 MCP header value

What it means

Error "Malformed Base64 MCP header value" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/13-tools-and-protocols/09-mcp-transports/code/main.py:130

    return origin is None or origin in ORIGIN_ALLOWLIST


def encode_header_value(value: str) -> str:
    safe = all(0x20 <= ord(character) <= 0x7E for character in value)
    sentinel = value.startswith("=?base64?") and value.endswith("?=")
    if safe and value == value.strip() and not sentinel:
        return value
    encoded = base64.b64encode(value.encode("utf-8")).decode("ascii")
    return f"=?base64?{encoded}?="


def decode_header_value(value: str) -> str:
    if value.startswith("=?base64?") and value.endswith("?="):
        encoded = value[len("=?base64?") : -2]
        try:
            return base64.b64decode(encoded, validate=True).decode("utf-8")
        except (ValueError, UnicodeDecodeError) as exc:
            raise RpcFault(-32020, "Malformed Base64 MCP header value") from exc
    if any(ord(character) < 0x20 or ord(character) > 0x7E for character in value):
        raise RpcFault(-32020, "Malformed MCP header value")
    return value


def body_name(message: dict[str, Any]) -> str | None:
    params = message.get("params", {})
    if message.get("method") == "resources/read":
        value = params.get("uri")
    elif message.get("method") in {"tools/call", "prompts/get"}:
        value = params.get("name")
    else:
        return None
    return value if isinstance(value, str) else None


def http_headers_for(
    message: dict[str, Any],

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/13-tools-and-protocols/09-mcp-transports/code/main.py:130 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/d0ec57169241b2c3. Report an issue: GitHub.