From cf459b1a0d526505940d7a7e53414032ecbb8b29 Mon Sep 17 00:00:00 2001 From: Jon Slominski Date: Thu, 10 Apr 2025 20:56:07 -0500 Subject: [PATCH] Ensure Authorization header is passed to SSE connections When using Bearer token authentication, the Authorization header was not being properly forwarded to SSE connections, causing 401 Unauthorized errors. This fix adds a custom EventSource initialization that explicitly includes the Authorization header in all SSE requests, allowing proper authentication with remote servers. --- src/lib/utils.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 30688af..fd60028 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -26,12 +26,16 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo transportToClient.onmessage = (message) => { // @ts-expect-error TODO log('[Local→Remote]', message.method || message.id) + // Log full outgoing request details + log('[Local→Remote Full]', JSON.stringify(message, null, 2)) transportToServer.send(message).catch(onServerError) } transportToServer.onmessage = (message) => { // @ts-expect-error TODO: fix this type log('[Remote→Local]', message.method || message.id) + // Log full response details + log('[Remote→Local Full]', JSON.stringify(message, null, 2)) transportToClient.send(message).catch(onClientError) } @@ -82,7 +86,25 @@ export async function connectToRemoteServer( ): Promise { log(`[${pid}] Connecting to remote server: ${serverUrl}`) const url = new URL(serverUrl) - const transport = new SSEClientTransport(url, { authProvider, requestInit: { headers } }) + + // Create transport with eventSourceInit to pass Authorization header if present + const eventSourceInit = { + fetch: (url: string | URL, init: RequestInit | undefined) => { + return fetch(url, { + ...init, + headers: { + ...init?.headers, + ...headers, + }, + }) + }, + } + + const transport = new SSEClientTransport(url, { + authProvider, + requestInit: { headers }, + eventSourceInit, + }) try { await transport.start()