Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
682ff1e
wip
praboud-ant Mar 6, 2025
331d51e
Unwind changes
praboud-ant Mar 6, 2025
d283f56
wip
praboud-ant Mar 7, 2025
e96d280
Get tests passing
praboud-ant Mar 10, 2025
1e9dd4c
Clean up provider interface
praboud-ant Mar 10, 2025
d535089
Lint
praboud-ant Mar 10, 2025
031cadf
Clean up registration endpoint
praboud-ant Mar 10, 2025
765efb6
Lint
praboud-ant Mar 10, 2025
0637bc3
update token + revoke to use form data
praboud-ant Mar 10, 2025
b99633a
Adjust more things to fit spec
praboud-ant Mar 10, 2025
9ae1c21
Lint
praboud-ant Mar 10, 2025
50683b9
Remove dup
praboud-ant Mar 10, 2025
2c5f26a
Comment
praboud-ant Mar 10, 2025
e605994
Refactor back to authorize()
praboud-ant Mar 10, 2025
e7c5f87
Improve validation for /token
praboud-ant Mar 11, 2025
83c0c9f
Improve validation for registration
praboud-ant Mar 11, 2025
0c1aae9
Improve /authorize validation & add tests
praboud-ant Mar 11, 2025
038fb04
Hoist oauth token expiration check into bearer auth middleware
praboud-ant Mar 11, 2025
a4e17f3
Add tests for /revoke validation
praboud-ant Mar 11, 2025
5f11c60
Lint + typecheck
praboud-ant Mar 11, 2025
571913a
Clean up unused error classes
praboud-ant Mar 11, 2025
d43647f
Update to use Python 3.10 types
praboud-ant Mar 11, 2025
9d72c1e
Use classes for handlers
praboud-ant Mar 11, 2025
a5079af
Refactor
praboud-ant Mar 11, 2025
c4c2608
Simplify bearer auth logic
praboud-ant Mar 11, 2025
bc62d73
Avoid asyncio dependency in tests
praboud-ant Mar 11, 2025
3852179
Add comment
praboud-ant Mar 11, 2025
874838a
Lint
praboud-ant Mar 11, 2025
f788d79
Add json_response.py comment
praboud-ant Mar 11, 2025
152feb9
Format
praboud-ant Mar 11, 2025
f37ebc4
Move around the response models to be closer to the handlers
praboud-ant Mar 11, 2025
c2873fd
Get rid of silly TS comments
praboud-ant Mar 11, 2025
fe2c029
Remove ClientAuthRequest
praboud-ant Mar 11, 2025
3a13f5d
Reorganize AuthInfo
praboud-ant Mar 11, 2025
37c5fc4
Refactor client metadata endpoint
praboud-ant Mar 11, 2025
792d302
Make metadata more spec compliant
praboud-ant Mar 12, 2025
6c48b11
Use python 3.10 types everywhere
praboud-ant Mar 12, 2025
a437566
Add back authorization to the /revoke endpoint, simplify revoke
praboud-ant Mar 12, 2025
9fee929
Move around validation logic
praboud-ant Mar 12, 2025
d79be8f
Fixups while integrating new auth capabilities
praboud-ant Mar 19, 2025
8d637b4
Pull all auth settings out into a separate config
praboud-ant Mar 19, 2025
8c86bce
Move router file to be routes
praboud-ant Mar 19, 2025
31618c1
Add auth context middleware
praboud-ant Mar 19, 2025
5ebbc19
Validate scopes + provide default
praboud-ant Mar 19, 2025
50673c6
Validate grant_types on registration
praboud-ant Mar 19, 2025
02d76f3
auth: client implementation
dsp-ant Mar 12, 2025
88edddc
update lock
dsp-ant Mar 12, 2025
d774be7
fix
dsp-ant Mar 12, 2025
a09e958
foo
dsp-ant Mar 14, 2025
4e73552
Format
praboud-ant Mar 19, 2025
56f694e
Move StreamingASGITransport into the library code, so MCP integration…
praboud-ant Mar 19, 2025
60da682
Improved error handling, generic types for provider
praboud-ant Mar 21, 2025
374a0b4
Rename AuthInfo to AccessToken
praboud-ant Mar 21, 2025
fb5a568
Rename
praboud-ant Mar 22, 2025
76ddc65
Add docs
praboud-ant Mar 22, 2025
e42dbf5
Merge remote-tracking branch 'origin/main' into praboud/auth
praboud-ant Mar 22, 2025
10e00e7
Typecheck
praboud-ant Mar 22, 2025
87571d8
Return 401 on missing auth, not 403
praboud-ant Mar 25, 2025
c6f991b
Convert AuthContextMiddleware to plain ASGI middleware & add tests
praboud-ant Mar 25, 2025
482149e
Fix redirect_uri handling
praboud-ant Mar 25, 2025
5230180
Remove client for now
praboud-ant Mar 25, 2025
8e15abc
Add test for auth context middleware
praboud-ant Mar 25, 2025
0a1a408
Add CORS support
praboud-ant Mar 25, 2025
3069aa3
Comment
praboud-ant Mar 27, 2025
16f0688
Merge remote-tracking branch 'origin/main' into praboud/auth
praboud-ant Mar 27, 2025
5ecc7f0
Remove client tests
praboud-ant Mar 27, 2025
8c251c9
Add ignores
praboud-ant Mar 27, 2025
f46dcb1
Merge remote-tracking branch 'origin/main' into praboud/auth
praboud-ant Apr 18, 2025
d3725cf
Review feedback
praboud-ant Apr 18, 2025
07f4e3a
Fix stream resource leaks and upgrade Starlette
bhosmer-ant Apr 13, 2025
1237148
Lint
praboud-ant Apr 18, 2025
1ad1842
Review comments
praboud-ant Apr 18, 2025
fa068dd
Rename OAuthServerProvider to OAuthAuthorizationServerProvider
praboud-ant Apr 18, 2025
9b5709a
Merge branch 'main' into praboud/auth
ihrpr Apr 30, 2025
67d568b
revert starlette upgrade
ihrpr Apr 30, 2025
16a7efa
add python-multipart - was missing
ihrpr Apr 30, 2025
91c09a4
ruff
ihrpr Apr 30, 2025
0582bf5
try fixing test
ihrpr Apr 30, 2025
8194bce
increse timeout
ihrpr Apr 30, 2025
2c63020
fix test
ihrpr May 1, 2025
2ea68f2
test
ihrpr May 1, 2025
b0fe041
fix test
ihrpr May 1, 2025
ba366e3
test
ihrpr May 1, 2025
e1a9fec
test
ihrpr May 1, 2025
af4221f
skip test
ihrpr May 1, 2025
f2840fe
Test auth (#609)
ihrpr May 1, 2025
cda4401
remove pyright upgrade and ruff format
ihrpr May 1, 2025
f2cc6ee
uv lock
ihrpr May 1, 2025
23ef519
Merge branch 'main' into praboud/auth
ihrpr May 1, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename OAuthServerProvider to OAuthAuthorizationServerProvider
  • Loading branch information
praboud-ant committed Apr 18, 2025
commit fa068dd027474d16fafbbb63cc902a86ef0f76b3
4 changes: 2 additions & 2 deletions src/mcp/server/auth/handlers/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
AuthorizationErrorCode,
AuthorizationParams,
AuthorizeError,
OAuthServerProvider,
OAuthAuthorizationServerProvider,
construct_redirect_uri,
)
from mcp.shared.auth import (
Expand Down Expand Up @@ -74,7 +74,7 @@ class AnyHttpUrlModel(RootModel[AnyHttpUrl]):

@dataclass
class AuthorizationHandler:
provider: OAuthServerProvider[Any, Any, Any]
provider: OAuthAuthorizationServerProvider[Any, Any, Any]

async def handle(self, request: Request) -> Response:
# implements authorization requests for grant_type=code;
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mcp.server.auth.errors import stringify_pydantic_error
from mcp.server.auth.json_response import PydanticJSONResponse
from mcp.server.auth.provider import (
OAuthServerProvider,
OAuthAuthorizationServerProvider,
RegistrationError,
RegistrationErrorCode,
)
Expand All @@ -32,7 +32,7 @@ class RegistrationErrorResponse(BaseModel):

@dataclass
class RegistrationHandler:
provider: OAuthServerProvider[Any, Any, Any]
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
options: ClientRegistrationOptions

async def handle(self, request: Request) -> Response:
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/handlers/revoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
AuthenticationError,
ClientAuthenticator,
)
from mcp.server.auth.provider import AccessToken, OAuthServerProvider, RefreshToken
from mcp.server.auth.provider import AccessToken, OAuthAuthorizationServerProvider, RefreshToken


