|
| 1 | +from abc import ABC, abstractmethod |
1 | 2 | from dataclasses import dataclass |
2 | | -from typing import Any |
| 3 | +from typing import Any, Optional |
3 | 4 | from urllib.parse import urlencode |
4 | 5 |
|
5 | 6 | from starlette.requests import Request |
|
9 | 10 | from mcp.server.auth.provider import OAuthAuthorizationServerProvider |
10 | 11 |
|
11 | 12 |
|
| 13 | +class AbstractConsentHandler(ABC): |
| 14 | + """Abstract base class for handling OAuth consent operations.""" |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + async def handle(self, request: Request) -> Response: |
| 18 | + """Handle consent requests - both showing the form and processing submissions.""" |
| 19 | + pass |
| 20 | + |
| 21 | + @abstractmethod |
| 22 | + async def _show_consent_form(self, request: Request) -> Response: |
| 23 | + """Display the consent form to the user.""" |
| 24 | + pass |
| 25 | + |
| 26 | + @abstractmethod |
| 27 | + async def _process_consent(self, request: Request) -> Response: |
| 28 | + """Process the user's consent decision.""" |
| 29 | + pass |
| 30 | + |
| 31 | + |
12 | 32 | @dataclass |
13 | | -class ConsentHandler: |
| 33 | +class ConsentHandler(AbstractConsentHandler): |
14 | 34 | provider: OAuthAuthorizationServerProvider[Any, Any, Any] |
15 | 35 |
|
16 | 36 | async def handle(self, request: Request) -> Response: |
@@ -157,7 +177,7 @@ async def _show_consent_form(self, request: Request) -> HTMLResponse: |
157 | 177 | """ |
158 | 178 | return HTMLResponse(content=html_content) |
159 | 179 |
|
160 | | - async def _process_consent(self, request: Request) -> RedirectResponse: |
| 180 | + async def _process_consent(self, request: Request) -> RedirectResponse | HTMLResponse: |
161 | 181 | form_data = await request.form() |
162 | 182 | action = form_data.get("action") |
163 | 183 |
|
|
0 commit comments