Files
simstudioai--sim/apps/sim/hooks/mcp/use-mcp-oauth-popup.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

141 lines
5.1 KiB
TypeScript

'use client'
import { useCallback, useEffect, useRef, useState } from 'react'
import { toast } from '@sim/emcn'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { useQueryClient } from '@tanstack/react-query'
import type { McpOauthCallbackMessage, McpOauthCallbackReason } from '@/lib/mcp/oauth'
import { mcpKeys, useStartMcpOauth } from '@/hooks/queries/mcp'
const logger = createLogger('useMcpOauthPopup')
function reasonToMessage(reason: McpOauthCallbackReason | undefined): string {
switch (reason) {
case 'provider_error':
return 'The authorization server returned an error. Please try again.'
case 'invalid_state':
return 'Authorization expired. Please try again.'
case 'user_mismatch':
return 'You must complete authorization as the same user who started it.'
case 'server_gone':
return 'This MCP server no longer exists.'
case 'insecure_url':
return 'MCP OAuth requires https.'
case 'token_exchange_failed':
return 'Failed to complete token exchange with the authorization server.'
case 'unauthenticated':
return 'Please sign in and try again.'
case 'missing_params':
return 'The authorization callback was missing required parameters.'
default:
return 'Authorization failed. Please try again.'
}
}
interface UseMcpOauthPopupProps {
workspaceId: string
}
export function useMcpOauthPopup({ workspaceId }: UseMcpOauthPopupProps) {
const queryClient = useQueryClient()
const { mutateAsync: startOauth } = useStartMcpOauth()
const [connectingServers, setConnectingServers] = useState<Set<string>>(() => new Set())
const popupIntervalsRef = useRef<Map<string, number>>(new Map())
useEffect(() => {
const intervals = popupIntervalsRef.current
return () => {
for (const id of intervals.values()) window.clearInterval(id)
intervals.clear()
}
}, [])
useEffect(() => {
function onMessage(event: MessageEvent) {
if (event.origin !== window.location.origin) return
const data = event.data as Partial<McpOauthCallbackMessage> | null
if (data?.type !== 'mcp-oauth') return
if (data.serverId) {
const serverId = data.serverId
const interval = popupIntervalsRef.current.get(serverId)
if (interval !== undefined) {
window.clearInterval(interval)
popupIntervalsRef.current.delete(serverId)
}
setConnectingServers((prev) => {
if (!prev.has(serverId)) return prev
const next = new Set(prev)
next.delete(serverId)
return next
})
} else if (!data.ok) {
// Early callback failures (missing params, invalid state) post back
// without a serverId, so we can't target a specific row — clear all
// in-flight popups instead of leaving the UI stuck on "Connecting…".
for (const id of popupIntervalsRef.current.values()) window.clearInterval(id)
popupIntervalsRef.current.clear()
setConnectingServers((prev) => (prev.size === 0 ? prev : new Set()))
}
if (data.ok) {
queryClient.invalidateQueries({ queryKey: mcpKeys.serversList(workspaceId) })
if (data.serverId) {
queryClient.invalidateQueries({
queryKey: mcpKeys.serverToolsList(workspaceId, data.serverId),
})
} else {
queryClient.invalidateQueries({
queryKey: mcpKeys.serverToolsWorkspace(workspaceId),
})
}
queryClient.invalidateQueries({ queryKey: mcpKeys.storedToolsList(workspaceId) })
toast.success('Server authorized')
} else {
toast.error(reasonToMessage(data.reason))
}
}
window.addEventListener('message', onMessage)
return () => window.removeEventListener('message', onMessage)
}, [queryClient, workspaceId])
const startOauthForServer = useCallback(
async (serverId: string) => {
setConnectingServers((prev) => new Set(prev).add(serverId))
const clear = () => {
const existing = popupIntervalsRef.current.get(serverId)
if (existing !== undefined) {
window.clearInterval(existing)
popupIntervalsRef.current.delete(serverId)
}
setConnectingServers((prev) => {
const next = new Set(prev)
next.delete(serverId)
return next
})
}
try {
const result = await startOauth({ serverId, workspaceId })
if (result.status === 'already_authorized') {
clear()
return
}
const { popup } = result
const existing = popupIntervalsRef.current.get(serverId)
if (existing !== undefined) window.clearInterval(existing)
const interval = window.setInterval(() => {
if (popup.closed) clear()
}, 500)
popupIntervalsRef.current.set(serverId, interval)
} catch (e) {
clear()
logger.error('Failed to start MCP OAuth', e)
toast.error(toError(e).message || 'Failed to start authorization')
}
},
[startOauth, workspaceId]
)
return { connectingServers, startOauthForServer }
}