@@ -42,6 +42,7 @@ import {
42
42
getMCPProxyAddress ,
43
43
getMCPServerRequestMaxTotalTimeout ,
44
44
resetRequestTimeoutOnProgress ,
45
+ getMCPProxyAuthToken ,
45
46
} from "@/utils/configUtils" ;
46
47
import { getMCPServerRequestTimeout } from "@/utils/configUtils" ;
47
48
import { InspectorConfig } from "../configurationTypes" ;
@@ -242,7 +243,12 @@ export function useConnection({
242
243
const checkProxyHealth = async ( ) => {
243
244
try {
244
245
const proxyHealthUrl = new URL ( `${ getMCPProxyAddress ( config ) } /health` ) ;
245
- const proxyHealthResponse = await fetch ( proxyHealthUrl ) ;
246
+ const proxyAuthToken = getMCPProxyAuthToken ( config ) ;
247
+ const headers : HeadersInit = { } ;
248
+ if ( proxyAuthToken ) {
249
+ headers [ 'Authorization' ] = `Bearer ${ proxyAuthToken } ` ;
250
+ }
251
+ const proxyHealthResponse = await fetch ( proxyHealthUrl , { headers } ) ;
246
252
const proxyHealth = await proxyHealthResponse . json ( ) ;
247
253
if ( proxyHealth ?. status !== "ok" ) {
248
254
throw new Error ( "MCP Proxy Server is not healthy" ) ;
@@ -261,6 +267,13 @@ export function useConnection({
261
267
) ;
262
268
} ;
263
269
270
+ const isProxyAuthError = ( error : unknown ) : boolean => {
271
+ return (
272
+ error instanceof Error &&
273
+ error . message . includes ( "Authentication required. Use the session token" )
274
+ ) ;
275
+ } ;
276
+
264
277
const handleAuthError = async ( error : unknown ) => {
265
278
if ( is401Error ( error ) ) {
266
279
const serverAuthProvider = new InspectorOAuthClientProvider ( sseUrl ) ;
@@ -318,6 +331,13 @@ export function useConnection({
318
331
}
319
332
}
320
333
334
+ // Add proxy authentication
335
+ const proxyAuthToken = getMCPProxyAuthToken ( config ) ;
336
+ const proxyHeaders : HeadersInit = { } ;
337
+ if ( proxyAuthToken ) {
338
+ proxyHeaders [ 'Authorization' ] = `Bearer ${ proxyAuthToken } ` ;
339
+ }
340
+
321
341
// Create appropriate transport
322
342
let transportOptions :
323
343
| StreamableHTTPClientTransportOptions
@@ -336,10 +356,10 @@ export function useConnection({
336
356
fetch : (
337
357
url : string | URL | globalThis . Request ,
338
358
init : RequestInit | undefined ,
339
- ) => fetch ( url , { ...init , headers } ) ,
359
+ ) => fetch ( url , { ...init , headers : { ... headers , ... proxyHeaders } } ) ,
340
360
} ,
341
361
requestInit : {
342
- headers,
362
+ headers : { ... headers , ... proxyHeaders } ,
343
363
} ,
344
364
} ;
345
365
break ;
@@ -352,10 +372,10 @@ export function useConnection({
352
372
fetch : (
353
373
url : string | URL | globalThis . Request ,
354
374
init : RequestInit | undefined ,
355
- ) => fetch ( url , { ...init , headers } ) ,
375
+ ) => fetch ( url , { ...init , headers : { ... headers , ... proxyHeaders } } ) ,
356
376
} ,
357
377
requestInit : {
358
- headers,
378
+ headers : { ... headers , ... proxyHeaders } ,
359
379
} ,
360
380
} ;
361
381
break ;
@@ -368,10 +388,10 @@ export function useConnection({
368
388
fetch : (
369
389
url : string | URL | globalThis . Request ,
370
390
init : RequestInit | undefined ,
371
- ) => fetch ( url , { ...init , headers } ) ,
391
+ ) => fetch ( url , { ...init , headers : { ... headers , ... proxyHeaders } } ) ,
372
392
} ,
373
393
requestInit : {
374
- headers,
394
+ headers : { ... headers , ... proxyHeaders } ,
375
395
} ,
376
396
// TODO these should be configurable...
377
397
reconnectionOptions : {
@@ -447,6 +467,17 @@ export function useConnection({
447
467
error ,
448
468
) ;
449
469
470
+ // Check if it's a proxy auth error
471
+ if ( isProxyAuthError ( error ) ) {
472
+ toast ( {
473
+ title : "Proxy Authentication Required" ,
474
+ description : "Please enter the session token from the proxy server console in the Configuration settings." ,
475
+ variant : "destructive" ,
476
+ } ) ;
477
+ setConnectionStatus ( "error" ) ;
478
+ return ;
479
+ }
480
+
450
481
const shouldRetry = await handleAuthError ( error ) ;
451
482
if ( shouldRetry ) {
452
483
return connect ( undefined , retryCount + 1 ) ;
0 commit comments