Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
bf0697f
1
Parent(s): 6d18388
Update tests
Browse files- tests/test_server.py +23 -3
tests/test_server.py
CHANGED
|
@@ -265,8 +265,11 @@ class TestServerResources:
|
|
| 265 |
== base64.b64encode(b"Binary file data").decode()
|
| 266 |
)
|
| 267 |
|
|
|
|
|
|
|
| 268 |
async def test_resource_with_params(self):
|
| 269 |
-
"""Test that a resource with function parameters
|
|
|
|
| 270 |
mcp = FastMCP()
|
| 271 |
|
| 272 |
with pytest.raises(ValueError, match="Mismatch between URI parameters"):
|
|
@@ -285,6 +288,14 @@ class TestServerResources:
|
|
| 285 |
def get_data() -> str:
|
| 286 |
return "Data"
|
| 287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
async def test_resource_matching_params(self):
|
| 289 |
"""Test that a resource with matching URI and function parameters works"""
|
| 290 |
mcp = FastMCP()
|
|
@@ -319,7 +330,16 @@ class TestServerResources:
|
|
| 319 |
result = await client.read_resource("resource://cursor/fastmcp/data")
|
| 320 |
assert result.contents[0].text == "Data for cursor/fastmcp"
|
| 321 |
|
| 322 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
"""Test that a resource with no parameters works as a regular resource"""
|
| 324 |
mcp = FastMCP()
|
| 325 |
|
|
@@ -341,7 +361,7 @@ class TestServerResources:
|
|
| 341 |
|
| 342 |
# Should be registered as a template
|
| 343 |
assert len(mcp._resource_manager._templates) == 1
|
| 344 |
-
assert len(mcp.
|
| 345 |
|
| 346 |
# When accessed, should create a concrete resource
|
| 347 |
resource = await mcp._resource_manager.get_resource("resource://test/data")
|
|
|
|
| 265 |
== base64.b64encode(b"Binary file data").decode()
|
| 266 |
)
|
| 267 |
|
| 268 |
+
|
| 269 |
+
class TestServerResourceTemplates:
|
| 270 |
async def test_resource_with_params(self):
|
| 271 |
+
"""Test that a resource with function parameters raises an error if the URI
|
| 272 |
+
parameters don't match"""
|
| 273 |
mcp = FastMCP()
|
| 274 |
|
| 275 |
with pytest.raises(ValueError, match="Mismatch between URI parameters"):
|
|
|
|
| 288 |
def get_data() -> str:
|
| 289 |
return "Data"
|
| 290 |
|
| 291 |
+
async def test_resource_with_untyped_params(self):
|
| 292 |
+
"""Test that a resource with untyped parameters raises an error"""
|
| 293 |
+
mcp = FastMCP()
|
| 294 |
+
|
| 295 |
+
@mcp.resource("resource://{param}")
|
| 296 |
+
def get_data(param) -> str:
|
| 297 |
+
return "Data"
|
| 298 |
+
|
| 299 |
async def test_resource_matching_params(self):
|
| 300 |
"""Test that a resource with matching URI and function parameters works"""
|
| 301 |
mcp = FastMCP()
|
|
|
|
| 330 |
result = await client.read_resource("resource://cursor/fastmcp/data")
|
| 331 |
assert result.contents[0].text == "Data for cursor/fastmcp"
|
| 332 |
|
| 333 |
+
async def test_resource_multiple_mismatched_params(self):
|
| 334 |
+
"""Test that mismatched parameters raise an error"""
|
| 335 |
+
mcp = FastMCP()
|
| 336 |
+
|
| 337 |
+
with pytest.raises(ValueError, match="Mismatch between URI parameters"):
|
| 338 |
+
|
| 339 |
+
@mcp.resource("resource://{org}/{repo}/data")
|
| 340 |
+
def get_data(org: str, repo_2: str) -> str:
|
| 341 |
+
return f"Data for {org}"
|
| 342 |
+
|
| 343 |
"""Test that a resource with no parameters works as a regular resource"""
|
| 344 |
mcp = FastMCP()
|
| 345 |
|
|
|
|
| 361 |
|
| 362 |
# Should be registered as a template
|
| 363 |
assert len(mcp._resource_manager._templates) == 1
|
| 364 |
+
assert len(await mcp.list_resources()) == 0
|
| 365 |
|
| 366 |
# When accessed, should create a concrete resource
|
| 367 |
resource = await mcp._resource_manager.get_resource("resource://test/data")
|