From c9733d405c3fa505473a015a6916326b0fa5a094 Mon Sep 17 00:00:00 2001 From: EricaLiu <30773688+Erica177@users.noreply.github.com> Date: Tue, 6 May 2025 15:18:45 +0800 Subject: [PATCH] fix : Add nacos username and password login option (#2170) --- registry/nacos/mcpserver/watcher.go | 11 +++++++++++ registry/reconcile/reconcile.go | 1 + 2 files changed, 12 insertions(+) diff --git a/registry/nacos/mcpserver/watcher.go b/registry/nacos/mcpserver/watcher.go index 9ed5b778a..3b47fd381 100644 --- a/registry/nacos/mcpserver/watcher.go +++ b/registry/nacos/mcpserver/watcher.go @@ -84,6 +84,7 @@ type watcher struct { nacosClientConfig *constant.ClientConfig namespace string clusterId string + authOption provider.AuthOption } type WatcherOption func(w *watcher) @@ -131,6 +132,8 @@ func NewWatcher(cache memory.Cache, opts ...WatcherOption) (provider.Watcher, er constant.WithNamespaceId(w.NacosNamespaceId), constant.WithAccessKey(w.NacosAccessKey), constant.WithSecretKey(w.NacosSecretKey), + constant.WithUsername(w.authOption.NacosUsername), + constant.WithPassword(w.authOption.NacosPassword), ) initTimer := time.NewTimer(DefaultInitTimeout) @@ -241,6 +244,12 @@ func WithClusterId(id string) WatcherOption { } } +func WithAuthOption(authOption provider.AuthOption) WatcherOption { + return func(w *watcher) { + w.authOption = authOption + } +} + func (w *watcher) Run() { ticker := time.NewTicker(time.Duration(w.NacosRefreshInterval)) defer ticker.Stop() @@ -633,6 +642,8 @@ func (w *watcher) buildServiceEntryForMcpServer(mcpServer *provider.McpServer, c constant.WithNamespaceId(serviceNamespace), constant.WithAccessKey(w.NacosAccessKey), constant.WithSecretKey(w.NacosSecretKey), + constant.WithUsername(w.authOption.NacosUsername), + constant.WithPassword(w.authOption.NacosPassword), ) client, err := clients.NewNamingClient(vo.NacosClientParam{ ClientConfig: namingConfig, diff --git a/registry/reconcile/reconcile.go b/registry/reconcile/reconcile.go index 49cce310b..edeaf8ad4 100644 --- a/registry/reconcile/reconcile.go +++ b/registry/reconcile/reconcile.go @@ -203,6 +203,7 @@ func (r *Reconciler) generateWatcherFromRegistryConfig(registry *apiv1.RegistryC mcpserver.WithEnableMcpServer(registry.EnableMCPServer), mcpserver.WithClusterId(r.clusterId), mcpserver.WithNamespace(r.namespace), + mcpserver.WithAuthOption(authOption), ) } else { watcher, err = nacosv2.NewWatcher(