Skip to content

Commit a2b6328

Browse files
authored
Merge pull request #5493 from 04cfb1ed/feature/add-headers-to-mcp
feat: add support for custom headers in SSE transport
2 parents 07fd72f + 18832e0 commit a2b6328

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

core/context/mcp/MCPConnection.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ describe("MCPConnection", () => {
5555
expect(conn.status).toBe("not-connected");
5656
});
5757

58+
it("should create instance with SSE transport and custom headers", () => {
59+
const options = {
60+
name: "test-mcp",
61+
id: "test-id",
62+
transport: {
63+
type: "sse" as const,
64+
url: "http://test.com/events",
65+
requestOptions: {
66+
headers: {
67+
"Authorization": "Bearer token123",
68+
"X-Custom-Header": "custom-value"
69+
}
70+
}
71+
},
72+
};
73+
74+
const conn = new MCPConnection(options);
75+
expect(conn).toBeInstanceOf(MCPConnection);
76+
expect(conn.status).toBe("not-connected");
77+
});
78+
5879
it("should throw on invalid transport type", () => {
5980
const options = {
6081
name: "test-mcp",

core/context/mcp/MCPConnection.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,19 @@ class MCPConnection {
6969
case "websocket":
7070
return new WebSocketClientTransport(new URL(options.transport.url));
7171
case "sse":
72-
return new SSEClientTransport(new URL(options.transport.url));
72+
return new SSEClientTransport(new URL(options.transport.url), {
73+
eventSourceInit: {
74+
fetch: (input, init) =>
75+
fetch(input, {
76+
...init,
77+
headers: {
78+
...init?.headers,
79+
...(options.transport.requestOptions?.headers as Record<string, string> | undefined),
80+
}
81+
}),
82+
},
83+
requestInit: { headers: options.transport.requestOptions?.headers }
84+
});
7385
default:
7486
throw new Error(
7587
`Unsupported transport type: ${(options.transport as any).type}`,

core/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1115,16 +1115,19 @@ export interface StdioOptions {
11151115
command: string;
11161116
args: string[];
11171117
env?: Record<string, string>;
1118+
requestOptions?: RequestOptions;
11181119
}
11191120

11201121
export interface WebSocketOptions {
11211122
type: "websocket";
11221123
url: string;
1124+
requestOptions?: RequestOptions;
11231125
}
11241126

11251127
export interface SSEOptions {
11261128
type: "sse";
11271129
url: string;
1130+
requestOptions?: RequestOptions;
11281131
}
11291132

11301133
export type TransportOptions = StdioOptions | WebSocketOptions | SSEOptions;

0 commit comments

Comments
 (0)