songxiaosheng commented on code in PR #15406:
URL: https://github.com/apache/dubbo/pull/15406#discussion_r2266551875


##########
dubbo-plugin/dubbo-mcp/src/main/java/org/apache/dubbo/mcp/core/McpServiceExportListener.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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 org.apache.dubbo.mcp.core;
+
+import org.apache.dubbo.common.constants.LoggerCodeConstants;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.ServiceListener;
+import org.apache.dubbo.mcp.tool.DubboServiceToolRegistry;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ProviderModel;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class McpServiceExportListener implements ServiceListener {
+
+    private static final ErrorTypeAwareLogger logger =
+            
LoggerFactory.getErrorTypeAwareLogger(McpServiceExportListener.class);
+
+    private final Map<String, RegisteredServiceInfo> registeredServiceTools = 
new ConcurrentHashMap<>();
+
+    private static class RegisteredServiceInfo {
+        final int toolCount;
+        final String interfaceName;
+        final ProviderModel providerModel;
+
+        RegisteredServiceInfo(int toolCount, String interfaceName, 
ProviderModel providerModel) {
+            this.toolCount = toolCount;
+            this.interfaceName = interfaceName;
+            this.providerModel = providerModel;
+        }
+    }
+
+    @Override
+    public void exported(ServiceConfig sc) {
+        try {
+            if (sc.getRef() == null) {
+                return;
+            }
+
+            String serviceKey = sc.getUniqueServiceName();
+            ProviderModel providerModel =
+                    
sc.getScopeModel().getServiceRepository().lookupExportedService(serviceKey);
+
+            if (providerModel == null) {
+                logger.warn(
+                        LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION,
+                        "",
+                        "",
+                        "ProviderModel not found for service: " + 
sc.getInterface() + " with key: " + serviceKey);
+                return;
+            }
+
+            DubboServiceToolRegistry toolRegistry = getToolRegistry(sc);
+            if (toolRegistry == null) {
+                return;
+            }
+
+            int registeredCount = toolRegistry.registerService(providerModel);
+
+            if (registeredCount > 0) {
+                registeredServiceTools.put(
+                        serviceKey,
+                        new RegisteredServiceInfo(
+                                registeredCount, 
providerModel.getServiceModel().getInterfaceName(), providerModel));
+                logger.info(
+                        "Dynamically registered {} MCP tools for exported 
service: {}",
+                        registeredCount,
+                        providerModel.getServiceModel().getInterfaceName());
+            }
+        } catch (Exception e) {
+            logger.error(
+                    LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION,
+                    "",
+                    "",
+                    "Failed to register MCP tools for exported service: " + 
sc.getInterface(),
+                    e);
+        }
+    }
+
+    @Override
+    public void unexported(ServiceConfig sc) {
+        try {
+            if (sc.getRef() == null) {
+                return;
+            }
+
+            String serviceKey = sc.getUniqueServiceName();
+            RegisteredServiceInfo serviceInfo = 
registeredServiceTools.remove(serviceKey);
+
+            if (serviceInfo != null && serviceInfo.toolCount > 0) {
+                DubboServiceToolRegistry toolRegistry = getToolRegistry(sc);
+                if (toolRegistry == null) {
+                    return;
+                }
+
+                toolRegistry.unregisterService(serviceInfo.providerModel);
+                logger.info(
+                        "Dynamically unregistered {} MCP tools for unexported 
service: {}",
+                        serviceInfo.toolCount,
+                        serviceInfo.interfaceName);
+            }
+
+        } catch (Exception e) {
+            logger.error(
+                    LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION,
+                    "",
+                    "",
+                    "Failed to unregister MCP tools for unexported service: " 
+ sc.getInterface(),
+                    e);
+        }
+    }
+
+    private DubboServiceToolRegistry getToolRegistry(ServiceConfig sc) {
+        try {
+            ApplicationModel applicationModel = 
sc.getScopeModel().getApplicationModel();
+            return 
applicationModel.getBeanFactory().getBean(DubboServiceToolRegistry.class);
+        } catch (Exception e) {
+            logger.debug("Failed to get DubboServiceToolRegistry from 
application context: {}", e.getMessage());

Review Comment:
   You can use a warn log here to remind users to pay attention to the 
exception, as debug logs are often ignored.



##########
dubbo-common/src/main/java/org/apache/dubbo/config/nested/McpConfig.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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 org.apache.dubbo.config.nested;
+
+import org.apache.dubbo.config.support.Nested;
+
+import java.io.Serializable;
+
+public class McpConfig implements Serializable {
+
+    private static final long serialVersionUID = 6943417856234001947L;
+
+    /**
+     * Whether to enable MCP Server support
+     * <p>The default value is 'true'.
+     */
+    private Boolean enabled;
+
+    /**
+     * The port of MCP Server
+     * <p>The default value is '0'.
+     */
+    private Integer port;
+
+    /**
+     * the path of mcp
+     */
+    @Nested
+    private MCPPath path;
+
+    /**
+     * streamable or sse
+     */
+    private String protocol;
+
+    public Boolean getEnabled() {

Review Comment:
   It is recommended to use `isEnabled`, which is more appropriate.



##########
dubbo-plugin/dubbo-mcp/src/main/java/org/apache/dubbo/mcp/core/McpServiceFilter.java:
##########
@@ -0,0 +1,383 @@
+/*
+ * 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 org.apache.dubbo.mcp.core;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.config.Configuration;
+import org.apache.dubbo.common.config.ConfigurationUtils;
+import org.apache.dubbo.common.constants.LoggerCodeConstants;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.config.annotation.DubboService;
+import org.apache.dubbo.mcp.McpConstant;
+import org.apache.dubbo.mcp.annotations.McpTool;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ProviderModel;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+
+public class McpServiceFilter {
+
+    private static final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(McpServiceFilter.class);
+
+    private final Configuration configuration;
+    private final Pattern[] includePatterns;
+    private final Pattern[] excludePatterns;
+    private final boolean defaultEnabled;
+
+    public McpServiceFilter(ApplicationModel applicationModel) {
+        this.configuration = 
ConfigurationUtils.getGlobalConfiguration(applicationModel);
+        this.defaultEnabled = 
configuration.getBoolean(McpConstant.SETTINGS_MCP_DEFAULT_ENABLED, true);
+
+        String includeStr = 
configuration.getString(McpConstant.SETTINGS_MCP_INCLUDE_PATTERNS, "");
+        String excludeStr = 
configuration.getString(McpConstant.SETTINGS_MCP_EXCLUDE_PATTERNS, "");
+
+        this.includePatterns = parsePatterns(includeStr);
+        this.excludePatterns = parsePatterns(excludeStr);
+    }
+
+    /**
+     * Check if service should be exposed as MCP tool.
+     * Priority: URL Parameters > Annotations > Configuration File > Default
+     */
+    public boolean shouldExposeAsMcpTool(ProviderModel providerModel) {
+        String interfaceName = 
providerModel.getServiceModel().getInterfaceName();
+
+        if (isMatchedByPatterns(interfaceName, excludePatterns)) {
+            return false;
+        }
+
+        URL serviceUrl = getServiceUrl(providerModel);
+        if (serviceUrl != null) {
+            String urlValue = 
serviceUrl.getParameter(McpConstant.PARAM_MCP_ENABLED);
+            if (urlValue != null && StringUtils.isNotEmpty(urlValue)) {
+                return Boolean.parseBoolean(urlValue);
+            }
+        }
+
+        Object serviceBean = providerModel.getServiceInstance();
+        if (serviceBean != null) {
+            DubboService dubboService = 
serviceBean.getClass().getAnnotation(DubboService.class);
+            if (dubboService != null && dubboService.mcpEnabled()) {
+                return true;
+            }
+        }
+
+        String serviceSpecificKey = McpConstant.SETTINGS_MCP_SERVICE_PREFIX + 
"." + interfaceName + ".enabled";
+        if (configuration.containsKey(serviceSpecificKey)) {
+            return configuration.getBoolean(serviceSpecificKey, false);
+        }
+
+        if (configuration.containsKey(McpConstant.PARAM_MCP_ENABLED)) {
+            return configuration.getBoolean(McpConstant.PARAM_MCP_ENABLED, 
false);
+        }
+
+        if (includePatterns.length > 0) {
+            return isMatchedByPatterns(interfaceName, includePatterns);
+        }
+
+        return defaultEnabled;
+    }
+
+    /**
+     * Check if specific method should be exposed as MCP tool.
+     * Priority: @McpTool(enabled=false) > URL Parameters > 
@McpTool(enabled=true) > Configuration > Service-level
+     */
+    public boolean shouldExposeMethodAsMcpTool(ProviderModel providerModel, 
Method method) {
+        String interfaceName = 
providerModel.getServiceModel().getInterfaceName();
+        String methodName = method.getName();
+
+        if (isMatchedByPatterns(interfaceName, excludePatterns)) {
+            return false;
+        }
+
+        McpTool methodMcpTool = getMethodMcpTool(providerModel, method);
+
+        if (methodMcpTool != null && !methodMcpTool.enabled()) {
+            return false;
+        }
+
+        URL serviceUrl = getServiceUrl(providerModel);
+        if (serviceUrl != null) {
+            String methodUrlValue = serviceUrl.getMethodParameter(methodName, 
McpConstant.PARAM_MCP_ENABLED);
+
+            if (methodUrlValue != null && 
StringUtils.isNotEmpty(methodUrlValue)) {
+                return Boolean.parseBoolean(methodUrlValue);
+            }
+
+            String serviceLevelValue = 
serviceUrl.getParameter(McpConstant.PARAM_MCP_ENABLED);
+
+            if (serviceLevelValue != null && 
StringUtils.isNotEmpty(serviceLevelValue)) {
+                return Boolean.parseBoolean(serviceLevelValue);
+            }
+        }
+
+        if (methodMcpTool != null && methodMcpTool.enabled()) {
+            return true;
+        }
+
+        String methodConfigKey =
+                McpConstant.SETTINGS_MCP_SERVICE_PREFIX + "." + interfaceName 
+ ".methods." + methodName + ".enabled";
+        if (configuration.containsKey(methodConfigKey)) {
+            return configuration.getBoolean(methodConfigKey, false);
+        }
+
+        if (shouldExposeAsMcpTool(providerModel)) {
+            return Modifier.isPublic(method.getModifiers());
+        }
+
+        return false;
+    }
+
+    /**
+     * Get @McpTool annotation from method, checking both interface and 
implementation class.
+     */
+    private McpTool getMethodMcpTool(ProviderModel providerModel, Method 
method) {
+        String methodName = method.getName();
+        Class<?>[] paramTypes = method.getParameterTypes();
+
+        Object serviceBean = providerModel.getServiceInstance();
+        if (serviceBean != null) {
+            try {
+                Method implMethod = 
serviceBean.getClass().getMethod(methodName, paramTypes);
+                McpTool implMcpTool = implMethod.getAnnotation(McpTool.class);
+                if (implMcpTool != null) {
+                    return implMcpTool;
+                }
+            } catch (NoSuchMethodException e) {
+                // Method not found in implementation class

Review Comment:
   don't ignore exception



-- 
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