{"record":{"id":"f12525156c060c10","repo":"PrefectHQ/fastmcp","slug":"directory-not-found-self-path","errorCode":null,"errorMessage":"Directory not found: {self.path}","messagePattern":"Directory not found: (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/resources/types.py","lineNumber":163,"sourceCode":"        default=\"application/json\", description=\"MIME type of the resource content\"\n    )\n\n    @property\n    def _async_path(self) -> AsyncPath:\n        return AsyncPath(self.path)\n\n    @pydantic.field_validator(\"path\")\n    @classmethod\n    def validate_absolute_path(cls, path: Path) -> Path:\n        \"\"\"Ensure path is absolute.\"\"\"\n        if not path.is_absolute():\n            raise ValueError(\"Path must be absolute\")\n        return path\n\n    async def list_files(self) -> list[Path]:\n        \"\"\"List files in the directory.\"\"\"\n        if not await self._async_path.exists():\n            raise FileNotFoundError(f\"Directory not found: {self.path}\")\n        if not await self._async_path.is_dir():\n            raise NotADirectoryError(f\"Not a directory: {self.path}\")\n\n        pattern = self.pattern or \"*\"\n\n        glob_fn = self._async_path.rglob if self.recursive else self._async_path.glob\n        try:\n            return [Path(p) async for p in glob_fn(pattern) if await p.is_file()]\n        except Exception as e:\n            raise ResourceError(f\"Error listing directory {self.path}\") from e\n\n    @override\n    async def read(self) -> ResourceResult:\n        \"\"\"Read the directory listing.\"\"\"\n        try:\n            files: list[Path] = await self.list_files()\n\n            file_list = [str(f.relative_to(self.path)) for f in files]","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/resources/types.py#L145-L181","documentation":"DirectoryResource.list_files() raises FileNotFoundError when the resource's configured `path` does not exist on disk at the time the resource is read. FastMCP validates only that the path is absolute at construction; existence is checked lazily on each read, so a directory can vanish (or never have existed) after the resource is registered.","triggerScenarios":"Register a DirectoryResource with `path` pointing to a non-existent directory (e.g. fastmcp_slim/fastmcp/resources/types.py:163) and then call resource.read() (or have a client fetch the resource), which invokes list_files().","commonSituations":"Typo in the configured directory path; deploying to a container/host where the directory was never created or isn't mounted; directory deleted after server startup; using a relative-looking path that resolved to a wrong absolute location.","solutions":["Create the directory at the configured path (mkdir -p) before serving/reading the resource.","Verify the absolute path configured on DirectoryResource is correct for the runtime environment (print it or log it in list_files).","Mount or copy the expected directory into containers/deployments; check volume mounts.","Wrap reads and surface a clear message, or fail fast at startup with an explicit existence check."],"exampleFix":"// before\nresource = DirectoryResource(path=Path('/data/reports'), name='reports')\nawait resource.read()  # FileNotFoundError if /data/reports missing\n\n// after\npath = Path('/data/reports')\npath.mkdir(parents=True, exist_ok=True)\nresource = DirectoryResource(path=path, name='reports')\nawait resource.read()","handlingStrategy":"validation","validationCode":"from pathlib import Path\npath = Path('/data/reports')\nif not path.is_dir():\n    raise FileNotFoundError(f'Directory resource path missing: {path}')","typeGuard":null,"tryCatchPattern":"try:\n    result = await resource.read()\nexcept FileNotFoundError as e:\n    logger.error('directory missing: %s', e)","preventionTips":["Create directories in deployment scripts before the server starts.","Verify volume mounts in containers.","Use absolute, environment-resolved paths and assert existence at startup."],"tags":["filesystem","resources","path-not-found"],"backgroundTag":"directory-not-found","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}