Skip to content

feat: add support for custom headers in SSE transport #5493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/context/mcp/MCPConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ 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",
requestOptions: {
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",
Expand Down
14 changes: 13 additions & 1 deletion core/context/mcp/MCPConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.requestOptions?.headers as Record<string, string> | undefined),
}
}),
},
requestInit: { headers: options.transport.requestOptions?.headers }
});
default:
throw new Error(
`Unsupported transport type: ${(options.transport as any).type}`,
Expand Down
3 changes: 3 additions & 0 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,16 +1114,19 @@ export interface StdioOptions {
command: string;
args: string[];
env?: Record<string, string>;
requestOptions?: RequestOptions;
}

export interface WebSocketOptions {
type: "websocket";
url: string;
requestOptions?: RequestOptions;
}

export interface SSEOptions {
type: "sse";
url: string;
requestOptions?: RequestOptions;
}

export type TransportOptions = StdioOptions | WebSocketOptions | SSEOptions;
Expand Down