Skip to content

Commit 8f04112

Browse files
committed
refactor
1 parent 91f0afb commit 8f04112

File tree

3 files changed

+172
-204
lines changed

3 files changed

+172
-204
lines changed

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,32 @@
1414
from http.server import BaseHTTPRequestHandler, HTTPServer
1515
from typing import Any
1616
from urllib.parse import parse_qs, urlparse
17+
import webbrowser
1718

18-
from mcp.client.auth import OAuthAuth
19+
from mcp.client.auth import OAuthClientProvider, TokenStorage
1920
from mcp.client.session import ClientSession
2021
from mcp.client.streamable_http import streamablehttp_client
21-
from mcp.shared.auth import OAuthClientMetadata
22+
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
23+
24+
25+
class InMemoryTokenStorage(TokenStorage):
26+
"""Simple in-memory token storage implementation."""
27+
28+
def __init__(self):
29+
self._tokens: OAuthToken | None = None
30+
self._client_info: OAuthClientInformationFull | None = None
31+
32+
async def get_tokens(self) -> OAuthToken | None:
33+
return self._tokens
34+
35+
async def set_tokens(self, tokens: OAuthToken) -> None:
36+
self._tokens = tokens
37+
38+
async def get_client_info(self) -> OAuthClientInformationFull | None:
39+
return self._client_info
40+
41+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
42+
self._client_info = client_info
2243

2344

2445
class CallbackHandler(BaseHTTPRequestHandler):
@@ -145,14 +166,19 @@ async def callback_handler() -> tuple[str, str | None]:
145166
"scope": "read", # Default scope, will be updated
146167
}
147168

169+
async def _default_redirect_handler(authorization_url: str) -> None:
170+
"""Default redirect handler that opens the URL in a browser."""
171+
print(f"Opening browser for authorization: {authorization_url}")
172+
webbrowser.open(authorization_url)
173+
148174
# Create OAuth authentication handler using the new interface
149-
oauth_auth = OAuthAuth(
175+
oauth_auth = OAuthClientProvider(
150176
server_url=self.server_url.replace("/mcp", ""),
151177
client_metadata=OAuthClientMetadata.model_validate(
152178
client_metadata_dict
153179
),
154-
storage=None, # Use in-memory storage
155-
redirect_handler=None, # Use default (open browser)
180+
storage=InMemoryTokenStorage(),
181+
redirect_handler=_default_redirect_handler,
156182
callback_handler=callback_handler,
157183
)
158184

0 commit comments

Comments
 (0)