|
1 | | -import re |
2 | 1 | from collections.abc import Callable |
3 | 2 | from typing import Annotated, Any, Generic, Literal, TypeAlias, TypeVar |
4 | 3 |
|
5 | | -from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel, field_validator |
| 4 | +from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel, StringConstraints |
6 | 5 | from pydantic.networks import AnyUrl, UrlConstraints |
7 | 6 | from typing_extensions import deprecated |
8 | 7 |
|
|
40 | 39 | RequestId = Annotated[int, Field(strict=True)] | str |
41 | 40 | AnyFunction: TypeAlias = Callable[..., Any] |
42 | 41 |
|
43 | | -# Tool name validation pattern (ASCII letters, digits, underscore, dash, dot) |
44 | | -# Pattern ensures entire string contains only valid characters by using ^ and $ anchors |
45 | | -TOOL_NAME_PATTERN = re.compile(r"^[A-Za-z0-9_.-]+$") |
46 | | - |
47 | 42 |
|
48 | 43 | class RequestParams(BaseModel): |
49 | 44 | class Meta(BaseModel): |
@@ -204,7 +199,7 @@ class EmptyResult(Result): |
204 | 199 | class BaseMetadata(BaseModel): |
205 | 200 | """Base class for entities with name and optional title fields.""" |
206 | 201 |
|
207 | | - name: str |
| 202 | + name: Annotated[str, StringConstraints(min_length=1, max_length=128, pattern=r"^[A-Za-z0-9_.-]+$")] |
208 | 203 | """The programmatic name of the entity.""" |
209 | 204 |
|
210 | 205 | title: str | None = None |
@@ -896,17 +891,6 @@ class Tool(BaseMetadata): |
896 | 891 | """ |
897 | 892 | model_config = ConfigDict(extra="allow") |
898 | 893 |
|
899 | | - @field_validator("name") |
900 | | - @classmethod |
901 | | - def _validate_tool_name(cls, value: str) -> str: |
902 | | - if not (1 <= len(value) <= 128): |
903 | | - raise ValueError(f"Invalid tool name length: {len(value)}. Tool name must be between 1 and 128 characters.") |
904 | | - |
905 | | - if not TOOL_NAME_PATTERN.fullmatch(value): |
906 | | - raise ValueError("Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.).") |
907 | | - |
908 | | - return value |
909 | | - |
910 | 894 | """ |
911 | 895 | See [MCP specification](https://modelcontextprotocol.io/specification/draft/server/tools#tool-names) |
912 | 896 | for more information on tool naming conventions. |
|
0 commit comments