Description
Describe the bug
When the MCP server, connected via SSE, returns an endpoint event with a data value that starts with / (e.g., /mcp/message?sessionId=...), the SDK appears to be using this absolute path directly without considering the base URL of the initial SSE connection. This results in the client attempting to access an incorrect URL.
To Reproduce
Steps to reproduce the behavior:
- Establish an SSE connection to an MCP server with a URL like: http://my.mcp-server.com/weather/sse.
- Observe the server sending an SSE event with event: endpoint and data: /mcp/message?sessionId=your_session_id.
- The client, using the Python MCP SDK, constructs the endpoint URL as http://my.mcp-server.com/mcp/message?sessionId=your_session_id.
- The expected behavior, based on the MCPEx specification (clients should use the same origin as the SSE connection and append the received path), is that the URL should be http://my.mcp-server.com/weather/mcp/message?sessionId=your_session_id.
Expected behavior
The expected URL should be http://my.mcp-server.com/weather/mcp/message?sessionId=your_session_id.
Possible Root Cause:
Relevant Code Snippet (from langchain-mcp-adapters/client.py - for context):
urllib.parse.urljoin treats paths starting with / as absolute paths, causing it to discard the path component of the base URL (http://my.mcp-server.com/weather/sse) and only use the hostname.
Suggested Solution
When the server returns an endpoint URL starting with /, the SDK should treat it as a path to be appended to the path of the initial SSE connection URL, rather than a completely new absolute path relative to the server root. This might involve checking if sse_data starts with / and adjusting the base URL used in the URL joining process accordingly.