RainYuY commented on code in PR #15406: URL: https://github.com/apache/dubbo/pull/15406#discussion_r2268605976
########## 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: 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]
