@@ -250,8 +250,12 @@ export function useConnection({
250250 } ;
251251
252252 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 ) {
255259 const serverAuthProvider = new InspectorOAuthClientProvider ( sseUrl ) ;
256260
257261 const result = await auth ( serverAuthProvider , { serverUrl : sseUrl } ) ;
@@ -330,7 +334,6 @@ export function useConnection({
330334 mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /sse` ) ;
331335 mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
332336 transportOptions = {
333- authProvider : serverAuthProvider ,
334337 eventSourceInit : {
335338 fetch : (
336339 url : string | URL | globalThis . Request ,
@@ -347,7 +350,6 @@ export function useConnection({
347350 mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /mcp` ) ;
348351 mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
349352 transportOptions = {
350- authProvider : serverAuthProvider ,
351353 eventSourceInit : {
352354 fetch : (
353355 url : string | URL | globalThis . Request ,
@@ -375,9 +377,9 @@ export function useConnection({
375377 const clientTransport =
376378 transportType === "streamable-http"
377379 ? new StreamableHTTPClientTransport ( mcpProxyServerUrl as URL , {
378- sessionId : undefined ,
379- ...transportOptions ,
380- } )
380+ sessionId : undefined ,
381+ ...transportOptions ,
382+ } )
381383 : new SSEClientTransport ( mcpProxyServerUrl as URL , transportOptions ) ;
382384
383385 if ( onNotification ) {
@@ -425,12 +427,19 @@ export function useConnection({
425427 `Failed to connect to MCP Server via the MCP Inspector Proxy: ${ mcpProxyServerUrl } :` ,
426428 error ,
427429 ) ;
428- const shouldRetry = await handleAuthError ( error ) ;
429- if ( shouldRetry ) {
430- return connect ( undefined , retryCount + 1 ) ;
431- }
432430
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+ }
434443 // Don't set error state if we're about to redirect for auth
435444 return ;
436445 }
0 commit comments