mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-21 04:54:17 +02:00
Fix field validator for resource
This commit is contained in:
parent
02ea970e99
commit
3095ce5e57
2 changed files with 14 additions and 17 deletions
|
|
@ -14,9 +14,10 @@ from pydantic import (
|
|||
ConfigDict,
|
||||
Field,
|
||||
UrlConstraints,
|
||||
ValidationInfo,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
from typing_extensions import Self
|
||||
|
||||
from fastmcp.server.dependencies import get_context
|
||||
from 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_type
|
||||
return "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.abstractmethod
|
||||
async def read(self) -> str | bytes:
|
||||
|
|
|
|||
|
|
@ -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 URI
|
||||
resource = FunctionResource(
|
||||
uri=AnyUrl("resource://uri-name"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue