Skip to content

Commit fa068dd

Browse files
committed
Rename OAuthServerProvider to OAuthAuthorizationServerProvider
1 parent 1ad1842 commit fa068dd

File tree

11 files changed

+41
-36
lines changed

11 files changed

+41
-36
lines changed

src/mcp/server/auth/handlers/authorize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
AuthorizationErrorCode,
1616
AuthorizationParams,
1717
AuthorizeError,
18-
OAuthServerProvider,
18+
OAuthAuthorizationServerProvider,
1919
construct_redirect_uri,
2020
)
2121
from mcp.shared.auth import (
@@ -74,7 +74,7 @@ class AnyHttpUrlModel(RootModel[AnyHttpUrl]):
7474

7575
@dataclass
7676
class AuthorizationHandler:
77-
provider: OAuthServerProvider[Any, Any, Any]
77+
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
7878

7979
async def handle(self, request: Request) -> Response:
8080
# implements authorization requests for grant_type=code;

src/mcp/server/auth/handlers/register.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mcp.server.auth.errors import stringify_pydantic_error
1212
from mcp.server.auth.json_response import PydanticJSONResponse
1313
from mcp.server.auth.provider import (
14-
OAuthServerProvider,
14+
OAuthAuthorizationServerProvider,
1515
RegistrationError,
1616
RegistrationErrorCode,
1717
)
@@ -32,7 +32,7 @@ class RegistrationErrorResponse(BaseModel):
3232

3333
@dataclass
3434
class RegistrationHandler:
35-
provider: OAuthServerProvider[Any, Any, Any]
35+
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
3636
options: ClientRegistrationOptions
3737

3838
async def handle(self, request: Request) -> Response:

src/mcp/server/auth/handlers/revoke.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
AuthenticationError,
1515
ClientAuthenticator,
1616
)
17-
from mcp.server.auth.provider import AccessToken, OAuthServerProvider, RefreshToken
17+
from mcp.server.auth.provider import AccessToken, OAuthAuthorizationServerProvider, RefreshToken
1818

1919

2020
class RevocationRequest(BaseModel):
@@ -35,7 +35,7 @@ class RevocationErrorResponse(BaseModel):
3535

3636
@dataclass
3737
class RevocationHandler:
38-
provider: OAuthServerProvider[Any, Any, Any]
38+
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
3939
client_authenticator: ClientAuthenticator
4040

4141
async def handle(self, request: Request) -> Response:

src/mcp/server/auth/handlers/token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
AuthenticationError,
1616
ClientAuthenticator,
1717
)
18-
from mcp.server.auth.provider import OAuthServerProvider, TokenError, TokenErrorCode
18+
from mcp.server.auth.provider import OAuthAuthorizationServerProvider, TokenError, TokenErrorCode
1919
from mcp.shared.auth import OAuthToken
2020

2121

@@ -76,7 +76,7 @@ class TokenSuccessResponse(RootModel[OAuthToken]):
7676

7777
@dataclass
7878
class TokenHandler:
79-
provider: OAuthServerProvider[Any, Any, Any]
79+
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
8080
client_authenticator: ClientAuthenticator
8181

8282
def response(self, obj: TokenSuccessResponse | TokenErrorResponse):

src/mcp/server/auth/middleware/bearer_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from starlette.requests import HTTPConnection
1111
from starlette.types import Receive, Scope, Send
1212

13-
from mcp.server.auth.provider import AccessToken, OAuthServerProvider
13+
from mcp.server.auth.provider import AccessToken, OAuthAuthorizationServerProvider
1414

1515

1616
class AuthenticatedUser(SimpleUser):
@@ -29,7 +29,7 @@ class BearerAuthBackend(AuthenticationBackend):
2929

3030
def __init__(
3131
self,
32-
provider: OAuthServerProvider[Any, Any, Any],
32+
provider: OAuthAuthorizationServerProvider[Any, Any, Any],
3333
):
3434
self.provider = provider
3535

src/mcp/server/auth/middleware/client_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22
from typing import Any
33

4-
from mcp.server.auth.provider import OAuthServerProvider
4+
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
55
from mcp.shared.auth import OAuthClientInformationFull
66

77

@@ -21,7 +21,7 @@ class ClientAuthenticator:
2121
logic is skipped.
2222
"""
2323

24-
def __init__(self, provider: OAuthServerProvider[Any, Any, Any]):
24+
def __init__(self, provider: OAuthAuthorizationServerProvider[Any, Any, Any]):
2525
"""
2626
Initialize the dependency.
2727

