From bdd802f44f6996527b7d5be9c67c7a253884f166 Mon Sep 17 00:00:00 2001 From: Xin Luo <65529035+luoxiner@users.noreply.github.com> Date: Tue, 1 Apr 2025 09:43:43 +0800 Subject: [PATCH] feat: support service delete event trigger for tool and some fix (#1987) --- .../mcp-server/registry/nacos/nacos.go | 44 +++++++++++++++++-- .../mcp-server/registry/nacos/server.go | 6 ++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/plugins/golang-filter/mcp-server/registry/nacos/nacos.go b/plugins/golang-filter/mcp-server/registry/nacos/nacos.go index ebb2b28df..b968f5bb6 100644 --- a/plugins/golang-filter/mcp-server/registry/nacos/nacos.go +++ b/plugins/golang-filter/mcp-server/registry/nacos/nacos.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "regexp" + "strings" "github.com/alibaba/higress/plugins/golang-filter/mcp-server/registry" "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) return false } + + currentServiceList := map[string]bool{} + for _, service := range serviceList { if !pattern.MatchString(service) { continue } - if _, ok := n.currentServiceSet[group+service]; !ok { + formatServiceName := getFormatServiceName(group, service) + if _, ok := n.currentServiceSet[formatServiceName]; !ok { changed = true n.refreshToolsListForService(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 } +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) { if newConfig == nil { @@ -167,8 +206,7 @@ func (n *NacosMcpRegsitry) refreshToolsListForServiceWithContent(group string, s n.toolsDescription[tool.Name] = tool n.toolsRpcContext[tool.Name] = &context } - n.currentServiceSet[group+service] = true - api.LogInfo(fmt.Sprintf("Refresh tools list for service success %s:%s", group, service)) + n.currentServiceSet[getFormatServiceName(group, service)] = true } func (n *NacosMcpRegsitry) GetCredential(name string, group string) *registry.CredentialInfo { diff --git a/plugins/golang-filter/mcp-server/registry/nacos/server.go b/plugins/golang-filter/mcp-server/registry/nacos/server.go index a5bec9eda..837e09337 100644 --- a/plugins/golang-filter/mcp-server/registry/nacos/server.go +++ b/plugins/golang-filter/mcp-server/registry/nacos/server.go @@ -45,7 +45,9 @@ func CreateNacosMcpRegsitry(config *NacosConfig) (*NacosMcpRegsitry, error) { constant.WithTimeoutMs(5000), constant.WithNotLoadCacheAtStart(true), constant.WithOpenKMS(true), + constant.WithLogLevel("error"), ) + cc.AppendToStdout = true if config.Namespace != nil { cc.NamespaceId = *config.Namespace @@ -150,7 +152,7 @@ func (c *NacosConfig) NewServer(serverName string) (*internal.MCPServer, error) if nacosRegistry.refreshToolsList() { resetToolsToMcpServer(mcpServer, nacosRegistry) } - time.Sleep(time.Second * 10) + time.Sleep(time.Second * 3) } }() return mcpServer, nil @@ -166,5 +168,5 @@ func resetToolsToMcpServer(mcpServer *internal.MCPServer, reg registry.McpServer }) } mcpServer.SetTools(wrappedTools...) - api.LogInfo("Config changed reset tools") + api.LogInfof("Tools reset, new tools list len %d", len(wrappedTools)) }