Feat: Synchronize MCP data to agent #3221 (#8832)

### What problem does this PR solve?

Feat: Synchronize MCP data to agent #3221
### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-07-15 09:37:08 +08:00
committed by GitHub
parent c642dbefca
commit f683580310
10 changed files with 242 additions and 18 deletions

View File

@ -1,5 +1,6 @@
import useGraphStore from '../../store';
import { ToolFormConfigMap } from './constant';
import MCPForm from './mcp-form';
const EmptyContent = () => <div></div>;
@ -8,6 +9,7 @@ const ToolForm = () => {
const ToolForm =
ToolFormConfigMap[clickedToolId as keyof typeof ToolFormConfigMap] ??
MCPForm ??
EmptyContent;
return (

View File

@ -0,0 +1,103 @@
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Checkbox } from '@/components/ui/checkbox';
import {
Form,
FormControl,
FormField,
FormItem,
FormMessage,
} from '@/components/ui/form';
import { useGetMcpServer } from '@/hooks/use-mcp-request';
import useGraphStore from '@/pages/agent/store';
import { zodResolver } from '@hookform/resolvers/zod';
import { memo } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { MCPCard } from './mcp-card';
import { useValues } from './use-values';
import { useWatchFormChange } from './use-watch-change';
const FormSchema = z.object({
items: z.array(z.string()),
});
function MCPForm() {
const clickedToolId = useGraphStore((state) => state.clickedToolId);
const values = useValues();
const form = useForm({
defaultValues: values,
resolver: zodResolver(FormSchema),
});
const { data } = useGetMcpServer(clickedToolId);
useWatchFormChange(form);
return (
<Form {...form}>
<form
className="space-y-6 p-4"
onSubmit={(e) => {
e.preventDefault();
}}
>
<Card className="bg-background-highlight p-5">
<CardHeader className="p-0 pb-3">
<CardTitle>{data.name}</CardTitle>
</CardHeader>
<CardContent className="p-0">
<span className="pr-2"> URL:</span>
<a href={data.url} className="text-background-checked">
{data.url}
</a>
</CardContent>
</Card>
<FormField
control={form.control}
name="items"
render={() => (
<FormItem className="space-y-2">
{Object.entries(data.variables?.tools || {}).map(
([name, mcp]) => (
<FormField
key={name}
control={form.control}
name="items"
render={({ field }) => {
return (
<FormItem
key={name}
className="flex flex-row items-center gap-2"
>
<FormControl>
<MCPCard key={name} data={{ ...mcp, name }}>
<Checkbox
className="translate-y-1"
checked={field.value?.includes(name)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, name])
: field.onChange(
field.value?.filter(
(value) => value !== name,
),
);
}}
/>
</MCPCard>
</FormControl>
</FormItem>
);
}}
/>
),
)}
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
);
}
export default memo(MCPForm);

View File

@ -0,0 +1,20 @@
import { Card, CardContent, CardTitle } from '@/components/ui/card';
import { IMCPTool } from '@/interfaces/database/mcp';
import { PropsWithChildren } from 'react';
export function MCPCard({
data,
children,
}: { data: IMCPTool } & PropsWithChildren) {
return (
<Card className="p-3">
<CardContent className="p-0 flex gap-3">
{children}
<section>
<CardTitle className="pb-3">{data.name}</CardTitle>
<p>{data.description}</p>
</section>
</CardContent>
</Card>
);
}

View File

@ -0,0 +1,26 @@
import useGraphStore from '@/pages/agent/store';
import { getAgentNodeMCP } from '@/pages/agent/utils';
import { isEmpty } from 'lodash';
import { useMemo } from 'react';
export function useValues() {
const { clickedToolId, clickedNodeId, findUpstreamNodeById } = useGraphStore(
(state) => state,
);
const values = useMemo(() => {
const agentNode = findUpstreamNodeById(clickedNodeId);
const mcpList = getAgentNodeMCP(agentNode);
const formData =
mcpList.find((x) => x.mcp_id === clickedToolId)?.tools || {};
if (isEmpty(formData)) {
return { items: [] };
}
return { items: Object.keys(formData) };
}, [clickedNodeId, clickedToolId, findUpstreamNodeById]);
return values;
}

View File

@ -0,0 +1,47 @@
import { useGetMcpServer } from '@/hooks/use-mcp-request';
import useGraphStore from '@/pages/agent/store';
import { getAgentNodeMCP } from '@/pages/agent/utils';
import { pick } from 'lodash';
import { useEffect, useMemo } from 'react';
import { UseFormReturn, useWatch } from 'react-hook-form';
export function useWatchFormChange(form?: UseFormReturn<any>) {
let values = useWatch({ control: form?.control });
const { clickedToolId, clickedNodeId, findUpstreamNodeById, updateNodeForm } =
useGraphStore((state) => state);
const { data } = useGetMcpServer(clickedToolId);
const nextMCPTools = useMemo(() => {
const mcpTools = data.variables?.tools || [];
values = form?.getValues();
return pick(mcpTools, values.items);
}, [values, data?.variables]);
useEffect(() => {
const agentNode = findUpstreamNodeById(clickedNodeId);
// Manually triggered form updates are synchronized to the canvas
if (agentNode) {
const agentNodeId = agentNode?.id;
const mcpList = getAgentNodeMCP(agentNode);
const nextMCP = mcpList.map((x) => {
if (x.mcp_id === clickedToolId) {
return {
...x,
tools: nextMCPTools,
};
}
return x;
});
updateNodeForm(agentNodeId, nextMCP, ['mcp']);
}
}, [
clickedNodeId,
clickedToolId,
findUpstreamNodeById,
nextMCPTools,
updateNodeForm,
]);
}