Skip to content

Commit 0f34280

Browse files
committed
fix: pass headers and apiKey to HTTP/SSE/WebSocket transports
1 parent 4f8087f commit 0f34280

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

src/transports/factory.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,60 @@ export class TransportFactory {
2222
} as { command: string; args?: string[]; env?: Record<string, string>; cwd?: string };
2323
return new StdioClientTransport(stdioParams) as unknown as Transport;
2424
}
25-
case 'http':
25+
case 'http': {
26+
const httpHeaders: Record<string, string> = {};
27+
28+
// Add API key if provided
29+
if (transportConfig.apiKey) {
30+
httpHeaders['X-API-Key'] = transportConfig.apiKey;
31+
}
32+
33+
// Merge custom headers
34+
if (transportConfig.headers) {
35+
Object.assign(httpHeaders, transportConfig.headers);
36+
}
37+
2638
return new StreamableHTTPClientTransport(
27-
new URL(transportConfig.url)
39+
new URL(transportConfig.url),
40+
Object.keys(httpHeaders).length > 0 ? { headers: httpHeaders } : undefined
2841
) as unknown as Transport;
29-
case 'sse':
42+
}
43+
case 'sse': {
44+
const sseHeaders: Record<string, string> = {};
45+
46+
// Add API key if provided
47+
if (transportConfig.apiKey) {
48+
sseHeaders['X-API-Key'] = transportConfig.apiKey;
49+
}
50+
51+
// Merge custom headers
52+
if (transportConfig.headers) {
53+
Object.assign(sseHeaders, transportConfig.headers);
54+
}
55+
3056
return new SSEClientTransport(
31-
new URL(transportConfig.sseUrl)
57+
new URL(transportConfig.sseUrl),
58+
Object.keys(sseHeaders).length > 0 ? { headers: sseHeaders } : undefined
3259
) as unknown as Transport;
33-
case 'websocket':
60+
}
61+
case 'websocket': {
62+
const wsHeaders: Record<string, string> = {};
63+
64+
// Add API key if provided
65+
if (transportConfig.apiKey) {
66+
wsHeaders['X-API-Key'] = transportConfig.apiKey;
67+
}
68+
69+
// Merge custom headers
70+
if (transportConfig.headers) {
71+
Object.assign(wsHeaders, transportConfig.headers);
72+
}
73+
3474
return new WebSocketClientTransport(
35-
new URL(transportConfig.url)
75+
new URL(transportConfig.url),
76+
Object.keys(wsHeaders).length > 0 ? { headers: wsHeaders } : undefined
3677
) as unknown as Transport;
78+
}
3779
default:
3880
throw new Error(`Unsupported transport type: ${(transportConfig as { type: string }).type}`);
3981
}

0 commit comments

Comments
 (0)