Skip to content

fix: client initial GET sse include requestInit (modelcontextprotocol#895) #896

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,30 @@ describe("StreamableHTTPClientTransport", () => {
expect(global.fetch).toHaveBeenCalledTimes(2);
});

it("should always include requestInit options for initial connection in SSE", async () => {
const requestInit: RequestInit = {
credentials: 'include'
};

transport = new StreamableHTTPClientTransport(new URL("http://localhost:1234/mcp"), {
requestInit: requestInit
});

let actualReqInit: RequestInit = {};

((global.fetch as jest.Mock)).mockImplementation(
async (_url, reqInit) => {
actualReqInit = reqInit;
return new Response(null, { status: 200, headers: { "content-type": "text/event-stream" } });
}
);

await transport.start();

await transport["_startOrAuthSse"]({});
expect(actualReqInit.credentials).toBe("include");
});

it("should always send specified custom headers (Headers class)", async () => {
const requestInit = {
headers: new Headers({
Expand Down
7 changes: 4 additions & 3 deletions src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export class StreamableHTTPClientTransport implements Transport {
}

const response = await (this._fetch ?? fetch)(this._url, {
...this._requestInit,
method: "GET",
headers,
signal: this._abortController?.signal,
Expand Down Expand Up @@ -301,7 +302,7 @@ export class StreamableHTTPClientTransport implements Transport {
}

private _handleSseStream(
stream: ReadableStream<Uint8Array> | null,
stream: ReadableStream<Uint8Array> | null,
options: StartSSEOptions,
isReconnectable: boolean,
): void {
Expand Down Expand Up @@ -352,8 +353,8 @@ export class StreamableHTTPClientTransport implements Transport {

// Attempt to reconnect if the stream disconnects unexpectedly and we aren't closing
if (
isReconnectable &&
this._abortController &&
isReconnectable &&
this._abortController &&
!this._abortController.signal.aborted
) {
// Use the exponential backoff reconnection strategy
Expand Down