Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 1999-2025 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.ai.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* MCP Cache Index configuration properties. Centralized configuration management for MCP cache index related settings.
*
* @author misselvexu
*/
@ConfigurationProperties(prefix = "nacos.mcp.cache")
public class McpCacheIndexProperties {

/**
* Whether MCP cache is enabled.
*/
private boolean enabled = true;

/**
* Maximum size of the cache.
*/
private int maxSize = 10000;

/**
* Cache entry expiration time in seconds.
*/
private long expireTimeSeconds = 3600;

/**
* Cache cleanup interval in seconds.
*/
private long cleanupIntervalSeconds = 300;

/**
* Cache synchronization interval in seconds.
*/
private long syncIntervalSeconds = 300;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public int getMaxSize() {
return maxSize;
}

public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}

public long getExpireTimeSeconds() {
return expireTimeSeconds;
}

public void setExpireTimeSeconds(long expireTimeSeconds) {
this.expireTimeSeconds = expireTimeSeconds;
}

public long getCleanupIntervalSeconds() {
return cleanupIntervalSeconds;
}

public void setCleanupIntervalSeconds(long cleanupIntervalSeconds) {
this.cleanupIntervalSeconds = cleanupIntervalSeconds;
}

public long getSyncIntervalSeconds() {
return syncIntervalSeconds;
}

public void setSyncIntervalSeconds(long syncIntervalSeconds) {
this.syncIntervalSeconds = syncIntervalSeconds;
}

@Override
public String toString() {
return "McpCacheIndexProperties{" + "enabled=" + enabled + ", maxSize=" + maxSize + ", expireTimeSeconds="
+ expireTimeSeconds + ", cleanupIntervalSeconds=" + cleanupIntervalSeconds + ", syncIntervalSeconds="
+ syncIntervalSeconds + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 1999-2025 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.ai.config;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.nacos.ai.index.CachedMcpServerIndex;
import com.alibaba.nacos.ai.index.McpCacheIndex;
import com.alibaba.nacos.ai.index.McpServerIndex;
import com.alibaba.nacos.ai.index.MemoryMcpCacheIndex;
import com.alibaba.nacos.ai.index.PlainMcpServerIndex;
import com.alibaba.nacos.config.server.service.ConfigDetailService;
import com.alibaba.nacos.config.server.service.query.ConfigQueryChainService;
import com.alibaba.nacos.core.service.NamespaceOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
* MCP server index configuration class.
*
* @author misselvexu
*/
@Configuration
@EnableConfigurationProperties(McpCacheIndexProperties.class)
public class McpServerIndexConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger(McpServerIndexConfiguration.class);

private final McpCacheIndexProperties cacheProperties;

public McpServerIndexConfiguration(McpCacheIndexProperties cacheProperties) {
this.cacheProperties = cacheProperties;
}

/**
* Create memory cache index Bean.
*/
@Bean
@ConditionalOnProperty(name = "nacos.mcp.cache.enabled", havingValue = "true", matchIfMissing = true)
public McpCacheIndex mcpCacheIndex() {
LOGGER.info("Creating McpCacheIndex bean with maxSize={}, expireTime={}s, cleanupInterval={}s",
cacheProperties.getMaxSize(), cacheProperties.getExpireTimeSeconds(),
cacheProperties.getCleanupIntervalSeconds());
return new MemoryMcpCacheIndex(cacheProperties);
}

/**
* Create scheduled task executor Bean.
*/
@Bean
@ConditionalOnProperty(name = "nacos.mcp.cache.enabled", havingValue = "true", matchIfMissing = true)
public ScheduledExecutorService mcpCacheScheduledExecutor() {
LOGGER.info("Creating ScheduledExecutorService for MCP cache with syncInterval={}s",
cacheProperties.getSyncIntervalSeconds());
// Manually create thread pool, following Alibaba coding standards
return new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r, "mcp-cache-sync");
t.setDaemon(true);
return t;
}, new ThreadPoolExecutor.CallerRunsPolicy());
}

/**
* Create the primary MCP server index Bean when cache is enabled.
*/
@Bean
@Primary
@ConditionalOnProperty(name = "nacos.mcp.cache.enabled", havingValue = "true", matchIfMissing = true)
public McpServerIndex cachedMcpServerIndex(ConfigDetailService configDetailService,
NamespaceOperationService namespaceOperationService, ConfigQueryChainService configQueryChainService,
McpCacheIndex mcpCacheIndex, ScheduledExecutorService mcpCacheScheduledExecutor) {
LOGGER.info("Creating CachedMcpServerIndex bean with cache enabled");
return new CachedMcpServerIndex(configDetailService, namespaceOperationService, configQueryChainService,
mcpCacheIndex, mcpCacheScheduledExecutor, cacheProperties.isEnabled(),
cacheProperties.getSyncIntervalSeconds());
}

/**
* Create the primary MCP server index Bean when cache is disabled.
*/
@Bean
@Primary
@ConditionalOnProperty(name = "nacos.mcp.cache.enabled", havingValue = "false")
public McpServerIndex plainMcpServerIndex(ConfigDetailService configDetailService,
NamespaceOperationService namespaceOperationService, ConfigQueryChainService configQueryChainService) {
LOGGER.info("Creating PlainMcpServerIndex bean as cache is disabled");
return new PlainMcpServerIndex(namespaceOperationService, configDetailService, configQueryChainService);
}
}
Loading