|
| 1 | +package com.github.codeboyzhou.mcp.declarative.configuration; |
| 2 | + |
| 3 | +import com.github.codeboyzhou.mcp.declarative.enums.ServerMode; |
| 4 | +import com.github.codeboyzhou.mcp.declarative.exception.McpServerConfigurationException; |
| 5 | +import com.github.codeboyzhou.mcp.declarative.util.StringHelper; |
| 6 | + |
| 7 | +/** |
| 8 | + * Utility class for validating MCP server configuration properties. |
| 9 | + * |
| 10 | + * <p>This class provides static methods to perform comprehensive validation of MCP server |
| 11 | + * configuration objects, ensuring that all required fields are present and properly configured. It |
| 12 | + * validates both base configuration properties and mode-specific settings for SSE and STREAMABLE |
| 13 | + * server modes. |
| 14 | + * |
| 15 | + * @see McpServerConfiguration |
| 16 | + * @author codeboyzhou |
| 17 | + */ |
| 18 | +public final class McpConfigurationChecker { |
| 19 | + /** |
| 20 | + * Private constructor to prevent instantiation of this utility class. |
| 21 | + * |
| 22 | + * @throws UnsupportedOperationException always thrown when attempting to instantiate |
| 23 | + */ |
| 24 | + private McpConfigurationChecker() { |
| 25 | + throw new UnsupportedOperationException("Utility class should not be instantiated"); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Performs comprehensive validation of the MCP server configuration. |
| 30 | + * |
| 31 | + * <p>This method validates all required configuration properties including: |
| 32 | + * |
| 33 | + * <ul> |
| 34 | + * <li>Basic server information (enabled, mode, name, version, type, instructions) |
| 35 | + * <li>Timeout and capabilities settings |
| 36 | + * <li>Change notification configuration |
| 37 | + * <li>Mode-specific settings (SSE or STREAMABLE) |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * @param configuration the MCP server configuration to validate |
| 41 | + * @throws McpServerConfigurationException if any required configuration property is missing |
| 42 | + */ |
| 43 | + public static void check(McpServerConfiguration configuration) { |
| 44 | + checkNull("enabled", configuration.enabled()); |
| 45 | + checkNull("mode", configuration.mode()); |
| 46 | + checkBlank("name", configuration.name()); |
| 47 | + checkBlank("version", configuration.version()); |
| 48 | + checkNull("type", configuration.type()); |
| 49 | + checkBlank("instructions", configuration.instructions()); |
| 50 | + checkNull("request-timeout", configuration.requestTimeout()); |
| 51 | + checkNull("capabilities", configuration.capabilities()); |
| 52 | + checkNull("resource", configuration.capabilities().resource()); |
| 53 | + checkNull("prompt", configuration.capabilities().prompt()); |
| 54 | + checkNull("tool", configuration.capabilities().tool()); |
| 55 | + checkNull("completion", configuration.capabilities().completion()); |
| 56 | + checkNull("change-notification", configuration.changeNotification()); |
| 57 | + checkNull("resource", configuration.changeNotification().resource()); |
| 58 | + checkNull("prompt", configuration.changeNotification().prompt()); |
| 59 | + checkNull("tool", configuration.changeNotification().tool()); |
| 60 | + if (configuration.mode() == ServerMode.SSE) { |
| 61 | + checkNull("sse", configuration.sse()); |
| 62 | + checkBlank("message-endpoint", configuration.sse().messageEndpoint()); |
| 63 | + checkBlank("endpoint", configuration.sse().endpoint()); |
| 64 | + checkBlank("base-url", configuration.sse().baseUrl()); |
| 65 | + checkNull("port", configuration.sse().port()); |
| 66 | + } |
| 67 | + if (configuration.mode() == ServerMode.STREAMABLE) { |
| 68 | + checkNull("streamable", configuration.streamable()); |
| 69 | + checkBlank("mcp-endpoint", configuration.streamable().mcpEndpoint()); |
| 70 | + checkNull("disallow-delete", configuration.streamable().disallowDelete()); |
| 71 | + checkNull("keep-alive-interval", configuration.streamable().keepAliveInterval()); |
| 72 | + checkNull("port", configuration.streamable().port()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Validates that at least one of two configuration values is not null. |
| 78 | + * |
| 79 | + * <p>This method is typically used to validate profile-based configurations where a base value |
| 80 | + * and a profile-specific value can both provide the same configuration property. At least one of |
| 81 | + * them must be non-null. |
| 82 | + * |
| 83 | + * @param <T> the type of the configuration values |
| 84 | + * @param configKey the configuration key name used for error reporting |
| 85 | + * @param baseValue the base configuration value |
| 86 | + * @param profileValue the profile-specific configuration value |
| 87 | + * @throws McpServerConfigurationException if both values are null |
| 88 | + */ |
| 89 | + public static <T> void checkNull(String configKey, T baseValue, T profileValue) { |
| 90 | + if (baseValue == null && profileValue == null) { |
| 91 | + throw new McpServerConfigurationException( |
| 92 | + String.format("Missing config key '%s' in the configuration file.", configKey)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Validates that a single configuration value is not null. |
| 98 | + * |
| 99 | + * <p>This method performs a simple null check on a configuration value and throws an exception if |
| 100 | + * the value is null, indicating that a required configuration property is missing. |
| 101 | + * |
| 102 | + * @param <T> the type of the configuration value |
| 103 | + * @param configKey the configuration key name used for error reporting |
| 104 | + * @param value the configuration value to validate |
| 105 | + * @throws McpServerConfigurationException if the value is null |
| 106 | + */ |
| 107 | + public static <T> void checkNull(String configKey, T value) { |
| 108 | + if (value == null) { |
| 109 | + throw new McpServerConfigurationException( |
| 110 | + String.format("Missing config key '%s' in the configuration file.", configKey)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Validates that at least one of two string configuration values is not blank. |
| 116 | + * |
| 117 | + * <p>This method is typically used to validate profile-based configurations where a base value |
| 118 | + * and a profile-specific value can both provide the same string configuration property. At least |
| 119 | + * one of them must be non-blank. A value is considered blank if it is null, empty, or contains |
| 120 | + * only whitespace. |
| 121 | + * |
| 122 | + * @param configKey the configuration key name used for error reporting |
| 123 | + * @param baseValue the base configuration string value |
| 124 | + * @param profileValue the profile-specific configuration string value |
| 125 | + * @throws McpServerConfigurationException if both values are blank |
| 126 | + */ |
| 127 | + public static void checkBlank(String configKey, String baseValue, String profileValue) { |
| 128 | + if (StringHelper.isBlank(baseValue) && StringHelper.isBlank(profileValue)) { |
| 129 | + throw new McpServerConfigurationException( |
| 130 | + String.format("Missing config key '%s' in the configuration file.", configKey)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Validates that a single string configuration value is not blank. |
| 136 | + * |
| 137 | + * <p>This method performs a blank check on a string configuration value and throws an exception |
| 138 | + * if the value is blank. A value is considered blank if it is null, empty, or contains only |
| 139 | + * whitespace. |
| 140 | + * |
| 141 | + * @param configKey the configuration key name used for error reporting |
| 142 | + * @param value the configuration string value to validate |
| 143 | + * @throws McpServerConfigurationException if the value is blank |
| 144 | + */ |
| 145 | + public static void checkBlank(String configKey, String value) { |
| 146 | + if (StringHelper.isBlank(value)) { |
| 147 | + throw new McpServerConfigurationException( |
| 148 | + String.format("Missing config key '%s' in the configuration file.", configKey)); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments