Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix consent form action for subpath mounting (#2382)

Jeremiah Lowin48fc8cbc9a5d
diff --git a/src/fastmcp/server/auth/oauth_proxy.py b/src/fastmcp/server/auth/oauth_proxy.py
index 9bfdd3595..a1452378b 100644
--- a/src/fastmcp/server/auth/oauth_proxy.py
+++ b/src/fastmcp/server/auth/oauth_proxy.py
@@ -309,10 +309,13 @@ def create_consent_html(
"""
# Build form with buttons
+ # Use empty action to submit to current URL (/consent or /mcp/consent)
+ # The POST handler is registered at the same path as GET
form = f"""
- <form id="consentForm" method="POST" action="/consent/submit">
+ <form id="consentForm" method="POST" action="">
<input type="hidden" name="txn_id" value="{txn_id}" />
<input type="hidden" name="csrf_token" value="{csrf_token}" />
+ <input type="hidden" name="submit" value="true" />
<div class="button-group">
<button type="submit" name="action" value="approve" class="btn-approve">Allow Access</button>
<button type="submit" name="action" value="deny" class="btn-deny">Deny</button>
@@ -1654,12 +1657,10 @@ class OAuthProxy(OAuthProvider):
)
# Add consent endpoints
- custom_routes.append(
- Route(path="/consent", endpoint=self._show_consent_page, methods=["GET"])
- )
+ # Handle both GET (show page) and POST (submit) at /consent
custom_routes.append(
Route(
- path="/consent/submit", endpoint=self._submit_consent, methods=["POST"]
+ path="/consent", endpoint=self._handle_consent, methods=["GET", "POST"]
)
)
@@ -1974,6 +1975,14 @@ class OAuthProxy(OAuthProvider):
separator = "&" if "?" in self._upstream_authorization_endpoint else "?"
return f"{self._upstream_authorization_endpoint}{separator}{urlencode(query_params)}"
+ async def _handle_consent(
+ self, request: Request
+ ) -> HTMLResponse | RedirectResponse:
+ """Handle consent page - dispatch to GET or POST handler based on method."""
+ if request.method == "POST":
+ return await self._submit_consent(request)
+ return await self._show_consent_page(request)
+
async def _show_consent_page(
self, request: Request
) -> HTMLResponse | RedirectResponse:
diff --git a/tests/integration_tests/auth/test_github_provider_integration.py b/tests/integration_tests/auth/test_github_provider_integration.py
index d003d5241..7623a7690 100644
--- a/tests/integration_tests/auth/test_github_provider_integration.py
+++ b/tests/integration_tests/auth/test_github_provider_integration.py
@@ -284,7 +284,7 @@ async def test_github_oauth_authorization_redirect(github_server: str):
# Step 4: Approve consent
approve_response = await http_client.post(
- f"{base_url}/consent/submit",
+ f"{base_url}/consent",
data={
"action": "approve",
"txn_id": txn_id,
diff --git a/tests/server/auth/test_oauth_consent_flow.py b/tests/server/auth/test_oauth_consent_flow.py
index 85840821b..a2a091af5 100644
--- a/tests/server/auth/test_oauth_consent_flow.py
+++ b/tests/server/auth/test_oauth_consent_flow.py
@@ -412,7 +412,7 @@ class TestCSRFProtection:
with TestClient(app) as test_client:
# Try to submit consent WITHOUT CSRF token
response = test_client.post(
- "/consent/submit",
+ "/consent",
data={"action": "approve", "txn_id": txn_id},
# No CSRF token!
follow_redirects=False,
@@ -571,7 +571,7 @@ class TestConsentSecurity:
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
- "/consent/submit",
+ "/consent",
data={"action": "deny", "txn_id": txn_id, "csrf_token": csrf},
follow_redirects=False,
)
@@ -602,7 +602,7 @@ class TestConsentSecurity:
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
- "/consent/submit",
+ "/consent",
data={"action": "approve", "txn_id": txn_id, "csrf_token": csrf},
follow_redirects=False,
)
@@ -644,7 +644,7 @@ class TestConsentSecurity:
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
- "/consent/submit",
+ "/consent",
data={
"action": "approve",
"txn_id": txn_id,