Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/mcp/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from collections.abc import Callable
from typing import Annotated, Any, Generic, Literal, TypeAlias, TypeVar

from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel
from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel, field_validator
from pydantic.networks import AnyUrl, UrlConstraints
from typing_extensions import deprecated

Expand Down Expand Up @@ -39,6 +40,10 @@
RequestId = Annotated[int, Field(strict=True)] | str
AnyFunction: TypeAlias = Callable[..., Any]

# Tool name validation pattern (ASCII letters, digits, underscore, dash, dot)
# Pattern ensures entire string contains only valid characters by using ^ and $ anchors
TOOL_NAME_PATTERN = re.compile(r"^[A-Za-z0-9_.-]+$")


class RequestParams(BaseModel):
class Meta(BaseModel):
Expand Down Expand Up @@ -891,6 +896,22 @@ class Tool(BaseMetadata):
"""
model_config = ConfigDict(extra="allow")

@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.
"""
Comment on lines +899 to +913
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.



class ListToolsResult(PaginatedResult):
"""The server's response to a tools/list request from the client."""
Expand Down
35 changes: 35 additions & 0 deletions tests/test_types.py
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.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
InitializeRequestParams,
JSONRPCMessage,
JSONRPCRequest,
Tool,
)


Expand Down Expand Up @@ -56,3 +57,37 @@ async def test_method_initialization():
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
assert initialize_request.params is not None
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION


@pytest.mark.parametrize(
"name",
[
"getUser",
"DATA_EXPORT_v2",
"admin.tools.list",
"a",
"Z9_.-",
"x" * 128, # max length
],
)
def test_tool_allows_valid_names(name: str) -> None:
Tool(name=name, inputSchema={"type": "object"})


@pytest.mark.parametrize(
("name", "expected"),
[
("", "Invalid tool name length: 0. Tool name must be between 1 and 128 characters."),
("x" * 129, "Invalid tool name length: 129. Tool name must be between 1 and 128 characters."),
("has space", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("comma,name", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("not/allowed", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name@", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name#", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name$", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
],
)
def test_tool_rejects_invalid_names(name: str, expected: str) -> None:
with pytest.raises(ValueError) as exc_info:
Tool(name=name, inputSchema={"type": "object"})
assert expected in str(exc_info.value)