feat: add support for custom headers in SSE transport

This commit is contained in:
04cfb1ed 2025-05-02 17:16:18 -05:00 • committed by Camilo
parent 8665a08360
commit d392d7736d
3 changed files with 35 additions and 1 deletions

View file

@ -55,6 +55,25 @@ describe("MCPConnection", () => {
expect(conn.status).toBe("not-connected");
});
it("should create instance with SSE transport and custom headers", () => {
const options = {
name: "test-mcp",
id: "test-id",
transport: {
type: "sse" as const,
url: "http://test.com/events",
headers: {
"Authorization": "Bearer token123",
"X-Custom-Header": "custom-value"
}
},
};
const conn = new MCPConnection(options);
expect(conn).toBeInstanceOf(MCPConnection);
expect(conn.status).toBe("not-connected");
});
it("should throw on invalid transport type", () => {
const options = {
name: "test-mcp",

View file

@ -69,7 +69,19 @@ class MCPConnection {
case "websocket":
return new WebSocketClientTransport(new URL(options.transport.url));
case "sse":
return new SSEClientTransport(new URL(options.transport.url));
return new SSEClientTransport(new URL(options.transport.url), {
eventSourceInit: {
fetch: (input, init) =>
fetch(input, {
...init,
headers: {
...init?.headers,
...(options.transport.headers as Record<string, string> | undefined),
}
}),
},
requestInit: { headers: options.transport.headers }
});
default:
throw new Error(
`Unsupported transport type: ${(options.transport as any).type}`,

3
core/index.d.ts vendored
View file

@ -1114,16 +1114,19 @@ export interface StdioOptions {
command: string;
args: string[];
env?: Record<string, string>;
headers?: HeadersInit;
}
export interface WebSocketOptions {
type: "websocket";
url: string;
headers?: HeadersInit;
}
export interface SSEOptions {
type: "sse";
url: string;
headers?: HeadersInit;
}
export type TransportOptions = StdioOptions | WebSocketOptions | SSEOptions;