src/mcp/server/auth/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class TokenError(Exception):
9696
AccessTokenT = TypeVar("AccessTokenT", bound=AccessToken)
9797

9898

99-
class OAuthServerProvider(
99+
class OAuthAuthorizationServerProvider(
100100
Protocol, Generic[AuthorizationCodeT, RefreshTokenT, AccessTokenT]
101101
):
102102
async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:

src/mcp/server/auth/routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from mcp.server.auth.handlers.revoke import RevocationHandler
1515
from mcp.server.auth.handlers.token import TokenHandler
1616
from mcp.server.auth.middleware.client_auth import ClientAuthenticator
17-
from mcp.server.auth.provider import OAuthServerProvider
17+
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
1818
from mcp.server.auth.settings import ClientRegistrationOptions, RevocationOptions
1919
from mcp.shared.auth import OAuthMetadata
2020

@@ -65,7 +65,7 @@ def cors_middleware(
6565

6666

6767
def create_auth_routes(
68-
provider: OAuthServerProvider[Any, Any, Any],
68+
provider: OAuthAuthorizationServerProvider[Any, Any, Any],
6969
issuer_url: AnyHttpUrl,
7070
service_documentation_url: AnyHttpUrl | None = None,
7171
client_registration_options: ClientRegistrationOptions | None = None,

src/mcp/server/fastmcp/server.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
BearerAuthBackend,
3232
RequireAuthMiddleware,
3333
)
34-
from mcp.server.auth.provider import OAuthServerProvider
34+
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
3535
from mcp.server.auth.settings import (
3636
AuthSettings,
3737
)
@@ -128,7 +128,7 @@ def __init__(
128128
self,
129129
name: str | None = None,
130130
instructions: str | None = None,
131-
auth_provider: OAuthServerProvider[Any, Any, Any] | None = None,
131+
auth_server_provider: OAuthAuthorizationServerProvider[Any, Any, Any] | None = None,
132132
**settings: Any,
133133
):
134134
self.settings = Settings(**settings)
@@ -149,12 +149,17 @@ def __init__(
149149
self._prompt_manager = PromptManager(
150150
warn_on_duplicate_prompts=self.settings.warn_on_duplicate_prompts
151151
)
152-
if (self.settings.auth is not None) != (auth_provider is not None):
152+
if (self.settings.auth is not None) != (auth_server_provider is not None):
153+
# TODO: after we support separate authorization servers (see
154+
# https://github.com/modelcontextprotocol/modelcontextprotocol/pull/284)
155+
# we should validate that if auth is enabled, we have either an
156+
# auth_server_provider to host our own authorization server,
157+
# OR the URL of a 3rd party authorization server.
153158
raise ValueError(
154-
"settings.auth must be specified if and only if auth_provider "
159+
"settings.auth must be specified if and only if auth_server_provider "
155160
"is specified"
156161
)
157-
self._auth_provider = auth_provider
162+
self._auth_server_provider = auth_server_provider
158163
self._custom_starlette_routes: list[Route] = []
159164
self.dependencies = self.settings.dependencies
160165

@@ -580,7 +585,7 @@ async def handle_sse(request: Request) -> EventSourceResponse:
580585
required_scopes = []
581586

582587
# Add auth endpoints if auth provider is configured
583-
if self._auth_provider:
588+
if self._auth_server_provider:
584589
assert self.settings.auth
585590
from mcp.server.auth.routes import create_auth_routes
586591

@@ -591,7 +596,7 @@ async def handle_sse(request: Request) -> EventSourceResponse:
591596
Middleware(
592597
AuthenticationMiddleware,
593598
backend=BearerAuthBackend(
594-
provider=self._auth_provider,
599+
provider=self._auth_server_provider,
595600
),
596601
),
597602
# Add the auth context middleware to store
@@ -600,7 +605,7 @@ async def handle_sse(request: Request) -> EventSourceResponse:
600605
]
601606
routes.extend(
602607
create_auth_routes(
603-
provider=self._auth_provider,
608+
provider=self._auth_server_provider,
604609
issuer_url=self.settings.auth.issuer_url,
605610
service_documentation_url=self.settings.auth.service_documentation_url,
606611
client_registration_options=self.settings.auth.client_registration_options,

tests/server/auth/middleware/test_bearer_auth.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from mcp.server.auth.provider import (
2020
AccessToken,
21-
OAuthServerProvider,
21+
OAuthAuthorizationServerProvider,
2222
)
2323

2424

@@ -42,7 +42,7 @@ async def load_access_token(self, token: str) -> AccessToken | None:
4242

4343

4444
def add_token_to_provider(
45-
provider: OAuthServerProvider[Any, Any, Any], token: str, access_token: AccessToken
45+
provider: OAuthAuthorizationServerProvider[Any, Any, Any], token: str, access_token: AccessToken
4646
) -> None:
4747
"""Helper function to add a token to a provider.
4848
@@ -70,10 +70,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
7070

7171

7272
@pytest.fixture
73-
def mock_oauth_provider() -> OAuthServerProvider[Any, Any, Any]:
73+
def mock_oauth_provider() -> OAuthAuthorizationServerProvider[Any, Any, Any]:
7474
"""Create a mock OAuth provider."""
7575
# Use type casting to satisfy the type checker
76-
return cast(OAuthServerProvider[Any, Any, Any], MockOAuthProvider())
76+
return cast(OAuthAuthorizationServerProvider[Any, Any, Any], MockOAuthProvider())
7777

7878

7979
@pytest.fixture
@@ -114,7 +114,7 @@ class TestBearerAuthBackend:
114114
"""Tests for the BearerAuthBackend class."""
115115

116116
async def test_no_auth_header(
117-
self, mock_oauth_provider: OAuthServerProvider[Any, Any, Any]
117+
self, mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any]
118118
):
119119
"""Test authentication with no Authorization header."""
120120
backend = BearerAuthBackend(provider=mock_oauth_provider)
@@ -123,7 +123,7 @@ async def test_no_auth_header(
123123
assert result is None
124124

125125
async def test_non_bearer_auth_header(
126-
self, mock_oauth_provider: OAuthServerProvider[Any, Any, Any]
126+
self, mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any]
127127
):
128128
"""Test authentication with non-Bearer Authorization header."""
129129
backend = BearerAuthBackend(provider=mock_oauth_provider)
@@ -137,7 +137,7 @@ async def test_non_bearer_auth_header(
137137
assert result is None
138138

139139
async def test_invalid_token(
140-
self, mock_oauth_provider: OAuthServerProvider[Any, Any, Any]
140+
self, mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any]
141141
):
142142
"""Test authentication with invalid token."""
143143
backend = BearerAuthBackend(provider=mock_oauth_provider)
@@ -152,7 +152,7 @@ async def test_invalid_token(
152152

153153
async def test_expired_token(
154154
self,
155-
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
155+
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
156156
expired_access_token: AccessToken,
157157
):
158158
"""Test authentication with expired token."""
@@ -171,7 +171,7 @@ async def test_expired_token(
171171

172172
async def test_valid_token(
173173
self,
174-
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
174+
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
175175
valid_access_token: AccessToken,
176176
):
177177
"""Test authentication with valid token."""
@@ -195,7 +195,7 @@ async def test_valid_token(
195195

196196
async def test_token_without_expiry(
197197
self,
198-
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
198+
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
199199
no_expiry_access_token: AccessToken,
200200
):
201201
"""Test authentication with token that has no expiry."""

tests/server/fastmcp/auth/test_auth_integration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
AccessToken,
2222
AuthorizationCode,
2323
AuthorizationParams,
24-
OAuthServerProvider,
24+
OAuthAuthorizationServerProvider,
2525
RefreshToken,
2626
construct_redirect_uri,
2727
)
@@ -41,7 +41,7 @@
4141

4242

4343
# Mock OAuth provider for testing
44-
class MockOAuthProvider(OAuthServerProvider):
44+
class MockOAuthProvider(OAuthAuthorizationServerProvider):
4545
def __init__(self):
4646
self.clients = {}
4747
self.auth_codes = {} # code -> {client_id, code_challenge, redirect_uri}
@@ -1003,7 +1003,7 @@ async def test_fastmcp_with_auth(
10031003
"""Test creating a FastMCP server with authentication."""
10041004
# Create FastMCP server with auth provider
10051005
mcp = FastMCP(
1006-
auth_provider=mock_oauth_provider,
1006+
auth_server_provider=mock_oauth_provider,
10071007
require_auth=True,
10081008
auth=AuthSettings(
10091009
issuer_url=AnyHttpUrl("https://auth.example.com"),

0 commit comments

Comments
 (0)