Skip to content
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

fix(sse): preserve base path when resolving message endpoint from SSE #422

Closed
wants to merge 3 commits into from

Conversation

sterankin
Copy link

@sterankin sterankin commented Apr 3, 2025

Problem

When using the sse_client() transport, the server sends the endpoint as /messages. This is resolved using urljoin(url, sse.data), but this strips the full base path (e.g., /mcp/weather-alerts) and results in incorrect POSTs to the root /messages.

Solution

This patch updates the endpoint resolution to correctly derive uri_base from the provided SSE URL by removing /sse, and then joining the relative path properly:

uri_base = url.rsplit("/sse", 1)[0]
endpoint_url = urljoin(uri_base + "/", sse.data.lstrip("/"))

This ensures that if the client connects to:
https://example-mcp-server.com/mcp/weather-alerts/sse

The messages are correctly posted to:
https://example-mcp-server..com/mcp/weather-alerts/messages

instead of:
https://example-mcp-server..com/messages

Notes
This is backward-compatible and only affects the computed POST endpoint.

Patch tested successfully against MCP servers behind API gateways.

fix: preserve base path when joining /messages from SSE endpoint

Previous behavour, a MCP server at:
https://my-mcp-server.com/mcp/weather-alerts/sse

would fail to connect to messages as the endpoint would be called as:

https://my-mcp-server.com/messages

It should be:

https://my-mcp-server.com/mcp/weather-alerts/messages
Update sse.py to preserve url base
line length fix
@danilpavlov
Copy link

Hi, @sterankin! We are working on this problem in this PR

Fell free to join us.

@sterankin
Copy link
Author

Closing as duplicate

@sterankin sterankin closed this Apr 8, 2025
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.

None yet

2 participants