({
agentServer,
onCancel,
onComplete
}: Props)
| 26 | * For HTTP/SSE servers, this allows pre-authentication before using the agent. |
| 27 | */ |
| 28 | export function MCPAgentServerMenu({ |
| 29 | agentServer, |
| 30 | onCancel, |
| 31 | onComplete |
| 32 | }: Props): React.ReactNode { |
| 33 | const [theme] = useTheme(); |
| 34 | const [isAuthenticating, setIsAuthenticating] = useState(false); |
| 35 | const [error, setError] = useState<string | null>(null); |
| 36 | const [authorizationUrl, setAuthorizationUrl] = useState<string | null>(null); |
| 37 | const authAbortControllerRef = useRef<AbortController | null>(null); |
| 38 | |
| 39 | // Abort OAuth flow on unmount so the callback server is closed even if a |
| 40 | // parent component's Esc handler navigates away before ours fires. |
| 41 | useEffect(() => () => authAbortControllerRef.current?.abort(), []); |
| 42 | |
| 43 | // Handle ESC to cancel authentication flow |
| 44 | const handleEscCancel = useCallback(() => { |
| 45 | if (isAuthenticating) { |
| 46 | authAbortControllerRef.current?.abort(); |
| 47 | authAbortControllerRef.current = null; |
| 48 | setIsAuthenticating(false); |
| 49 | setAuthorizationUrl(null); |
| 50 | } |
| 51 | }, [isAuthenticating]); |
| 52 | useKeybinding('confirm:no', handleEscCancel, { |
| 53 | context: 'Confirmation', |
| 54 | isActive: isAuthenticating |
| 55 | }); |
| 56 | const handleAuthenticate = useCallback(async () => { |
| 57 | if (!agentServer.needsAuth || !agentServer.url) { |
| 58 | return; |
| 59 | } |
| 60 | setIsAuthenticating(true); |
| 61 | setError(null); |
| 62 | const controller = new AbortController(); |
| 63 | authAbortControllerRef.current = controller; |
| 64 | try { |
| 65 | // Create a temporary config for OAuth |
| 66 | const tempConfig = { |
| 67 | type: agentServer.transport as 'http' | 'sse', |
| 68 | url: agentServer.url |
| 69 | }; |
| 70 | await performMCPOAuthFlow(agentServer.name, tempConfig, setAuthorizationUrl, controller.signal); |
| 71 | onComplete?.(`Authentication successful for ${agentServer.name}. The server will connect when the agent runs.`); |
| 72 | } catch (err) { |
| 73 | // Don't show error if it was a cancellation |
| 74 | if (err instanceof Error && !(err instanceof AuthenticationCancelledError)) { |
| 75 | setError(err.message); |
| 76 | } |
| 77 | } finally { |
| 78 | setIsAuthenticating(false); |
| 79 | authAbortControllerRef.current = null; |
| 80 | } |
| 81 | }, [agentServer, onComplete]); |
| 82 | const capitalizedServerName = capitalize(String(agentServer.name)); |
| 83 | if (isAuthenticating) { |
| 84 | return <Box flexDirection="column" gap={1} padding={1}> |
| 85 | <Text color="claude">Authenticating with {agentServer.name}…</Text> |
nothing calls this directly
no test coverage detected