'use client' import type { ToolWithProvider } from '@/app/components/workflow/types' import { Button } from '@langgenius/dify-ui/button' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { STEP_BY_STEP_TOUR_TARGETS } from '@/app/components/step-by-step-tour/target-registry' import { useCanManageMCP } from '@/app/components/tools/hooks/use-tool-permissions' import { useDocLink } from '@/context/i18n' import { useCreateMCP } from '@/service/use-tools' import CreateEntryCard from '../provider/create-entry-card' import MCPModal from './modal' type Props = Readonly<{ handleCreate: (provider: ToolWithProvider) => Promise | void }> function useMCPCreateAction({ handleCreate }: Props) { const canManageMCP = useCanManageMCP() const { mutateAsync: createMCP } = useCreateMCP() const [showModal, setShowModal] = useState(false) const create = async (info: Parameters[0]) => { if (!canManageMCP) return const provider = await createMCP(info) await handleCreate(provider) } return { canManageMCP, create, setShowModal, showModal, } } export function NewMCPButton({ handleCreate }: Props) { const { t } = useTranslation() const addMCPServerLabel = t(($) => $['mcp.create.cardTitle'], { ns: 'tools' }) const { canManageMCP, create, setShowModal, showModal } = useMCPCreateAction({ handleCreate }) if (!canManageMCP) return null return ( <> {canManageMCP && showModal && ( setShowModal(false)} /> )} ) } const NewMCPCard = ({ handleCreate }: Props) => { const { t } = useTranslation() const docLink = useDocLink() const { canManageMCP, create, setShowModal, showModal } = useMCPCreateAction({ handleCreate }) const linkUrl = useMemo(() => docLink('/use-dify/workspace/tools#mcp'), [docLink]) return ( <> {canManageMCP && ( $['mcp.create.cardTitle'], { ns: 'tools' })} linkText={t(($) => $['mcp.create.cardLink'], { ns: 'tools' })} linkUrl={linkUrl} onCreate={() => setShowModal(true)} /> )} {canManageMCP && showModal && ( setShowModal(false)} /> )} ) } export default NewMCPCard