Skip to content

Conversation

@vincent0426
Copy link

@vincent0426 vincent0426 commented Oct 30, 2025

Implement SEP-986: Tool Name Guidance for Tool Type

Changes

  • Added a length check that raises an error when the tool name is not within the allowed range:
    raise ValueError(f"Invalid tool name length: {len(value)}. Tool name must be between 1 and 128 characters.")
  • Added a regex validation using re.compile(r"^[A-Za-z0-9_.-]+$") that raises an error for invalid characters:
    raise ValueError("Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.).")

Motivation and Context

Related issues: SEP-986 #1537

Implements the guidance from modelcontextprotocol/modelcontextprotocol#986

- Tool names SHOULD be between 1 and 128 characters in length (inclusive).
- Tool names SHOULD be considered case-sensitive.
- The following SHOULD be the only allowed characters: uppercase and lowercase ASCII letters (A-Z, a-z), digits
  (0-9), underscore (\_), dash (-), and dot (.)
- Tool names SHOULD NOT contain spaces, commas, or other special characters.
- Tool names SHOULD be unique within a server.
- Example valid tool names:
  - getUser
  - DATA_EXPORT_v2
  - admin.tools.list

How Has This Been Tested?

Unit Tests

Valid Values

  • getUser
  • DATA_EXPORT_v2
  • admin.tools.list
  • a
  • Z9_.-
  • "x" * 128 (max length)

Invalid Values

  • "" (empty string)
  • "x" * 129 (exceeds max length)
  • has space
  • comma,name
  • not/allowed
  • name@
  • name#
  • name$

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Comment on lines +899 to +913
@field_validator("name")
@classmethod
def _validate_tool_name(cls, value: str) -> str:
if not (1 <= len(value) <= 128):
raise ValueError(f"Invalid tool name length: {len(value)}. Tool name must be between 1 and 128 characters.")

if not TOOL_NAME_PATTERN.fullmatch(value):
raise ValueError("Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.).")

return value

"""
See [MCP specification](https://modelcontextprotocol.io/specification/draft/server/tools#tool-names)
for more information on tool naming conventions.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxisbey This is a less verbose way to write the same thing:

from typing import Annotated

from pydantic import StringConstraints

tool: Annotated[str, StringConstraints(max_length=128, pattern=r"^[A-Za-z0-9_.-]+$")]

I'm not sure if this should be applied on BaseMetadata, but if not, please drop the inheritance, and apply this here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, most or all the field_validators in this code source can be replaced by proper annotated versions - that you can see are cuter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't think there's a need to test that Pydantic is working as expected.

@maxisbey maxisbey added improves spec compliance When a change improves ability of SDK users to comply with spec definition P2 Moderate issues affecting some users, edge cases, potentially valuable feature labels Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improves spec compliance When a change improves ability of SDK users to comply with spec definition P2 Moderate issues affecting some users, edge cases, potentially valuable feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants