python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Fix Streamable HTTP Accept header quality-factor handling

Open dchou1618 opened this issue 4 months ago • 1 comments

Initial Checks

  • [x] I confirm that I'm using the latest version of MCP Python SDK
  • [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue

Description

Streamable HTTP currently ignores q=0 values in the Accept header, so requests that explicitly mark a media type as unacceptable can still be accepted. This violates RFC 7231 semantics for content negotiation and can cause clients and servers to disagree about which response format is actually allowed.

Example Code

from starlette.requests import Request

def check_accept_headers(request: Request) -> tuple[bool, bool]:
    accept_header = request.headers.get("accept", "")
    accept_types = [media_type.strip().split(";")[0].strip().lower() for media_type in accept_header.split(",")]

    has_wildcard = "*/*" in accept_types
    has_json = has_wildcard or any(t in ("application/json", "application/*") for t in accept_types)
    has_sse = has_wildcard or any(t in ("text/event-stream", "text/*") for t in accept_types)

    return has_json, has_sse

# Example: client explicitly rejects JSON
request = Request({"type": "http", "method": "GET", "headers": [(b"accept", b"application/json;q=0, text/event-stream;q=1.0")]})

print(check_accept_headers(request))
# -> (True, True)

Python & MCP Python SDK

Python 3.12.10
MCP Python SDK: 1.28.1

dchou1618 avatar Jul 23 '26 23:07 dchou1618

Hi, I'd like to work on this — I have a fix ready (with AI assistance, disclosed per CONTRIBUTING.md). check_accept_headers() now parses each media-range's q value and treats q<=0 as "not acceptable" per RFC 7231 §5.3.1, with a specific type's rejection taking precedence over a broader wildcard that would otherwise accept it. Includes 3 new tests: the exact example from this issue, a wildcard-precedence case, and a nonzero-q regression check. Could this be assigned to me?

SpiliosDimakopoulos avatar Jul 24 '26 10:07 SpiliosDimakopoulos