@@ -250,8 +250,12 @@ export function useConnection({
250
250
} ;
251
251
252
252
const handleAuthError = async ( error : unknown ) => {
253
- if ( error instanceof SseError && error . code === 401 ) {
254
- // Create a new auth provider with the current server URL
253
+ const is401Error =
254
+ ( error instanceof SseError && error . code === 401 ) ||
255
+ ( error instanceof Error && error . message . includes ( '401' ) ) ||
256
+ ( error instanceof Error && error . message . includes ( 'Unauthorized' ) ) ;
257
+
258
+ if ( is401Error ) {
255
259
const serverAuthProvider = new InspectorOAuthClientProvider ( sseUrl ) ;
256
260
257
261
const result = await auth ( serverAuthProvider , { serverUrl : sseUrl } ) ;
@@ -330,7 +334,6 @@ export function useConnection({
330
334
mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /sse` ) ;
331
335
mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
332
336
transportOptions = {
333
- authProvider : serverAuthProvider ,
334
337
eventSourceInit : {
335
338
fetch : (
336
339
url : string | URL | globalThis . Request ,
@@ -347,7 +350,6 @@ export function useConnection({
347
350
mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /mcp` ) ;
348
351
mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
349
352
transportOptions = {
350
- authProvider : serverAuthProvider ,
351
353
eventSourceInit : {
352
354
fetch : (
353
355
url : string | URL | globalThis . Request ,
@@ -375,9 +377,9 @@ export function useConnection({
375
377
const clientTransport =
376
378
transportType === "streamable-http"
377
379
? new StreamableHTTPClientTransport ( mcpProxyServerUrl as URL , {
378
- sessionId : undefined ,
379
- ...transportOptions ,
380
- } )
380
+ sessionId : undefined ,
381
+ ...transportOptions ,
382
+ } )
381
383
: new SSEClientTransport ( mcpProxyServerUrl as URL , transportOptions ) ;
382
384
383
385
if ( onNotification ) {
@@ -425,12 +427,19 @@ export function useConnection({
425
427
`Failed to connect to MCP Server via the MCP Inspector Proxy: ${ mcpProxyServerUrl } :` ,
426
428
error ,
427
429
) ;
428
- const shouldRetry = await handleAuthError ( error ) ;
429
- if ( shouldRetry ) {
430
- return connect ( undefined , retryCount + 1 ) ;
431
- }
432
430
433
- if ( error instanceof SseError && error . code === 401 ) {
431
+ // Check for auth-related errors
432
+ const is401Error =
433
+ ( error instanceof SseError && error . code === 401 ) ||
434
+ ( error instanceof Error && error . message . includes ( '401' ) ) ||
435
+ ( error instanceof Error && error . message . includes ( 'Unauthorized' ) ) ;
436
+
437
+ if ( is401Error ) {
438
+ console . log ( "Detected 401 error, attempting OAuth flow" ) ;
439
+ const shouldRetry = await handleAuthError ( error ) ;
440
+ if ( shouldRetry ) {
441
+ return connect ( undefined , retryCount + 1 ) ;
442
+ }
434
443
// Don't set error state if we're about to redirect for auth
435
444
return ;
436
445
}
0 commit comments