🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
'use client';
import { Alert, Block, Flexbox, Text, toast } from '@lobehub/ui';
import { memo, useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import ImperativeModal from '@/components/ImperativeModal';
import InlineErrorText from '@/components/InlineErrorText';
import PluginAvatar from '@/components/Plugins/PluginAvatar';
import PluginTag from '@/components/Plugins/PluginTag';
import { usePermission } from '@/hooks/usePermission';
import { useAgentStore } from '@/store/agent';
import { useToolStore } from '@/store/tool';
import { mcpStoreSelectors } from '@/store/tool/selectors';
import { type McpConnectionParams } from '@/types/plugins';
import ConfigDisplay from './ConfigDisplay';
import { type McpInstallRequest } from './types';
interface CustomPluginInstallModalProps {
installRequest: McpInstallRequest | null;
isMarketplace?: boolean;
onComplete?: () => void;
}
const CustomPluginInstallModal = memo<CustomPluginInstallModalProps>(
({ installRequest, isMarketplace = false, onComplete }) => {
const { t } = useTranslation('plugin');
const [loading, setLoading] = useState(false);
const [installError, setInstallError] = useState<{ detail?: string; summary: string } | null>(
null,
);
const { allowed: canCreate } = usePermission('create_content');
const { allowed: canEdit } = usePermission('edit_own_content');
// Track config updates
const [updatedConfig, setUpdatedConfig] = useState<{
env?: Record<string, string>;
headers?: Record<string, string>;
}>({});
const installCustomMcpConnector = useToolStore((s) => s.installCustomMcpConnector);
const testMcpConnection = useToolStore((s) => s.testMcpConnection);
const togglePlugin = useAgentStore((s) => s.togglePlugin);
// Generate a unique identifier for custom plugin connection testing
const identifier = installRequest?.schema?.identifier || '';
const testState = useToolStore(mcpStoreSelectors.getMCPConnectionTestState(identifier));
const schema = installRequest?.schema;
const isStdioMcp = schema?.config.type === 'stdio';
// Reset loading state and config
useEffect(() => {
if (!installRequest) {
setLoading(false);
setInstallError(null);
setUpdatedConfig({});
}
}, [installRequest]);
const handleConfirm = useCallback(async () => {
if (!canCreate || !canEdit || !installRequest || !schema) return;
setLoading(true);
setInstallError(null);
try {
// Merge original config with user-updated config
const finalConfig = {
...schema.config,
env: updatedConfig.env || schema.config.env,
headers: updatedConfig.headers || schema.config.headers,
};
// Custom plugin: test connection first to get the real manifest
const testParams: McpConnectionParams = {
connection: finalConfig,
identifier,
metadata: {
avatar: schema.icon,
description: schema.description,
},
};
const testResult = await testMcpConnection(testParams);
if (!testResult.success) {
throw new Error(testResult.error || t('protocolInstall.messages.connectionTestFailed'));
}
if (!testResult.manifest) {
throw new Error(t('protocolInstall.messages.manifestNotFound'));
}
// Third-party marketplace and custom MCP servers are added as custom connectors
await installCustomMcpConnector({
config: finalConfig,
description: schema.description,
icon: schema.icon,
identifier: schema.identifier,
name: schema.name,
});
await togglePlugin(schema.identifier, true);
toast.success(t('protocolInstall.messages.installSuccess', { name: schema.name }));
onComplete?.();
} catch (error) {
console.error('Plugin installation error:', error);
setInstallError({
detail: error instanceof Error ? error.message : undefined,
summary: t('protocolInstall.messages.installFailedRetry'),
});
setLoading(false);
}
}, [
installRequest,
canCreate,
canEdit,
schema,
updatedConfig,
onComplete,
installCustomMcpConnector,
testMcpConnection,
togglePlugin,
t,
identifier,
]);
const handleCancel = useCallback(() => {
onComplete?.();
}, [onComplete]);
if (!installRequest || !schema) return null;
// Render different Alert components based on type
const renderAlert = () => {
const sourceAlert = !isMarketplace ? (
<Alert
showIcon
title={t('protocolInstall.custom.security.description')}
type="warning"
variant={'borderless'}
/>
) : (
<Alert
showIcon
title={t('protocolInstall.marketplace.unverified.warning')}
type="warning"
variant={'borderless'}
/>
);
return (
<Flexbox gap={8}>
{sourceAlert}
{isStdioMcp && (
<Alert
showIcon
description={t('protocolInstall.stdio.commandExecution.description')}
title={t('protocolInstall.stdio.commandExecution.title')}
type="warning"
variant={'borderless'}
/>
)}
</Flexbox>
);
};
const modalTitle = isMarketplace
? t('protocolInstall.marketplace.title')
: t('protocolInstall.custom.title');
const okText = isStdioMcp
? t('protocolInstall.actions.runCommandAndInstall')
: isMarketplace
? t('protocolInstall.actions.install')
: t('protocolInstall.actions.installAnyway');
return (
<ImperativeModal
open
confirmLoading={loading || testState.loading}
okButtonProps={{ disabled: !canCreate || !canEdit }}
okText={okText}
title={modalTitle}
width={680}
onCancel={handleCancel}
onOk={handleConfirm}
>
<Flexbox gap={24}>
{renderAlert()}
<Block horizontal gap={16} justify={'space-between'} padding={16} variant={'outlined'}>
<Flexbox horizontal gap={16}>
<PluginAvatar avatar={schema.icon} size={40} />
<Flexbox gap={2}>
<Flexbox horizontal align={'center'} gap={8}>
{schema.name}
<PluginTag type={'customPlugin'} />
</Flexbox>
<Text style={{ fontSize: 12 }} type={'secondary'}>
{schema.description}
</Text>
</Flexbox>
</Flexbox>
</Block>
<Flexbox>
<ConfigDisplay
schema={schema}
onConfigUpdate={(config) => {
setInstallError(null);
setUpdatedConfig(config);
}}
/>
{/* Show connection test error */}
{testState.error && (
<Alert
closable
showIcon
description={testState.error}
title={t('protocolInstall.messages.connectionTestFailed')}
type="error"
variant={'filled'}
/>
)}
</Flexbox>
{installError && (
<InlineErrorText detail={installError.detail}>{installError.summary}</InlineErrorText>
)}
</Flexbox>
</ImperativeModal>
);
},
);
CustomPluginInstallModal.displayName = 'CustomPluginInstallModal';
export default CustomPluginInstallModal;