Skip to content

Commit c6b2fc0

Browse files
maxisbeyOtis0408
andcommitted
fix: reject trailing newline in tool-name validation
With re.match, a $-anchored pattern also matches just before a single trailing newline, so tool-name validation accepted "name\n" without a SEP-986 warning. Switch to re.fullmatch. Backport of #3076. Co-authored-by: otiscuilei <otiscui@icloud.com>
1 parent ba33472 commit c6b2fc0

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/mcp/shared/tool_name_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def validate_tool_name(name: str) -> ToolNameValidationResult:
7777
warnings.append("Tool name starts or ends with a dot, which may cause parsing issues in some contexts")
7878

7979
# Check for invalid characters
80-
if not TOOL_NAME_REGEX.match(name):
80+
if not TOOL_NAME_REGEX.fullmatch(name):
8181
# Find all invalid characters (unique, preserving order)
8282
invalid_chars: list[str] = []
8383
seen: set[str] = set()

tests/shared/test_tool_name_validation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ def test_rejects_name_exceeding_max_length(self) -> None:
6666
("get,user,profile", "','"),
6767
("user/profile/update", "'/'"),
6868
("user@domain.com", "'@'"),
69+
# a single trailing newline slipped past `$` with re.match
70+
("valid_name\n", "'\\n'"),
71+
("a" * 127 + "\n", "'\\n'"),
6972
],
7073
ids=[
7174
"with_spaces",
7275
"with_commas",
7376
"with_slashes",
7477
"with_at_symbol",
78+
"with_trailing_newline",
79+
"max_length_with_trailing_newline",
7580
],
7681
)
7782
def test_rejects_invalid_characters(self, tool_name: str, expected_char: str) -> None:

0 commit comments

Comments
 (0)