Title: Tool name validation accepts a trailing newline (regex uses `$` instead of `\Z`) · Issue #3084 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Tool name validation accepts a trailing newline (regex uses `$` instead of `\Z`) · Issue #3084 · modelcontextprotocol/python-sdk
X Title: Tool name validation accepts a trailing newline (regex uses `$` instead of `\Z`) · Issue #3084 · modelcontextprotocol/python-sdk
Description: Describe the bug TOOL_NAME_REGEX in src/mcp/shared/tool_name_validation.py is end-anchored with $: TOOL_NAME_REGEX = re.compile(r"^[A-Za-z0-9._-]{1,128}$") In Python's default (non-MULTILINE) mode, $ matches at end-of-string or immediate...
Open Graph Description: Describe the bug TOOL_NAME_REGEX in src/mcp/shared/tool_name_validation.py is end-anchored with $: TOOL_NAME_REGEX = re.compile(r"^[A-Za-z0-9._-]{1,128}$") In Python's default (non-MULTILINE) mode,...
X Description: Describe the bug TOOL_NAME_REGEX in src/mcp/shared/tool_name_validation.py is end-anchored with $: TOOL_NAME_REGEX = re.compile(r"^[A-Za-z0-9._-]{1,128}$") In Python's default (non-MU...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3084
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Tool name validation accepts a trailing newline (regex uses `$` instead of `\\Z`)","articleBody":"### Describe the bug\n\n`TOOL_NAME_REGEX` in `src/mcp/shared/tool_name_validation.py` is end-anchored with `$`:\n\n```python\nTOOL_NAME_REGEX = re.compile(r\"^[A-Za-z0-9._-]{1,128}$\")\n```\n\nIn 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 `\\n` is not in the allowed character set.\n\nThe length guard uses `len()`, which counts the `\\n`, so `\"a\" * 127 + \"\\n\"` (length 128) slips past both the length check and the character check.\n\nEmbedded newlines and other trailing control characters are already rejected correctly. Only the single-trailing-newline case leaks.\n\n### To Reproduce\n\n```python\nfrom mcp.shared.tool_name_validation import validate_tool_name, validate_and_warn_tool_name\n\nprint(validate_tool_name(\"evil_tool\\n\").is_valid) # True (expected: False)\nprint(validate_and_warn_tool_name(\"evil_tool\\n\")) # True, and no warning is logged\n\nprint(validate_tool_name(\"a\" * 127 + \"\\n\").is_valid) # True, length 128 passes too\n```\n\n### Expected behavior\n\n`validate_tool_name(\"evil_tool\\n\").is_valid` should be `False`, since `\\n` is not one of the allowed characters.\n\n### Additional context\n\nAnchoring 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.\n\nI 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\").\n","author":{"url":"https://github.com/Otis0408","@type":"Person","name":"Otis0408"},"datePublished":"2026-07-10T11:27:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/3084/python-sdk/issues/3084"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b44dc579-9571-2a6f-9eff-51608b231116 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9500:2D74E4:178E22B:2056622:6A572B8F |
| html-safe-nonce | be850453a6caf83c50c9ac8da78795e7b5c8e03bfad92a1a01822d60be2a4e5c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTAwOjJENzRFNDoxNzhFMjJCOjIwNTY2MjI6NkE1NzJCOEYiLCJ2aXNpdG9yX2lkIjoiNDU5MDYwMDUyNjgxMTgzNTI3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5ddf35ad8691da705b93bd92e343c0f435e003c340be1791b0e7a04e802fec14 |
| hovercard-subject-tag | issue:4854577663 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3084/issue_layout |
| twitter:image | https://opengraph.githubassets.com/04082bac69aae0eb83b5567782987e78e632ddb90f36f602421f5831f1bf39f9/modelcontextprotocol/python-sdk/issues/3084 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/04082bac69aae0eb83b5567782987e78e632ddb90f36f602421f5831f1bf39f9/modelcontextprotocol/python-sdk/issues/3084 |
| og:image:alt | Describe the bug TOOL_NAME_REGEX in src/mcp/shared/tool_name_validation.py is end-anchored with $: TOOL_NAME_REGEX = re.compile(r"^[A-Za-z0-9._-]{1,128}$") In Python's default (non-MULTILINE) mode,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Otis0408 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4e7a7296a3830877cf21a6ad2a972c9e618a48915e03966cf0c53eb08e5aad98 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2576d1f0198cf1588faf2edf27f1ed120903be10 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width