Spaces:
Running
Running
zzstoatzz commited on
Commit ·
f99530e
1
Parent(s): 4e4e683
go back to baseurl
Browse files
src/fastmcp/resources/base.py
CHANGED
|
@@ -13,6 +13,7 @@ from pydantic import (
|
|
| 13 |
ValidationInfo,
|
| 14 |
field_validator,
|
| 15 |
)
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def maybe_cast_str_to_any_url(x) -> AnyUrl:
|
|
@@ -27,7 +28,7 @@ def maybe_cast_str_to_any_url(x) -> AnyUrl:
|
|
| 27 |
raise ValueError(f"Expected str or AnyUrl, got {type(x)}")
|
| 28 |
|
| 29 |
|
| 30 |
-
LaxAnyUrl = Annotated[
|
| 31 |
|
| 32 |
|
| 33 |
class Resource(BaseModel, abc.ABC):
|
|
@@ -35,7 +36,7 @@ class Resource(BaseModel, abc.ABC):
|
|
| 35 |
|
| 36 |
model_config = ConfigDict(validate_default=True)
|
| 37 |
|
| 38 |
-
uri: LaxAnyUrl = Field(description="URI of the resource")
|
| 39 |
name: str | None = Field(description="Name of the resource", default=None)
|
| 40 |
description: str | None = Field(
|
| 41 |
description="Description of the resource", default=None
|
|
|
|
| 13 |
ValidationInfo,
|
| 14 |
field_validator,
|
| 15 |
)
|
| 16 |
+
from pydantic.networks import _BaseUrl # TODO: remove this once pydantic is updated
|
| 17 |
|
| 18 |
|
| 19 |
def maybe_cast_str_to_any_url(x) -> AnyUrl:
|
|
|
|
| 28 |
raise ValueError(f"Expected str or AnyUrl, got {type(x)}")
|
| 29 |
|
| 30 |
|
| 31 |
+
LaxAnyUrl = Annotated[_BaseUrl | str, BeforeValidator(maybe_cast_str_to_any_url)]
|
| 32 |
|
| 33 |
|
| 34 |
class Resource(BaseModel, abc.ABC):
|
|
|
|
| 36 |
|
| 37 |
model_config = ConfigDict(validate_default=True)
|
| 38 |
|
| 39 |
+
uri: LaxAnyUrl = Field(default=..., description="URI of the resource")
|
| 40 |
name: str | None = Field(description="Name of the resource", default=None)
|
| 41 |
description: str | None = Field(
|
| 42 |
description="Description of the resource", default=None
|
tests/resources/test_resources.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import pytest
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class TestResourceValidation:
|
|
@@ -95,4 +96,4 @@ class TestResourceValidation:
|
|
| 95 |
pass
|
| 96 |
|
| 97 |
with pytest.raises(TypeError, match="abstract method"):
|
| 98 |
-
ConcreteResource(uri="test://test", name="test")
|
|
|
|
| 1 |
import pytest
|
| 2 |
+
|
| 3 |
+
from fastmcp.resources import FunctionResource, Resource
|
| 4 |
|
| 5 |
|
| 6 |
class TestResourceValidation:
|
|
|
|
| 96 |
pass
|
| 97 |
|
| 98 |
with pytest.raises(TypeError, match="abstract method"):
|
| 99 |
+
ConcreteResource(uri="test://test", name="test") # type: ignore
|
tests/test_server.py
CHANGED
|
@@ -37,7 +37,7 @@ class TestServer:
|
|
| 37 |
|
| 38 |
with pytest.raises(TypeError, match="The @tool decorator was used incorrectly"):
|
| 39 |
|
| 40 |
-
@mcp.tool # Missing parentheses
|
| 41 |
def add(x: int, y: int) -> int:
|
| 42 |
return x + y
|
| 43 |
|
|
@@ -57,7 +57,7 @@ class TestServer:
|
|
| 57 |
TypeError, match="The @resource decorator was used incorrectly"
|
| 58 |
):
|
| 59 |
|
| 60 |
-
@mcp.resource # Missing parentheses
|
| 61 |
def get_data(x: str) -> str:
|
| 62 |
return f"Data: {x}"
|
| 63 |
|
|
|
|
| 37 |
|
| 38 |
with pytest.raises(TypeError, match="The @tool decorator was used incorrectly"):
|
| 39 |
|
| 40 |
+
@mcp.tool # Missing parentheses #type: ignore
|
| 41 |
def add(x: int, y: int) -> int:
|
| 42 |
return x + y
|
| 43 |
|
|
|
|
| 57 |
TypeError, match="The @resource decorator was used incorrectly"
|
| 58 |
):
|
| 59 |
|
| 60 |
+
@mcp.resource # Missing parentheses #type: ignore
|
| 61 |
def get_data(x: str) -> str:
|
| 62 |
return f"Data: {x}"
|
| 63 |
|