Skip to content

fix(cli): propagate url_prefix to FastAPI root_path - #7071

Open
luissilva-mollie wants to merge 2 commits into
google:mainfrom
luissilva-mollie:fix/root-path-from-url-prefix
Open

fix(cli): propagate url_prefix to FastAPI root_path#7071
luissilva-mollie wants to merge 2 commits into
google:mainfrom
luissilva-mollie:fix/root-path-from-url-prefix

Conversation

@luissilva-mollie

Copy link
Copy Markdown

Fixes #7070

Problem

--url_prefix is documented as making generated URLs work correctly when adk web is mounted behind a reverse proxy under a path prefix, but it's currently only used to set the dev-ui's backendUrl and the /dev-ui/ redirect target. It's never passed to the FastAPI app as root_path, so /docs and /openapi.json (and any other Starlette-generated URL) are always generated relative to /, breaking them when the app is proxied under a prefix that gets stripped before reaching the app.

Fix

Derive root_path from url_prefix via urllib.parse.urlparse(url_prefix).path and pass it into FastAPI(...). This handles both forms url_prefix can take today: a bare path (e.g. /adk, per the CLI help text) and a full absolute URL (e.g. https://host/adk, which some deployments pass since the same value is reused for the frontend's absolute backendUrl).

Testing

Verified locally with a minimal FastAPI app that setting root_path causes /docs to reference /<prefix>/openapi.json instead of /openapi.json:

from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI(root_path="/agents/mcpolliegpt")
c = TestClient(app, base_url="http://testserver")
r = c.get("/docs")
# embedded openapi.json url is now "/agents/mcpolliegpt/openapi.json"

No existing tests cover url_prefix/root_path behavior in tests/unittests/cli/test_fast_api.py; happy to add coverage if maintainers can point me at the preferred test harness pattern for ApiServer.get_fast_api_app/run_server.

/docs and /openapi.json ignore --url_prefix because the FastAPI app is
constructed without root_path. url_prefix is currently only used to set
the dev-ui's backendUrl and the /dev-ui/ redirect, so when the app is
served behind a reverse proxy that strips the prefix, Swagger UI's
embedded reference to /openapi.json resolves against the bare domain
root instead of the proxied path, breaking /docs.

Derive root_path from url_prefix (accepting both a bare path like
'/adk' and a full absolute URL like 'https://host/adk') and pass it to
FastAPI so generated URLs are correctly prefixed.
Parametrized over: no prefix, bare path (with/without trailing
slash), and full absolute URL (with/without trailing slash), asserting
both app.root_path and that the /docs page's embedded openapi.json
reference is correctly prefixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adk web --url_prefix doesn't fix /docs and /openapi.json behind a reverse proxy

2 participants