class RevocationRequest(BaseModel):
Expand All @@ -35,7 +35,7 @@ class RevocationErrorResponse(BaseModel):

@dataclass
class RevocationHandler:
provider: OAuthServerProvider[Any, Any, Any]
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
client_authenticator: ClientAuthenticator

async def handle(self, request: Request) -> Response:
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/handlers/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
AuthenticationError,
ClientAuthenticator,
)
from mcp.server.auth.provider import OAuthServerProvider, TokenError, TokenErrorCode
from mcp.server.auth.provider import OAuthAuthorizationServerProvider, TokenError, TokenErrorCode
from mcp.shared.auth import OAuthToken


Expand Down Expand Up @@ -76,7 +76,7 @@ class TokenSuccessResponse(RootModel[OAuthToken]):

@dataclass
class TokenHandler:
provider: OAuthServerProvider[Any, Any, Any]
provider: OAuthAuthorizationServerProvider[Any, Any, Any]
client_authenticator: ClientAuthenticator

def response(self, obj: TokenSuccessResponse | TokenErrorResponse):
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/middleware/bearer_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from starlette.requests import HTTPConnection
from starlette.types import Receive, Scope, Send

from mcp.server.auth.provider import AccessToken, OAuthServerProvider
from mcp.server.auth.provider import AccessToken, OAuthAuthorizationServerProvider


