Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

use-mcp-request.ts 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import message from '@/components/ui/message';
  2. import { IMcpServerListResponse, IMCPTool } from '@/interfaces/database/mcp';
  3. import { ITestMcpRequestBody } from '@/interfaces/request/mcp';
  4. import i18n from '@/locales/config';
  5. import mcpServerService from '@/services/mcp-server-service';
  6. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  7. import { useState } from 'react';
  8. export const enum McpApiAction {
  9. ListMcpServer = 'listMcpServer',
  10. GetMcpServer = 'getMcpServer',
  11. CreateMcpServer = 'createMcpServer',
  12. UpdateMcpServer = 'updateMcpServer',
  13. DeleteMcpServer = 'deleteMcpServer',
  14. ImportMcpServer = 'importMcpServer',
  15. ExportMcpServer = 'exportMcpServer',
  16. ListMcpServerTools = 'listMcpServerTools',
  17. TestMcpServerTool = 'testMcpServerTool',
  18. CacheMcpServerTool = 'cacheMcpServerTool',
  19. TestMcpServer = 'testMcpServer',
  20. }
  21. export const useListMcpServer = () => {
  22. const { data, isFetching: loading } = useQuery<IMcpServerListResponse>({
  23. queryKey: [McpApiAction.ListMcpServer],
  24. initialData: { total: 0, mcp_servers: [] },
  25. gcTime: 0,
  26. queryFn: async () => {
  27. const { data } = await mcpServerService.list({});
  28. return data?.data;
  29. },
  30. });
  31. return { data, loading };
  32. };
  33. export const useGetMcpServer = () => {
  34. const [id, setId] = useState('');
  35. const { data, isFetching: loading } = useQuery({
  36. queryKey: [McpApiAction.GetMcpServer, id],
  37. initialData: {},
  38. gcTime: 0,
  39. enabled: !!id,
  40. queryFn: async () => {
  41. const { data } = await mcpServerService.get();
  42. return data?.data ?? {};
  43. },
  44. });
  45. return { data, loading, setId, id };
  46. };
  47. export const useCreateMcpServer = () => {
  48. const queryClient = useQueryClient();
  49. const {
  50. data,
  51. isPending: loading,
  52. mutateAsync,
  53. } = useMutation({
  54. mutationKey: [McpApiAction.CreateMcpServer],
  55. mutationFn: async (params: Record<string, any>) => {
  56. const { data = {} } = await mcpServerService.create(params);
  57. if (data.code === 0) {
  58. message.success(i18n.t(`message.created`));
  59. queryClient.invalidateQueries({
  60. queryKey: [McpApiAction.ListMcpServer],
  61. });
  62. }
  63. return data.code;
  64. },
  65. });
  66. return { data, loading, createMcpServer: mutateAsync };
  67. };
  68. export const useUpdateMcpServer = () => {
  69. const queryClient = useQueryClient();
  70. const {
  71. data,
  72. isPending: loading,
  73. mutateAsync,
  74. } = useMutation({
  75. mutationKey: [McpApiAction.UpdateMcpServer],
  76. mutationFn: async (params: Record<string, any>) => {
  77. const { data = {} } = await mcpServerService.update(params);
  78. if (data.code === 0) {
  79. message.success(i18n.t(`message.updated`));
  80. queryClient.invalidateQueries({
  81. queryKey: [McpApiAction.ListMcpServer],
  82. });
  83. }
  84. return data.code;
  85. },
  86. });
  87. return { data, loading, updateMcpServer: mutateAsync };
  88. };
  89. export const useDeleteMcpServer = () => {
  90. const queryClient = useQueryClient();
  91. const {
  92. data,
  93. isPending: loading,
  94. mutateAsync,
  95. } = useMutation({
  96. mutationKey: [McpApiAction.DeleteMcpServer],
  97. mutationFn: async (params: Record<string, any>) => {
  98. const { data = {} } = await mcpServerService.delete(params);
  99. if (data.code === 0) {
  100. message.success(i18n.t(`message.deleted`));
  101. queryClient.invalidateQueries({
  102. queryKey: [McpApiAction.ListMcpServer],
  103. });
  104. }
  105. return data;
  106. },
  107. });
  108. return { data, loading, deleteMcpServer: mutateAsync };
  109. };
  110. export const useImportMcpServer = () => {
  111. const queryClient = useQueryClient();
  112. const {
  113. data,
  114. isPending: loading,
  115. mutateAsync,
  116. } = useMutation({
  117. mutationKey: [McpApiAction.ImportMcpServer],
  118. mutationFn: async (params: Record<string, any>) => {
  119. const { data = {} } = await mcpServerService.import(params);
  120. if (data.code === 0) {
  121. message.success(i18n.t(`message.created`));
  122. queryClient.invalidateQueries({
  123. queryKey: [McpApiAction.ListMcpServer],
  124. });
  125. }
  126. return data;
  127. },
  128. });
  129. return { data, loading, importMcpServer: mutateAsync };
  130. };
  131. export const useListMcpServerTools = () => {
  132. const { data, isFetching: loading } = useQuery({
  133. queryKey: [McpApiAction.ListMcpServerTools],
  134. initialData: [],
  135. gcTime: 0,
  136. queryFn: async () => {
  137. const { data } = await mcpServerService.listTools();
  138. return data?.data ?? [];
  139. },
  140. });
  141. return { data, loading };
  142. };
  143. export const useTestMcpServer = () => {
  144. const {
  145. data,
  146. isPending: loading,
  147. mutateAsync,
  148. } = useMutation<IMCPTool[], Error, ITestMcpRequestBody>({
  149. mutationKey: [McpApiAction.TestMcpServer],
  150. mutationFn: async (params) => {
  151. const { data } = await mcpServerService.test(params);
  152. return data?.data || [];
  153. },
  154. });
  155. return { data, loading, testMcpServer: mutateAsync };
  156. };
  157. export const useCacheMcpServerTool = () => {
  158. const {
  159. data,
  160. isPending: loading,
  161. mutateAsync,
  162. } = useMutation({
  163. mutationKey: [McpApiAction.CacheMcpServerTool],
  164. mutationFn: async (params: Record<string, any>) => {
  165. const { data = {} } = await mcpServerService.cacheTool(params);
  166. return data;
  167. },
  168. });
  169. return { data, loading, cacheMcpServerTool: mutateAsync };
  170. };
  171. export const useTestMcpServerTool = () => {
  172. const {
  173. data,
  174. isPending: loading,
  175. mutateAsync,
  176. } = useMutation({
  177. mutationKey: [McpApiAction.TestMcpServerTool],
  178. mutationFn: async (params: Record<string, any>) => {
  179. const { data = {} } = await mcpServerService.testTool(params);
  180. return data;
  181. },
  182. });
  183. return { data, loading, testMcpServerTool: mutateAsync };
  184. };