Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import message from '@/components/ui/message';
  2. import { ResponseType } from '@/interfaces/database/base';
  3. import {
  4. IExportedMcpServers,
  5. IMcpServer,
  6. IMcpServerListResponse,
  7. IMCPTool,
  8. IMCPToolRecord,
  9. } from '@/interfaces/database/mcp';
  10. import {
  11. IImportMcpServersRequestBody,
  12. ITestMcpRequestBody,
  13. } from '@/interfaces/request/mcp';
  14. import i18n from '@/locales/config';
  15. import mcpServerService, {
  16. listMcpServers,
  17. } from '@/services/mcp-server-service';
  18. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  19. import { useDebounce } from 'ahooks';
  20. import { useState } from 'react';
  21. import {
  22. useGetPaginationWithRouter,
  23. useHandleSearchChange,
  24. } from './logic-hooks';
  25. export const enum McpApiAction {
  26. ListMcpServer = 'listMcpServer',
  27. GetMcpServer = 'getMcpServer',
  28. CreateMcpServer = 'createMcpServer',
  29. UpdateMcpServer = 'updateMcpServer',
  30. DeleteMcpServer = 'deleteMcpServer',
  31. ImportMcpServer = 'importMcpServer',
  32. ExportMcpServer = 'exportMcpServer',
  33. ListMcpServerTools = 'listMcpServerTools',
  34. TestMcpServerTool = 'testMcpServerTool',
  35. CacheMcpServerTool = 'cacheMcpServerTool',
  36. TestMcpServer = 'testMcpServer',
  37. }
  38. export const useListMcpServer = () => {
  39. const { searchString, handleInputChange } = useHandleSearchChange();
  40. const { pagination, setPagination } = useGetPaginationWithRouter();
  41. const debouncedSearchString = useDebounce(searchString, { wait: 500 });
  42. const { data, isFetching: loading } = useQuery<IMcpServerListResponse>({
  43. queryKey: [
  44. McpApiAction.ListMcpServer,
  45. {
  46. debouncedSearchString,
  47. ...pagination,
  48. },
  49. ],
  50. initialData: { total: 0, mcp_servers: [] },
  51. gcTime: 0,
  52. queryFn: async () => {
  53. const { data } = await listMcpServers({
  54. keywords: debouncedSearchString,
  55. page_size: pagination.pageSize,
  56. page: pagination.current,
  57. });
  58. return data?.data;
  59. },
  60. });
  61. return {
  62. data,
  63. loading,
  64. handleInputChange,
  65. setPagination,
  66. searchString,
  67. pagination: { ...pagination, total: data?.total },
  68. };
  69. };
  70. export const useGetMcpServer = (id: string) => {
  71. const { data, isFetching: loading } = useQuery<IMcpServer>({
  72. queryKey: [McpApiAction.GetMcpServer, id],
  73. initialData: {} as IMcpServer,
  74. gcTime: 0,
  75. enabled: !!id,
  76. queryFn: async () => {
  77. const { data } = await mcpServerService.get({ mcp_id: id });
  78. return data?.data ?? {};
  79. },
  80. });
  81. return { data, loading, id };
  82. };
  83. export const useCreateMcpServer = () => {
  84. const queryClient = useQueryClient();
  85. const {
  86. data,
  87. isPending: loading,
  88. mutateAsync,
  89. } = useMutation({
  90. mutationKey: [McpApiAction.CreateMcpServer],
  91. mutationFn: async (params: Record<string, any>) => {
  92. const { data = {} } = await mcpServerService.create(params);
  93. if (data.code === 0) {
  94. message.success(i18n.t(`message.created`));
  95. queryClient.invalidateQueries({
  96. queryKey: [McpApiAction.ListMcpServer],
  97. });
  98. }
  99. return data.code;
  100. },
  101. });
  102. return { data, loading, createMcpServer: mutateAsync };
  103. };
  104. export const useUpdateMcpServer = () => {
  105. const queryClient = useQueryClient();
  106. const {
  107. data,
  108. isPending: loading,
  109. mutateAsync,
  110. } = useMutation({
  111. mutationKey: [McpApiAction.UpdateMcpServer],
  112. mutationFn: async (params: Record<string, any>) => {
  113. const { data = {} } = await mcpServerService.update(params);
  114. if (data.code === 0) {
  115. message.success(i18n.t(`message.updated`));
  116. queryClient.invalidateQueries({
  117. queryKey: [McpApiAction.ListMcpServer],
  118. });
  119. }
  120. return data.code;
  121. },
  122. });
  123. return { data, loading, updateMcpServer: mutateAsync };
  124. };
  125. export const useDeleteMcpServer = () => {
  126. const queryClient = useQueryClient();
  127. const {
  128. data,
  129. isPending: loading,
  130. mutateAsync,
  131. } = useMutation({
  132. mutationKey: [McpApiAction.DeleteMcpServer],
  133. mutationFn: async (ids: string[]) => {
  134. const { data = {} } = await mcpServerService.delete({ mcp_ids: ids });
  135. if (data.code === 0) {
  136. message.success(i18n.t(`message.deleted`));
  137. queryClient.invalidateQueries({
  138. queryKey: [McpApiAction.ListMcpServer],
  139. });
  140. }
  141. return data;
  142. },
  143. });
  144. return { data, loading, deleteMcpServer: mutateAsync };
  145. };
  146. export const useImportMcpServer = () => {
  147. const queryClient = useQueryClient();
  148. const {
  149. data,
  150. isPending: loading,
  151. mutateAsync,
  152. } = useMutation({
  153. mutationKey: [McpApiAction.ImportMcpServer],
  154. mutationFn: async (params: IImportMcpServersRequestBody) => {
  155. const { data = {} } = await mcpServerService.import(params);
  156. if (data.code === 0) {
  157. message.success(i18n.t(`message.operated`));
  158. queryClient.invalidateQueries({
  159. queryKey: [McpApiAction.ListMcpServer],
  160. });
  161. }
  162. return data;
  163. },
  164. });
  165. return { data, loading, importMcpServer: mutateAsync };
  166. };
  167. export const useExportMcpServer = () => {
  168. const {
  169. data,
  170. isPending: loading,
  171. mutateAsync,
  172. } = useMutation<ResponseType<IExportedMcpServers>, Error, string[]>({
  173. mutationKey: [McpApiAction.ExportMcpServer],
  174. mutationFn: async (ids) => {
  175. const { data = {} } = await mcpServerService.export({ mcp_ids: ids });
  176. if (data.code === 0) {
  177. message.success(i18n.t(`message.operated`));
  178. }
  179. return data;
  180. },
  181. });
  182. return { data, loading, exportMcpServer: mutateAsync };
  183. };
  184. export const useListMcpServerTools = () => {
  185. const [ids, setIds] = useState<string[]>([]);
  186. const { data, isFetching: loading } = useQuery<IMCPToolRecord>({
  187. queryKey: [McpApiAction.ListMcpServerTools],
  188. initialData: {} as IMCPToolRecord,
  189. gcTime: 0,
  190. enabled: ids.length > 0,
  191. queryFn: async () => {
  192. const { data } = await mcpServerService.listTools({ mcp_ids: ids });
  193. return data?.data ?? {};
  194. },
  195. });
  196. return { data, loading, setIds };
  197. };
  198. export const useTestMcpServer = () => {
  199. const {
  200. data,
  201. isPending: loading,
  202. mutateAsync,
  203. } = useMutation<ResponseType<IMCPTool[]>, Error, ITestMcpRequestBody>({
  204. mutationKey: [McpApiAction.TestMcpServer],
  205. mutationFn: async (params) => {
  206. const { data } = await mcpServerService.test(params);
  207. return data;
  208. },
  209. });
  210. return { data, loading, testMcpServer: mutateAsync };
  211. };
  212. export const useCacheMcpServerTool = () => {
  213. const {
  214. data,
  215. isPending: loading,
  216. mutateAsync,
  217. } = useMutation({
  218. mutationKey: [McpApiAction.CacheMcpServerTool],
  219. mutationFn: async (params: Record<string, any>) => {
  220. const { data = {} } = await mcpServerService.cacheTool(params);
  221. return data;
  222. },
  223. });
  224. return { data, loading, cacheMcpServerTool: mutateAsync };
  225. };
  226. export const useTestMcpServerTool = () => {
  227. const {
  228. data,
  229. isPending: loading,
  230. mutateAsync,
  231. } = useMutation({
  232. mutationKey: [McpApiAction.TestMcpServerTool],
  233. mutationFn: async (params: Record<string, any>) => {
  234. const { data = {} } = await mcpServerService.testTool(params);
  235. return data;
  236. },
  237. });
  238. return { data, loading, testMcpServerTool: mutateAsync };
  239. };