fix: preserve API gateway path prefix in SSE client URL resolution#2243
Open
Varun6578 wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: preserve API gateway path prefix in SSE client URL resolution#2243Varun6578 wants to merge 2 commits intomodelcontextprotocol:mainfrom
Varun6578 wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Fixes modelcontextprotocol#795 When an MCP server sits behind a reverse proxy or API gateway that adds a path prefix, urljoin drops the prefix for absolute endpoint paths (starting with '/'). Add _resolve_endpoint_url() that detects gateway prefixes by finding where the endpoint's first path segment appears in the base URL path. Everything before that match is preserved as the prefix. Example: base_url: https://host/gateway/v1/sse endpoint: /v1/messages/?session_id=abc before: https://host/v1/messages/?session_id=abc (prefix lost) after: https://host/gateway/v1/messages/?session_id=abc Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Reported-by: lizzzcai <https://github.com/lizzzcai>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #795
When an MCP server sits behind a reverse proxy or API gateway that adds a path prefix (e.g.,
/gateway), the server's endpoint events contain paths without that prefix. The SSE client previously usedurljoin()which drops the base URL's path prefix for absolute paths (starting with/), causing incorrect routing through the gateway.Changes
src/mcp/client/sse.py:_resolve_endpoint_url()that detects and preserves reverse proxy/API gateway path prefixesurljoin(url, sse.data)with_resolve_endpoint_url(url, sse.data)on the endpoint event handlertests/shared/test_sse.py:_resolve_endpoint_url()covering:/org/team/v1/sse)How it works
The function detects gateway prefixes by finding where the endpoint's first path segment appears in the base URL path. Everything before that match is the prefix to preserve.