Skip to content

Commit aa2a2a9

Browse files
committed
fix: allow client scopes when no scope restriction is set
1 parent b33c811 commit aa2a2a9

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/mcp/shared/auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def validate_scope(self, requested_scope: str | None) -> list[str] | None:
7171
if requested_scope is None:
7272
return None
7373
requested_scopes = requested_scope.split(" ")
74-
allowed_scopes = [] if self.scope is None else self.scope.split(" ")
74+
if self.scope is None:
75+
return requested_scopes
76+
allowed_scopes = self.scope.split(" ")
7577
for scope in requested_scopes:
7678
if scope not in allowed_scopes: # pragma: no branch
7779
raise InvalidScopeError(f"Client was not registered with scope {scope}")

tests/shared/test_auth.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Tests for OAuth 2.0 shared code."""
22

3-
from mcp.shared.auth import OAuthMetadata
3+
import pytest
4+
from pydantic import AnyUrl
5+
6+
from mcp.shared.auth import InvalidScopeError, OAuthClientMetadata, OAuthMetadata
47

58

69
def test_oauth():
@@ -58,3 +61,22 @@ def test_oauth_with_jarm():
5861
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"],
5962
}
6063
)
64+
65+
66+
def test_validate_scope_allows_requested_scopes_when_client_scope_is_none():
67+
metadata = OAuthClientMetadata(
68+
redirect_uris=[AnyUrl("https://client.example.com/callback")],
69+
scope=None,
70+
)
71+
72+
assert metadata.validate_scope("read write") == ["read", "write"]
73+
74+
75+
def test_validate_scope_rejects_scope_not_registered_with_client():
76+
metadata = OAuthClientMetadata(
77+
redirect_uris=[AnyUrl("https://client.example.com/callback")],
78+
scope="read write",
79+
)
80+
81+
with pytest.raises(InvalidScopeError, match="profile"):
82+
metadata.validate_scope("read profile")

0 commit comments

Comments
 (0)