From f473d2e3f4459dd6d073e23a2fc5f9c9e73b57c6 Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Sat, 30 Nov 2024 17:50:18 -0600 Subject: [PATCH] go back to baseurl --- src/fastmcp/resources/base.py | 5 +++-- tests/resources/test_resources.py | 5 +++-- tests/test_server.py | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/fastmcp/resources/base.py b/src/fastmcp/resources/base.py index ca17e2e72..c3e55d22c 100644 --- a/src/fastmcp/resources/base.py +++ b/src/fastmcp/resources/base.py @@ -13,6 +13,7 @@ from pydantic import ( ValidationInfo, field_validator, ) +from pydantic.networks import _BaseUrl # TODO: remove this once pydantic is updated def maybe_cast_str_to_any_url(x) -> AnyUrl: @@ -27,7 +28,7 @@ def maybe_cast_str_to_any_url(x) -> AnyUrl: raise ValueError(f"Expected str or AnyUrl, got {type(x)}") -LaxAnyUrl = Annotated[AnyUrl, BeforeValidator(maybe_cast_str_to_any_url)] +LaxAnyUrl = Annotated[_BaseUrl | str, BeforeValidator(maybe_cast_str_to_any_url)] class Resource(BaseModel, abc.ABC): @@ -35,7 +36,7 @@ class Resource(BaseModel, abc.ABC): model_config = ConfigDict(validate_default=True) - uri: LaxAnyUrl = Field(description="URI of the resource") + uri: LaxAnyUrl = Field(default=..., description="URI of the resource") name: str | None = Field(description="Name of the resource", default=None) description: str | None = Field( description="Description of the resource", default=None diff --git a/tests/resources/test_resources.py b/tests/resources/test_resources.py index 658a1f1d9..417e79960 100644 --- a/tests/resources/test_resources.py +++ b/tests/resources/test_resources.py @@ -1,5 +1,6 @@ import pytest -from fastmcp.resources import Resource, FunctionResource + +from fastmcp.resources import FunctionResource, Resource class TestResourceValidation: @@ -95,4 +96,4 @@ class TestResourceValidation: pass with pytest.raises(TypeError, match="abstract method"): - ConcreteResource(uri="test://test", name="test") + ConcreteResource(uri="test://test", name="test") # type: ignore diff --git a/tests/test_server.py b/tests/test_server.py index 9c66f9fd1..950cf2246 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -37,7 +37,7 @@ class TestServer: with pytest.raises(TypeError, match="The @tool decorator was used incorrectly"): - @mcp.tool # Missing parentheses + @mcp.tool # Missing parentheses #type: ignore def add(x: int, y: int) -> int: return x + y @@ -57,7 +57,7 @@ class TestServer: TypeError, match="The @resource decorator was used incorrectly" ): - @mcp.resource # Missing parentheses + @mcp.resource # Missing parentheses #type: ignore def get_data(x: str) -> str: return f"Data: {x}"