Commit ded69214 by Finley Ge Committed by GitHub

fix(mcp): avoid duplicating headers in SSE fallback (#7447)

parent 98f1479d
......@@ -269,20 +269,6 @@ export class MCPClient {
fetch: safeFetch,
requestInit: {
headers: this.headers
},
eventSourceInit: {
fetch: (url, init) => {
const mergedHeaders = {
...this.headers
};
Object.assign(mergedHeaders, headersInitToRecord(init?.headers));
return safeFetch(url, {
...init,
headers: mergedHeaders
});
}
}
})
);
......
......@@ -495,6 +495,24 @@ describe('MCPClient', () => {
expect(result).toBe(client);
});
it('should pass custom headers once to the SSE fallback transport', async () => {
const mcpClient = new MCPClient(config);
const client = getPrivateClient(mcpClient);
client.connect = vi
.fn()
.mockRejectedValueOnce(new StreamableHTTPError(405, 'Method Not Allowed'))
.mockResolvedValueOnce(undefined);
await (mcpClient as any).getConnection();
const sseTransport = client.connect.mock.calls[1][0] as {
_requestInit?: RequestInit;
_eventSourceInit?: EventSourceInit;
};
expect(sseTransport._requestInit?.headers).toEqual(config.headers);
expect(sseTransport._eventSourceInit).toBeUndefined();
});
it('should not fallback to SSE on a non-HTTP (e.g. network) error', async () => {
const mcpClient = new MCPClient(config);
const client = getPrivateClient(mcpClient);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment