fixed the effect looping, can call an MCP function without auth

This commit is contained in:
Glen Maddern 2025-05-06 15:06:35 +10:00
commit d9454bcdbf
2 changed files with 350 additions and 337 deletions

View file

@ -4,8 +4,6 @@ import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
// Assuming StoredState is defined in ./types.js and includes fields for provider options // Assuming StoredState is defined in ./types.js and includes fields for provider options
import { StoredState } from './types.js'; // Adjust path if necessary import { StoredState } from './types.js'; // Adjust path if necessary
/** /**
* Browser-compatible OAuth client provider for MCP using localStorage. * Browser-compatible OAuth client provider for MCP using localStorage.
*/ */

View file

@ -1,18 +1,16 @@
// useMcp.ts // useMcp.ts
import { CallToolResultSchema, JSONRPCMessage, ListToolsResultSchema, Tool } from '@modelcontextprotocol/sdk/types.js' import { CallToolResultSchema, JSONRPCMessage, ListToolsResultSchema, Tool } from '@modelcontextprotocol/sdk/types.js';
import { useCallback, useEffect, useRef, useState } from 'react' import { useCallback, useEffect, useRef, useState } from 'react';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js' import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js' import { Client } from '@modelcontextprotocol/sdk/client/index.js';
// Import the main auth function and error type import { auth, UnauthorizedError, OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
import { auth, UnauthorizedError, OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js' import { BrowserOAuthClientProvider } from '../auth/browser-provider.js'; // Adjust path
// Keep BrowserOAuthClientProvider import { assert } from '../utils/assert.js'; // Adjust path
import { BrowserOAuthClientProvider } from '../auth/browser-provider.js' // Adjust path import type { UseMcpOptions, UseMcpResult } from './types.js'; // Adjust path
import { assert } from '../utils/assert.js' // Adjust path
import type { UseMcpOptions, UseMcpResult } from './types.js' // Adjust path
const DEFAULT_RECONNECT_DELAY = 3000 const DEFAULT_RECONNECT_DELAY = 3000;
const DEFAULT_RETRY_DELAY = 5000 const DEFAULT_RETRY_DELAY = 5000;
const AUTH_TIMEOUT = 5 * 60 * 1000 // 5 minutes for user to complete auth in popup const AUTH_TIMEOUT = 5 * 60 * 1000;
export function useMcp(options: UseMcpOptions): UseMcpResult { export function useMcp(options: UseMcpOptions): UseMcpResult {
const { const {
@ -22,454 +20,471 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
callbackUrl = typeof window !== 'undefined' ? new URL('/oauth/callback', window.location.origin).toString() : '/oauth/callback', callbackUrl = typeof window !== 'undefined' ? new URL('/oauth/callback', window.location.origin).toString() : '/oauth/callback',
storageKeyPrefix = 'mcp:auth', storageKeyPrefix = 'mcp:auth',
clientConfig = {}, clientConfig = {},
debug = false, debug = false, // Consider using this in addLog
autoRetry = false, autoRetry = false,
autoReconnect = DEFAULT_RECONNECT_DELAY, autoReconnect = DEFAULT_RECONNECT_DELAY,
// popupFeatures is now handled within BrowserOAuthClientProvider } = options;
} = options
const [state, setState] = useState<UseMcpResult['state']>('discovering') const [state, setState] = useState<UseMcpResult['state']>('discovering');
const [tools, setTools] = useState<Tool[]>([]) const [tools, setTools] = useState<Tool[]>([]);
const [error, setError] = useState<string | undefined>(undefined) const [error, setError] = useState<string | undefined>(undefined);
const [log, setLog] = useState<UseMcpResult['log']>([]) const [log, setLog] = useState<UseMcpResult['log']>([]);
// Store the URL for manual fallback if needed const [authUrl, setAuthUrl] = useState<string | undefined>(undefined);
const [authUrl, setAuthUrl] = useState<string | undefined>(undefined)
const clientRef = useRef<Client | null>(null) const clientRef = useRef<Client | null>(null);
const transportRef = useRef<SSEClientTransport | null>(null) const transportRef = useRef<SSEClientTransport | null>(null);
const authProviderRef = useRef<BrowserOAuthClientProvider | null>(null) const authProviderRef = useRef<BrowserOAuthClientProvider | null>(null);
const connectingRef = useRef<boolean>(false) const connectingRef = useRef<boolean>(false);
const isMountedRef = useRef<boolean>(true) const isMountedRef = useRef<boolean>(true);
const connectAttemptRef = useRef<number>(0) const connectAttemptRef = useRef<number>(0);
const authTimeoutRef = useRef<number | null>(null) // Use number for type safety const authTimeoutRef = useRef<number | null>(null);
// --- Refs for values used in callbacks ---
// Ref to hold the latest state value for use in callbacks without causing dependency changes
const stateRef = useRef(state);
// Ref to hold autoReconnect option
const autoReconnectRef = useRef(autoReconnect);
// --- Effect to keep refs updated ---
useEffect(() => {
stateRef.current = state;
autoReconnectRef.current = autoReconnect;
}, [state, autoReconnect]);
// --- Stable Callbacks ---
// addLog is stable (empty dependency array)
const addLog = useCallback( const addLog = useCallback(
(level: UseMcpResult['log'][0]['level'], message: string, ...args: unknown[]) => { (level: UseMcpResult['log'][0]['level'], message: string, ...args: unknown[]) => {
const fullMessage = args.length > 0 ? `${message} ${args.map((arg) => JSON.stringify(arg)).join(' ')}` : message // if (level === 'debug' && !debug) return; // Uncomment if using debug flag
console[level](`[useMcp] ${fullMessage}`) const fullMessage = args.length > 0 ? `${message} ${args.map(arg => JSON.stringify(arg)).join(' ')}` : message;
console[level](`[useMcp] ${fullMessage}`);
// Use isMountedRef to prevent state updates after unmount
if (isMountedRef.current) { if (isMountedRef.current) {
setLog((prevLog) => [...prevLog.slice(-100), { level, message: fullMessage, timestamp: Date.now() }]) // Limit log size setLog((prevLog) => [...prevLog.slice(-100), { level, message: fullMessage, timestamp: Date.now() }]);
} }
}, },
[], // No dependencies needed if `debug` isn't used inside [], // Empty dependency array makes this stable
) );
// disconnect is stable (depends only on stable addLog)
const disconnect = useCallback( const disconnect = useCallback(
async (quiet = false) => { async (quiet = false) => {
if (!quiet) addLog('info', 'Disconnecting...') if (!quiet) addLog('info', 'Disconnecting...');
connectingRef.current = false connectingRef.current = false;
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current) if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
authTimeoutRef.current = null authTimeoutRef.current = null;
const client = clientRef.current const transport = transportRef.current;
const transport = transportRef.current clientRef.current = null; // Ensure client is cleared
clientRef.current = null transportRef.current = null; // Ensure transport is cleared
transportRef.current = null // Prevent reconnection logic on explicit disconnect
// Only reset state if mounted and not a quiet disconnect
if (isMountedRef.current && !quiet) { if (isMountedRef.current && !quiet) {
setState('discovering') setState('discovering');
setTools([]) setTools([]);
setError(undefined) setError(undefined);
setAuthUrl(undefined) setAuthUrl(undefined);
} }
if (transport) { if (transport) {
try { try {
await transport.close() await transport.close();
if (!quiet) addLog('debug', 'Transport closed') if (!quiet) addLog('debug', 'Transport closed');
} catch (err) { } catch (err) {
if (!quiet) addLog('warn', 'Error closing transport:', err) if (!quiet) addLog('warn', 'Error closing transport:', err);
} }
} }
}, },
[addLog], [addLog], // Depends only on stable addLog
) );
const failConnection = useCallback( // failConnection is stable (depends only on stable addLog)
(errorMessage: string, connectionError?: Error) => { const failConnection = useCallback((errorMessage: string, connectionError?: Error) => {
addLog('error', errorMessage, connectionError ?? '') addLog('error', errorMessage, connectionError ?? '');
if (isMountedRef.current) { if (isMountedRef.current) {
setState('failed') setState('failed'); // Set state to failed
setError(errorMessage) setError(errorMessage);
// Attempt to get manual auth URL if provider exists const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl();
const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl() if (manualUrl) {
if (manualUrl) { setAuthUrl(manualUrl);
setAuthUrl(manualUrl) addLog('info', 'Manual authentication URL may be available.', manualUrl);
addLog('info', 'Manual authentication URL may be available.', manualUrl)
}
} }
connectingRef.current = false }
// Don't disconnect here, allow user to potentially retry/authenticate connectingRef.current = false; // Ensure connection attempt is marked as finished
// disconnect(true); // No longer needed, state is 'failed' // Do not call disconnect here - allow user to see error and retry
}, }, [addLog]); // Depends only on stable addLog
[addLog],
)
// connect needs to be stable. Remove state/autoReconnect deps, use refs inside.
const connect = useCallback(async () => { const connect = useCallback(async () => {
if (connectingRef.current) { if (connectingRef.current) {
addLog('debug', 'Connection attempt already in progress.') addLog('debug', 'Connection attempt already in progress.');
return return;
}
// Check mounted status *before* starting async logic
if (!isMountedRef.current) {
addLog('debug', 'Connect called after unmount, aborting.');
return;
} }
if (!isMountedRef.current) return
connectingRef.current = true connectingRef.current = true;
connectAttemptRef.current += 1 connectAttemptRef.current += 1;
setError(undefined) setError(undefined);
setAuthUrl(undefined) // Clear previous manual URL setAuthUrl(undefined);
setState('discovering') setState('discovering'); // Set state *before* async calls
addLog('info', `Connecting attempt #${connectAttemptRef.current} to ${url}...`) addLog('info', `Connecting attempt #${connectAttemptRef.current} to ${url}...`);
// 1. Initialize Auth Provider (only once) // Ensure provider/client are initialized (idempotent check)
if (!authProviderRef.current) { if (!authProviderRef.current) {
authProviderRef.current = new BrowserOAuthClientProvider(url, { authProviderRef.current = new BrowserOAuthClientProvider(url, {
storageKeyPrefix, storageKeyPrefix, clientName, clientUri, callbackUrl
clientName, });
clientUri, addLog('debug', 'BrowserOAuthClientProvider initialized in connect.');
callbackUrl,
})
addLog('debug', 'BrowserOAuthClientProvider initialized.')
} }
// 2. Initialize MCP Client (only once)
if (!clientRef.current) { if (!clientRef.current) {
clientRef.current = new Client( clientRef.current = new Client(
{ { name: clientConfig.name || 'use-mcp-react-client', version: clientConfig.version || '0.1.0' },
name: clientConfig.name || 'use-mcp-react-client',
version: clientConfig.version || '0.1.0',
},
{ capabilities: {} }, { capabilities: {} },
) );
addLog('debug', 'MCP Client initialized.') addLog('debug', 'MCP Client initialized in connect.');
} }
// 3. Create/Recreate SSE Transport // Recreate transport
setState('connecting') setState('connecting'); // Update state
if (transportRef.current) { if (transportRef.current) {
await transportRef.current.close().catch((e) => addLog('warn', 'Error closing previous transport:', e)) await transportRef.current.close().catch(e => addLog('warn', 'Error closing previous transport:', e));
transportRef.current = null transportRef.current = null;
} }
try { try {
// Assert provider/client are definitely created now
assert(authProviderRef.current, "Auth Provider must be initialized");
assert(clientRef.current, "Client must be initialized");
transportRef.current = new SSEClientTransport(new URL(url), { transportRef.current = new SSEClientTransport(new URL(url), {
authProvider: authProviderRef.current, // Pass provider for automatic token attachment authProvider: authProviderRef.current,
}) });
addLog('debug', 'SSEClientTransport created.') addLog('debug', 'SSEClientTransport created.');
} catch (err) { } catch(err) {
failConnection( failConnection(`Failed to create transport: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined);
`Failed to create transport: ${err instanceof Error ? err.message : String(err)}`, // connectingRef is set to false inside failConnection
err instanceof Error ? err : undefined, return; // Stop execution
)
return
} }
// 4. Set up Transport Handlers // --- Transport Handlers ---
// These handlers are defined inline within connect's scope.
// They will implicitly capture the *current* stable callbacks like addLog, failConnection.
// They use refs (stateRef, autoReconnectRef) to access current state/options when needed.
transportRef.current.onmessage = (message: JSONRPCMessage) => { transportRef.current.onmessage = (message: JSONRPCMessage) => {
addLog('debug', `[Transport] Received: ${JSON.stringify(message)}`) addLog('debug', `[Transport] Received: ${JSON.stringify(message)}`);
// Forward to the *current* client instance
// @ts-ignore // @ts-ignore
clientRef.current?.handleMessage(message) clientRef.current?.handleMessage(message);
} };
transportRef.current.onerror = (err: Error) => { transportRef.current.onerror = (err: Error) => {
// Transport errors usually mean connection is lost/failed failConnection(`Transport error: ${err.message}`, err);
failConnection(`Transport error: ${err.message}`, err) };
}
transportRef.current.onclose = () => { transportRef.current.onclose = () => {
if (!isMountedRef.current || connectingRef.current) return // Ignore if unmounting or mid-connect // Use refs to check state and options without needing them as dependencies
if (!isMountedRef.current || connectingRef.current) return;
addLog('info', 'Transport connection closed.') addLog('info', 'Transport connection closed.');
const currentState = state // Capture state at time of closure const currentState = stateRef.current; // Use ref
if (currentState === 'ready' && autoReconnect) { const currentAutoReconnect = autoReconnectRef.current; // Use ref
const delay = typeof autoReconnect === 'number' ? autoReconnect : DEFAULT_RECONNECT_DELAY
addLog('info', `Attempting to reconnect in ${delay}ms...`) if (currentState === 'ready' && currentAutoReconnect) {
const delay = typeof currentAutoReconnect === 'number' ? currentAutoReconnect : DEFAULT_RECONNECT_DELAY;
addLog('info', `Attempting to reconnect in ${delay}ms...`);
setState('connecting'); // Update state for UI feedback
setTimeout(() => { setTimeout(() => {
if (isMountedRef.current && clientRef.current /* Check if still relevant */) { // Check mounted status again before actually connecting
connect() // Re-run the connection logic if (isMountedRef.current) {
connect(); // Call stable connect again
} }
}, delay) }, delay);
setState('connecting')
} else if (currentState !== 'failed' && currentState !== 'authenticating') { } else if (currentState !== 'failed' && currentState !== 'authenticating') {
// If closed unexpectedly and not already failed/authenticating failConnection('Connection closed unexpectedly.');
failConnection('Connection closed unexpectedly.')
} }
} };
// 5. Connect Client & Handle Auth // --- Connect Client & Handle Auth ---
try { try {
addLog('info', 'Connecting client...') addLog('info', 'Connecting client...');
assert(clientRef.current, 'Client not initialized') // Client/transport refs are already asserted above
assert(transportRef.current, 'Transport not initialized') await clientRef.current!.connect(transportRef.current);
await clientRef.current.connect(transportRef.current) addLog('info', 'Client connected. Loading tools...');
setState('loading'); // Update state
addLog('info', 'Client connected. Loading tools...') const toolsResponse = await clientRef.current!.request({ method: 'tools/list' }, ListToolsResultSchema);
setState('loading') // Check mounted status *before* final state update
const toolsResponse = await clientRef.current.request({ method: 'tools/list' }, ListToolsResultSchema)
if (isMountedRef.current) { if (isMountedRef.current) {
setTools(toolsResponse.tools) setTools(toolsResponse.tools);
addLog('info', `Loaded ${toolsResponse.tools.length} tools.`) addLog('info', `Loaded ${toolsResponse.tools.length} tools.`);
setState('ready') setState('ready'); // Final success state
connectingRef.current = false connectingRef.current = false; // Connection attempt finished successfully
connectAttemptRef.current = 0 // Reset on success connectAttemptRef.current = 0; // Reset attempt counter
} }
} catch (connectErr) { } catch (connectErr) {
addLog('debug', 'Client connect error:', connectErr) addLog('debug', 'Client connect error:', connectErr);
const errorInstance = connectErr instanceof Error ? connectErr : new Error(String(connectErr)) const errorInstance = connectErr instanceof Error ? connectErr : new Error(String(connectErr));
// Check if it's specifically an UnauthorizedError or smells like one if (errorInstance instanceof UnauthorizedError || errorInstance.message.includes('Unauthorized') || errorInstance.message.includes('401')) {
// NOTE: The SSE Transport might wrap the underlying 401. Check for UnauthorizedError first. addLog('info', 'Authentication required. Initiating SDK auth flow...');
if ( setState('authenticating'); // Update state
errorInstance instanceof UnauthorizedError || // Start auth timeout
errorInstance.message.includes('Unauthorized') || if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
errorInstance.message.includes('401')
) {
addLog('info', 'Authentication required. Initiating SDK auth flow...')
setState('authenticating')
// Start auth timeout timer
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current)
authTimeoutRef.current = setTimeout(() => { authTimeoutRef.current = setTimeout(() => {
addLog('warn', 'Authentication timed out.') // Check ref state before failing
if (isMountedRef.current && state === 'authenticating') { if (isMountedRef.current && stateRef.current === 'authenticating') {
// Check state again failConnection('Authentication timed out. Please try again or use the manual link if available.');
failConnection('Authentication timed out. Please try again or use the manual link if available.')
} }
}, AUTH_TIMEOUT) }, AUTH_TIMEOUT);
try { try {
assert(authProviderRef.current, 'Auth Provider not available for auth flow') assert(authProviderRef.current, "Auth Provider not available for auth flow");
// This call will handle discovery, checks, token refresh attempts, const authResult = await auth(authProviderRef.current, { serverUrl: url });
// and finally trigger provider.redirectToAuthorization if needed.
const authResult = await auth(authProviderRef.current, { serverUrl: url }) // Check mounted status before proceeding
// If auth() resolves with "AUTHORIZED", it means refresh worked or existing token is valid. if (!isMountedRef.current) return;
if (authResult === 'AUTHORIZED') { if (authResult === 'AUTHORIZED') {
addLog('info', 'Authentication successful via existing token or refresh. Reconnecting transport...') addLog('info', 'Authentication successful via existing token or refresh. Reconnecting...');
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current) // Stop timeout if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
// We might need to reconnect the transport or the whole client connectingRef.current = false; // Allow reconnect
// Let's try reconnecting the client fully connect(); // Call stable connect to re-establish
connectingRef.current = false // Allow reconnect } else if (authResult === 'REDIRECT') {
connect() // Re-initiate connection flow addLog('info', 'Redirecting for authentication. Waiting for callback...');
} // State is 'authenticating', timeout running. connectingRef remains true.
// If authResult is 'REDIRECT', the provider's redirectToAuthorization was called.
// We are now waiting for the popup callback. State is already 'authenticating'.
else if (authResult === 'REDIRECT') {
addLog('info', 'Redirecting for authentication. Waiting for callback...')
// The state is 'authenticating', timeout is running.
} }
} catch (sdkAuthError) { } catch (sdkAuthError) {
// Handle errors from the SDK's auth() function itself (e.g., metadata fetch fail) if (!isMountedRef.current) return; // Check mount before failConnection
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current) if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
failConnection( failConnection(`Failed to initiate authentication: ${sdkAuthError instanceof Error ? sdkAuthError.message : String(sdkAuthError)}`, sdkAuthError instanceof Error ? sdkAuthError : undefined);
`Failed to initiate authentication: ${sdkAuthError instanceof Error ? sdkAuthError.message : String(sdkAuthError)}`, // connectingRef set to false inside failConnection
sdkAuthError instanceof Error ? sdkAuthError : undefined,
)
} }
} else { } else {
// Handle other connection errors // Handle other connection errors
failConnection(`Failed to connect: ${errorInstance.message}`, errorInstance) failConnection(`Failed to connect: ${errorInstance.message}`, errorInstance);
} // connectingRef set to false inside failConnection
// Set connectingRef false only if not waiting for redirect
if (state !== 'authenticating') {
connectingRef.current = false
} }
// Only set connectingRef false here if NOT waiting for redirect
// Note: failConnection already sets it to false. If redirect happens, it remains true.
// If AUTHORIZED happens, we immediately call connect again, which sets it true.
// This seems correct.
} }
}, [ }, [
url, // Stable callback dependencies
storageKeyPrefix, addLog, failConnection,
clientName, // Configuration dependencies that require full reconnect if changed
clientUri, url, storageKeyPrefix, clientName, clientUri, callbackUrl,
callbackUrl, // clientConfig changes might require reconnect? Add if necessary:
clientConfig, // Provider/Client configs // clientConfig.name, clientConfig.version
addLog, // Note: connect does NOT depend on state or autoReconnect anymore
failConnection, ]);
disconnect, // Core actions
state,
autoReconnect, // State/Config used in handlers
])
// callTool is stable (depends on stable addLog, failConnection, connect, and URL)
const callTool = useCallback( const callTool = useCallback(
async (name: string, args?: Record<string, unknown>) => { async (name: string, args?: Record<string, unknown>) => {
if (state !== 'ready' || !clientRef.current) { // Use stateRef for check, state for throwing error message
throw new Error(`MCP client is not ready (state: ${state}). Cannot call tool "${name}".`) if (stateRef.current !== 'ready' || !clientRef.current) {
throw new Error(`MCP client is not ready (current state: ${state}). Cannot call tool "${name}".`);
} }
addLog('info', `Calling tool: ${name}`, args) addLog('info', `Calling tool: ${name}`, args);
try { try {
const result = await clientRef.current.request({ method: 'tools/call', params: { name, arguments: args } }, CallToolResultSchema) const result = await clientRef.current.request(
addLog('info', `Tool "${name}" call successful:`, result) { method: 'tools/call', params: { name, arguments: args } },
return result CallToolResultSchema,
);
addLog('info', `Tool "${name}" call successful:`, result);
return result;
} catch (err) { } catch (err) {
addLog('error', `Error calling tool "${name}": ${err instanceof Error ? err.message : String(err)}`, err) addLog('error', `Error calling tool "${name}": ${err instanceof Error ? err.message : String(err)}`, err);
// TODO: Check if error is UnauthorizedError and trigger re-authentication? const errorInstance = err instanceof Error ? err : new Error(String(err));
if (
err instanceof UnauthorizedError || if (errorInstance instanceof UnauthorizedError || errorInstance.message.includes('Unauthorized') || errorInstance.message.includes('401')) {
(err instanceof Error && (err.message.includes('Unauthorized') || err.message.includes('401'))) addLog('warn', 'Tool call unauthorized, attempting re-authentication...');
) { setState('authenticating'); // Update UI state
addLog('warn', 'Tool call unauthorized, attempting re-authentication...') if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current); // Reset timeout
setState('authenticating') // Signal auth needed authTimeoutRef.current = setTimeout(() => { /* ... timeout logic ... */ }, AUTH_TIMEOUT);
// Don't disconnect, just trigger auth
assert(authProviderRef.current, 'Auth Provider not available for tool re-auth') try {
auth(authProviderRef.current, { serverUrl: url }).catch((sdkAuthError) => { assert(authProviderRef.current, "Auth Provider not available for tool re-auth");
failConnection( const authResult = await auth(authProviderRef.current, { serverUrl: url });
`Re-authentication failed: ${sdkAuthError instanceof Error ? sdkAuthError.message : String(sdkAuthError)}`,
sdkAuthError instanceof Error ? sdkAuthError : undefined, if (!isMountedRef.current) return; // Check mount
)
}) if (authResult === 'AUTHORIZED') {
addLog('info', 'Re-authentication successful. Retrying tool call is recommended, or reconnecting.');
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
// Option 1: Just set state ready and let user retry tool?
// setState('ready');
// Option 2: Reconnect client completely? Safer.
connectingRef.current = false;
connect(); // Reconnect session
} else if (authResult === 'REDIRECT') {
addLog('info', 'Redirecting for re-authentication for tool call.');
// State is authenticating, wait for callback
}
} catch (sdkAuthError) {
if (!isMountedRef.current) return;
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
failConnection(`Re-authentication failed: ${sdkAuthError instanceof Error ? sdkAuthError.message : String(sdkAuthError)}`, sdkAuthError instanceof Error ? sdkAuthError : undefined);
}
} }
throw err // Re-throw original error unless handled by re-auth redirect
// @ts-ignore
if (stateRef.current !== 'authenticating') { // Only re-throw if not waiting for redirect
throw err;
}
// If authenticating, we might want to signal the caller differently,
// but for now, we don't re-throw, assuming the UI will react to the 'authenticating' state.
return undefined; // Or indicate auth required?
} }
}, },
[state, addLog, url], // Add url dependency for re-auth attempt [state, url, addLog, failConnection, connect], // Depends on state for error message, url, and stable callbacks
) );
// retry is stable (depends on stable addLog, connect)
const retry = useCallback(() => { const retry = useCallback(() => {
// Retry should attempt to connect again, regardless of the specific failure reason // Use stateRef for check
if (state === 'failed') { if (stateRef.current === 'failed') {
addLog('info', 'Retry requested...') addLog('info', 'Retry requested...');
// Reset state and attempt connection // connect() will handle resetting state and error internally
setState('discovering') connect();
setError(undefined)
setAuthUrl(undefined)
connect()
} else { } else {
addLog('warn', `Retry called but state is not 'failed' (state: ${state}). Ignoring.`) addLog('warn', `Retry called but state is not 'failed' (state: ${stateRef.current}). Ignoring.`);
} }
}, [state, addLog, connect]) }, [addLog, connect]); // Depends only on stable callbacks
// authenticate is stable (depends on stable addLog, retry, connect)
const authenticate = useCallback(() => { const authenticate = useCallback(() => {
addLog('info', 'Manual authentication requested...') addLog('info', 'Manual authentication requested...');
// Option 1: If failed, just retry connection which includes auth logic const currentState = stateRef.current; // Use ref
if (state === 'failed') {
addLog('info', 'Attempting to reconnect and authenticate...') if (currentState === 'failed') {
retry() // Retry will call connect() addLog('info', 'Attempting to reconnect and authenticate via retry...');
} retry();
// Option 2: If stuck authenticating (popup possibly blocked), maybe re-trigger } else if (currentState === 'authenticating') {
else if (state === 'authenticating') { addLog('warn', 'Already attempting authentication. Check for blocked popups or wait for timeout.');
addLog('warn', 'Already attempting authentication. Check for blocked popups or wait for timeout.') const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl();
// Optionally, try retrieving and setting the manual URL again if (manualUrl && !authUrl) { // Use component state `authUrl` here
const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl() setAuthUrl(manualUrl);
if (manualUrl && !authUrl) { addLog('info', 'Manual authentication URL retrieved:', manualUrl);
// Only set if not already set
setAuthUrl(manualUrl)
addLog('info', 'Manual authentication URL retrieved:', manualUrl)
} }
// Maybe try calling SDK auth again? Could be risky if popup is open. } else {
// assert(authProviderRef.current, "Auth Provider not available for manual auth trigger"); addLog('info', `Client not in a state requiring manual authentication trigger (state: ${currentState}). If needed, try disconnecting and reconnecting.`);
// Optionally, force re-auth even if ready?
// addLog('info', 'Forcing re-authentication...');
// setState('authenticating');
// assert(authProviderRef.current, "Auth Provider not available");
// auth(authProviderRef.current, { serverUrl: url }).catch(failConnection); // auth(authProviderRef.current, { serverUrl: url }).catch(failConnection);
} }
// Option 3: If already ready, maybe force a refresh/re-auth? Less common. }, [addLog, retry, authUrl]); // Depends on stable callbacks and authUrl state
else {
addLog(
'info',
'Client not in a state requiring manual authentication trigger (state: ${state}). If needed, try disconnecting and reconnecting.',
)
}
}, [state, addLog, retry, authUrl]) // Add authUrl
// clearStorage is stable (depends on stable addLog, disconnect)
const clearStorage = useCallback(() => { const clearStorage = useCallback(() => {
if (authProviderRef.current) { if (authProviderRef.current) {
const count = authProviderRef.current.clearStorage() const count = authProviderRef.current.clearStorage();
addLog('info', `Cleared ${count} item(s) from localStorage for ${url}.`) addLog('info', `Cleared ${count} item(s) from localStorage for ${url}.`);
setAuthUrl(undefined) // Clear any displayed manual URL setAuthUrl(undefined); // Clear manual URL state
// Force disconnect and state reset as auth info is gone // Disconnect should reset state appropriately
disconnect().then(() => { disconnect();
if (isMountedRef.current) {
setState('discovering') // Ready for a fresh connect attempt
}
})
} else { } else {
addLog('warn', 'Auth provider not initialized, cannot clear storage.') addLog('warn', 'Auth provider not initialized, cannot clear storage.');
} }
}, [addLog, url, disconnect]) }, [url, addLog, disconnect]); // Depends on url and stable callbacks
// ===== Effects ===== // ===== Effects =====
// Effect for handling auth callback messages from popup // Effect for handling auth callback messages from popup (Stable dependencies)
useEffect(() => { useEffect(() => {
const messageHandler = (event: MessageEvent) => { const messageHandler = (event: MessageEvent) => {
if (event.origin !== window.location.origin) return if (event.origin !== window.location.origin) return;
if (event.data?.type === 'mcp_auth_callback') {
if (event.data && event.data.type === 'mcp_auth_callback') { addLog('info', 'Received auth callback message.', event.data);
addLog('info', 'Received auth callback message.', event.data) if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current) // Stop timeout
if (event.data.success) { if (event.data.success) {
addLog('info', 'Authentication successful via popup. Reconnecting client...') addLog('info', 'Authentication successful via popup. Reconnecting client...');
// Tokens are saved by callback handler via provider.saveTokens. connectingRef.current = false;
// Re-run connect() to establish the session with the new tokens. connect(); // Call stable connect
connectingRef.current = false // Allow connect again
connect()
} else { } else {
// Auth failed in the popup/callback handler failConnection(`Authentication failed in callback: ${event.data.error || 'Unknown reason.'}`); // Call stable failConnection
failConnection(`Authentication failed in callback: ${event.data.error || 'Unknown reason.'}`)
} }
} }
} };
window.addEventListener('message', messageHandler);
window.addEventListener('message', messageHandler) addLog('debug', 'Auth callback message listener added.');
addLog('debug', 'Auth callback message listener added.')
return () => { return () => {
window.removeEventListener('message', messageHandler) window.removeEventListener('message', messageHandler);
addLog('debug', 'Auth callback message listener removed.') addLog('debug', 'Auth callback message listener removed.');
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current) if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
} };
}, [addLog, failConnection, connect]) // connect needed for success path // Dependencies are stable callbacks
}, [addLog, failConnection, connect]);
// Effect for initial connection // Effect for initial connection (Stable dependencies)
useEffect(() => { useEffect(() => {
isMountedRef.current = true isMountedRef.current = true;
addLog('debug', 'useMcp mounted, initiating connection.') addLog('debug', 'useMcp mounted, initiating connection.');
connectAttemptRef.current = 0 connectAttemptRef.current = 0;
// Ensure provider exists before connecting // Initialize provider here if it doesn't exist, ensuring it uses latest options
if (!authProviderRef.current) { if (!authProviderRef.current || authProviderRef.current.serverUrl !== url) {
authProviderRef.current = new BrowserOAuthClientProvider(url, { authProviderRef.current = new BrowserOAuthClientProvider(url, {
storageKeyPrefix, storageKeyPrefix, clientName, clientUri, callbackUrl
clientName, });
clientUri, addLog('debug', 'BrowserOAuthClientProvider initialized/updated on mount/option change.');
callbackUrl,
})
addLog('debug', 'BrowserOAuthClientProvider initialized on mount.')
} }
connect() // Initial connection attempt connect(); // Call stable connect
return () => { return () => {
isMountedRef.current = false isMountedRef.current = false;
addLog('debug', 'useMcp unmounting, disconnecting.') addLog('debug', 'useMcp unmounting, disconnecting.');
disconnect(true) // Quiet disconnect on unmount disconnect(true); // Call stable disconnect
} };
// eslint-disable-next-line react-hooks/exhaustive-deps // Ensure all config options that influence provider/connect are dependencies
}, [ }, [
url, // Reconnect if URL changes url, storageKeyPrefix, callbackUrl, clientName, clientUri, // Core config
storageKeyPrefix, clientConfig.name, clientConfig.version, // Include client config if it affects Client init
callbackUrl, connect, disconnect // Stable callbacks are OK as deps
clientName, ]);
clientUri, // Reconnect if provider config changes
// connect, disconnect are stable due to useCallback
])
// Effect for auto-retry logic // Effect for auto-retry logic (Stable dependencies)
useEffect(() => { useEffect(() => {
let retryTimeoutId: number | null = null let retryTimeoutId: number | null = null;
// Use state directly here, as this effect *should* run when state changes to 'failed'
if (state === 'failed' && autoRetry && connectAttemptRef.current > 0) { if (state === 'failed' && autoRetry && connectAttemptRef.current > 0) {
const delay = typeof autoRetry === 'number' ? autoRetry : DEFAULT_RETRY_DELAY const delay = typeof autoRetry === 'number' ? autoRetry : DEFAULT_RETRY_DELAY;
addLog('info', `Connection failed, auto-retrying in ${delay}ms...`) addLog('info', `Connection failed, auto-retrying in ${delay}ms...`);
retryTimeoutId = setTimeout(() => { retryTimeoutId = setTimeout(() => {
if (isMountedRef.current && state === 'failed') { // Check mount status and state again before retrying
retry() if (isMountedRef.current && stateRef.current === 'failed') {
retry(); // Call stable retry
} }
}, delay) }, delay);
} }
return () => { return () => {
if (retryTimeoutId) clearTimeout(retryTimeoutId) if (retryTimeoutId) clearTimeout(retryTimeoutId);
} };
}, [state, autoRetry, retry, addLog]) // Depends on state (to trigger), autoRetry config, and stable retry/addLog callbacks
}, [state, autoRetry, retry, addLog]);
return { return {
state, state,
tools, tools,
error, error,
log, log,
authUrl, // Expose the manually retrieved auth URL authUrl,
callTool, callTool,
retry, retry,
disconnect, disconnect,
authenticate, authenticate,
clearStorage, clearStorage,
} };
} }