Commit ded69214 by Finley Ge Committed by GitHub

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

parent 98f1479d
...@@ -265,24 +265,10 @@ export class MCPClient { ...@@ -265,24 +265,10 @@ export class MCPClient {
try { try {
await this.client.connect( await this.client.connect(
new SSEClientTransport(new URL(this.url), { new SSEClientTransport(new URL(this.url), {
fetch: safeFetch, fetch: safeFetch,
requestInit: { requestInit: {
headers: this.headers 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', () => { ...@@ -495,6 +495,24 @@ describe('MCPClient', () => {
expect(result).toBe(client); 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 () => { it('should not fallback to SSE on a non-HTTP (e.g. network) error', async () => {
const mcpClient = new MCPClient(config); const mcpClient = new MCPClient(config);
const client = getPrivateClient(mcpClient); 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