Skip to content

Commit 554b2ca

Browse files
authored
perf: mcp tool type (#4820)
1 parent 4e83840 commit 554b2ca

File tree

20 files changed

+74
-61
lines changed

20 files changed

+74
-61
lines changed

packages/global/core/app/mcpTools/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
FlowNodeTypeEnum
66
} from '../../workflow/node/constant';
77
import { nanoid } from 'nanoid';
8-
import { type ToolType } from '../type';
8+
import { type McpToolConfigType } from '../type';
99
import { i18nT } from '../../../../web/i18n/utils';
1010
import { type RuntimeNodeItemType } from '../../workflow/runtime/type';
1111

@@ -16,7 +16,7 @@ export const getMCPToolSetRuntimeNode = ({
1616
avatar
1717
}: {
1818
url: string;
19-
toolList: ToolType[];
19+
toolList: McpToolConfigType[];
2020
name?: string;
2121
avatar?: string;
2222
}): RuntimeNodeItemType => {
@@ -45,7 +45,7 @@ export const getMCPToolRuntimeNode = ({
4545
url,
4646
avatar = 'core/app/type/mcpToolsFill'
4747
}: {
48-
tool: ToolType;
48+
tool: McpToolConfigType;
4949
url: string;
5050
avatar?: string;
5151
}): RuntimeNodeItemType => {
@@ -65,7 +65,7 @@ export const getMCPToolRuntimeNode = ({
6565
...Object.entries(tool.inputSchema?.properties || {}).map(([key, value]) => ({
6666
key,
6767
label: key,
68-
valueType: value.type as WorkflowIOValueTypeEnum,
68+
valueType: value.type as WorkflowIOValueTypeEnum, // TODO: 这里需要做一个映射
6969
description: value.description,
7070
toolDescription: value.description || key,
7171
required: tool.inputSchema?.required?.includes(key) || false,

packages/global/core/app/type.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ import { FlowNodeInputTypeEnum } from '../../core/workflow/node/constant';
1616
import type { WorkflowTemplateBasicType } from '@fastgpt/global/core/workflow/type';
1717
import type { SourceMemberType } from '../../support/user/type';
1818

19-
export type ToolType = {
20-
name: string;
21-
description: string;
22-
inputSchema: {
23-
type: string;
24-
properties?: Record<string, { type: string; description?: string }>;
25-
required?: string[];
26-
};
27-
};
28-
2919
export type AppSchema = {
3020
_id: string;
3121
parentId?: ParentIdType;
@@ -117,6 +107,16 @@ export type AppSimpleEditFormType = {
117107
chatConfig: AppChatConfigType;
118108
};
119109

110+
export type McpToolConfigType = {
111+
name: string;
112+
description: string;
113+
inputSchema: {
114+
type: string;
115+
properties?: Record<string, { type: string; description?: string }>;
116+
required?: string[];
117+
};
118+
};
119+
120120
/* app chat config type */
121121
export type AppChatConfigType = {
122122
welcomeText?: string;

packages/global/core/workflow/runtime/type.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from '../../chat/type';
88
import { NodeOutputItemType } from '../../chat/type';
99
import type { FlowNodeInputItemType, FlowNodeOutputItemType } from '../type/io.d';
10-
import type { StoreNodeItemType } from '../type/node';
10+
import type { NodeToolConfigType, StoreNodeItemType } from '../type/node';
1111
import type { DispatchNodeResponseKeyEnum } from './constants';
1212
import type { StoreEdgeItemType } from '../type/edge';
1313
import type { NodeInputKeyEnum } from '../constants';
@@ -102,6 +102,9 @@ export type RuntimeNodeItemType = {
102102

103103
pluginId?: string; // workflow id / plugin id
104104
version?: string;
105+
106+
// tool
107+
toolConfig?: NodeToolConfigType;
105108
};
106109

107110
export type RuntimeEdgeItemType = StoreEdgeItemType & {
@@ -114,7 +117,7 @@ export type DispatchNodeResponseType = {
114117
runningTime?: number;
115118
query?: string;
116119
textOutput?: string;
117-
error?: Record<string, any>;
120+
error?: Record<string, any> | string;
118121
customInputs?: Record<string, any>;
119122
customOutputs?: Record<string, any>;
120123
nodeInputs?: Record<string, any>;

packages/global/core/workflow/type/node.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ import { RuntimeNodeItemType } from '../runtime/type';
2020
import { PluginTypeEnum } from '../../plugin/constants';
2121
import { RuntimeEdgeItemType, StoreEdgeItemType } from './edge';
2222
import { NextApiResponse } from 'next';
23-
import { AppDetailType, AppSchema } from '../../app/type';
23+
import type { AppDetailType, AppSchema, McpToolConfigType } from '../../app/type';
2424
import type { ParentIdType } from 'common/parentFolder/type';
25-
import { AppTypeEnum } from 'core/app/constants';
25+
import { AppTypeEnum } from '../../app/constants';
2626
import type { WorkflowInteractiveResponseType } from '../template/system/interactive/type';
2727

28+
export type NodeToolConfigType = {
29+
mcpTool?: McpToolConfigType & {
30+
url: string;
31+
};
32+
};
33+
2834
export type FlowNodeCommonType = {
2935
parentNodeId?: string;
3036
flowNodeType: FlowNodeTypeEnum; // render node card
@@ -46,8 +52,10 @@ export type FlowNodeCommonType = {
4652
// plugin data
4753
pluginId?: string;
4854
isFolder?: boolean;
49-
// pluginType?: AppTypeEnum;
5055
pluginData?: PluginDataType;
56+
57+
// tool data
58+
toolData?: NodeToolConfigType;
5159
};
5260

5361
export type PluginDataType = {

packages/service/core/app/mcp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
22
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
33
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
4-
import { type ToolType } from '@fastgpt/global/core/app/type';
4+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
55
import { addLog } from '../../common/system/log';
66
import { retryFn } from '@fastgpt/global/common/system/utils';
77

@@ -41,7 +41,7 @@ export class MCPClient {
4141
* Get available tools list
4242
* @returns List of tools
4343
*/
44-
public async getTools(): Promise<ToolType[]> {
44+
public async getTools(): Promise<McpToolConfigType[]> {
4545
try {
4646
const client = await this.getConnection();
4747
const response = await client.listTools();

projects/app/src/components/core/chat/ChatContainer/ChatBox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ const ChatBox = ({
553553
const responseData = mergeChatResponseData(item.responseData || []);
554554
if (responseData[responseData.length - 1]?.error) {
555555
toast({
556-
title: t(responseData[responseData.length - 1].error?.message),
556+
title: t(getErrText(responseData[responseData.length - 1].error)),
557557
status: 'error'
558558
});
559559
}

projects/app/src/components/core/chat/ChatContainer/PluginRunBox/context.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ChatRecordContext } from '@/web/core/chat/context/chatRecordContext';
2222
import { type AppFileSelectConfigType } from '@fastgpt/global/core/app/type';
2323
import { defaultAppSelectFileConfig } from '@fastgpt/global/core/app/constants';
2424
import { mergeChatResponseData } from '@fastgpt/global/core/chat/utils';
25+
import { getErrText } from '@fastgpt/global/common/error/utils';
2526

2627
type PluginRunContextType = PluginRunBoxProps & {
2728
isChatting: boolean;
@@ -258,7 +259,7 @@ const PluginRunContextProvider = ({
258259
const responseData = mergeChatResponseData(item.responseData || []);
259260
if (responseData[responseData.length - 1]?.error) {
260261
toast({
261-
title: t(responseData[responseData.length - 1].error?.message),
262+
title: t(getErrText(responseData[responseData.length - 1].error)),
262263
status: 'error'
263264
});
264265
}

projects/app/src/pageComponents/app/detail/MCPTools/ChatTest.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ChatRecordContextProvider from '@/web/core/chat/context/chatRecordContext
77
import { Box, Button, Flex, Switch, Textarea } from '@chakra-ui/react';
88
import { cardStyles } from '../constants';
99
import { useTranslation } from 'react-i18next';
10-
import { type ToolType } from '@fastgpt/global/core/app/type';
10+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
1111
import FormLabel from '@fastgpt/web/components/common/MyBox/FormLabel';
1212
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
1313
import { Controller, useForm } from 'react-hook-form';
@@ -19,7 +19,7 @@ import { postRunMCPTool } from '@/web/core/app/api/plugin';
1919

2020
const JsonEditor = dynamic(() => import('@fastgpt/web/components/common/Textarea/JsonEditor'));
2121

22-
const ChatTest = ({ currentTool, url }: { currentTool: ToolType | null; url: string }) => {
22+
const ChatTest = ({ currentTool, url }: { currentTool: McpToolConfigType | null; url: string }) => {
2323
const { t } = useTranslation();
2424

2525
const [output, setOutput] = useState<string>('');
@@ -135,7 +135,7 @@ const ChatTest = ({ currentTool, url }: { currentTool: ToolType | null; url: str
135135
);
136136
};
137137

138-
const Render = ({ currentTool, url }: { currentTool: ToolType | null; url: string }) => {
138+
const Render = ({ currentTool, url }: { currentTool: McpToolConfigType | null; url: string }) => {
139139
const { chatId } = useChatStore();
140140
const { appDetail } = useContextSelector(AppContext, (v) => v);
141141

@@ -178,7 +178,7 @@ const RenderToolInput = ({
178178
type: string;
179179
description?: string;
180180
};
181-
toolData: ToolType | null;
181+
toolData: McpToolConfigType | null;
182182
value: any;
183183
onChange: (value: any) => void;
184184
isInvalid: boolean;

projects/app/src/pageComponents/app/detail/MCPTools/Edit.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import AppCard from './AppCard';
77
import ChatTest from './ChatTest';
88
import MyBox from '@fastgpt/web/components/common/MyBox';
99
import EditForm from './EditForm';
10-
import { type ToolType } from '@fastgpt/global/core/app/type';
10+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
1111

1212
const Edit = ({
1313
url,
@@ -19,10 +19,10 @@ const Edit = ({
1919
}: {
2020
url: string;
2121
setUrl: (url: string) => void;
22-
toolList: ToolType[];
23-
setToolList: (toolList: ToolType[]) => void;
24-
currentTool: ToolType | null;
25-
setCurrentTool: (tool: ToolType) => void;
22+
toolList: McpToolConfigType[];
23+
setToolList: (toolList: McpToolConfigType[]) => void;
24+
currentTool: McpToolConfigType | null;
25+
setCurrentTool: (tool: McpToolConfigType) => void;
2626
}) => {
2727
const { isPc } = useSystem();
2828

projects/app/src/pageComponents/app/detail/MCPTools/EditForm.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
77
import { AppContext } from '../context';
88
import { useContextSelector } from 'use-context-selector';
99
import MyIconButton from '@fastgpt/web/components/common/Icon/button';
10-
import { type ToolType } from '@fastgpt/global/core/app/type';
10+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
1111
import MyModal from '@fastgpt/web/components/common/MyModal';
1212
import Avatar from '@fastgpt/web/components/common/Avatar';
1313
import MyBox from '@fastgpt/web/components/common/MyBox';
@@ -24,14 +24,14 @@ const EditForm = ({
2424
}: {
2525
url: string;
2626
setUrl: (url: string) => void;
27-
toolList: ToolType[];
28-
setToolList: (toolList: ToolType[]) => void;
29-
currentTool: ToolType | null;
30-
setCurrentTool: (tool: ToolType) => void;
27+
toolList: McpToolConfigType[];
28+
setToolList: (toolList: McpToolConfigType[]) => void;
29+
currentTool: McpToolConfigType | null;
30+
setCurrentTool: (tool: McpToolConfigType) => void;
3131
}) => {
3232
const { t } = useTranslation();
3333

34-
const [toolDetail, setToolDetail] = useState<ToolType | null>(null);
34+
const [toolDetail, setToolDetail] = useState<McpToolConfigType | null>(null);
3535

3636
const { runAsync: runGetMCPTools, loading: isGettingTools } = useRequest2(
3737
async (data: getMCPToolsBody) => await getMCPTools(data),
@@ -180,7 +180,7 @@ const EditForm = ({
180180

181181
export default React.memo(EditForm);
182182

183-
const ToolDetailModal = ({ tool, onClose }: { tool: ToolType; onClose: () => void }) => {
183+
const ToolDetailModal = ({ tool, onClose }: { tool: McpToolConfigType; onClose: () => void }) => {
184184
const { t } = useTranslation();
185185
const appDetail = useContextSelector(AppContext, (v) => v.appDetail);
186186

projects/app/src/pageComponents/app/detail/MCPTools/Header.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { getAppFolderPath } from '@/web/core/app/api/app';
88
import { useCallback } from 'react';
99
import { useRouter } from 'next/router';
1010
import { useSystemStore } from '@/web/common/system/useSystemStore';
11-
import { type ToolType } from '@fastgpt/global/core/app/type';
11+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
1212
import { postUpdateMCPTools } from '@/web/core/app/api/plugin';
1313

14-
const Header = ({ url, toolList }: { url: string; toolList: ToolType[] }) => {
14+
const Header = ({ url, toolList }: { url: string; toolList: McpToolConfigType[] }) => {
1515
const { t } = useTranslation();
1616
const appId = useContextSelector(AppContext, (v) => v.appId);
1717
const router = useRouter();

projects/app/src/pageComponents/app/detail/MCPTools/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Edit from './Edit';
55
import { useContextSelector } from 'use-context-selector';
66
import { AppContext } from '../context';
77
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
8-
import { type ToolType } from '@fastgpt/global/core/app/type';
8+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
99
import { type MCPToolSetData } from '@/pageComponents/dashboard/apps/MCPToolsEditModal';
1010

1111
const MCPTools = () => {
@@ -18,8 +18,10 @@ const MCPTools = () => {
1818
}, [appDetail.modules]);
1919

2020
const [url, setUrl] = useState(toolSetData?.url || '');
21-
const [toolList, setToolList] = useState<ToolType[]>(toolSetData?.toolList || []);
22-
const [currentTool, setCurrentTool] = useState<ToolType | null>(toolSetData?.toolList[0] || null);
21+
const [toolList, setToolList] = useState<McpToolConfigType[]>(toolSetData?.toolList || []);
22+
const [currentTool, setCurrentTool] = useState<McpToolConfigType | null>(
23+
toolSetData?.toolList[0] || null
24+
);
2325

2426
return (
2527
<Flex h={'100%'} flexDirection={'column'} px={[3, 0]} pr={[3, 3]}>

projects/app/src/pageComponents/app/detail/WorkflowComponents/Flow/nodes/NodeToolSet.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import NodeCard from './render/NodeCard';
55
import IOTitle from '../components/IOTitle';
66
import Container from '../components/Container';
77
import { useTranslation } from 'react-i18next';
8-
import { type ToolType } from '@fastgpt/global/core/app/type';
8+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
99
import { Box, Flex } from '@chakra-ui/react';
1010

1111
const NodeToolSet = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
1212
const { t } = useTranslation();
1313

1414
const { inputs } = data;
15-
const toolList: ToolType[] = inputs.find((item) => item.key === 'toolSetData')?.value?.toolList;
15+
const toolList: McpToolConfigType[] = inputs.find((item) => item.key === 'toolSetData')?.value
16+
?.toolList;
1617

1718
return (
1819
<NodeCard minW={'350px'} selected={selected} {...data}>

projects/app/src/pageComponents/dashboard/apps/MCPToolsEditModal.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ import Avatar from '@fastgpt/web/components/common/Avatar';
2020
import MyModal from '@fastgpt/web/components/common/MyModal';
2121
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
2222
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
23-
import { useToast } from '@fastgpt/web/hooks/useToast';
2423
import { useForm } from 'react-hook-form';
2524
import { useTranslation } from 'react-i18next';
2625
import { AppListContext } from './context';
2726
import { useContextSelector } from 'use-context-selector';
28-
import { type ToolType } from '@fastgpt/global/core/app/type';
27+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
2928
import type { getMCPToolsBody } from '@/pages/api/support/mcp/client/getTools';
3029

3130
export type MCPToolSetData = {
3231
url: string;
33-
toolList: ToolType[];
32+
toolList: McpToolConfigType[];
3433
};
3534

3635
export type EditMCPToolsProps = {
@@ -41,7 +40,6 @@ export type EditMCPToolsProps = {
4140

4241
const MCPToolsEditModal = ({ onClose }: { onClose: () => void }) => {
4342
const { t } = useTranslation();
44-
const { toast } = useToast();
4543

4644
const { parentId, loadMyApps } = useContextSelector(AppListContext, (v) => v);
4745

@@ -81,7 +79,7 @@ const MCPToolsEditModal = ({ onClose }: { onClose: () => void }) => {
8179
const { runAsync: runGetMCPTools, loading: isGettingTools } = useRequest2(
8280
(data: getMCPToolsBody) => getMCPTools(data),
8381
{
84-
onSuccess: (res: ToolType[]) => {
82+
onSuccess: (res: McpToolConfigType[]) => {
8583
setValue('mcpData.toolList', res);
8684
},
8785
errorToast: t('app:MCP_tools_parse_failed')

projects/app/src/pages/api/core/app/mcpTools/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TeamAppCreatePermissionVal } from '@fastgpt/global/support/permission/u
44
import { authApp } from '@fastgpt/service/support/permission/app/auth';
55
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
66
import { type CreateAppBody, onCreateApp } from '../create';
7-
import { type ToolType } from '@fastgpt/global/core/app/type';
7+
import { type McpToolConfigType } from '@fastgpt/global/core/app/type';
88
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
99
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
1010
import {
@@ -22,7 +22,7 @@ export type createMCPToolsBody = Omit<
2222
'type' | 'modules' | 'edges' | 'chatConfig'
2323
> & {
2424
url: string;
25-
toolList: ToolType[];
25+
toolList: McpToolConfigType[];
2626
};
2727

2828
export type createMCPToolsResponse = {};

projects/app/src/pages/api/core/app/mcpTools/update.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
22
import { NextAPI } from '@/service/middleware/entry';
3-
import { type AppDetailType, type ToolType } from '@fastgpt/global/core/app/type';
3+
import { type AppDetailType, type McpToolConfigType } from '@fastgpt/global/core/app/type';
44
import { authApp } from '@fastgpt/service/support/permission/app/auth';
55
import { ManagePermissionVal } from '@fastgpt/global/support/permission/constant';
66
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
@@ -24,7 +24,7 @@ export type updateMCPToolsQuery = {};
2424
export type updateMCPToolsBody = {
2525
appId: string;
2626
url: string;
27-
toolList: ToolType[];
27+
toolList: McpToolConfigType[];
2828
};
2929

3030
export type updateMCPToolsResponse = {};

0 commit comments

Comments
 (0)