Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/mcp/server/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,19 @@ def _check_accept_headers(self, request: Request) -> tuple[bool, bool]:
"""Check if the request accepts the required media types.

Supports wildcard media types per RFC 7231, section 5.3.2:
- Missing Accept header matches any media type
- */* matches any media type
- application/* matches any application/ subtype
- text/* matches any text/ subtype
"""
accept_header = request.headers.get("accept", "")
accept_header = request.headers.get("accept")

# RFC 7231, Section 5.3.2:
# A request without any Accept header field implies that the user agent
# will accept any media type in response.
if not accept_header:
return True, True

accept_types = [media_type.strip().split(";")[0].strip().lower() for media_type in accept_header.split(",")]

has_wildcard = "*/*" in accept_types
Expand Down
14 changes: 6 additions & 8 deletions tests/shared/test_streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,7 @@ def test_accept_header_validation(basic_server: None, basic_server_url: str):
headers={"Content-Type": "application/json"},
json={"jsonrpc": "2.0", "method": "initialize", "id": 1},
)
assert response.status_code == 406
assert "Not Acceptable" in response.text
assert response.status_code == 200


@pytest.mark.parametrize(
Expand Down Expand Up @@ -613,8 +612,9 @@ def test_accept_header_wildcard(basic_server: None, basic_server_url: str, accep
"accept_header",
[
"text/html",
"application/*",
"text/*",
"text/html",
"image/*",
"audio/*",
],
)
def test_accept_header_incompatible(basic_server: None, basic_server_url: str, accept_header: str):
Expand Down Expand Up @@ -885,8 +885,7 @@ def test_json_response_missing_accept_header(json_response_server: None, json_se
},
json=INIT_REQUEST,
)
assert response.status_code == 406
assert "Not Acceptable" in response.text
assert response.status_code == 200


def test_json_response_incorrect_accept_header(json_response_server: None, json_server_url: str):
Expand Down Expand Up @@ -1027,8 +1026,7 @@ def test_get_validation(basic_server: None, basic_server_url: str):
},
stream=True,
)
assert response.status_code == 406
assert "Not Acceptable" in response.text
assert response.status_code == 200

# Test with wrong Accept header
response = requests.get(
Expand Down