Skip to content

Commit b42b35a

Browse files
author
eyeonyou
committed
fix(): SSE client handling of nested path URLs
Fix issue with SSE client failing when connecting to servers with nested paths (e.g., http://domain/path1/path2/sse). Previously, the client worked only with simple URLs like http://domain/sse. The fix extracts the base path segment between domain and /sse endpoint using regex, then correctly constructs the response URL by preserving the path structure. This ensures proper communication with MCP servers hosted under non-root paths. Without this fix, clients couldn't connect to SSE servers deployed under nested paths, as the endpoint URLs for posting messages were constructed incorrectly.
1 parent 6d66ab8 commit b42b35a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/mcp/client/sse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from contextlib import asynccontextmanager
33
from typing import Any
44
from urllib.parse import urljoin, urlparse
5+
import re
56

67
import anyio
78
import httpx
@@ -61,7 +62,9 @@ async def sse_reader(
6162
logger.debug(f"Received SSE event: {sse.event}")
6263
match sse.event:
6364
case "endpoint":
64-
endpoint_url = urljoin(url, sse.data)
65+
base_path = re.search(r"https?://[^/]+/(.+)/sse$", url)
66+
base_path = base_path.group(1) if base_path else ""
67+
endpoint_url = urljoin(url, base_path + sse.data)
6568
logger.info(
6669
f"Received endpoint URL: {endpoint_url}"
6770
)

0 commit comments

Comments
 (0)