Skip to content

Commit 005926c

Browse files
author
RJ Lopez
committed
fix: accept wildcard patterns in Accept header per RFC 7231 §5.3.2
1 parent 5cbd259 commit 005926c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/mcp/server/streamable_http.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,19 @@ def _check_accept_headers(self, request: Request) -> tuple[bool, bool]:
396396
"""Check if the request accepts the required media types.
397397
398398
Supports wildcard media types per RFC 7231, section 5.3.2:
399+
- Missing Accept header matches any media type
399400
- */* matches any media type
400401
- application/* matches any application/ subtype
401402
- text/* matches any text/ subtype
402403
"""
403-
accept_header = request.headers.get("accept", "")
404+
accept_header = request.headers.get("accept")
405+
406+
# RFC 7231, Section 5.3.2:
407+
# A request without any Accept header field implies that the user agent
408+
# will accept any media type in response.
409+
if not accept_header:
410+
return True, True
411+
404412
accept_types = [media_type.strip().split(";")[0].strip().lower() for media_type in accept_header.split(",")]
405413

406414
has_wildcard = "*/*" in accept_types

tests/shared/test_streamable_http.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ def test_accept_header_validation(basic_server: None, basic_server_url: str):
581581
headers={"Content-Type": "application/json"},
582582
json={"jsonrpc": "2.0", "method": "initialize", "id": 1},
583583
)
584-
assert response.status_code == 406
585-
assert "Not Acceptable" in response.text
584+
assert response.status_code == 200
586585

587586

588587
@pytest.mark.parametrize(
@@ -613,8 +612,9 @@ def test_accept_header_wildcard(basic_server: None, basic_server_url: str, accep
613612
"accept_header",
614613
[
615614
"text/html",
616-
"application/*",
617-
"text/*",
615+
"text/html",
616+
"image/*",
617+
"audio/*",
618618
],
619619
)
620620
def test_accept_header_incompatible(basic_server: None, basic_server_url: str, accept_header: str):
@@ -885,8 +885,7 @@ def test_json_response_missing_accept_header(json_response_server: None, json_se
885885
},
886886
json=INIT_REQUEST,
887887
)
888-
assert response.status_code == 406
889-
assert "Not Acceptable" in response.text
888+
assert response.status_code == 200
890889

891890

892891
def test_json_response_incorrect_accept_header(json_response_server: None, json_server_url: str):
@@ -1027,8 +1026,7 @@ def test_get_validation(basic_server: None, basic_server_url: str):
10271026
},
10281027
stream=True,
10291028
)
1030-
assert response.status_code == 406
1031-
assert "Not Acceptable" in response.text
1029+
assert response.status_code == 200
10321030

10331031
# Test with wrong Accept header
10341032
response = requests.get(

0 commit comments

Comments
 (0)