feat: support service delete event trigger for tool and some fix (#1987)

This commit is contained in:
Xin Luo
2025-04-01 09:43:43 +08:00
committed by GitHub
parent d58b66df8f
commit bdd802f44f
2 changed files with 45 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
"strings"
"github.com/alibaba/higress/plugins/golang-filter/mcp-server/registry" "github.com/alibaba/higress/plugins/golang-filter/mcp-server/registry"
"github.com/envoyproxy/envoy/contrib/golang/common/go/api" "github.com/envoyproxy/envoy/contrib/golang/common/go/api"
@@ -76,20 +77,58 @@ func (n *NacosMcpRegsitry) refreshToolsListForGroup(group string, serviceMatcher
api.LogErrorf("Match service error for patter %s", serviceMatcher) api.LogErrorf("Match service error for patter %s", serviceMatcher)
return false return false
} }
currentServiceList := map[string]bool{}
for _, service := range serviceList { for _, service := range serviceList {
if !pattern.MatchString(service) { if !pattern.MatchString(service) {
continue continue
} }
if _, ok := n.currentServiceSet[group+service]; !ok { formatServiceName := getFormatServiceName(group, service)
if _, ok := n.currentServiceSet[formatServiceName]; !ok {
changed = true changed = true
n.refreshToolsListForService(group, service) n.refreshToolsListForService(group, service)
n.listenToService(group, service) n.listenToService(group, service)
} }
currentServiceList[formatServiceName] = true
} }
serviceShouldBeDeleted := []string{}
for serviceName, _ := range n.currentServiceSet {
if !strings.HasPrefix(serviceName, group) {
continue
}
if _, ok := currentServiceList[serviceName]; !ok {
serviceShouldBeDeleted = append(serviceShouldBeDeleted, serviceName)
changed = true
toolsShouldBeDeleted := []string{}
for toolName, _ := range n.toolsDescription {
if strings.HasPrefix(toolName, serviceName) {
toolsShouldBeDeleted = append(toolsShouldBeDeleted, toolName)
}
}
for _, toolName := range toolsShouldBeDeleted {
delete(n.toolsDescription, toolName)
delete(n.toolsRpcContext, toolName)
}
}
}
for _, service := range serviceShouldBeDeleted {
delete(n.currentServiceSet, service)
}
return changed return changed
} }
func getFormatServiceName(group string, service string) string {
return fmt.Sprintf("%s_%s", group, service)
}
func (n *NacosMcpRegsitry) refreshToolsListForServiceWithContent(group string, service string, newConfig *string, instances *[]model.Instance) { func (n *NacosMcpRegsitry) refreshToolsListForServiceWithContent(group string, service string, newConfig *string, instances *[]model.Instance) {
if newConfig == nil { if newConfig == nil {
@@ -167,8 +206,7 @@ func (n *NacosMcpRegsitry) refreshToolsListForServiceWithContent(group string, s
n.toolsDescription[tool.Name] = tool n.toolsDescription[tool.Name] = tool
n.toolsRpcContext[tool.Name] = &context n.toolsRpcContext[tool.Name] = &context
} }
n.currentServiceSet[group+service] = true n.currentServiceSet[getFormatServiceName(group, service)] = true
api.LogInfo(fmt.Sprintf("Refresh tools list for service success %s:%s", group, service))
} }
func (n *NacosMcpRegsitry) GetCredential(name string, group string) *registry.CredentialInfo { func (n *NacosMcpRegsitry) GetCredential(name string, group string) *registry.CredentialInfo {

View File

@@ -45,7 +45,9 @@ func CreateNacosMcpRegsitry(config *NacosConfig) (*NacosMcpRegsitry, error) {
constant.WithTimeoutMs(5000), constant.WithTimeoutMs(5000),
constant.WithNotLoadCacheAtStart(true), constant.WithNotLoadCacheAtStart(true),
constant.WithOpenKMS(true), constant.WithOpenKMS(true),
constant.WithLogLevel("error"),
) )
cc.AppendToStdout = true
if config.Namespace != nil { if config.Namespace != nil {
cc.NamespaceId = *config.Namespace cc.NamespaceId = *config.Namespace
@@ -150,7 +152,7 @@ func (c *NacosConfig) NewServer(serverName string) (*internal.MCPServer, error)
if nacosRegistry.refreshToolsList() { if nacosRegistry.refreshToolsList() {
resetToolsToMcpServer(mcpServer, nacosRegistry) resetToolsToMcpServer(mcpServer, nacosRegistry)
} }
time.Sleep(time.Second * 10) time.Sleep(time.Second * 3)
} }
}() }()
return mcpServer, nil return mcpServer, nil
@@ -166,5 +168,5 @@ func resetToolsToMcpServer(mcpServer *internal.MCPServer, reg registry.McpServer
}) })
} }
mcpServer.SetTools(wrappedTools...) mcpServer.SetTools(wrappedTools...)
api.LogInfo("Config changed reset tools") api.LogInfof("Tools reset, new tools list len %d", len(wrappedTools))
} }