| | |
| | | private final BuiltinMcpToolRegistry builtinMcpToolRegistry; |
| | | private final McpMountRuntimeFactory mcpMountRuntimeFactory; |
| | | private final ObjectMapper objectMapper; |
| | | private final AiRedisSupport aiRedisSupport; |
| | | |
| | | /** 查询某个租户下当前启用的 MCP 挂载列表。 */ |
| | | @Override |
| | | public List<AiMcpMount> listActiveMounts(Long tenantId) { |
| | | ensureTenantId(tenantId); |
| | |
| | | .orderByAsc(AiMcpMount::getId)); |
| | | } |
| | | |
| | | /** 保存前校验 MCP 挂载草稿,并补全运行时默认值。 */ |
| | | @Override |
| | | public void validateBeforeSave(AiMcpMount aiMcpMount, Long tenantId) { |
| | | ensureTenantId(tenantId); |
| | |
| | | ensureRequiredFields(aiMcpMount, tenantId); |
| | | } |
| | | |
| | | /** 更新前校验并锁定记录所属租户,防止跨租户修改。 */ |
| | | @Override |
| | | public void validateBeforeUpdate(AiMcpMount aiMcpMount, Long tenantId) { |
| | | ensureTenantId(tenantId); |
| | |
| | | ensureRequiredFields(aiMcpMount, tenantId); |
| | | } |
| | | |
| | | /** |
| | | * 预览当前挂载最终会暴露给模型的工具目录。 |
| | | * 对内置 MCP 会额外合并治理目录信息,对外部 MCP 则以实际解析结果为准。 |
| | | */ |
| | | @Override |
| | | public List<AiMcpToolPreviewDto> previewTools(Long mountId, Long userId, Long tenantId) { |
| | | AiMcpMount mount = requireMount(mountId, tenantId); |
| | | // 工具目录预览初始化成本高,但变化频率低,适合做管理端短缓存。 |
| | | List<AiMcpToolPreviewDto> cached = aiRedisSupport.getToolPreview(tenantId, mountId); |
| | | if (cached != null) { |
| | | return cached; |
| | | } |
| | | long startedAt = System.currentTimeMillis(); |
| | | try (McpMountRuntimeFactory.McpMountRuntime runtime = mcpMountRuntimeFactory.create(List.of(mount), userId)) { |
| | | List<AiMcpToolPreviewDto> tools = buildToolPreviewDtos(runtime.getToolCallbacks(), |
| | |
| | | } |
| | | updateHealthStatus(mount.getId(), AiDefaults.MCP_HEALTH_HEALTHY, |
| | | "工具解析成功,共 " + tools.size() + " 个工具", System.currentTimeMillis() - startedAt); |
| | | aiRedisSupport.cacheToolPreview(tenantId, mountId, tools); |
| | | return tools; |
| | | } catch (CoolException e) { |
| | | throw e; |
| | |
| | | } |
| | | } |
| | | |
| | | /** 对已保存的挂载做真实连通性测试,并把结果回写到运行态字段。 */ |
| | | @Override |
| | | public AiMcpConnectivityTestDto testConnectivity(Long mountId, Long userId, Long tenantId) { |
| | | AiMcpMount mount = requireMount(mountId, tenantId); |
| | |
| | | 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); |
| | | AiMcpConnectivityTestDto connectivity = buildConnectivityDto(latest, message, elapsedMs, runtime.getToolCallbacks().length); |
| | | aiRedisSupport.cacheConnectivity(tenantId, mountId, connectivity); |
| | | return connectivity; |
| | | } |
| | | String message = "连通性测试成功,解析出 " + 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); |
| | | AiMcpConnectivityTestDto connectivity = buildConnectivityDto(latest, message, elapsedMs, runtime.getToolCallbacks().length); |
| | | aiRedisSupport.cacheConnectivity(tenantId, mountId, connectivity); |
| | | return connectivity; |
| | | } catch (CoolException e) { |
| | | throw e; |
| | | } catch (Exception e) { |
| | |
| | | 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); |
| | | AiMcpConnectivityTestDto connectivity = buildConnectivityDto(latest, message, elapsedMs, 0); |
| | | aiRedisSupport.cacheConnectivity(tenantId, mountId, connectivity); |
| | | return connectivity; |
| | | } |
| | | } |
| | | |
| | | /** 对表单里的草稿配置做临时连通性测试,不落库。 */ |
| | | @Override |
| | | public AiMcpConnectivityTestDto testDraftConnectivity(AiMcpMount mount, Long userId, Long tenantId) { |
| | | ensureTenantId(tenantId); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 直接执行某一个工具的测试调用。 |
| | | * 该方法主要服务于管理端的“工具测试”面板,不参与正式对话链路。 |
| | | */ |
| | | @Override |
| | | public AiMcpToolTestDto testTool(Long mountId, Long userId, Long tenantId, AiMcpToolTestRequest request) { |
| | | if (userId == null) { |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean save(AiMcpMount entity) { |
| | | boolean saved = super.save(entity); |
| | | if (saved && entity != null && entity.getTenantId() != null) { |
| | | aiRedisSupport.evictMcpMountCaches(entity.getTenantId(), entity.getId()); |
| | | } |
| | | return saved; |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateById(AiMcpMount entity) { |
| | | boolean updated = super.updateById(entity); |
| | | if (updated && entity != null && entity.getTenantId() != null) { |
| | | aiRedisSupport.evictMcpMountCaches(entity.getTenantId(), entity.getId()); |
| | | } |
| | | return updated; |
| | | } |
| | | |
| | | @Override |
| | | public boolean removeByIds(java.util.Collection<?> list) { |
| | | java.util.List<java.io.Serializable> ids = list == null ? java.util.List.of() : list.stream() |
| | | .filter(java.util.Objects::nonNull) |
| | | .map(item -> (java.io.Serializable) item) |
| | | .toList(); |
| | | java.util.List<AiMcpMount> records = this.listByIds(ids); |
| | | boolean removed = super.removeByIds(list); |
| | | if (removed) { |
| | | records.stream() |
| | | .filter(java.util.Objects::nonNull) |
| | | .forEach(item -> aiRedisSupport.evictMcpMountCaches(item.getTenantId(), item.getId())); |
| | | } |
| | | return removed; |
| | | } |
| | | |
| | | private void fillDefaults(AiMcpMount aiMcpMount) { |
| | | /** 为挂载草稿补齐统一默认值,保证后续运行时代码不需要重复判断空值。 */ |
| | | if (!StringUtils.hasText(aiMcpMount.getTransportType())) { |
| | | aiMcpMount.setTransportType(AiDefaults.MCP_TRANSPORT_SSE_HTTP); |
| | | } |
| | |
| | | } |
| | | |
| | | private void ensureRequiredFields(AiMcpMount aiMcpMount, Long tenantId) { |
| | | /** |
| | | * 按 transportType 校验挂载必填项。 |
| | | * 这里把“字段合法性”和“跨记录冲突”一起收口,避免校验逻辑分散在 controller 层。 |
| | | */ |
| | | if (!StringUtils.hasText(aiMcpMount.getName())) { |
| | | throw new CoolException("MCP 挂载名称不能为空"); |
| | | } |
| | |
| | | } |
| | | |
| | | private AiMcpMount requireMount(Long mountId, Long tenantId) { |
| | | /** 按租户加载挂载记录,不存在直接抛错。 */ |
| | | ensureTenantId(tenantId); |
| | | if (mountId == null) { |
| | | throw new CoolException("MCP 挂载 ID 不能为空"); |
| | |
| | | } |
| | | |
| | | private void ensureBuiltinConflictFree(AiMcpMount aiMcpMount, Long tenantId) { |
| | | /** 校验同租户下是否存在与当前内置编码互斥的启用挂载。 */ |
| | | if (aiMcpMount.getStatus() == null || aiMcpMount.getStatus() != StatusType.ENABLE.val) { |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | private List<AiMcpToolPreviewDto> buildToolPreviewDtos(ToolCallback[] callbacks, List<AiMcpToolPreviewDto> governedCatalog) { |
| | | /** 把底层 ToolCallback 和治理目录信息拼成前端需要的结构化工具卡片数据。 */ |
| | | List<AiMcpToolPreviewDto> tools = new ArrayList<>(); |
| | | Map<String, AiMcpToolPreviewDto> catalogMap = new java.util.LinkedHashMap<>(); |
| | | for (AiMcpToolPreviewDto item : governedCatalog) { |