Skip to content

fix sse endpoint path preservation #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/client/sse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("SSEClientTransport", () => {
await transport.send(message);

// Verify the POST request maintains the custom path
expect(lastServerRequest.url).toBe("/custom/path/messages");
expect(lastServerRequest.url).toBe("/custom/path/sse");
});

it("handles multiple levels of custom paths", async () => {
Expand All @@ -107,7 +107,7 @@ describe("SSEClientTransport", () => {
await transport.send(message);

// Verify the POST request maintains the full custom path
expect(lastServerRequest.url).toBe("/api/v1/custom/deep/path/messages");
expect(lastServerRequest.url).toBe("/api/v1/custom/deep/path/sse");
});

it("maintains custom path for SSE connection", async () => {
Expand All @@ -130,7 +130,7 @@ describe("SSEClientTransport", () => {
};

await transport.send(message);
expect(lastServerRequest.url).toBe("/custom/path/messages");
expect(lastServerRequest.url).toBe("/custom/path/sse");
});

it("establishes SSE connection and receives endpoint", async () => {
Expand Down
8 changes: 1 addition & 7 deletions src/client/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,7 @@ export class SSEClientTransport implements Transport {
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still might be incorrect, in the sense that the server can advertise any URI and expect the client to send POST messages there:

When a client connects, the server MUST send an endpoint event containing a URI for the client to use for sending messages. All subsequent client messages MUST be sent as HTTP POST requests to this endpoint.

spec link

I think the problem in modelcontextprotocol/inspector#313 et al is upstream of the client – specifically, the client should not be able to control the endpoint, but there's something in the inspector that's ignoring the correct endpoint (or the server transport is sending an incorrect endpoint value.)

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';
}
this._endpoint.pathname = this._url.pathname;

if (this._endpoint.origin !== this._url.origin) {
throw new Error(
Expand Down