The open-source agent harness - the runtime layer that turns an LLM into a working agent.
chore: remove oauth connection flow fron settings connectors list and add connect in details page (#227)
sayan-truefoundry07cb7f52a717
diff --git a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorDetails.tsx b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorDetails.tsxindex 2024417e6..2152695bc 100644--- a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorDetails.tsx+++ b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorDetails.tsx@@ -4,6 +4,7 @@ import { useEffect, useLayoutEffect, useRef, useState } from 'react';import { cn } from '../../atoms/lib/cn.js';import { Button } from '../../atoms/primitives/Button.js';+import { useMCPAuth } from '../../hooks/useMcpAuth.js';import { Icon } from '../../icons/Icon.js';import { useCatalogServer } from '../../server/ServerContext.js';import type { ConnectorBase, ToolBase } from '../../server/types.js';@@ -13,6 +14,7 @@ import { AUTH_TYPE_LABELS } from './authTypeLabels.js';type ConnectorDetailsProps = {connector: ConnectorBase;onBack: () => void;+ onConnectorRefreshed: (connector: ConnectorBase) => void;onDisconnect: () => void;busy?: boolean;};@@ -92,11 +94,19 @@ function ExpandableDescription({ text }: { text: string }) {);}-const ConnectorDetails = ({ connector, onBack, onDisconnect, busy = false }: ConnectorDetailsProps) => {+const ConnectorDetails = ({+ connector,+ onBack,+ onConnectorRefreshed,+ onDisconnect,+ busy = false,+}: ConnectorDetailsProps) => {const { connectorCatalog } = useCatalogServer();+ const { handleAuthorize, isOAuthLoading } = useMCPAuth();const [tools, setTools] = useState<ToolBase[]>([]);const [toolsLoading, setToolsLoading] = useState(true);const [toolsError, setToolsError] = useState<string | null>(null);+ const [refreshingAfterAuth, setRefreshingAfterAuth] = useState(false);useEffect(() => {let cancelled = false;@@ -121,7 +131,23 @@ const ConnectorDetails = ({ connector, onBack, onDisconnect, busy = false }: Conreturn () => {cancelled = true;};- }, [connector.id, connectorCatalog]);+ }, [connector.authenticated, connector.id, connectorCatalog]);++ const refreshAfterAuthentication = async () => {+ setRefreshingAfterAuth(true);+ setToolsError(null);++ try {+ const refreshedConnector = await connectorCatalog.getConnector({ id: connector.id });+ onConnectorRefreshed(refreshedConnector);+ } catch (err: unknown) {+ setToolsError(getErrorMessage(err, 'Failed to refresh connector details'));+ } finally {+ setRefreshingAfterAuth(false);+ }+ };++ const connecting = isOAuthLoading || refreshingAfterAuth;return (<div className="flex h-full flex-col overflow-hidden">@@ -167,7 +193,21 @@ const ConnectorDetails = ({ connector, onBack, onDisconnect, busy = false }: Con</span></div>- {connector.auth.type === 'dcr' && !connector.requiresAuth ? (+ {connector.auth.type === 'dcr' && !connector.authenticated ? (+ <Button+ variant="outline"+ size="sm"+ type="button"+ disabled={busy || connecting}+ onClick={() => {+ void handleAuthorize(connector.id, isSuccess => {+ if (isSuccess) void refreshAfterAuthentication();+ });+ }}+ >+ {connecting ? 'Connecting…' : 'Connect'}+ </Button>+ ) : connector.auth.type === 'dcr' && !connector.requiresAuth ? (<Button variant="outline" size="sm" type="button" disabled={busy} onClick={onDisconnect}>Disconnect</Button>@@ -182,6 +222,9 @@ const ConnectorDetails = ({ connector, onBack, onDisconnect, busy = false }: Con) : toolsError ? (<div className="rounded-xl border border-failure-bg/30 bg-failure-bg/10 p-6 text-center text-sm text-failure-bg">{toolsError}+ {connector.auth.type === 'dcr' && !connector.authenticated && (+ <div className="font-bold mt-1">Click Connect and authenticate successfully to view tools.</div>+ )}</div>) : tools.length > 0 ? (<>diff --git a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorSettings.tsx b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorSettings.tsxindex 7f8117f46..c3997edfe 100644--- a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorSettings.tsx+++ b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ConnectorSettings.tsx@@ -8,7 +8,6 @@ import { Button } from '@/atoms/primitives/Button.js';import { CatalogLogo } from '@/atoms/primitives/CatalogLogo.js';import { CenteredModal } from '@/atoms/primitives/CenteredModal.js';import SearchInput from '@/atoms/primitives/SearchInput.js';-import { useMCPAuth } from '@/hooks/useMcpAuth.js';import { Icon } from '@/icons/Icon.js';import { useCatalogServer } from '@/server/ServerContext.js';import type { ConnectorAuth, ConnectorBase, ConnectorCatalogEntry } from '@/server/types.js';@@ -28,7 +27,6 @@ type ConnectorsState = {const ConnectorSettings = () => {const { connectorCatalog } = useCatalogServer();- const { handleAuthorize, isOAuthLoading } = useMCPAuth();const toaster = useToasterOptional();const [query, setQuery] = useState('');@@ -146,25 +144,6 @@ const ConnectorSettings = () => {setFormError(null);};- const authorizeOAuthConnector = async (integrationId: string) => {- await handleAuthorize(integrationId, isSuccess => {- if (isSuccess) {- void connectorCatalog- .getConnector({ id: integrationId })- .then(connector => {- setConnectors(current => {- const updated = new Map(current.authStatusById);- updated.set(connector.id, connector.authenticated);- return { ...current, authStatusById: updated };- });- })- .catch(err => {- setError(getErrorMessage(err, 'Failed to load connector'));- });- }- });- };-const createFromCatalog = async (entry: ConnectorCatalogEntry, authOverride?: ConnectorAuth) => {const auth: ConnectorAuth =authOverride ??@@ -176,14 +155,11 @@ const ConnectorSettings = () => {: entry.auth.type === 'dcr'? { type: 'dcr' }: { type: 'none' });- const created = await connectorCatalog.createConnector({+ await connectorCatalog.createConnector({name: entry.name,url: entry.url,auth,});- if (auth.type === 'dcr') {- await authorizeOAuthConnector(created.id);- }};const handleConnect = (entry: ConnectorCatalogEntry) => {@@ -233,14 +209,11 @@ const ConnectorSettings = () => {const handleAddMcpServer = async (draft: AddMcpServerDraft) => {setFormError(null);await runMutation(async () => {- const created = await connectorCatalog.createConnector({+ await connectorCatalog.createConnector({name: draft.name,url: draft.url,auth: draft.auth,});- if (draft.auth.type === 'dcr') {- await authorizeOAuthConnector(created.id);- }}, setFormError);setTimeout(() => {toaster?.showSuccess({ title: `${draft.name} added` });@@ -254,6 +227,23 @@ const ConnectorSettings = () => {}).catch(() => {});};+ const handleConnectorRefreshed = (refreshedConnector: ConnectorBase) => {+ setSelectedConnector(current => (current?.id === refreshedConnector.id ? refreshedConnector : current));+ setConnectors(current => {+ const authStatusById = new Map(current.authStatusById);+ authStatusById.set(refreshedConnector.id, refreshedConnector.authenticated);++ return {+ ordered: current.ordered.map(item =>+ item.isConfigured && item.connector.id === refreshedConnector.id+ ? { connector: refreshedConnector, isConfigured: true }+ : item,+ ),+ authStatusById,+ };+ });+ };+const renderConnector = (connector: ConnectorBase, isConnected: boolean) => {const logoSrc = connectorIconMap[connector.name];const content = (@@ -311,7 +301,7 @@ const ConnectorSettings = () => {variant="secondary"size="sm"type="button"- disabled={busy || isOAuthLoading}+ disabled={busy}onClick={event => {event.stopPropagation();setApiKey('');@@ -368,7 +358,7 @@ const ConnectorSettings = () => {variant="secondary"size="sm"type="button"- disabled={busy || isOAuthLoading}+ disabled={busy}onClick={() => {handleConnect(entry);}}@@ -397,6 +387,7 @@ const ConnectorSettings = () => {onBack={() => {setSelectedConnector(null);}}+ onConnectorRefreshed={handleConnectorRefreshed}onDisconnect={() => {handleDisconnect(selectedConnector);}}@@ -523,10 +514,10 @@ const ConnectorSettings = () => {</div><footer className="flex justify-end gap-2 border-t border-border px-5 py-4">- <Button variant="ghost" type="button" onClick={closeApiKeyModal} disabled={busy || isOAuthLoading}>+ <Button variant="ghost" type="button" onClick={closeApiKeyModal} disabled={busy}>Cancel</Button>- <Button type="submit" disabled={!apiKey.trim() || busy || isOAuthLoading}>+ <Button type="submit" disabled={!apiKey.trim() || busy}>{isReplacingKey ? 'Replace Key' : 'Connect'}</Button></footer>@@ -540,7 +531,7 @@ const ConnectorSettings = () => {if (!open) setFormError(null);}}onAdd={handleAddMcpServer}- busy={busy || isOAuthLoading}+ busy={busy}error={formError}/></div>diff --git a/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ConnectorDetails.test.tsx b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ConnectorDetails.test.tsxindex 28b7d412d..2ff0877be 100644--- a/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ConnectorDetails.test.tsx+++ b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ConnectorDetails.test.tsx@@ -1,6 +1,6 @@// @vitest-environment jsdomimport { fireEvent, render, screen, waitFor } from '@testing-library/react';-import type { ReactNode } from 'react';+import { useState, type ReactNode } from 'react';import { describe, expect, it, vi } from 'vitest';import ConnectorDetails from '@/containers/SettingsBuilder/ConnectorDetails.js';@@ -8,6 +8,15 @@ import { ServerProvider } from '@/server/ServerContext.js';import type { ConnectorBase, ToolBase } from '@/server/types.js';import { createMockAgentUIServer, createMockCatalog } from '../../server/mockServer.js';+const oauthMock = vi.hoisted(() => ({+ handleAuthorize: vi.fn(),+ isOAuthLoading: false,+}));++vi.mock('@/hooks/useMcpAuth.js', () => ({+ useMCPAuth: () => oauthMock,+}));+const connector: ConnectorBase = {id: 'conn-1',name: 'Linear',@@ -50,7 +59,12 @@ describe('ConnectorDetails tool descriptions', () => {render(<Wrapper>- <ConnectorDetails connector={connector} onBack={() => {}} onDisconnect={() => {}} />+ <ConnectorDetails+ connector={connector}+ onBack={() => {}}+ onConnectorRefreshed={() => {}}+ onDisconnect={() => {}}+ /></Wrapper>,);@@ -81,7 +95,12 @@ describe('ConnectorDetails tool descriptions', () => {render(<Wrapper>- <ConnectorDetails connector={connector} onBack={() => {}} onDisconnect={() => {}} />+ <ConnectorDetails+ connector={connector}+ onBack={() => {}}+ onConnectorRefreshed={() => {}}+ onDisconnect={() => {}}+ /></Wrapper>,);@@ -92,3 +111,153 @@ describe('ConnectorDetails tool descriptions', () => {expect(screen.queryByRole('button', { name: 'Read more' })).not.toBeInTheDocument();});});++describe('ConnectorDetails OAuth connection', () => {+ const unauthenticatedConnector: ConnectorBase = {+ ...connector,+ authenticated: false,+ requiresAuth: true,+ };++ it('shows Connect only for an unauthenticated DCR connector', () => {+ const Wrapper = wrapperFor([]);+ const headerConnector: ConnectorBase = {+ ...unauthenticatedConnector,+ auth: { type: 'header', headerName: 'Authorization' },+ };++ const { rerender } = render(+ <Wrapper>+ <ConnectorDetails+ connector={unauthenticatedConnector}+ onBack={() => {}}+ onConnectorRefreshed={() => {}}+ onDisconnect={() => {}}+ />+ </Wrapper>,+ );++ expect(screen.getByRole('button', { name: 'Connect' })).toBeInTheDocument();+ expect(screen.queryByRole('button', { name: 'Disconnect' })).not.toBeInTheDocument();++ rerender(+ <Wrapper>+ <ConnectorDetails+ connector={headerConnector}+ onBack={() => {}}+ onConnectorRefreshed={() => {}}+ onDisconnect={() => {}}+ />+ </Wrapper>,+ );++ expect(screen.queryByRole('button', { name: 'Connect' })).not.toBeInTheDocument();+ });++ it('does not refresh connector details or tools when OAuth is unsuccessful', async () => {+ const getConnector = vi.fn(async () => unauthenticatedConnector);+ const getToolsByConnectorId = vi.fn(async () => []);+ oauthMock.handleAuthorize.mockImplementation(async (_id: string, callback: (isSuccess: boolean) => void) => {+ callback(false);+ });+ const server = createMockAgentUIServer({+ catalog: createMockCatalog({+ connectorCatalog: {+ getConnectorCatalog: async () => [],+ listConnectors: async () => [],+ getConnector,+ getToolsByConnectorId,+ createConnector: async () => unauthenticatedConnector,+ updateConnector: async () => unauthenticatedConnector,+ authenticateConnector: async () => ({ authorization_endpoint: '' }),+ disconnectConnector: async () => unauthenticatedConnector,+ },+ }),+ });++ render(+ <ServerProvider server={server}>+ <ConnectorDetails+ connector={unauthenticatedConnector}+ onBack={() => {}}+ onConnectorRefreshed={() => {}}+ onDisconnect={() => {}}+ />+ </ServerProvider>,+ );++ await waitFor(() => {+ expect(getToolsByConnectorId).toHaveBeenCalledTimes(1);+ });+ fireEvent.click(screen.getByRole('button', { name: 'Connect' }));++ await waitFor(() => {+ expect(oauthMock.handleAuthorize).toHaveBeenCalledWith(unauthenticatedConnector.id, expect.any(Function));+ });+ expect(getConnector).not.toHaveBeenCalled();+ expect(getToolsByConnectorId).toHaveBeenCalledTimes(1);+ });++ it('refreshes the connector and its tools after successful OAuth', async () => {+ const refreshedConnector: ConnectorBase = {+ ...unauthenticatedConnector,+ authenticated: true,+ requiresAuth: false,+ };+ const initialTools: ToolBase[] = [+ { id: 'tool-before-auth', name: 'before_auth', description: 'Available before auth' },+ ];+ const refreshedTools: ToolBase[] = [+ { id: 'tool-after-auth', name: 'after_auth', description: 'Available after auth' },+ ];+ const getConnector = vi.fn(async () => refreshedConnector);+ const getToolsByConnectorId = vi.fn().mockResolvedValueOnce(initialTools).mockResolvedValueOnce(refreshedTools);+ oauthMock.handleAuthorize.mockImplementation(async (_id: string, callback: (isSuccess: boolean) => void) => {+ callback(true);+ });+ const server = createMockAgentUIServer({+ catalog: createMockCatalog({+ connectorCatalog: {+ getConnectorCatalog: async () => [],+ listConnectors: async () => [],+ getConnector,+ getToolsByConnectorId,+ createConnector: async () => unauthenticatedConnector,+ updateConnector: async () => unauthenticatedConnector,+ authenticateConnector: async () => ({ authorization_endpoint: '' }),+ disconnectConnector: async () => unauthenticatedConnector,+ },+ }),+ });++ function StatefulDetails() {+ const [currentConnector, setCurrentConnector] = useState(unauthenticatedConnector);+ return (+ <ConnectorDetails+ connector={currentConnector}+ onBack={() => {}}+ onConnectorRefreshed={setCurrentConnector}+ onDisconnect={() => {}}+ />+ );+ }++ render(+ <ServerProvider server={server}>+ <StatefulDetails />+ </ServerProvider>,+ );++ expect(await screen.findByText('before_auth')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: 'Connect' }));++ await waitFor(() => {+ expect(screen.getByText('Connected')).toBeInTheDocument();+ expect(screen.getByText('after_auth')).toBeInTheDocument();+ });+ expect(getConnector).toHaveBeenCalledWith({ id: unauthenticatedConnector.id });+ expect(getToolsByConnectorId).toHaveBeenCalledTimes(2);+ expect(screen.queryByRole('button', { name: 'Connect' })).not.toBeInTheDocument();+ expect(screen.getByRole('button', { name: 'Disconnect' })).toBeInTheDocument();+ });+});