-
Notifications
You must be signed in to change notification settings - Fork 795
feat: added tools name format validation accordingly #SEP-986 #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ashakirin
wants to merge
1
commit into
modelcontextprotocol:main
from
ashakirin:feature/sep-986-validate-tool-name-format
+224
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
mcp-core/src/main/java/io/modelcontextprotocol/spec/ToolNameValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.spec; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Validates tool names according to the MCP specification. | ||
| * | ||
| * <p> | ||
| * Tool names must conform to the following rules: | ||
| * <ul> | ||
| * <li>Must be between 1 and 128 characters in length</li> | ||
| * <li>May only contain: A-Z, a-z, 0-9, underscore (_), hyphen (-), and dot (.)</li> | ||
| * <li>Must not contain spaces, commas, or other special characters</li> | ||
| * </ul> | ||
| * | ||
| * @see <a href= | ||
| * "https://modelcontextprotocol.io/specification/draft/server/tools#tool-names">MCP | ||
| * Specification - Tool Names</a> | ||
| */ | ||
| public final class ToolNameValidator { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(ToolNameValidator.class); | ||
|
|
||
| private static final int MAX_LENGTH = 128; | ||
|
|
||
| private static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[A-Za-z0-9_\\-.]+$"); | ||
|
|
||
| private ToolNameValidator() { | ||
| } | ||
|
|
||
| /** | ||
| * Validates a tool name according to MCP specification. | ||
| * @param name the tool name to validate | ||
| * @param strict if true, throws exception on invalid name; if false, logs warning | ||
| * @throws IllegalArgumentException if strict is true and name is invalid | ||
| */ | ||
| public static void validate(String name, boolean strict) { | ||
| if (name == null || name.isEmpty()) { | ||
| handleError("Tool name must not be null or empty", name, strict); | ||
| return; | ||
| } | ||
| if (name.length() > MAX_LENGTH) { | ||
| handleError("Tool name must not exceed 128 characters", name, strict); | ||
| return; | ||
| } | ||
| if (!VALID_NAME_PATTERN.matcher(name).matches()) { | ||
| handleError("Tool name contains invalid characters (allowed: A-Z, a-z, 0-9, _, -, .)", name, strict); | ||
| } | ||
| } | ||
|
|
||
| private static void handleError(String message, String name, boolean strict) { | ||
| String fullMessage = message + ": '" + name + "'"; | ||
| if (strict) { | ||
| throw new IllegalArgumentException(fullMessage); | ||
| } | ||
| else { | ||
| logger.warn("{}. Processing continues, but tool name should be fixed.", fullMessage); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
mcp-core/src/test/java/io/modelcontextprotocol/spec/ToolNameValidatorTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.spec; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatCode; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Tests for {@link ToolNameValidator}. | ||
| */ | ||
| class ToolNameValidatorTests { | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { "getUser", "DATA_EXPORT_v2", "admin.tools.list", "my-tool", "Tool123", "a", "A", | ||
| "_private", "tool_name", "tool-name", "tool.name", "UPPERCASE", "lowercase", "MixedCase123" }) | ||
| void validToolNames_strictMode(String name) { | ||
| assertThatCode(() -> ToolNameValidator.validate(name, true)).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| void validToolName_maxLength() { | ||
| String name = "a".repeat(128); | ||
| assertThatCode(() -> ToolNameValidator.validate(name, true)).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidToolName_null_strictMode() { | ||
| assertThatThrownBy(() -> ToolNameValidator.validate(null, true)).isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidToolName_empty_strictMode() { | ||
| assertThatThrownBy(() -> ToolNameValidator.validate("", true)).isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidToolName_tooLong_strictMode() { | ||
| String name = "a".repeat(129); | ||
| assertThatThrownBy(() -> ToolNameValidator.validate(name, true)).isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("128 characters"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { "tool name", // space | ||
| "tool,name", // comma | ||
| "tool@name", // at sign | ||
| "tool#name", // hash | ||
| "tool$name", // dollar | ||
| "tool%name", // percent | ||
| "tool&name", // ampersand | ||
| "tool*name", // asterisk | ||
| "tool+name", // plus | ||
| "tool=name", // equals | ||
| "tool/name", // slash | ||
| "tool\\name", // backslash | ||
| "tool:name", // colon | ||
| "tool;name", // semicolon | ||
| "tool'name", // single quote | ||
| "tool\"name", // double quote | ||
| "tool<name", // less than | ||
| "tool>name", // greater than | ||
| "tool?name", // question mark | ||
| "tool!name", // exclamation | ||
| "tool(name)", // parentheses | ||
| "tool[name]", // brackets | ||
| "tool{name}", // braces | ||
| "tool|name", // pipe | ||
| "tool~name", // tilde | ||
| "tool`name", // backtick | ||
| "tool^name", // caret | ||
| "tööl", // non-ASCII | ||
| "工具" // unicode | ||
| }) | ||
| void invalidToolNames_specialCharacters_strictMode(String name) { | ||
| assertThatThrownBy(() -> ToolNameValidator.validate(name, true)).isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("invalid characters"); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidToolName_nonStrictMode_doesNotThrow() { | ||
| // Non-strict mode should not throw, just warn | ||
| assertThatCode(() -> ToolNameValidator.validate("invalid name", false)).doesNotThrowAnyException(); | ||
| assertThatCode(() -> ToolNameValidator.validate(null, false)).doesNotThrowAnyException(); | ||
| assertThatCode(() -> ToolNameValidator.validate("", false)).doesNotThrowAnyException(); | ||
| assertThatCode(() -> ToolNameValidator.validate("a".repeat(129), false)).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| void toolBuilder_validatesName_strictMode() { | ||
| assertThatThrownBy(() -> McpSchema.Tool.builder().name("invalid name with space").build()) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("invalid characters"); | ||
| } | ||
|
|
||
| @Test | ||
| void toolBuilder_validName() { | ||
| McpSchema.Tool tool = McpSchema.Tool.builder().name("valid_tool-name.v1").build(); | ||
| assertThat(tool.name()).isEqualTo("valid_tool-name.v1"); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.