From 3d81df739dc45599c257d8cdefe0996f66ccdeae Mon Sep 17 00:00:00 2001
From: zhou zhou <3272660260@qq.com>
Date: 星期四, 19 三月 2026 12:18:14 +0800
Subject: [PATCH] #AI.MCP 管理增强

---
 rsf-server/src/main/java/com/vincent/rsf/server/ai/service/impl/AiMcpMountServiceImpl.java |  108 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 96 insertions(+), 12 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/ai/service/impl/AiMcpMountServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/ai/service/impl/AiMcpMountServiceImpl.java
index 7221714..5bde192 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/ai/service/impl/AiMcpMountServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/ai/service/impl/AiMcpMountServiceImpl.java
@@ -1,10 +1,12 @@
 package com.vincent.rsf.server.ai.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.vincent.rsf.framework.exception.CoolException;
 import com.vincent.rsf.server.ai.config.AiDefaults;
+import com.vincent.rsf.server.ai.dto.AiMcpConnectivityTestDto;
 import com.vincent.rsf.server.ai.dto.AiMcpToolPreviewDto;
 import com.vincent.rsf.server.ai.dto.AiMcpToolTestDto;
 import com.vincent.rsf.server.ai.dto.AiMcpToolTestRequest;
@@ -22,6 +24,7 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -67,20 +70,50 @@
     @Override
     public List<AiMcpToolPreviewDto> previewTools(Long mountId, Long userId, Long tenantId) {
         AiMcpMount mount = requireMount(mountId, tenantId);
+        long startedAt = System.currentTimeMillis();
         try (McpMountRuntimeFactory.McpMountRuntime runtime = mcpMountRuntimeFactory.create(List.of(mount), userId)) {
-            List<AiMcpToolPreviewDto> tools = new ArrayList<>();
-            for (ToolCallback callback : runtime.getToolCallbacks()) {
-                if (callback == null || callback.getToolDefinition() == null) {
-                    continue;
-                }
-                tools.add(AiMcpToolPreviewDto.builder()
-                        .name(callback.getToolDefinition().name())
-                        .description(callback.getToolDefinition().description())
-                        .inputSchema(callback.getToolDefinition().inputSchema())
-                        .returnDirect(callback.getToolMetadata() == null ? null : callback.getToolMetadata().returnDirect())
-                        .build());
+            List<AiMcpToolPreviewDto> tools = buildToolPreviewDtos(runtime.getToolCallbacks());
+            if (!runtime.getErrors().isEmpty()) {
+                String message = String.join("锛�", runtime.getErrors());
+                updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY, message, System.currentTimeMillis() - startedAt);
+                throw new CoolException(message);
             }
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_HEALTHY,
+                    "宸ュ叿瑙f瀽鎴愬姛锛屽叡 " + tools.size() + " 涓伐鍏�", System.currentTimeMillis() - startedAt);
             return tools;
+        } catch (CoolException e) {
+            throw e;
+        } catch (Exception e) {
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY,
+                    "宸ュ叿瑙f瀽澶辫触: " + e.getMessage(), System.currentTimeMillis() - startedAt);
+            throw new CoolException("鑾峰彇宸ュ叿鍒楄〃澶辫触: " + e.getMessage());
+        }
+    }
+
+    @Override
+    public AiMcpConnectivityTestDto testConnectivity(Long mountId, Long userId, Long tenantId) {
+        AiMcpMount mount = requireMount(mountId, tenantId);
+        long startedAt = System.currentTimeMillis();
+        try (McpMountRuntimeFactory.McpMountRuntime runtime = mcpMountRuntimeFactory.create(List.of(mount), userId)) {
+            long elapsedMs = System.currentTimeMillis() - startedAt;
+            if (!runtime.getErrors().isEmpty()) {
+                String message = String.join("锛�", runtime.getErrors());
+                updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY, message, elapsedMs);
+                AiMcpMount latest = requireMount(mount.getId(), tenantId);
+                return buildConnectivityDto(latest, message, elapsedMs, runtime.getToolCallbacks().length);
+            }
+            String message = "杩為�氭�ф祴璇曟垚鍔燂紝瑙f瀽鍑� " + runtime.getToolCallbacks().length + " 涓伐鍏�";
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_HEALTHY, message, elapsedMs);
+            AiMcpMount latest = requireMount(mount.getId(), tenantId);
+            return buildConnectivityDto(latest, message, elapsedMs, runtime.getToolCallbacks().length);
+        } catch (CoolException e) {
+            throw e;
+        } catch (Exception e) {
+            long elapsedMs = System.currentTimeMillis() - startedAt;
+            String message = "杩為�氭�ф祴璇曞け璐�: " + e.getMessage();
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY, message, elapsedMs);
+            AiMcpMount latest = requireMount(mount.getId(), tenantId);
+            return buildConnectivityDto(latest, message, elapsedMs, 0);
         }
     }
 
