@@ -2,6 +2,7 @@ import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit'
22import { resolvePrincipalAuditAttribution } from '@sim/auth/principal'
33import { credentialGroup as credentialGroupTable } from '@sim/db/schema'
44import { eq } from 'drizzle-orm'
5+ import type { StartOrganizationAccountConnectionBody } from '@/lib/api/contracts/organization-accounts'
56import type { OperationUseCase } from '@/lib/core/application/operation'
67import {
78 authorizeOrganizationOperation ,
@@ -14,7 +15,10 @@ import {
1415import { OrchestrationError } from '@/lib/core/orchestration/types'
1516import { validateUpdateCredentialGroupInput } from '@/lib/credential-groups/application/validation'
1617import { loadScopedAccountsCredentialListContext } from '@/lib/credential-groups/credentials'
17- import { createCredentialGroupOAuthStartUrl } from '@/lib/credential-groups/enrollment-links'
18+ import {
19+ createCredentialGroupMcpOAuthStartUrl ,
20+ createCredentialGroupOAuthStartUrl ,
21+ } from '@/lib/credential-groups/enrollment-links'
1822import { CredentialGroupEnrollmentError } from '@/lib/credential-groups/enrollments'
1923import { ManagedMcpConnectorError } from '@/lib/credential-groups/managed-mcp-service'
2024import { requireOrganizationAccountsSetup } from '@/lib/credential-groups/organization-setup'
@@ -30,7 +34,10 @@ import type {
3034 CredentialGroupOptionInput ,
3135 UpdateCredentialGroupInput ,
3236} from '@/lib/credential-groups/types'
33- import { listViewerOrganizationAccounts } from '@/lib/credential-groups/viewer-accounts'
37+ import {
38+ listViewerOrganizationAccounts ,
39+ listViewerOrganizationMcpAccounts ,
40+ } from '@/lib/credential-groups/viewer-accounts'
3441import { isKnowledgeMemberAccessAvailable } from '@/lib/knowledge/access/availability'
3542
3643export const organizationAccountOperations = {
@@ -149,6 +156,13 @@ export const getOrganizationAccountsSettings = defineOrganizationAccountsUseCase
149156 const credentialGroup = await getOrganizationAccountsGroup ( context . organizationId )
150157 return {
151158 credentialGroup,
159+ viewerMcpAccounts : credentialGroup
160+ ? await listViewerOrganizationMcpAccounts ( {
161+ organizationId : context . organizationId ,
162+ userId : context . userId ,
163+ matching : eq ( credentialGroupTable . id , credentialGroup . id ) ,
164+ } )
165+ : [ ] ,
152166 viewerAccounts : credentialGroup
153167 ? await listViewerOrganizationAccounts ( {
154168 organizationId : context . organizationId ,
@@ -225,12 +239,25 @@ export const startOrganizationAccountConnection = defineOrganizationAccountsUseC
225239 input,
226240 context,
227241 } : {
228- input : OrganizationAccountsInput & { optionId : string }
242+ input : OrganizationAccountsInput & StartOrganizationAccountConnectionBody
229243 context : OrganizationMembershipContext
230244 } ) {
231245 const group = await getOrganizationAccountsGroup ( context . organizationId )
232246 if ( ! group || group . status !== 'active' )
233247 throw new OrchestrationError ( 'not_found' , 'Ask an organization admin to set up this source' )
248+ if ( 'mcpServerId' in input ) {
249+ if ( ! group . mcpServers . some ( ( server ) => server . id === input . mcpServerId && server . enabled ) )
250+ throw new OrchestrationError ( 'not_found' , 'This account provider is no longer available' )
251+ const { invitationLink } = await createViewerCredentialGroupEnrollment ( {
252+ organizationId : context . organizationId ,
253+ userId : context . userId ,
254+ credentialGroupId : group . id ,
255+ } )
256+ return {
257+ invitationLink,
258+ authorizationUrl : createCredentialGroupMcpOAuthStartUrl ( invitationLink , input . mcpServerId ) ,
259+ }
260+ }
234261 if (
235262 ! group . options . some ( ( option ) => option . id === input . optionId && option . status === 'active' )
236263 ) {
0 commit comments