diff --git a/src/client/sse.ts b/src/client/sse.ts index 5e9f0cf0..1e481773 100644 --- a/src/client/sse.ts +++ b/src/client/sse.ts @@ -143,7 +143,18 @@ export class SSEClientTransport implements Transport { const messageEvent = event as MessageEvent; try { + // Use the original URL as the base to resolve the received endpoint this._endpoint = new URL(messageEvent.data, this._url); + + // If the original URL had a custom path, preserve it in the endpoint URL + const originalPath = this._url.pathname; + if (originalPath && originalPath !== '/' && originalPath !== '/sse') { + // Extract the base path from the original URL (everything before the /sse suffix) + const basePath = originalPath.replace(/\/sse$/, ''); + // The endpoint should use the same base path but with /messages instead of /sse + this._endpoint.pathname = basePath + '/messages'; + } + if (this._endpoint.origin !== this._url.origin) { throw new Error( `Endpoint origin does not match connection origin: ${this._endpoint.origin}`,