π The fast, Pythonic way to build MCP servers and clients.
Async FileResource and DirectoryResource (#2241)
William Easton380835593c27
diff --git a/src/fastmcp/resources/types.py b/src/fastmcp/resources/types.pyindex 61c29bf3b..5af01035a 100644--- a/src/fastmcp/resources/types.py+++ b/src/fastmcp/resources/types.py@@ -5,11 +5,11 @@ from __future__ import annotationsimport jsonfrom pathlib import Path-import anyio-import anyio.to_threadimport httpximport pydantic.json+from anyio import Path as AsyncPathfrom pydantic import Field, ValidationInfo+from typing_extensions import overridefrom fastmcp.exceptions import ResourceErrorfrom fastmcp.resources.resource import Resource@@ -54,6 +54,10 @@ class FileResource(Resource):description="MIME type of the resource content",)+ @property+ def _async_path(self) -> AsyncPath:+ return AsyncPath(self.path)+@pydantic.field_validator("path")@classmethoddef validate_absolute_path(cls, path: Path) -> Path:@@ -71,12 +75,13 @@ class FileResource(Resource):mime_type = info.data.get("mime_type", "text/plain")return not mime_type.startswith("text/")+ @overrideasync def read(self) -> str | bytes:"""Read the file content."""try:if self.is_binary:- return await anyio.to_thread.run_sync(self.path.read_bytes)- return await anyio.to_thread.run_sync(self.path.read_text)+ return await self._async_path.read_bytes()+ return await self._async_path.read_text()except Exception as e:raise ResourceError(f"Error reading file {self.path}") from e@@ -89,11 +94,12 @@ class HttpResource(Resource):default="application/json", description="MIME type of the resource content")+ @overrideasync def read(self) -> str | bytes:"""Read the HTTP content."""async with httpx.AsyncClient() as client:response = await client.get(self.url)- response.raise_for_status()+ _ = response.raise_for_status()return response.text@@ -111,6 +117,10 @@ class DirectoryResource(Resource):default="application/json", description="MIME type of the resource content")+ @property+ def _async_path(self) -> AsyncPath:+ return AsyncPath(self.path)+@pydantic.field_validator("path")@classmethoddef validate_absolute_path(cls, path: Path) -> Path:@@ -119,33 +129,29 @@ class DirectoryResource(Resource):raise ValueError("Path must be absolute")return path- def list_files(self) -> list[Path]:+ async def list_files(self) -> list[Path]:"""List files in the directory."""- if not self.path.exists():+ if not await self._async_path.exists():raise FileNotFoundError(f"Directory not found: {self.path}")- if not self.path.is_dir():+ if not await self._async_path.is_dir():raise NotADirectoryError(f"Not a directory: {self.path}")+ pattern = self.pattern or "*"++ glob_fn = self._async_path.rglob if self.recursive else self._async_path.globtry:- if self.pattern:- return (- list(self.path.glob(self.pattern))- if not self.recursive- else list(self.path.rglob(self.pattern))- )- return (- list(self.path.glob("*"))- if not self.recursive- else list(self.path.rglob("*"))- )+ return [Path(p) async for p in glob_fn(pattern) if await p.is_file()]except Exception as e:- raise ResourceError(f"Error listing directory {self.path}: {e}")+ raise ResourceError(f"Error listing directory {self.path}") from e+ @overrideasync def read(self) -> str: # Always returns JSON string"""Read the directory listing."""try:- files = await anyio.to_thread.run_sync(self.list_files)- file_list = [str(f.relative_to(self.path)) for f in files if f.is_file()]+ files: list[Path] = await self.list_files()++ file_list = [str(f.relative_to(self.path)) for f in files]+return json.dumps({"files": file_list}, indent=2)- except Exception:- raise ResourceError(f"Error reading directory {self.path}")+ except Exception as e:+ raise ResourceError(f"Error reading directory {self.path}") from e