@@ -107,6 +140,7 @@
             throw new CoolException("宸ュ叿杈撳叆 JSON 鏍煎紡閿欒: " + e.getMessage());
         }
         AiMcpMount mount = requireMount(mountId, tenantId);
+        long startedAt = System.currentTimeMillis();
         try (McpMountRuntimeFactory.McpMountRuntime runtime = mcpMountRuntimeFactory.create(List.of(mount), userId)) {
             ToolCallback callback = Arrays.stream(runtime.getToolCallbacks())
                     .filter(item -> item != null && item.getToolDefinition() != null)
@@ -117,11 +151,21 @@
                     request.getInputJson(),
                     new ToolContext(Map.of("userId", userId, "tenantId", tenantId, "mountId", mountId))
             );
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_HEALTHY,
+                    "宸ュ叿娴嬭瘯鎴愬姛: " + request.getToolName(), System.currentTimeMillis() - startedAt);
             return AiMcpToolTestDto.builder()
                     .toolName(request.getToolName())
                     .inputJson(request.getInputJson())
                     .output(output)
                     .build();
+        } catch (CoolException e) {
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY,
+                    "宸ュ叿娴嬭瘯澶辫触: " + e.getMessage(), System.currentTimeMillis() - startedAt);
+            throw e;
+        } catch (Exception e) {
+            updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_UNHEALTHY,
+                    "宸ュ叿娴嬭瘯澶辫触: " + e.getMessage(), System.currentTimeMillis() - startedAt);
+            throw new CoolException("宸ュ叿娴嬭瘯澶辫触: " + e.getMessage());
         }
     }
 
@@ -137,6 +181,9 @@
         }
         if (aiMcpMount.getStatus() == null) {
             aiMcpMount.setStatus(StatusType.ENABLE.val);
+        }
+        if (!StringUtils.hasText(aiMcpMount.getHealthStatus())) {
+            aiMcpMount.setHealthStatus(AiDefaults.MCP_HEALTH_NOT_TESTED);
         }
     }
 
@@ -226,4 +273,41 @@
             throw new CoolException("褰撳墠绉熸埛涓嶅瓨鍦�");
         }
     }
+
+    private List<AiMcpToolPreviewDto> buildToolPreviewDtos(ToolCallback[] callbacks) {
+        List<AiMcpToolPreviewDto> tools = new ArrayList<>();
+        for (ToolCallback callback : callbacks) {
+            if (callback == null || callback.getToolDefinition() == null) {
+                continue;
+            }
+            tools.add(AiMcpToolPreviewDto.builder()
+                    .name(callback.getToolDefinition().name())
+                    .description(callback.getToolDefinition().description())
+                    .inputSchema(callback.getToolDefinition().inputSchema())
+                    .returnDirect(callback.getToolMetadata() == null ? null : callback.getToolMetadata().returnDirect())
+                    .build());
+        }
+        return tools;
+    }
+
+    private void updateHealthStatus(Long mountId, String healthStatus, String message, Long initElapsedMs) {
+        this.update(new LambdaUpdateWrapper<AiMcpMount>()
+                .eq(AiMcpMount::getId, mountId)
+                .set(AiMcpMount::getHealthStatus, healthStatus)
+                .set(AiMcpMount::getLastTestTime, new Date())
+                .set(AiMcpMount::getLastTestMessage, message)
+                .set(AiMcpMount::getLastInitElapsedMs, initElapsedMs));
+    }
+
+    private AiMcpConnectivityTestDto buildConnectivityDto(AiMcpMount mount, String message, Long initElapsedMs, Integer toolCount) {
+        return AiMcpConnectivityTestDto.builder()
+                .mountId(mount.getId())
+                .mountName(mount.getName())
+                .healthStatus(mount.getHealthStatus())
+                .message(message)
+                .initElapsedMs(initElapsedMs)
+                .toolCount(toolCount)
+                .testedAt(mount.getLastTestTime$())
+                .build();
+    }
 }

--
Gitblit v1.9.1