Thank you Claude
This commit is contained in:
parent
9904603168
commit
65e55c3823
1 changed files with 46 additions and 27 deletions
|
@ -471,13 +471,6 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discover OAuth metadata if not already discovered
|
|
||||||
if (!metadataRef.current) {
|
|
||||||
addLog('info', 'Discovering OAuth metadata...')
|
|
||||||
metadataRef.current = await discoverOAuthMetadata(url)
|
|
||||||
addLog('debug', `OAuth metadata: ${metadataRef.current ? 'Found' : 'Not available'}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create MCP client
|
// Create MCP client
|
||||||
clientRef.current = new Client(
|
clientRef.current = new Client(
|
||||||
{
|
{
|
||||||
|
@ -491,10 +484,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set up auth flow - check if we have tokens
|
// Create SSE transport - try connecting without auth first
|
||||||
const tokens = await authProviderRef.current.tokens()
|
|
||||||
|
|
||||||
// Create SSE transport
|
|
||||||
setState('connecting')
|
setState('connecting')
|
||||||
addLog('info', 'Creating transport...')
|
addLog('info', 'Creating transport...')
|
||||||
|
|
||||||
|
@ -514,13 +504,8 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
addLog('error', `Transport error: ${err.message}`)
|
addLog('error', `Transport error: ${err.message}`)
|
||||||
|
|
||||||
if (err.message.includes('Unauthorized')) {
|
if (err.message.includes('Unauthorized')) {
|
||||||
setState('authenticating')
|
// Only discover OAuth metadata and authenticate if we get a 401
|
||||||
handleAuthentication().catch((authErr) => {
|
discoverOAuthAndAuthenticate(err)
|
||||||
addLog('error', `Authentication error: ${authErr.message}`)
|
|
||||||
setState('failed')
|
|
||||||
setError(`Authentication failed: ${authErr.message}`)
|
|
||||||
connectingRef.current = false
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
setState('failed')
|
setState('failed')
|
||||||
setError(`Connection error: ${err.message}`)
|
setError(`Connection error: ${err.message}`)
|
||||||
|
@ -540,7 +525,38 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect transport
|
// Helper function to handle OAuth discovery and authentication
|
||||||
|
const discoverOAuthAndAuthenticate = async (error: Error) => {
|
||||||
|
try {
|
||||||
|
// Discover OAuth metadata now that we know we need it
|
||||||
|
if (!metadataRef.current) {
|
||||||
|
addLog('info', 'Discovering OAuth metadata...')
|
||||||
|
metadataRef.current = await discoverOAuthMetadata(url)
|
||||||
|
addLog('debug', `OAuth metadata: ${metadataRef.current ? 'Found' : 'Not available'}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If metadata is found, start auth flow
|
||||||
|
if (metadataRef.current) {
|
||||||
|
setState('authenticating')
|
||||||
|
// Start authentication process
|
||||||
|
await handleAuthentication()
|
||||||
|
// After successful auth, retry connection
|
||||||
|
return connect()
|
||||||
|
} else {
|
||||||
|
// No OAuth metadata available
|
||||||
|
setState('failed')
|
||||||
|
setError(`Authentication required but no OAuth metadata found: ${error.message}`)
|
||||||
|
connectingRef.current = false
|
||||||
|
}
|
||||||
|
} catch (oauthErr) {
|
||||||
|
addLog('error', `OAuth discovery error: ${oauthErr instanceof Error ? oauthErr.message : String(oauthErr)}`)
|
||||||
|
setState('failed')
|
||||||
|
setError(`Authentication setup failed: ${oauthErr instanceof Error ? oauthErr.message : String(oauthErr)}`)
|
||||||
|
connectingRef.current = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try connecting transport first without OAuth discovery
|
||||||
try {
|
try {
|
||||||
addLog('info', 'Starting transport...')
|
addLog('info', 'Starting transport...')
|
||||||
// await transportRef.current.start()
|
// await transportRef.current.start()
|
||||||
|
@ -548,11 +564,8 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
addLog('error', `Transport start error: ${err instanceof Error ? err.message : String(err)}`)
|
addLog('error', `Transport start error: ${err instanceof Error ? err.message : String(err)}`)
|
||||||
|
|
||||||
if (err instanceof Error && err.message.includes('Unauthorized')) {
|
if (err instanceof Error && err.message.includes('Unauthorized')) {
|
||||||
setState('authenticating')
|
// Only discover OAuth and authenticate if we get a 401
|
||||||
// Start authentication process
|
await discoverOAuthAndAuthenticate(err)
|
||||||
await handleAuthentication()
|
|
||||||
// After successful auth, retry connection
|
|
||||||
return connect()
|
|
||||||
} else {
|
} else {
|
||||||
setState('failed')
|
setState('failed')
|
||||||
setError(`Connection error: ${err instanceof Error ? err.message : String(err)}`)
|
setError(`Connection error: ${err instanceof Error ? err.message : String(err)}`)
|
||||||
|
@ -586,9 +599,15 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
}
|
}
|
||||||
} catch (connectErr) {
|
} catch (connectErr) {
|
||||||
addLog('error', `Client connect error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
addLog('error', `Client connect error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
||||||
setState('failed')
|
|
||||||
setError(`Connection error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
if (connectErr instanceof Error && connectErr.message.includes('Unauthorized')) {
|
||||||
connectingRef.current = false
|
// Only discover OAuth and authenticate if we get a 401
|
||||||
|
await discoverOAuthAndAuthenticate(connectErr)
|
||||||
|
} else {
|
||||||
|
setState('failed')
|
||||||
|
setError(`Connection error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
||||||
|
connectingRef.current = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
addLog('error', `Unexpected error: ${err instanceof Error ? err.message : String(err)}`)
|
addLog('error', `Unexpected error: ${err instanceof Error ? err.message : String(err)}`)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue