fix: preserve custom paths in SSE endpoint URLs

When an SSE client connects with a custom path (e.g., /api/v1/custom/sse),
ensure the endpoint URL maintains the same base path structure but with
/messages instead of /sse. This fixes issues where custom endpoints were
getting collapsed to the root path.

Should fix issues reported in modelcontextprotocol/inspector#313 and #296
Tests demonstrating the issue were added in PR #439

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Basil Hosmer 2025-05-08 13:31:18 -04:00
commit 5a25fe3a6c
No known key found for this signature in database

View file

@ -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}`,