mark4z commented on code in PR #757: URL: https://github.com/apache/dubbo-go-pixiu/pull/757#discussion_r2352905548
########## pkg/filter/mcp/mcpserver/dynamic.go: ########## @@ -0,0 +1,73 @@ +/* + * 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 ( + "sync" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" + "github.com/apache/dubbo-go-pixiu/pkg/model" +) + +var ( + globalRegistry *ToolRegistry + globalDynamic *DynamicConsumer + + // sync.Once variables for thread-safe singleton initialization + registryOnce sync.Once + dynamicOnce sync.Once +) + +// GetOrInitRegistry returns a singleton ToolRegistry +func GetOrInitRegistry() *ToolRegistry { + registryOnce.Do(func() { + globalRegistry = NewToolRegistry() + }) + return globalRegistry +} + +// GetOrInitDynamic returns a singleton DynamicConsumer +func GetOrInitDynamic() *DynamicConsumer { + dynamicOnce.Do(func() { + globalDynamic = NewDynamicConsumer(GetOrInitRegistry()) + }) + return globalDynamic +} + +// DynamicConsumer applies dynamic MCP configurations into the registry +type DynamicConsumer struct { + registry *ToolRegistry +} + +func NewDynamicConsumer(reg *ToolRegistry) *DynamicConsumer { + return &DynamicConsumer{registry: reg} +} + +// update tools from the remote config in nacos +func (d *DynamicConsumer) ApplyMcpServerConfig(cfg *model.McpServerConfig) error { + if cfg == nil { + return nil + } + + // full sync tools + d.registry.ReplaceAllTools(cfg.Tools) Review Comment: registry 的 ReplaceAllTools 采用全量替换,若外部调用未做防抖或幂等校验,可能导致并发/丢失写入。 ########## 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: BackendURL 解析 host:port,若端口解析失败或格式异常则直接跳过注册,建议增加错误日志并做 fallback。 ########## 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 { Review Comment: 多线程并发 ApplyMcpServerConfig,存在全量工具覆盖丢失部分数据风险。 Nacos 服务列表返回为空,导致所有 watcher 被取消,后续工具无法被重新注册。 工具参数 In 字段自动转换失败,导致实际请求 body/query/path 参数不正确。 环境变量覆盖 Nacos 地址时传入非法字符串,导致注册中心 init 失败。 -- 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]
