Skip to content

fix: extra headers when they are a Headers object #571

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
31 changes: 31 additions & 0 deletions src/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,37 @@ describe("StreamableHTTPClientTransport", () => {
expect(global.fetch).toHaveBeenCalledTimes(2);
});

it("should always send specified custom headers (Headers class)", async () => {
const requestInit = {
headers: new Headers({
"X-Custom-Header": "CustomValue"
})
};
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.headers as Headers).get("x-custom-header")).toBe("CustomValue");

(requestInit.headers as Headers).set("X-Custom-Header","SecondCustomValue");

await transport.send({ jsonrpc: "2.0", method: "test", params: {} } as JSONRPCMessage);
expect((actualReqInit.headers as Headers).get("x-custom-header")).toBe("SecondCustomValue");

expect(global.fetch).toHaveBeenCalledTimes(2);
});

it("should have exponential backoff with configurable maxRetries", () => {
// This test verifies the maxRetries and backoff calculation directly
Expand Down
23 changes: 20 additions & 3 deletions src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ export class StreamableHTTPClientTransport implements Transport {
headers["mcp-session-id"] = this._sessionId;
}

return new Headers(
{ ...headers, ...this._requestInit?.headers }
);
const extraHeaders = this._normalizeHeaders(this._requestInit?.headers);

return new Headers({
...headers,
...extraHeaders,
});
}


Expand Down Expand Up @@ -242,6 +245,20 @@ export class StreamableHTTPClientTransport implements Transport {

}

private _normalizeHeaders(headers: HeadersInit | undefined): Record<string, string> {
if (!headers) return {};

if (headers instanceof Headers) {
return Object.fromEntries(headers.entries());
}

if (Array.isArray(headers)) {
return Object.fromEntries(headers);
}

return { ...headers as Record<string, string> };
}

/**
* Schedule a reconnection attempt with exponential backoff
*
Expand Down