Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Treat non-Bearer Authorization schemes as missing authentication (#5503)

Asem ahmeddb4c97949d49
diff --git a/fastmcp_slim/fastmcp/server/auth/middleware.py b/fastmcp_slim/fastmcp/server/auth/middleware.py
index 3540f9125..0f6ecd89a 100644
--- a/fastmcp_slim/fastmcp/server/auth/middleware.py
+++ b/fastmcp_slim/fastmcp/server/auth/middleware.py
@@ -57,8 +57,12 @@ class RequireAuthMiddleware(SDKRequireAuthMiddleware):
"""Process ASGI scope, distinguishing missing vs invalid auth.
Per RFC 6750 Β§3.1:
- - Missing auth (no Authorization header) β†’ 401 without error attribute
- - Invalid auth (Authorization header present) β†’ 401 with error attribute
+ - Missing auth (no Bearer credentials) β†’ 401 without error attribute
+ - Invalid auth (Bearer credentials present) β†’ 401 with error attribute
+
+ An Authorization header using another scheme (e.g. Basic) is an
+ unsupported authentication method, which Β§3.1 treats like missing
+ authentication.
This ensures OAuth flow initialization works correctly in MCP clients
during initial discovery phase.
@@ -72,18 +76,20 @@ class RequireAuthMiddleware(SDKRequireAuthMiddleware):
await self.app(scope, receive, send)
return
- # Check if Authorization header is present
+ # Check if Bearer credentials are present. This mirrors the SDK's
+ # BearerAuthBackend, which ignores any other Authorization scheme.
headers = scope.get("headers", [])
- has_auth_header = any(
- header[0].lower() == b"authorization" for header in headers
+ has_bearer_credentials = any(
+ name.lower() == b"authorization" and value[:7].lower() == b"bearer "
+ for name, value in headers
)
- if not has_auth_header:
+ if not has_bearer_credentials:
# Per RFC 6750 Β§3.1: missing auth should not include error attribute
await self._send_missing_auth(send)
return
- # Authorization header is present - use parent's validation logic
+ # Bearer credentials are present - use parent's validation logic
# This will check token validity and call _send_auth_error if invalid
await super().__call__(scope, receive, send)
diff --git a/tests/server/auth/test_enhanced_error_responses.py b/tests/server/auth/test_enhanced_error_responses.py
index aba2a5870..091205493 100644
--- a/tests/server/auth/test_enhanced_error_responses.py
+++ b/tests/server/auth/test_enhanced_error_responses.py
@@ -699,6 +699,42 @@ class TestEnhancedRequireAuthMiddleware:
assert "error=" not in www_auth
assert response.content == b""
+ @pytest.mark.parametrize("authorization", ["Basic dXNlcjpwYXNz", "Token abc"])
+ def test_non_bearer_scheme_no_error_attribute(self, jwt_verifier, authorization):
+ """An unsupported auth scheme is treated like missing auth (RFC 6750 Β§3.1)."""
+ server = FastMCP("Test Server")
+ app = create_streamable_http_app(
+ server=server,
+ streamable_http_path="/mcp",
+ auth=jwt_verifier,
+ )
+
+ with TestClient(app) as client:
+ response = client.post("/mcp", headers={"Authorization": authorization})
+
+ assert response.status_code == 401
+ www_auth = response.headers["www-authenticate"]
+ assert www_auth.startswith("Bearer")
+ assert "error=" not in www_auth
+ assert response.content == b""
+
+ def test_lowercase_bearer_scheme_is_still_validated(self, jwt_verifier):
+ """The Bearer scheme is case-insensitive, so a bad token still errors."""
+ server = FastMCP("Test Server")
+ app = create_streamable_http_app(
+ server=server,
+ streamable_http_path="/mcp",
+ auth=jwt_verifier,
+ )
+
+ with TestClient(app) as client:
+ response = client.post(
+ "/mcp", headers={"Authorization": "bearer invalid-token"}
+ )
+
+ assert response.status_code == 401
+ assert 'error="invalid_token"' in response.headers["www-authenticate"]
+
def test_missing_auth_challenge_includes_supported_scopes(self):
app = self.create_scoped_app(
required_scopes=["read"],