Skip to content

Commit cdb373f

Browse files
ESnarkcrivetimihai
andauthored
fix: root redirect uses app_root_path with trailing slash (#1547)
- Changed from request.scope.get("root_path") to settings.app_root_path for consistency with other parts of the codebase - Added trailing slash (/admin/) to avoid 307 redirect - Removed unused request parameter from root_redirect function - Updated tests to verify correct redirect path with app_root_path Closes #1547 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
1 parent b811e52 commit cdb373f

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

mcpgateway/main.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5261,25 +5261,21 @@ async def cleanup_import_statuses(max_age_hours: int = 24, user=Depends(get_curr
52615261

52625262
# Redirect root path to admin UI
52635263
@app.get("/")
5264-
async def root_redirect(request: Request):
5264+
async def root_redirect():
52655265
"""
5266-
Redirects the root path ("/") to "/admin".
5266+
Redirects the root path ("/") to "/admin/".
52675267
52685268
Logs a debug message before redirecting.
52695269
5270-
Args:
5271-
request (Request): The incoming HTTP request (used only to build the
5272-
target URL via :pymeth:`starlette.requests.Request.url_for`).
5273-
52745270
Returns:
5275-
RedirectResponse: Redirects to /admin.
5271+
RedirectResponse: Redirects to /admin/.
52765272
52775273
Raises:
52785274
HTTPException: If there is an error during redirection.
52795275
"""
5280-
logger.debug("Redirecting root path to /admin")
5281-
root_path = request.scope.get("root_path", "")
5282-
return RedirectResponse(f"{root_path}/admin", status_code=303)
5276+
logger.debug("Redirecting root path to /admin/")
5277+
root_path = settings.app_root_path
5278+
return RedirectResponse(f"{root_path}/admin/", status_code=303)
52835279
# return RedirectResponse(request.url_for("admin_home"))
52845280

52855281
else:

tests/unit/mcpgateway/test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ def test_root_redirect(self, test_client):
328328

329329
# Check if UI is enabled
330330
if settings.mcpgateway_ui_enabled:
331-
# When UI is enabled, should redirect to admin
331+
# When UI is enabled, should redirect to admin with trailing slash
332332
assert response.status_code == 303
333-
assert response.headers["location"] == "/admin"
333+
assert response.headers["location"] == f"{settings.app_root_path}/admin/"
334334
else:
335335
# When UI is disabled, should return API info
336336
assert response.status_code == 200

tests/unit/mcpgateway/test_main_extended.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pytest
1919

2020
# First-Party
21+
from mcpgateway.config import settings
2122
from mcpgateway.main import app
2223

2324

@@ -186,9 +187,9 @@ def test_root_endpoint_conditional_behavior(self):
186187
client = TestClient(app)
187188
response = client.get("/", follow_redirects=False)
188189

189-
# Should redirect to /admin when UI is enabled
190+
# Should redirect to /admin/ when UI is enabled
190191
if response.status_code == 303:
191-
assert response.headers.get("location") == "/admin"
192+
assert response.headers.get("location") == f"{settings.app_root_path}/admin/"
192193
else:
193194
# Fallback behavior
194195
assert response.status_code == 200

0 commit comments

Comments
 (0)