RainYuY commented on code in PR #15406: URL: https://github.com/apache/dubbo/pull/15406#discussion_r2264632621
########## 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; + } Review Comment: I don't know what this comment means… It exists. -- 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]
