diff --git a/core/context/mcp/MCPConnection.test.ts b/core/context/mcp/MCPConnection.test.ts index 4b5ccd2a0..ee32b44c6 100644 --- a/core/context/mcp/MCPConnection.test.ts +++ b/core/context/mcp/MCPConnection.test.ts @@ -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", diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 6db095de4..505d20fb1 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -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 | undefined), + } + }), + }, + requestInit: { headers: options.transport.headers } + }); default: throw new Error( `Unsupported transport type: ${(options.transport as any).type}`, diff --git a/core/index.d.ts b/core/index.d.ts index e781aa5a5..1f0460161 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1114,16 +1114,19 @@ export interface StdioOptions { command: string; args: string[]; env?: Record; + 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;