|
| 1 | +/* |
| 2 | + * Copyright 1999-2025 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.nacos.ai.service; |
| 18 | + |
| 19 | +import com.alibaba.nacos.ai.constant.Constants; |
| 20 | +import com.alibaba.nacos.ai.index.McpServerIndex; |
| 21 | +import com.alibaba.nacos.common.notify.Event; |
| 22 | +import com.alibaba.nacos.common.notify.NotifyCenter; |
| 23 | +import com.alibaba.nacos.common.notify.listener.Subscriber; |
| 24 | +import com.alibaba.nacos.common.utils.StringUtils; |
| 25 | +import com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent; |
| 26 | +import com.alibaba.nacos.config.server.utils.GroupKey; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.stereotype.Service; |
| 31 | + |
| 32 | +/** |
| 33 | + * MCP Server cache invalidation service. |
| 34 | + * |
| 35 | + * <p>This service listens to ConfigDataChangeEvent and invalidates MCP server cache |
| 36 | + * when MCP-related configurations are deleted or modified. The implementation follows |
| 37 | + * the same pattern as AsyncNotifyService for configuration synchronization.</p> |
| 38 | + * |
| 39 | + * @author xinluo |
| 40 | + */ |
| 41 | +@Service |
| 42 | +public class McpServerCacheInvalidateService extends Subscriber<LocalDataChangeEvent> { |
| 43 | + |
| 44 | + private static final Logger LOGGER = LoggerFactory.getLogger(McpServerCacheInvalidateService.class); |
| 45 | + |
| 46 | + private final McpServerIndex mcpServerIndex; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + public McpServerCacheInvalidateService(McpServerIndex mcpServerIndex) { |
| 50 | + this.mcpServerIndex = mcpServerIndex; |
| 51 | + NotifyCenter.registerSubscriber(this); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Handle ConfigDataChangeEvent to invalidate MCP server cache. |
| 56 | + * |
| 57 | + * @param event configuration change event |
| 58 | + */ |
| 59 | + void handleConfigDataChangeEvent(LocalDataChangeEvent event) { |
| 60 | + // Check if the configuration is MCP server related |
| 61 | + String groupKey = event.groupKey; |
| 62 | + |
| 63 | + String[] strings = GroupKey.parseKey(groupKey); |
| 64 | + String dataId = strings[0]; |
| 65 | + String group = strings[1]; |
| 66 | + String tenant = strings.length > 2 ? strings[2] : ""; |
| 67 | + if (!isMcpServerConfig(group)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Extract server ID from dataId |
| 72 | + String serverId = extractServerIdFromDataId(group, dataId); |
| 73 | + if (StringUtils.isEmpty(serverId)) { |
| 74 | + LOGGER.warn("Failed to extract server ID from dataId: {}, group: {}", dataId, group); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Invalidate cache |
| 79 | + invalidateCache(tenant, serverId); |
| 80 | + |
| 81 | + LOGGER.info("Handled MCP server config change event: namespaceId={}, group={}, dataId={}, serverId={}", |
| 82 | + tenant, group, dataId, serverId); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check if the configuration group is MCP server related. |
| 87 | + * |
| 88 | + * @param group configuration group |
| 89 | + * @return true if the group is MCP server related |
| 90 | + */ |
| 91 | + private boolean isMcpServerConfig(String group) { |
| 92 | + return Constants.MCP_SERVER_VERSIONS_GROUP.equals(group); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Extract server ID from dataId based on the configuration group. |
| 97 | + * |
| 98 | + * <p>MCP server configurations follow these naming patterns:</p> |
| 99 | + * <ul> |
| 100 | + * <li>Version info: {serverId}-mcp-versions.json (group: mcp-server-versions)</li> |
| 101 | + * <li>Server spec: {serverId}-{version}-mcp-server.json (group: mcp-server)</li> |
| 102 | + * <li>Tool spec: {serverId}-{version}-mcp-tools.json (group: mcp-tools)</li> |
| 103 | + * </ul> |
| 104 | + * |
| 105 | + * @param group configuration group |
| 106 | + * @param dataId configuration dataId |
| 107 | + * @return extracted server ID, or null if extraction fails |
| 108 | + */ |
| 109 | + private String extractServerIdFromDataId(String group, String dataId) { |
| 110 | + if (Constants.MCP_SERVER_VERSIONS_GROUP.equals(group)) { |
| 111 | + // Version info: remove "-mcp-versions.json" suffix |
| 112 | + if (StringUtils.isNotEmpty(dataId) && dataId.endsWith(Constants.MCP_SERVER_VERSION_DATA_ID_SUFFIX)) { |
| 113 | + return dataId.substring(0, dataId.length() - Constants.MCP_SERVER_VERSION_DATA_ID_SUFFIX.length()); |
| 114 | + } |
| 115 | + } |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Invalidate MCP server cache by server ID. |
| 121 | + * |
| 122 | + * <p>This method is idempotent - calling it multiple times with the same |
| 123 | + * serverId will not cause any side effects.</p> |
| 124 | + * |
| 125 | + * @param namespaceId namespace ID |
| 126 | + * @param serverId MCP server ID |
| 127 | + */ |
| 128 | + private void invalidateCache(String namespaceId, String serverId) { |
| 129 | + try { |
| 130 | + // Clear cache by server ID |
| 131 | + mcpServerIndex.removeMcpServerById(serverId); |
| 132 | + |
| 133 | + LOGGER.info("MCP Server cache invalidated successfully: namespaceId={}, serverId={}", namespaceId, |
| 134 | + serverId); |
| 135 | + } catch (Exception e) { |
| 136 | + // Cache invalidation failure should not affect configuration deletion |
| 137 | + LOGGER.error("Failed to invalidate MCP Server cache: namespaceId={}, serverId={}, error={}", namespaceId, |
| 138 | + serverId, e.getMessage(), e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onEvent(LocalDataChangeEvent event) { |
| 144 | + handleConfigDataChangeEvent(event); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Class<? extends Event> subscribeType() { |
| 149 | + return LocalDataChangeEvent.class; |
| 150 | + } |
| 151 | +} |
0 commit comments