@@ -39,9 +39,15 @@ function getTimestamp(): string {
3939}
4040
4141// Debug logging function
42- export async function debugLog ( serverUrlHash : string , message : string , ...args : any [ ] ) : Promise < void > {
42+ export async function debugLog ( message : string , ...args : any [ ] ) : Promise < void > {
4343 if ( ! DEBUG ) return ;
4444
45+ const serverUrlHash = global . currentServerUrlHash ;
46+ if ( ! serverUrlHash ) {
47+ console . error ( "[DEBUG LOG ERROR] global.currentServerUrlHash is not set. Cannot write debug log." ) ;
48+ return ;
49+ }
50+
4551 try {
4652 // Format with timestamp and PID
4753 const formattedMessage = `[${ getTimestamp ( ) } ][${ pid } ] ${ message } ` ;
@@ -72,7 +78,7 @@ export function log(str: string, ...rest: unknown[]) {
7278
7379 // If debug mode is on, also log to debug file
7480 if ( DEBUG && global . currentServerUrlHash ) {
75- debugLog ( global . currentServerUrlHash , str , ...rest ) . catch ( ( ) => { } ) ;
81+ debugLog ( str , ...rest ) . catch ( ( ) => { } ) ;
7682 }
7783}
7884
@@ -90,7 +96,7 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
9096 log ( '[Local→Remote]' , message . method || message . id )
9197
9298 if ( DEBUG ) {
93- debugLog ( global . currentServerUrlHash ! , 'Local → Remote message' , {
99+ debugLog ( 'Local → Remote message' , {
94100 method : message . method ,
95101 id : message . id ,
96102 params : message . params ? JSON . stringify ( message . params ) . substring ( 0 , 500 ) : undefined
@@ -103,7 +109,7 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
103109 log ( JSON . stringify ( message , null , 2 ) )
104110
105111 if ( DEBUG ) {
106- debugLog ( global . currentServerUrlHash ! , 'Initialize message with modified client info' , { clientInfo } ) . catch ( ( ) => { } )
112+ debugLog ( 'Initialize message with modified client info' , { clientInfo } ) . catch ( ( ) => { } )
107113 }
108114 }
109115
@@ -116,7 +122,7 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
116122 log ( '[Remote→Local]' , message . method || message . id )
117123
118124 if ( DEBUG ) {
119- debugLog ( global . currentServerUrlHash ! , 'Remote → Local message' , {
125+ debugLog ( 'Remote → Local message' , {
120126 method : message . method ,
121127 id : message . id ,
122128 result : message . result ? 'result-present' : undefined ,
@@ -133,7 +139,7 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
133139 }
134140
135141 transportToClientClosed = true
136- if ( DEBUG ) debugLog ( global . currentServerUrlHash ! , 'Local transport closed, closing remote transport' ) . catch ( ( ) => { } )
142+ if ( DEBUG ) debugLog ( 'Local transport closed, closing remote transport' ) . catch ( ( ) => { } )
137143 transportToServer . close ( ) . catch ( onServerError )
138144 }
139145
@@ -142,7 +148,7 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
142148 return
143149 }
144150 transportToServerClosed = true
145- if ( DEBUG ) debugLog ( global . currentServerUrlHash ! , 'Remote transport closed, closing local transport' ) . catch ( ( ) => { } )
151+ if ( DEBUG ) debugLog ( 'Remote transport closed, closing local transport' ) . catch ( ( ) => { } )
146152 transportToClient . close ( ) . catch ( onClientError )
147153 }
148154
@@ -151,12 +157,12 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
151157
152158 function onClientError ( error : Error ) {
153159 log ( 'Error from local client:' , error )
154- if ( DEBUG ) debugLog ( global . currentServerUrlHash ! , 'Error from local client' , { errorMessage : error . message , stack : error . stack } ) . catch ( ( ) => { } )
160+ if ( DEBUG ) debugLog ( 'Error from local client' , { errorMessage : error . message , stack : error . stack } ) . catch ( ( ) => { } )
155161 }
156162
157163 function onServerError ( error : Error ) {
158164 log ( 'Error from remote server:' , error )
159- if ( DEBUG ) debugLog ( global . currentServerUrlHash ! , 'Error from remote server' , { errorMessage : error . message , stack : error . stack } ) . catch ( ( ) => { } )
165+ if ( DEBUG ) debugLog ( 'Error from remote server' , { errorMessage : error . message , stack : error . stack } ) . catch ( ( ) => { } )
160166 }
161167}
162168
@@ -227,27 +233,27 @@ export async function connectToRemoteServer(
227233 } )
228234
229235 try {
230- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Attempting to connect to remote server' , { sseTransport } )
236+ if ( DEBUG ) await debugLog ( 'Attempting to connect to remote server' , { sseTransport } )
231237
232238 if ( client ) {
233- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Connecting client to transport' )
239+ if ( DEBUG ) await debugLog ( 'Connecting client to transport' )
234240 await client . connect ( transport )
235241 } else {
236- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Starting transport directly' )
242+ if ( DEBUG ) await debugLog ( 'Starting transport directly' )
237243 await transport . start ( )
238244 if ( ! sseTransport ) {
239245 // Extremely hacky, but we didn't actually send a request when calling transport.start() above, so we don't
240246 // know if we're even talking to an HTTP server. But if we forced that now we'd get an error later saying that
241247 // the client is already connected. So let's just create a one-off client to make a single request and figure
242248 // out if we're actually talking to an HTTP server or not.
243- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Creating test transport for HTTP-only connection test' )
249+ if ( DEBUG ) await debugLog ( 'Creating test transport for HTTP-only connection test' )
244250 const testTransport = new StreamableHTTPClientTransport ( url , { authProvider, requestInit : { headers } } )
245251 const testClient = new Client ( { name : 'mcp-remote-fallback-test' , version : '0.0.0' } , { capabilities : { } } )
246252 await testClient . connect ( testTransport )
247253 }
248254 }
249255 log ( `Connected to remote server using ${ transport . constructor . name } ` )
250- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , `Connected to remote server successfully` , { transportType : transport . constructor . name } )
256+ if ( DEBUG ) await debugLog ( `Connected to remote server successfully` , { transportType : transport . constructor . name } )
251257
252258 return transport
253259 } catch ( error ) {
@@ -287,57 +293,57 @@ export async function connectToRemoteServer(
287293 } else if ( error instanceof UnauthorizedError || ( error instanceof Error && error . message . includes ( 'Unauthorized' ) ) ) {
288294 log ( 'Authentication required. Initializing auth...' )
289295 if ( DEBUG ) {
290- await debugLog ( global . currentServerUrlHash ! , 'Authentication required, initializing auth process' , {
296+ await debugLog ( 'Authentication required, initializing auth process' , {
291297 errorMessage : error . message ,
292298 stack : error . stack
293299 } )
294300 }
295301
296302 // Initialize authentication on-demand
297- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Calling authInitializer to start auth flow' )
303+ if ( DEBUG ) await debugLog ( 'Calling authInitializer to start auth flow' )
298304 const { waitForAuthCode, skipBrowserAuth } = await authInitializer ( )
299305
300306 if ( skipBrowserAuth ) {
301307 log ( 'Authentication required but skipping browser auth - using shared auth' )
302- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Authentication required but skipping browser auth - using shared auth' )
308+ if ( DEBUG ) await debugLog ( 'Authentication required but skipping browser auth - using shared auth' )
303309 } else {
304310 log ( 'Authentication required. Waiting for authorization...' )
305- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Authentication required. Waiting for authorization...' )
311+ if ( DEBUG ) await debugLog ( 'Authentication required. Waiting for authorization...' )
306312 }
307313
308314 // Wait for the authorization code from the callback
309- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Waiting for auth code from callback server' )
315+ if ( DEBUG ) await debugLog ( 'Waiting for auth code from callback server' )
310316 const code = await waitForAuthCode ( )
311- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Received auth code from callback server' )
317+ if ( DEBUG ) await debugLog ( 'Received auth code from callback server' )
312318
313319 try {
314320 log ( 'Completing authorization...' )
315- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Completing authorization with transport.finishAuth' )
321+ if ( DEBUG ) await debugLog ( 'Completing authorization with transport.finishAuth' )
316322 await transport . finishAuth ( code )
317- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Authorization completed successfully' )
323+ if ( DEBUG ) await debugLog ( 'Authorization completed successfully' )
318324
319325 if ( recursionReasons . has ( REASON_AUTH_NEEDED ) ) {
320326 const errorMessage = `Already attempted reconnection for reason: ${ REASON_AUTH_NEEDED } . Giving up.`
321327 log ( errorMessage )
322- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Already attempted auth reconnection, giving up' , { recursionReasons : Array . from ( recursionReasons ) } )
328+ if ( DEBUG ) await debugLog ( 'Already attempted auth reconnection, giving up' , { recursionReasons : Array . from ( recursionReasons ) } )
323329 throw new Error ( errorMessage )
324330 }
325331
326332 // Track this reason for recursion
327333 recursionReasons . add ( REASON_AUTH_NEEDED )
328334 log ( `Recursively reconnecting for reason: ${ REASON_AUTH_NEEDED } ` )
329- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Recursively reconnecting after auth' , { recursionReasons : Array . from ( recursionReasons ) } )
335+ if ( DEBUG ) await debugLog ( 'Recursively reconnecting after auth' , { recursionReasons : Array . from ( recursionReasons ) } )
330336
331337 // Recursively call connectToRemoteServer with the updated recursion tracking
332338 return connectToRemoteServer ( client , serverUrl , authProvider , headers , authInitializer , transportStrategy , recursionReasons )
333339 } catch ( authError ) {
334340 log ( 'Authorization error:' , authError )
335- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Authorization error during finishAuth' , { errorMessage : authError . message , stack : authError . stack } )
341+ if ( DEBUG ) await debugLog ( 'Authorization error during finishAuth' , { errorMessage : authError . message , stack : authError . stack } )
336342 throw authError
337343 }
338344 } else {
339345 log ( 'Connection error:' , error )
340- if ( DEBUG ) await debugLog ( global . currentServerUrlHash ! , 'Connection error' , {
346+ if ( DEBUG ) await debugLog ( 'Connection error' , {
341347 errorMessage : error . message ,
342348 stack : error . stack ,
343349 transportType : transport . constructor . name
@@ -617,7 +623,7 @@ export async function parseCommandLineArgs(args: string[], usage: string) {
617623 global . currentServerUrlHash = serverUrlHash
618624
619625 if ( DEBUG ) {
620- debugLog ( serverUrlHash , `Starting mcp-remote with server URL: ${ serverUrl } ` ) . catch ( ( ) => { } )
626+ debugLog ( `Starting mcp-remote with server URL: ${ serverUrl } ` ) . catch ( ( ) => { } )
621627 }
622628
623629 const defaultPort = calculateDefaultPort ( serverUrlHash )
0 commit comments