Skip to content

Commit 91f0afb

Browse files
committed
refactor
1 parent ebd4a70 commit 91f0afb

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

examples/clients/simple-auth-client/mcp_simple_auth_client/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Any
1616
from urllib.parse import parse_qs, urlparse
1717

18-
from mcp.client.oauth_auth import OAuthAuth
18+
from mcp.client.auth import OAuthAuth
1919
from mcp.client.session import ClientSession
2020
from mcp.client.streamable_http import streamablehttp_client
2121
from mcp.shared.auth import OAuthClientMetadata

src/mcp/client/oauth_auth.py renamed to src/mcp/client/auth.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Production-ready OAuth2 Authentication implementation for HTTPX using anyio.
33
44
This module provides a complete OAuth 2.0 authentication implementation
5-
that handles authorization code flow with PKCE, automatic token refresh and proper error handling.
5+
that handles authorization code flow with PKCE,
6+
automatic token refresh and proper error handling.
67
The callback server implementation should be handled by the calling code.
78
"""
89

@@ -12,13 +13,13 @@
1213
import string
1314
import time
1415
import webbrowser
15-
from abc import ABC, abstractmethod
1616
from collections.abc import AsyncGenerator, Awaitable, Callable
1717
from urllib.parse import urljoin
1818

1919
import anyio
2020
import httpx
2121

22+
from mcp.client.token_storage import InMemoryTokenStorage, TokenStorage
2223
from mcp.shared.auth import (
2324
OAuthClientInformationFull,
2425
OAuthClientMetadata,
@@ -28,50 +29,6 @@
2829
from mcp.types import LATEST_PROTOCOL_VERSION
2930

3031

31-
class TokenStorage(ABC):
32-
"""Abstract base class for token storage implementations."""
33-
34-
@abstractmethod
35-
async def get_tokens(self) -> OAuthToken | None:
36-
"""Get stored tokens."""
37-
pass
38-
39-
@abstractmethod
40-
async def set_tokens(self, tokens: OAuthToken) -> None:
41-
"""Store tokens."""
42-
pass
43-
44-
@abstractmethod
45-
async def get_client_info(self) -> OAuthClientInformationFull | None:
46-
"""Get stored client information."""
47-
pass
48-
49-
@abstractmethod
50-
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
51-
"""Store client information."""
52-
pass
53-
54-
55-
class InMemoryTokenStorage(TokenStorage):
56-
"""Simple in-memory token storage implementation."""
57-
58-
def __init__(self):
59-
self._tokens: OAuthToken | None = None
60-
self._client_info: OAuthClientInformationFull | None = None
61-
62-
async def get_tokens(self) -> OAuthToken | None:
63-
return self._tokens
64-
65-
async def set_tokens(self, tokens: OAuthToken) -> None:
66-
self._tokens = tokens
67-
68-
async def get_client_info(self) -> OAuthClientInformationFull | None:
69-
return self._client_info
70-
71-
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
72-
self._client_info = client_info
73-
74-
7532
async def discover_oauth_metadata(server_url: str) -> OAuthMetadata | None:
7633
"""
7734
Discovers OAuth metadata from the server's well-known endpoint.

src/mcp/client/token_storage.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Token storage implementations for OAuth authentication.
3+
4+
This module provides protocol and concrete implementations for storing
5+
OAuth tokens and client information.
6+
"""
7+
8+
from typing import Protocol
9+
10+
from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
11+
12+
13+
class TokenStorage(Protocol):
14+
"""Protocol for token storage implementations."""
15+
16+
async def get_tokens(self) -> OAuthToken | None:
17+
"""Get stored tokens."""
18+
...
19+
20+
async def set_tokens(self, tokens: OAuthToken) -> None:
21+
"""Store tokens."""
22+
...
23+
24+
async def get_client_info(self) -> OAuthClientInformationFull | None:
25+
"""Get stored client information."""
26+
...
27+
28+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
29+
"""Store client information."""
30+
...
31+
32+
33+
class InMemoryTokenStorage(TokenStorage):
34+
"""Simple in-memory token storage implementation."""
35+
36+
def __init__(self):
37+
self._tokens: OAuthToken | None = None
38+
self._client_info: OAuthClientInformationFull | None = None
39+
40+
async def get_tokens(self) -> OAuthToken | None:
41+
return self._tokens
42+
43+
async def set_tokens(self, tokens: OAuthToken) -> None:
44+
self._tokens = tokens
45+
46+
async def get_client_info(self) -> OAuthClientInformationFull | None:
47+
return self._client_info
48+
49+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
50+
self._client_info = client_info

0 commit comments

Comments
 (0)