instead of `\\Z`) · Issue #3084 · modelcontextprotocol/python-sdk · GitHub","description":"imports react https github.githubassets.com assets react e27d1b3e03961e68.js react dom https github.githubassets.com assets react dom e5fd46a22d5c4058.js rea...","inLanguage":"en-US","isPartOf":{"@id":"https://kelaenderkoe.pages.dev/#website"},"datePublished":"2026-09-15T06:50:32.695Z","dateModified":"2026-09-15T06:50:32.695Z","breadcrumb":{"@id":"https://kelaenderkoe.pages.dev/pattern-https-github.com/modelcontextprotocol/python-sdk/issues/3084#breadcrumb"},"potentialAction":[{"@type":"ReadAction","target":["https://kelaenderkoe.pages.dev/pattern-https-github.com/modelcontextprotocol/python-sdk/issues/3084"]}]},{"@type":"Article","@id":"https://kelaenderkoe.pages.dev/pattern-https-github.com/modelcontextprotocol/python-sdk/issues/3084#article","headline":"Tool name validation accepts a trailing newline (regex uses `
$ instead of \Z) #3084
Describe the bug
TOOL_NAME_REGEXinsrc/mcp/shared/tool_name_validation.pyis end-anchored with$:In Python's default (non-
MULTILINE) mode,$matches at end-of-string or immediately before a single trailing\n. So a tool name ending in exactly one newline passes validation, even though\nis not in the allowed character set.The length guard uses
len(), which counts the\n, so"a" * 127 + "\n"(length 128) slips past both the length check and the character check.Embedded newlines and other trailing control characters are already rejected correctly. Only the single-trailing-newline case leaks.
To Reproduce
Expected behavior
validate_tool_name("evil_tool\n").is_validshould beFalse, since\nis not one of the allowed characters.Additional context
Anchoring the end with
\Z(strict end-of-string) instead of$fixes it, and no valid name is affected:re.match(r"^[A-Za-z0-9._-]{1,128}\Z", "abc")still matches, while"abc\n"correctly fails.I have an open PR with this fix and a regression test: #3076. Filing this issue to track it, per CONTRIBUTING ("Bug fixes for clear, reproducible issues are welcome—but still create an issue to track the fix").