|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dubbo.mcp.core; |
| 18 | +import org.apache.dubbo.common.config.Configuration; |
| 19 | +import org.apache.dubbo.common.config.ConfigurationUtils; |
| 20 | +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; |
| 21 | +import org.apache.dubbo.common.logger.LoggerFactory; |
| 22 | +import org.apache.dubbo.common.utils.StringUtils; |
| 23 | +import org.apache.dubbo.config.annotation.DubboService; |
| 24 | +import org.apache.dubbo.mcp.McpConstant; |
| 25 | +import org.apache.dubbo.rpc.model.ApplicationModel; |
| 26 | +import org.apache.dubbo.rpc.model.ProviderModel; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.regex.Pattern; |
| 31 | + |
| 32 | +public class McpServiceFilter { |
| 33 | + |
| 34 | + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(McpServiceFilter.class); |
| 35 | + |
| 36 | + private final Configuration configuration; |
| 37 | + private final Pattern[] includePatterns; |
| 38 | + private final Pattern[] excludePatterns; |
| 39 | + private final boolean defaultEnabled; |
| 40 | + |
| 41 | + public McpServiceFilter(ApplicationModel applicationModel) { |
| 42 | + this.configuration = ConfigurationUtils.getGlobalConfiguration(applicationModel); |
| 43 | + this.defaultEnabled = configuration.getBoolean(McpConstant.SETTINGS_MCP_DEFAULT_ENABLED, true); |
| 44 | + |
| 45 | + // 解析包含和排除模式 |
| 46 | + String includeStr = configuration.getString(McpConstant.SETTINGS_MCP_INCLUDE_PATTERNS, ""); |
| 47 | + String excludeStr = configuration.getString(McpConstant.SETTINGS_MCP_EXCLUDE_PATTERNS, ""); |
| 48 | + |
| 49 | + this.includePatterns = parsePatterns(includeStr); |
| 50 | + this.excludePatterns = parsePatterns(excludeStr); |
| 51 | + |
| 52 | + logger.debug( |
| 53 | + "MCP service filter initialized: defaultEnabled={}, includePatterns={}, excludePatterns={}", |
| 54 | + defaultEnabled, |
| 55 | + includeStr, |
| 56 | + excludeStr); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Check if service should be exposed as MCP tool |
| 61 | + */ |
| 62 | + public boolean shouldExposeAsMcpTool(ProviderModel providerModel) { |
| 63 | + String interfaceName = providerModel.getServiceModel().getInterfaceName(); |
| 64 | + |
| 65 | + // 1. Check exclude patterns (highest priority) |
| 66 | + if (isMatchedByPatterns(interfaceName, excludePatterns)) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // 2. Check annotation configuration |
| 71 | + Object serviceBean = providerModel.getServiceInstance(); |
| 72 | + if (serviceBean != null) { |
| 73 | + DubboService dubboService = serviceBean.getClass().getAnnotation(DubboService.class); |
| 74 | + if (dubboService != null && dubboService.mcpEnabled()) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // 3. Check specific service configuration |
| 80 | + String serviceSpecificKey = McpConstant.SETTINGS_MCP_SERVICE_PREFIX + "." + interfaceName + "." |
| 81 | + + McpConstant.SETTINGS_MCP_SERVICE_ENABLED_SUFFIX; |
| 82 | + Boolean configEnabled = configuration.getBoolean(serviceSpecificKey, (Boolean) null); |
| 83 | + if (configEnabled != null) { |
| 84 | + return configEnabled; |
| 85 | + } |
| 86 | + |
| 87 | + // 4. Check include patterns |
| 88 | + if (includePatterns.length > 0) { |
| 89 | + // If include patterns are defined, only services matching them should be included |
| 90 | + return isMatchedByPatterns(interfaceName, includePatterns); |
| 91 | + } |
| 92 | + |
| 93 | + // 5. Use default configuration |
| 94 | + return defaultEnabled; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get MCP tool configuration for service |
| 99 | + */ |
| 100 | + public McpToolConfig getMcpToolConfig(ProviderModel providerModel) { |
| 101 | + String interfaceName = providerModel.getServiceModel().getInterfaceName(); |
| 102 | + McpToolConfig config = new McpToolConfig(); |
| 103 | + |
| 104 | + // Get configuration from annotation |
| 105 | + Object serviceBean = providerModel.getServiceInstance(); |
| 106 | + if (serviceBean != null) { |
| 107 | + DubboService dubboService = serviceBean.getClass().getAnnotation(DubboService.class); |
| 108 | + if (dubboService != null) { |
| 109 | + config.setToolName(dubboService.mcpToolName()); |
| 110 | + config.setDescription(dubboService.mcpDescription()); |
| 111 | + config.setTags(Arrays.asList(dubboService.mcpTags())); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Get configuration from config file (higher priority) |
| 116 | + String servicePrefix = McpConstant.SETTINGS_MCP_SERVICE_PREFIX + "." + interfaceName + "."; |
| 117 | + |
| 118 | + String configToolName = configuration.getString(servicePrefix + McpConstant.SETTINGS_MCP_SERVICE_NAME_SUFFIX); |
| 119 | + if (StringUtils.isNotEmpty(configToolName)) { |
| 120 | + config.setToolName(configToolName); |
| 121 | + } |
| 122 | + |
| 123 | + String configDescription = |
| 124 | + configuration.getString(servicePrefix + McpConstant.SETTINGS_MCP_SERVICE_DESCRIPTION_SUFFIX); |
| 125 | + if (StringUtils.isNotEmpty(configDescription)) { |
| 126 | + config.setDescription(configDescription); |
| 127 | + } |
| 128 | + |
| 129 | + String configTags = configuration.getString(servicePrefix + McpConstant.SETTINGS_MCP_SERVICE_TAGS_SUFFIX); |
| 130 | + if (StringUtils.isNotEmpty(configTags)) { |
| 131 | + config.setTags(Arrays.asList(configTags.split(","))); |
| 132 | + } |
| 133 | + |
| 134 | + return config; |
| 135 | + } |
| 136 | + |
| 137 | + private Pattern[] parsePatterns(String patternStr) { |
| 138 | + if (StringUtils.isEmpty(patternStr)) { |
| 139 | + return new Pattern[0]; |
| 140 | + } |
| 141 | + |
| 142 | + return Arrays.stream(patternStr.split(",")) |
| 143 | + .map(String::trim) |
| 144 | + .filter(StringUtils::isNotEmpty) |
| 145 | + .map(pattern -> Pattern.compile(pattern.replace("*", ".*"))) |
| 146 | + .toArray(Pattern[]::new); |
| 147 | + } |
| 148 | + |
| 149 | + private boolean isMatchedByPatterns(String text, Pattern[] patterns) { |
| 150 | + for (Pattern pattern : patterns) { |
| 151 | + if (pattern.matcher(text).matches()) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * MCP tool configuration |
| 160 | + */ |
| 161 | + public static class McpToolConfig { |
| 162 | + private String toolName; |
| 163 | + private String description; |
| 164 | + private List<String> tags; |
| 165 | + |
| 166 | + public String getToolName() { |
| 167 | + return toolName; |
| 168 | + } |
| 169 | + |
| 170 | + public void setToolName(String toolName) { |
| 171 | + this.toolName = toolName; |
| 172 | + } |
| 173 | + |
| 174 | + public String getDescription() { |
| 175 | + return description; |
| 176 | + } |
| 177 | + |
| 178 | + public void setDescription(String description) { |
| 179 | + this.description = description; |
| 180 | + } |
| 181 | + |
| 182 | + public List<String> getTags() { |
| 183 | + return tags; |
| 184 | + } |
| 185 | + |
| 186 | + public void setTags(List<String> tags) { |
| 187 | + this.tags = tags; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments