RequireAuthMiddleware omits RFC 6750 scope in WWW-Authenticate on 401/403
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- python
- Lĩnh vực
- authentication, backend, security
Hướng nghiên cứu
Bắt đầu tại src/mcp/server/auth/middleware/bearer_auth.py, tập trung vào RequireAuthMiddleware._send_auth_error và cách xây dựng WWW-Authenticate hiện có. Xem xét phạm vi bao phủ của các phản hồi xác thực xung quanh, sau đó xác minh cả hai trường hợp 401 và 403 khi required_scopes được cấu hình; được xem là hoàn tất khi challenge hiển thị scope bắt buộc đồng thời giữ nguyên các tham số hiện có.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
Summary
RequireAuthMiddleware._send_auth_error builds WWW-Authenticate with error and error_description (and optional resource_metadata), but never includes the scope parameter, even when required_scopes is configured.
This breaks RFC 6750 Section 3.1 and the MCP Authorization scope-selection / step-up flow. The SDK client already expects scope via extract_scope_from_www_auth() and uses it in get_client_metadata_scopes() as the highest-priority source when handling 403 insufficient_scope.
Actual behavior
For a server configured with required_scopes=["api.read"]:
401 (no/invalid token):
WWW-Authenticate: Bearer error="invalid_token", error_description="Authentication required"
403 (token missing required scope):
WWW-Authenticate: Bearer error="insufficient_scope", error_description="Required scope: api.read"
Neither response includes scope="api.read".
Expected behavior
When required_scopes is non-empty, the challenge should include the RFC 6750 scope parameter, e.g.:
WWW-Authenticate: Bearer error="insufficient_scope", error_description="Required scope: api.read", scope="api.read"
(and similarly for 401 when scopes are configured, so the client can request the correct scopes on initial authorization).
Impact
- MCP clients cannot reliably discover required scopes from the challenge header.
- Step-up authorization falls back to PRM
scopes_supported(or omits scope), which is lower priority per the MCP scope selection strategy and can fail when PRM does not advertise scopes. - The SDK client already implements the correct consumer side; only server emission is missing.
Root cause
In src/mcp/server/auth/middleware/bearer_auth.py, _send_auth_error builds:
www_auth_parts = [f'error="{error}"', f'error_description="{description}"']
if self.resource_metadata_url:
www_auth_parts.append(f'resource_metadata="{self.resource_metadata_url}"')
It never appends scope= from self.required_scopes, which is already available on the middleware instance.
Present on both v1.x and main (verified in current source).
Suggested fix
When self.required_scopes is non-empty, append:
www_auth_parts.append(f'scope="{" ".join(self.required_scopes)}"')
Happy to open a PR against v1.x once a maintainer assigns this issue to me. Please also advise whether a follow-up for main (v2) is desired.
Example Code
Python & MCP Python SDK
Python: 3.14.6
MCP Python SDK: 1.28.1 (also reproduced against current v1.x / main source of RequireAuthMiddleware._send_auth_error)
- Ngôn ngữ chính
- Python
- Star
- 24.3k
- Fork
- 4k
- Merge trung bình
- 1 ngày 19 phút
- Pull request đã merge (30 ngày)
- 29
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/python-sdk
-
Streamable HTTP client logs a WARNING for valid 202 Accepted on session termination (DELETE) Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
modelcontextprotocol/python-sdk#3546 · 5 bình luận ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
modelcontextprotocol/python-sdk#3545 · 1 bình luận ·
-
v1 v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
modelcontextprotocol/python-sdk#3508 · 2 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
modelcontextprotocol/python-sdk#3504 ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/python-sdk#3492 · 1 bình luận ·