mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-28 18:22:07 +02:00
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import json
|
|
from fastmcp import FastMCP
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_dir(tmp_path_factory) -> Path:
|
|
"""Create a temporary directory with test files."""
|
|
tmp = tmp_path_factory.mktemp("test_files")
|
|
|
|
# Create test files
|
|
(tmp / "example.py").write_text("print('hello world')")
|
|
(tmp / "readme.md").write_text("# Test Directory\nThis is a test.")
|
|
(tmp / "config.json").write_text('{"test": true}')
|
|
|
|
return tmp
|
|
|
|
|
|
@pytest.fixture
|
|
def mcp(test_dir: Path) -> FastMCP:
|
|
mcp = FastMCP()
|
|
|
|
@mcp.resource("fs://test_dir")
|
|
def list_files() -> list[str]:
|
|
"""List the files in the test directory"""
|
|
return [str(f) for f in test_dir.iterdir()]
|
|
|
|
return mcp
|
|
|
|
|
|
async def test_list_resources(mcp: FastMCP):
|
|
resources = await mcp.list_resources()
|
|
assert len(resources) == 1
|
|
assert str(resources[0].uri) == "fs://test_dir"
|
|
assert resources[0].name == "test_dir"
|
|
|
|
|
|
async def test_read_resource(mcp: FastMCP):
|
|
files = await mcp.read_resource("fs://test_dir")
|
|
files = json.loads(files)
|
|
|
|
assert isinstance(files, list)
|
|
assert len(files) == 3
|
|
assert any("example.py" in f for f in files)
|
|
assert any("readme.md" in f for f in files)
|
|
assert any("config.json" in f for f in files)
|