|
14 | 14 | from http.server import BaseHTTPRequestHandler, HTTPServer
|
15 | 15 | from typing import Any
|
16 | 16 | from urllib.parse import parse_qs, urlparse
|
| 17 | +import webbrowser |
17 | 18 |
|
18 |
| -from mcp.client.auth import OAuthAuth |
| 19 | +from mcp.client.auth import OAuthClientProvider, TokenStorage |
19 | 20 | from mcp.client.session import ClientSession
|
20 | 21 | 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 |
22 | 43 |
|
23 | 44 |
|
24 | 45 | class CallbackHandler(BaseHTTPRequestHandler):
|
@@ -145,14 +166,19 @@ async def callback_handler() -> tuple[str, str | None]:
|
145 | 166 | "scope": "read", # Default scope, will be updated
|
146 | 167 | }
|
147 | 168 |
|
| 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 | + |
148 | 174 | # Create OAuth authentication handler using the new interface
|
149 |
| - oauth_auth = OAuthAuth( |
| 175 | + oauth_auth = OAuthClientProvider( |
150 | 176 | server_url=self.server_url.replace("/mcp", ""),
|
151 | 177 | client_metadata=OAuthClientMetadata.model_validate(
|
152 | 178 | client_metadata_dict
|
153 | 179 | ),
|
154 |
| - storage=None, # Use in-memory storage |
155 |
| - redirect_handler=None, # Use default (open browser) |
| 180 | + storage=InMemoryTokenStorage(), |
| 181 | + redirect_handler=_default_redirect_handler, |
156 | 182 | callback_handler=callback_handler,
|
157 | 183 | )
|
158 | 184 |
|
|
0 commit comments