-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add root_path support to properly route subpaths for SSE and streamable-http endpoints when behind Nginx or other HTTP reverse proxies #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cc @ihrpr |
…t-oidc-discovery feat: enhance auth server discovery with OAuth2 and OpenID metadata support
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're running into this issue as well. This seems an elegant fix!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our case, we'd want this to be provided in the FastMCP
initializer, as well. Can we expose that there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @LiangYang666 thank you for this contribution! And apologies for the time it took to get back to this.
There've been quite a few changes to FastMCP
since this PR and it looks like FastMCP.__init__()
now supports an argument mount_path="/"
- I wonder if that potentially already addresses your use case here?
See:
python-sdk/src/mcp/server/fastmcp/server.py
Line 135 in c47c767
mount_path: str = "/", |
@LiangYang666 @lukehsiao keen to figure out if this may already support your use cases or where specifically it falls short. If it doesn't it would be excellent to create a test case that demonstrates where it falls short + ensure we stay backwards compatible.
@felixweinberger, I confirmed that mount_path does NOT address the use case. In fact, I don't see any difference in behavior. However, I did find a different workaround. In my particular case, I'm running FastMCP mounted to a FastAPI server, that is served behind a proxy, which strips prefixes. It turns out FastAPI has a bug, which, if I workaround that, also seems to resolve the issue here. In this setting, FastAPI docs instruct us to provide
This is wrong. It is NOT equivalent. There is a very old PR to address this: fastapi/fastapi#11160 Long story short, switching to providing |
Thank you for reporting back and the additional details on your workaround! Given this sounds like the workaround needed to address a FastAPI specific issue, I'm going to close this PR for now. If you find yourself needing this feature in future for some other purpose, happy to look at a resubmission. |
This PR adds support for
root_path
in subpath routing of SSE andstreamable-http endpoints when using Nginx or other HTTP middleware as a reverse proxy.Motivation and Context
When deploying applications behind a reverse proxy (e.g., Nginx), it's common to route requests via subpaths like
/server1/
or/server2/
. Without proper handling of these subpaths, SSE and streamable-http endpoints may fail to receive correctly routed requests.This issue was originally reported in #242
Similar functionality exists in other web applications:
How Has This Been Tested?
Yes, this has been tested and running stably in production environments for several days. Both sse and streamable-http endpoints have been verified to work correctly under subpaths behind Nginx.
Breaking Changes
No breaking changes are introduced. Users do not need to update their code or configuration files. This is an optional enhancement that activates only when
root_path
is provided.Types of changes
Checklist
Additional context
This change ensures that the application respects the
root_path
setting for streaming endpoints by utilizing Uvicorn’s built-in support for it. It does not introduce any custom routing logic but instead makes sure that streaming responses behave consistently with regular HTTP responses under subpath deployments.