diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/CredentialsGroupedView.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/CredentialsGroupedView.tsx index 22d0a318a9..da7623b130 100644 --- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/CredentialsGroupedView.tsx +++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/CredentialsGroupedView.tsx @@ -16,6 +16,7 @@ import { areSystemCredentialProvidersLoading, CredentialField, findSavedCredentialByProviderAndType, + findSavedUserCredentialByProviderAndType, hasMissingRequiredSystemCredentials, splitCredentialFieldsBySystem, } from "./helpers"; @@ -45,6 +46,7 @@ export function CredentialsGroupedView({ const hasSystemCredentials = systemCredentialFields.length > 0; const hasUserCredentials = userCredentialFields.length > 0; const hasAttemptedAutoSelect = useRef(false); + const hasAttemptedUserAutoSelect = useRef(false); const isLoadingProviders = useMemo( () => @@ -111,6 +113,49 @@ export function CredentialsGroupedView({ isLoadingProviders, ]); + // Auto-select user credentials when there is exactly one matching credential. + // This handles cases like MCP where the credential is optional but should + // still be pre-selected when only one credential matches the server URL. + useEffect(() => { + if (hasAttemptedUserAutoSelect.current) return; + if (!hasUserCredentials) return; + if (!allProviders) return; + + for (const [key, schema] of userCredentialFields) { + const alreadySelected = inputCredentials?.[key]; + if (alreadySelected) continue; + + const providerNames = schema.credentials_provider || []; + const credentialTypes = schema.credentials_types || []; + const requiredScopes = schema.credentials_scopes; + const discriminatorValues = schema.discriminator_values; + const savedCredential = findSavedUserCredentialByProviderAndType( + providerNames, + credentialTypes, + requiredScopes, + allProviders, + discriminatorValues, + ); + + if (savedCredential) { + onCredentialChange(key, { + id: savedCredential.id, + provider: savedCredential.provider, + type: savedCredential.type as CredentialsType, + title: savedCredential.title, + }); + } + } + + hasAttemptedUserAutoSelect.current = true; + }, [ + allProviders, + hasUserCredentials, + userCredentialFields, + inputCredentials, + onCredentialChange, + ]); + return (
{hasUserCredentials && (