Similarityoung commented on code in PR #757:
URL: https://github.com/apache/dubbo-go-pixiu/pull/757#discussion_r2365719195


##########
pkg/adapter/mcpserver/registrycenter.go:
##########
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 mcpserver
+
+import (
+       "context"
+       "os"
+       "strconv"
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/adapter/mcpserver/common"
+       "github.com/apache/dubbo-go-pixiu/pkg/adapter/mcpserver/registry"
+       _ 
"github.com/apache/dubbo-go-pixiu/pkg/adapter/mcpserver/registry/nacos"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/extension/adapter"
+       "github.com/apache/dubbo-go-pixiu/pkg/filter/mcp/mcpserver"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+       "github.com/apache/dubbo-go-pixiu/pkg/model"
+       "github.com/apache/dubbo-go-pixiu/pkg/server"
+)
+
+// TODO: Implement mcpserver/registry package
+// "github.com/apache/dubbo-go-pixiu/pkg/adapter/mcpserver/registry"
+func init() {
+       adapter.RegisterAdapterPlugin(&Plugin{})
+}
+
+var (
+       _ adapter.AdapterPlugin = new(Plugin)
+       _ adapter.Adapter       = new(Adapter)
+)
+
+type (
+       // Plugin to monitor mcp services on registry center
+       Plugin struct{}
+
+       // AdapterConfig holds configuration for multiple registries
+       AdapterConfig struct {
+               Registries map[string]model.Registry `yaml:"registries" 
json:"registries" mapstructure:"registries"`
+       }
+
+       // Adapter to monitor mcp services on registry center
+       Adapter struct {
+               id  string
+               cfg *AdapterConfig
+               // single provider controller (provider-agnostic)
+               controller registry.Controller
+               ctx        context.Context
+               cancel     context.CancelFunc
+               mu         sync.RWMutex
+       }
+
+       // McpServerInfo represents an MCP server instance from service 
discovery
+       McpServerInfo struct {
+               ServerID string            `json:"server_id"`
+               Endpoint string            `json:"endpoint"`
+               Protocol string            `json:"protocol"`
+               Metadata map[string]string `json:"metadata"`
+       }
+)
+
+// Kind returns the identifier of the plugin
+func (p Plugin) Kind() string {
+       return constant.McpServerAdapter
+}
+
+// CreateAdapter returns the mcp server adapter
+func (p *Plugin) CreateAdapter(a *model.Adapter) (adapter.Adapter, error) {
+       return &Adapter{
+               id:  a.ID,
+               cfg: &AdapterConfig{Registries: 
make(map[string]model.Registry)},
+               // TODO: Initialize registries when implemented
+       }, nil
+}
+
+// Start starts the adapter
+func (a *Adapter) Start() {
+       a.mu.RLock()
+       defer a.mu.RUnlock()
+
+       if a.controller == nil {
+               logger.Warnf("MCP server adapter %s start skipped: controller 
not initialized (call Apply first)", a.id)
+               return
+       }
+
+       if a.cancel != nil {
+               logger.Infof("MCP server adapter %s already running", a.id)
+               return
+       }
+
+       a.ctx, a.cancel = context.WithCancel(context.Background())
+       go func() {
+               if err := a.controller.Run(a.ctx, 30*time.Second); err != nil {
+                       logger.Errorf("MCP server controller run error: %v", 
err)
+               }
+       }()
+
+       logger.Infof("MCP server adapter %s started successfully", a.id)
+}
+
+// Stop stops the adapter
+func (a *Adapter) Stop() {
+       a.mu.RLock()
+       defer a.mu.RUnlock()
+
+       if a.cancel != nil {
+               a.cancel()
+               a.cancel = nil
+       }
+
+       if a.controller != nil {
+               if err := a.controller.Close(); err != nil {
+                       logger.Errorf("MCP server controller close error: %v", 
err)
+               }
+       }
+       logger.Infof("MCP server adapter %s stopped successfully", a.id)
+}
+
+// Apply inits the registries according to the configuration
+func (a *Adapter) Apply() error {
+       a.mu.Lock()
+       defer a.mu.Unlock()
+
+       // Support environment variable override for Nacos address
+       nacosAddrFromEnv := 
os.Getenv(constant.EnvDubbogoPixiuNacosRegistryAddress)
+
+       for k, registryConfig := range a.cfg.Registries {
+               if nacosAddrFromEnv != "" && registryConfig.Protocol == 
constant.Nacos {
+                       registryConfig.Address = nacosAddrFromEnv
+               }
+
+               // only handle nacos for now
+               if registryConfig.Protocol != constant.Nacos {
+                       logger.Infof("MCP registry %s skipped (protocol=%s)", 
k, registryConfig.Protocol)
+                       continue
+               }
+
+               onChange := func(cfg *model.McpServerConfig) {
+                       if cfg == nil {
+                               return
+                       }
+                       // 1) apply tools dynamically to registry for filter 
usage
+                       if dc := mcpserver.GetOrInitDynamic(); dc != nil {
+                               if err := dc.ApplyMcpServerConfig(cfg); err != 
nil {
+                                       logger.Errorf("[MCP Adapter] apply 
config error: %v", err)
+                               }
+                       } else {
+                               logger.Infof("[MCP Adapter] update received: 
tools=%d", len(cfg.Tools))
+                       }
+                       // 2) register endpoint for each tool using BackendURL 
(host:port) into cluster named by tool.Name
+                       for _, tool := range cfg.Tools {
+                               if tool.BackendURL == "" {
+                                       continue
+                               }
+                               host, port := 
common.ParseHostPortFromURL(tool.BackendURL)
+                               if host == "" || port <= 0 {

Review Comment:
   done



##########
pkg/adapter/mcpserver/registry/nacos/converter.go:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 nacos
+
+import (
+       "encoding/json"
+       "fmt"
+       "net/url"
+       "regexp"
+       "strings"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+       "github.com/apache/dubbo-go-pixiu/pkg/model"
+)
+
+// ConvertNacosToolsToToolConfig converts Nacos Tools to the Filter's 
ToolConfig
+func ConvertNacosToolsToToolConfig(toolsSpec *ToolsSpec) ([]model.ToolConfig, 
error) {
+       var toolConfigs []model.ToolConfig
+
+       for _, nacosTool := range toolsSpec.Tools {
+               meta := toolsSpec.ToolsMeta[nacosTool.Name]
+               if !meta.Enabled {
+                       continue
+               }
+
+               // Extract json-go-template
+               templateData, ok := meta.Templates["json-go-template"]
+               if !ok {
+                       logger.Warnf("Tool %s has no json-go-template, 
skipping", nacosTool.Name)
+                       continue
+               }
+
+               toolConfig, err := convertSingleTool(nacosTool, templateData)
+               if err != nil {
+                       return nil, fmt.Errorf("failed to convert tool %s: %w", 
nacosTool.Name, err)
+               }
+
+               toolConfigs = append(toolConfigs, toolConfig)
+       }
+
+       return toolConfigs, nil
+}
+
+func convertSingleTool(nacosTool NacosTool, templateData any) 
(model.ToolConfig, error) {
+       // Parse template data
+       templateBytes, err := json.Marshal(templateData)
+       if err != nil {
+               return model.ToolConfig{}, err
+       }
+
+       var template JsonGoTemplate
+       if err := json.Unmarshal(templateBytes, &template); err != nil {
+               return model.ToolConfig{}, err
+       }
+
+       toolConfig := model.ToolConfig{
+               Name:        nacosTool.Name,
+               Description: nacosTool.Description,
+               Cluster:     nacosTool.Name, // Directly use the tool name as 
the cluster name
+               BackendURL:  template.RequestTemplate.URL,
+               Request: model.RequestConfig{
+                       Method:  template.RequestTemplate.Method,
+                       Path:    
extractPathFromURL(template.RequestTemplate.URL),
+                       Headers: 
convertHeaders(template.RequestTemplate.Headers),
+               },
+               Args: func() []model.ArgConfig {
+                       args, err := 
convertInputSchemaToArgs(nacosTool.InputSchema, template.RequestTemplate)
+                       if err != nil {
+                               logger.Warnf("Failed to convert args for tool 
%s: %v", nacosTool.Name, err)
+                               return []model.ArgConfig{}
+                       }
+                       return args
+               }(),
+       }
+
+       return toolConfig, nil
+}
+
+func extractPathFromURL(raw string) string {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to