class AuthenticatedUser(SimpleUser):
Expand All @@ -29,7 +29,7 @@ class BearerAuthBackend(AuthenticationBackend):

def __init__(
self,
provider: OAuthServerProvider[Any, Any, Any],
provider: OAuthAuthorizationServerProvider[Any, Any, Any],
):
self.provider = provider

Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/middleware/client_auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from typing import Any

from mcp.server.auth.provider import OAuthServerProvider
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
from mcp.shared.auth import OAuthClientInformationFull


Expand All @@ -21,7 +21,7 @@ class ClientAuthenticator:
logic is skipped.
"""

def __init__(self, provider: OAuthServerProvider[Any, Any, Any]):
def __init__(self, provider: OAuthAuthorizationServerProvider[Any, Any, Any]):
"""
Initialize the dependency.

Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/auth/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TokenError(Exception):
AccessTokenT = TypeVar("AccessTokenT", bound=AccessToken)


class OAuthServerProvider(
class OAuthAuthorizationServerProvider(
Protocol, Generic[AuthorizationCodeT, RefreshTokenT, AccessTokenT]
):
async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mcp.server.auth.handlers.revoke import RevocationHandler
from mcp.server.auth.handlers.token import TokenHandler
from mcp.server.auth.middleware.client_auth import ClientAuthenticator
from mcp.server.auth.provider import OAuthServerProvider
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
from mcp.server.auth.settings import ClientRegistrationOptions, RevocationOptions
from mcp.shared.auth import OAuthMetadata

Expand Down Expand Up @@ -65,7 +65,7 @@ def cors_middleware(


def create_auth_routes(
provider: OAuthServerProvider[Any, Any, Any],
provider: OAuthAuthorizationServerProvider[Any, Any, Any],
issuer_url: AnyHttpUrl,
service_documentation_url: AnyHttpUrl | None = None,
client_registration_options: ClientRegistrationOptions | None = None,
Expand Down
21 changes: 13 additions & 8 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
BearerAuthBackend,
RequireAuthMiddleware,
)
from mcp.server.auth.provider import OAuthServerProvider
from mcp.server.auth.provider import OAuthAuthorizationServerProvider
from mcp.server.auth.settings import (
AuthSettings,
)
Expand Down Expand Up @@ -128,7 +128,7 @@ def __init__(
self,
name: str | None = None,
instructions: str | None = None,
auth_provider: OAuthServerProvider[Any, Any, Any] | None = None,
auth_server_provider: OAuthAuthorizationServerProvider[Any, Any, Any] | None = None,
**settings: Any,
):
self.settings = Settings(**settings)
Expand All @@ -149,12 +149,17 @@ def __init__(
self._prompt_manager = PromptManager(
warn_on_duplicate_prompts=self.settings.warn_on_duplicate_prompts
)
if (self.settings.auth is not None) != (auth_provider is not None):
if (self.settings.auth is not None) != (auth_server_provider is not None):
# TODO: after we support separate authorization servers (see
# https://github.com/modelcontextprotocol/modelcontextprotocol/pull/284)
# we should validate that if auth is enabled, we have either an
# auth_server_provider to host our own authorization server,
# OR the URL of a 3rd party authorization server.
raise ValueError(
"settings.auth must be specified if and only if auth_provider "
"settings.auth must be specified if and only if auth_server_provider "
"is specified"
)
self._auth_provider = auth_provider
self._auth_server_provider = auth_server_provider
self._custom_starlette_routes: list[Route] = []
self.dependencies = self.settings.dependencies

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

# Add auth endpoints if auth provider is configured
if self._auth_provider:
if self._auth_server_provider:
assert self.settings.auth
from mcp.server.auth.routes import create_auth_routes

Expand All @@ -591,7 +596,7 @@ async def handle_sse(request: Request) -> EventSourceResponse:
Middleware(
AuthenticationMiddleware,
backend=BearerAuthBackend(
provider=self._auth_provider,
provider=self._auth_server_provider,
),
),
# Add the auth context middleware to store
Expand All @@ -600,7 +605,7 @@ async def handle_sse(request: Request) -> EventSourceResponse:
]
routes.extend(
create_auth_routes(
provider=self._auth_provider,
provider=self._auth_server_provider,
issuer_url=self.settings.auth.issuer_url,
service_documentation_url=self.settings.auth.service_documentation_url,
client_registration_options=self.settings.auth.client_registration_options,
Expand Down
20 changes: 10 additions & 10 deletions tests/server/auth/middleware/test_bearer_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from mcp.server.auth.provider import (
AccessToken,
OAuthServerProvider,
OAuthAuthorizationServerProvider,
)


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


def add_token_to_provider(
provider: OAuthServerProvider[Any, Any, Any], token: str, access_token: AccessToken
provider: OAuthAuthorizationServerProvider[Any, Any, Any], token: str, access_token: AccessToken
) -> None:
"""Helper function to add a token to a provider.

Expand Down Expand Up @@ -70,10 +70,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:


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


@pytest.fixture
Expand Down Expand Up @@ -114,7 +114,7 @@ class TestBearerAuthBackend:
"""Tests for the BearerAuthBackend class."""

async def test_no_auth_header(
self, mock_oauth_provider: OAuthServerProvider[Any, Any, Any]
self, mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any]
):
"""Test authentication with no Authorization header."""
backend = BearerAuthBackend(provider=mock_oauth_provider)
Expand All @@ -123,7 +123,7 @@ async def test_no_auth_header(
assert result is None

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

async def test_invalid_token(
self, mock_oauth_provider: OAuthServerProvider[Any, Any, Any]
self, mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any]
):
"""Test authentication with invalid token."""
backend = BearerAuthBackend(provider=mock_oauth_provider)
Expand All @@ -152,7 +152,7 @@ async def test_invalid_token(

async def test_expired_token(
self,
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
expired_access_token: AccessToken,
):
"""Test authentication with expired token."""
Expand All @@ -171,7 +171,7 @@ async def test_expired_token(

async def test_valid_token(
self,
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
valid_access_token: AccessToken,
):
"""Test authentication with valid token."""
Expand All @@ -195,7 +195,7 @@ async def test_valid_token(

async def test_token_without_expiry(
self,
mock_oauth_provider: OAuthServerProvider[Any, Any, Any],
mock_oauth_provider: OAuthAuthorizationServerProvider[Any, Any, Any],
no_expiry_access_token: AccessToken,
):
"""Test authentication with token that has no expiry."""
Expand Down
6 changes: 3 additions & 3 deletions tests/server/fastmcp/auth/test_auth_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
AccessToken,
AuthorizationCode,
AuthorizationParams,
OAuthServerProvider,
OAuthAuthorizationServerProvider,
RefreshToken,
construct_redirect_uri,
)
Expand All @@ -41,7 +41,7 @@


# Mock OAuth provider for testing
class MockOAuthProvider(OAuthServerProvider):
class MockOAuthProvider(OAuthAuthorizationServerProvider):
def __init__(self):
self.clients = {}
self.auth_codes = {} # code -> {client_id, code_challenge, redirect_uri}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ async def test_fastmcp_with_auth(
"""Test creating a FastMCP server with authentication."""
# Create FastMCP server with auth provider
mcp = FastMCP(
auth_provider=mock_oauth_provider,
auth_server_provider=mock_oauth_provider,
require_auth=True,
auth=AuthSettings(
issuer_url=AnyHttpUrl("https://auth.example.com"),
Expand Down
Loading