RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
/*
* Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Collapse } from '@/components/collapse';
import { Button, ButtonLoading } from '@/components/ui/button';
import { useMcpReadOnly } from '@/ee/pages/user-setting/mcp/use-mcp-read-only';
import { Card, CardContent } from '@/components/ui/card';
import { DialogFooter } from '@/components/ui/dialog';
import { Modal } from '@/components/ui/modal/modal';
import { useGetMcpServer, useTestMcpServer } from '@/hooks/use-mcp-request';
import { IModalProps } from '@/interfaces/common';
import { IMCPTool, IMCPToolObject } from '@/interfaces/database/mcp';
import { cn } from '@/lib/utils';
import { zodResolver } from '@hookform/resolvers/zod';
import { isEmpty, omit, pick } from 'lodash';
import { RefreshCw } from 'lucide-react';
import {
MouseEventHandler,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
import {
EditMcpForm,
FormId,
ServerType,
useBuildFormSchema,
} from './edit-mcp-form';
import { McpToolCard } from './tool-card';
import { connectionFields, TokenMask } from './mcp-connection';
function transferToolToArray(tools: IMCPToolObject) {
return Object.entries(tools).reduce<IMCPTool[]>((pre, [name, tool]) => {
pre.push({ ...tool, name });
return pre;
}, []);
}
const DefaultValues = {
name: '',
server_type: ServerType.SSE,
url: '',
authorization_token: '',
};
export function EditMcpDialog({
hideModal,
loading,
onOk,
id,
operatorPermission,
}: IModalProps<any> & { id: string; operatorPermission?: number }) {
const { t } = useTranslation();
const {
testMcpServer,
data: testData,
loading: testLoading,
} = useTestMcpServer();
const [isTriggeredBySaving, setIsTriggeredBySaving] = useState(false);
const FormSchema = useBuildFormSchema();
const [collapseOpen, setCollapseOpen] = useState(true);
const { data } = useGetMcpServer(id);
const [fieldChanged, setFieldChanged] = useState(false);
const tools = useMemo<IMCPTool[]>(() => {
const tested = testData?.data;
if (tested?.length) {
return tested;
}
return transferToolToArray(data.variables?.tools || {});
}, [testData?.data, data.variables?.tools]);
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: DefaultValues,
});
const handleTest: MouseEventHandler<HTMLButtonElement> = useCallback((e) => {
e.stopPropagation();
setIsTriggeredBySaving(false);
}, []);
const handleSave: MouseEventHandler<HTMLButtonElement> = useCallback(() => {
setIsTriggeredBySaving(true);
}, []);
const handleOk = async (values: z.infer<typeof FormSchema>) => {
// A masked token is preserved by the backend when editing a saved server.
const nextValues = {
...omit(values, 'authorization_token'),
...connectionFields(values.authorization_token, data),
};
if (isTriggeredBySaving) {
onOk?.(nextValues);
} else {
const ret = await testMcpServer({ ...nextValues, mcp_id: id });
if (ret.code === 0) {
setFieldChanged(false);
}
}
};
useEffect(() => {
if (!isEmpty(data)) {
form.reset({
...pick(data, ['name', 'server_type', 'url']),
authorization_token: data.variables?.authorization_token
? TokenMask
: '',
});
}
}, [data, form]);
const disabled = !tools?.length || testLoading || fieldChanged;
const isReadOnly = useMcpReadOnly(
operatorPermission ?? data.operator_permission,
);
return (
<Modal
title={
id
? isReadOnly
? t('common.view')
: t('mcp.editMCP')
: t('mcp.addMCP')
}
open={true}
onCancel={hideModal}
cancelText={t('common.cancel')}
okText={t('common.save')}
maskClosable={false}
footer={
<DialogFooter>
<Button variant="outline" type="button" onClick={hideModal}>
{t('common.cancel')}
</Button>
<ButtonLoading
type="submit"
form={FormId}
loading={loading}
onClick={handleSave}
disabled={disabled}
>
{t('common.save')}
</ButtonLoading>
</DialogFooter>
}
>
<EditMcpForm
onOk={handleOk}
form={form}
setFieldChanged={setFieldChanged}
readOnly={isReadOnly}
></EditMcpForm>
<Card className="bg-transparent mt-4">
<CardContent className="p-3">
<Collapse
title={
<div>
{tools?.length || 0} {t('mcp.toolsAvailable')}
</div>
}
open={collapseOpen}
onOpenChange={setCollapseOpen}
rightContent={
isReadOnly ? undefined : (
<Button
variant={'transparent'}
form={FormId}
type="submit"
onClick={handleTest}
className="border-none p-0 text-text-secondary hover:bg-transparent hover:text-text-primary"
>
<RefreshCw
className={cn({
'animate-spin': testLoading,
})}
/>
</Button>
)
}
>
<div className="overflow-auto max-h-80 divide-y-[0.5px] divide-border-button bg-bg-card rounded-md px-2.5 scrollbar-auto">
{tools?.map((x) => (
<McpToolCard key={x.name} data={x}></McpToolCard>
))}
</div>
</Collapse>
</CardContent>
</Card>
</Modal>
);
}