π The fast, Pythonic way to build MCP servers and clients.
Fix field validator for resource
Jeremiah Lowin3095ce5e5753
diff --git a/src/fastmcp/resources/resource.py b/src/fastmcp/resources/resource.pyindex 8155ccaa4..ad25ec10d 100644--- a/src/fastmcp/resources/resource.py+++ b/src/fastmcp/resources/resource.py@@ -14,9 +14,10 @@ from pydantic import (ConfigDict,Field,UrlConstraints,- ValidationInfo,field_validator,+ model_validator,)+from typing_extensions import Selffrom fastmcp.server.dependencies import get_contextfrom fastmcp.utilities.components import FastMCPComponent@@ -36,6 +37,7 @@ class Resource(FastMCPComponent, abc.ABC):uri: Annotated[AnyUrl, UrlConstraints(host_required=False)] = Field(default=..., description="URI of the resource")+ name: str = Field(default="", description="Name of the resource")mime_type: str = Field(default="text/plain",description="MIME type of the resource content",@@ -68,15 +70,16 @@ class Resource(FastMCPComponent, abc.ABC):return mime_typereturn "text/plain"- @field_validator("name", mode="before")- @classmethod- def set_default_name(cls, name: str | None, info: ValidationInfo) -> str:+ @model_validator(mode="after")+ def set_default_name(self) -> Self:"""Set default name from URI if not provided."""- if name:- return name- if uri := info.data.get("uri"):- return str(uri)- raise ValueError("Either name or uri must be provided")+ if self.name:+ pass+ elif self.uri:+ self.name = str(self.uri)+ else:+ raise ValueError("Either name or uri must be provided")+ return self@abc.abstractmethodasync def read(self) -> str | bytes:diff --git a/tests/resources/test_resources.py b/tests/resources/test_resources.pyindex 1621d1f3e..33e76d71e 100644--- a/tests/resources/test_resources.py+++ b/tests/resources/test_resources.py@@ -50,18 +50,12 @@ class TestResourceValidation:)assert resource.name == "resource://my-resource"- def test_resource_name_validation(self):- """Test name validation."""+ def test_provided_name_takes_precedence_over_uri(self):+ """Test that provided name takes precedence over URI."""def dummy_func() -> str:return "data"- # Must provide either name or URI- with pytest.raises(ValueError, match="Either name or uri must be provided"):- FunctionResource(- fn=dummy_func,- )-# Explicit name takes precedence over URIresource = FunctionResource(uri=AnyUrl("resource://uri-name"),