Skip to content

Python MCP SDK: Incorrect handling of absolute paths in SSE endpoint URLs leads to incorrect URL construction. #585

Open
@footuser

Description

@footuser

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:

  1. Establish an SSE connection to an MCP server with a URL like: http://my.mcp-server.com/weather/sse.
  2. Observe the server sending an SSE event with event: endpoint and data: /mcp/message?sessionId=your_session_id.
  3. The client, using the Python MCP SDK, constructs the endpoint URL as http://my.mcp-server.com/mcp/message?sessionId=your_session_id.
  4. 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):

Image

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.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions