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
9 changes: 7 additions & 2 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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, StringConstraints
from pydantic.networks import AnyUrl, UrlConstraints
from typing_extensions import deprecated

Expand Down Expand Up @@ -199,7 +199,7 @@ class EmptyResult(Result):
class BaseMetadata(BaseModel):
"""Base class for entities with name and optional title fields."""

name: str
name: Annotated[str, StringConstraints(min_length=1, max_length=128, pattern=r"^[A-Za-z0-9_.-]+$")]
"""The programmatic name of the entity."""

title: str | None = None
Expand Down Expand Up @@ -891,6 +891,11 @@ class Tool(BaseMetadata):
"""
model_config = ConfigDict(extra="allow")

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


class ListToolsResult(PaginatedResult):
"""The server's response to a tools/list request from the client."""
Expand Down
16 changes: 16 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,18 @@ 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"})
Loading