Skip to content

Commit 26393a7

Browse files
committed
Update to use Python 3.10 types
1 parent 1d4b540 commit 26393a7

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import logging
8-
from typing import Callable, Literal, Optional, Union
8+
from typing import Callable, Literal
99
from urllib.parse import urlencode, urlparse, urlunparse
1010

1111
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, RootModel, ValidationError
@@ -44,8 +44,8 @@ class AuthorizationRequest(BaseModel):
4444
code_challenge_method: Literal["S256"] = Field(
4545
"S256", description="PKCE code challenge method, must be S256"
4646
)
47-
state: Optional[str] = Field(None, description="Optional state parameter")
48-
scope: Optional[str] = Field(
47+
state: str | None = Field(None, description="Optional state parameter")
48+
scope: str | None = Field(
4949
None,
5050
description="Optional scope; if specified, should be "
5151
"a space-separated list of scope strings",
@@ -97,14 +97,14 @@ def validate_redirect_uri(
9797
class ErrorResponse(BaseModel):
9898
error: ErrorCode
9999
error_description: str
100-
error_uri: Optional[AnyUrl] = None
100+
error_uri: AnyUrl | None = None
101101
# must be set if provided in the request
102-
state: Optional[str]
102+
state: str | None = None
103103

104104

105105
def best_effort_extract_string(
106106
key: str, params: None | FormData | QueryParams
107-
) -> Optional[str]:
107+
) -> str | None:
108108
if params is None:
109109
return None
110110
value = params.get(key)
@@ -257,7 +257,7 @@ async def error_response(
257257

258258

259259
def create_error_redirect(
260-
redirect_uri: AnyUrl, error: Union[Exception, ErrorResponse]
260+
redirect_uri: AnyUrl, error: Exception | ErrorResponse
261261
) -> str:
262262
parsed_uri = urlparse(str(redirect_uri))
263263

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
Corresponds to TypeScript file: src/server/auth/handlers/metadata.ts
55
"""
66

7-
from typing import Any, Callable, Dict
7+
from typing import Any, Callable
88

99
from starlette.requests import Request
1010
from starlette.responses import JSONResponse, Response
1111

1212

13-
def create_metadata_handler(metadata: Dict[str, Any]) -> Callable:
13+
def create_metadata_handler(metadata: dict[str, Any]) -> Callable:
1414
"""
1515
Create a handler for OAuth 2.0 Authorization Server Metadata.
1616

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import base64
88
import hashlib
99
import time
10-
from typing import Annotated, Callable, Literal, Optional, Union
10+
from typing import Annotated, Callable, Literal
1111

1212
from pydantic import AnyHttpUrl, Field, RootModel, ValidationError
1313
from starlette.requests import Request
@@ -42,12 +42,12 @@ class RefreshTokenRequest(ClientAuthRequest):
4242
# See https://datatracker.ietf.org/doc/html/rfc6749#section-6
4343
grant_type: Literal["refresh_token"]
4444
refresh_token: str = Field(..., description="The refresh token")
45-
scope: Optional[str] = Field(None, description="Optional scope parameter")
45+
scope: str | None = Field(None, description="Optional scope parameter")
4646

4747

4848
class TokenRequest(RootModel):
4949
root: Annotated[
50-
Union[AuthorizationCodeRequest, RefreshTokenRequest],
50+
AuthorizationCodeRequest | RefreshTokenRequest,
5151
Field(discriminator="grant_type"),
5252
]
5353

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
"""
66

77
import time
8-
from typing import Optional
98

109
from pydantic import BaseModel
1110

12-
from mcp.server.auth.errors import (
13-
InvalidClientError,
14-
)
11+
from mcp.server.auth.errors import InvalidClientError
1512
from mcp.server.auth.provider import OAuthRegisteredClientsStore
1613
from mcp.shared.auth import OAuthClientInformationFull
1714

@@ -25,7 +22,7 @@ class ClientAuthRequest(BaseModel):
2522
"""
2623

2724
client_id: str
28-
client_secret: Optional[str] = None
25+
client_secret: str | None = None
2926

3027

3128
class ClientAuthenticator:

src/mcp/server/auth/provider.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Corresponds to TypeScript file: src/server/auth/provider.ts
55
"""
66

7-
from typing import List, Literal, Optional, Protocol
7+
from typing import Literal, Protocol
88
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
99

1010
from pydantic import AnyHttpUrl, BaseModel
@@ -23,8 +23,8 @@ class AuthorizationParams(BaseModel):
2323
Corresponds to AuthorizationParams in src/server/auth/provider.ts
2424
"""
2525

26-
state: Optional[str] = None
27-
scopes: Optional[List[str]] = None
26+
state: str | None = None
27+
scopes: list[str] | None = None
2828
code_challenge: str
2929
redirect_uri: AnyHttpUrl
3030

@@ -41,8 +41,8 @@ class AuthorizationCode(BaseModel):
4141
class RefreshToken(BaseModel):
4242
token: str
4343
client_id: str
44-
scopes: List[str]
45-
expires_at: Optional[int] = None
44+
scopes: list[str]
45+
expires_at: int | None = None
4646

4747

4848
class OAuthTokenRevocationRequest(BaseModel):
@@ -51,7 +51,7 @@ class OAuthTokenRevocationRequest(BaseModel):
5151
"""
5252

5353
token: str
54-
token_type_hint: Optional[Literal["access_token", "refresh_token"]] = None
54+
token_type_hint: Literal["access_token", "refresh_token"] | None = None
5555

5656

5757
class OAuthRegisteredClientsStore(Protocol):
@@ -61,7 +61,7 @@ class OAuthRegisteredClientsStore(Protocol):
6161
Corresponds to OAuthRegisteredClientsStore in src/server/auth/clients.ts
6262
"""
6363

64-
async def get_client(self, client_id: str) -> Optional[OAuthClientInformationFull]:
64+
async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:
6565
"""
6666
Retrieves client information by client ID.
6767
@@ -170,7 +170,7 @@ async def exchange_refresh_token(
170170
self,
171171
client: OAuthClientInformationFull,
172172
refresh_token: RefreshToken,
173-
scopes: List[str],
173+
scopes: list[str],
174174
) -> TokenSuccessResponse:
175175
"""
176176
Exchanges a refresh token for an access token.

src/mcp/server/auth/router.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from dataclasses import dataclass
8-
from typing import Any, Dict, Optional
8+
from typing import Any
99

1010
from pydantic import AnyUrl
1111
from starlette.routing import Route, Router
@@ -23,7 +23,7 @@
2323
@dataclass
2424
class ClientRegistrationOptions:
2525
enabled: bool = False
26-
client_secret_expiry_seconds: Optional[int] = None
26+
client_secret_expiry_seconds: int | None = None
2727

2828

2929
@dataclass
@@ -143,10 +143,10 @@ def create_auth_router(
143143

144144
def build_metadata(
145145
issuer_url: AnyUrl,
146-
service_documentation_url: Optional[AnyUrl],
146+
service_documentation_url: AnyUrl | None,
147147
client_registration_options: ClientRegistrationOptions,
148148
revocation_options: RevocationOptions,
149-
) -> Dict[str, Any]:
149+
) -> dict[str, Any]:
150150
issuer_url_str = str(issuer_url).rstrip("/")
151151
# Create metadata
152152
metadata = {

src/mcp/server/auth/types.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Corresponds to TypeScript file: src/server/auth/types.ts
55
"""
66

7-
from typing import List, Optional
8-
97
from pydantic import BaseModel
108

119

@@ -18,5 +16,5 @@ class AuthInfo(BaseModel):
1816

1917
token: str
2018
client_id: str
21-
scopes: List[str]
22-
expires_at: Optional[int] = None
19+
scopes: list[str]
20+
expires_at: int | None = None

0 commit comments

Comments
 (0)