mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-20 04:24:17 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
103 lines
2.8 KiB
Text
103 lines
2.8 KiB
Text
---
|
|
title: discovery
|
|
sidebarTitle: discovery
|
|
---
|
|
|
|
# `fastmcp.fs.discovery`
|
|
|
|
|
|
File discovery and module import utilities for filesystem-based routing.
|
|
|
|
This module provides functions to:
|
|
1. Discover Python files in a directory tree
|
|
2. Import modules (as packages if __init__.py exists, else directly)
|
|
3. Extract decorated functions from imported modules
|
|
|
|
|
|
## Functions
|
|
|
|
### `discover_files` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/fs/discovery.py#L35" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
discover_files(root: Path) -> list[Path]
|
|
```
|
|
|
|
|
|
Recursively discover all Python files under a directory.
|
|
|
|
Excludes __init__.py files (they're for package structure, not components).
|
|
|
|
**Args:**
|
|
- `root`: Root directory to scan.
|
|
|
|
**Returns:**
|
|
- List of .py file paths, sorted for deterministic order.
|
|
|
|
|
|
### `import_module_from_file` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/fs/discovery.py#L112" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
import_module_from_file(file_path: Path) -> ModuleType
|
|
```
|
|
|
|
|
|
Import a Python file as a module.
|
|
|
|
If the file is part of a package (directory has __init__.py), imports
|
|
it as a proper package member (relative imports work). Otherwise,
|
|
imports directly using spec_from_file_location.
|
|
|
|
**Args:**
|
|
- `file_path`: Path to the Python file.
|
|
|
|
**Returns:**
|
|
- The imported module.
|
|
|
|
**Raises:**
|
|
- `ImportError`: If the module cannot be imported.
|
|
|
|
|
|
### `extract_components` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/fs/discovery.py#L178" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
extract_components(module: ModuleType) -> list[tuple[Any, FSMeta]]
|
|
```
|
|
|
|
|
|
Extract all decorated functions from a module.
|
|
|
|
Scans all module attributes for functions that have been decorated
|
|
with @tool, @resource, or @prompt.
|
|
|
|
**Args:**
|
|
- `module`: The imported module to scan.
|
|
|
|
**Returns:**
|
|
- List of (function, metadata) tuples for each decorated function.
|
|
|
|
|
|
### `discover_and_import` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/fs/discovery.py#L210" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
discover_and_import(root: Path) -> DiscoveryResult
|
|
```
|
|
|
|
|
|
Discover files, import modules, and extract components.
|
|
|
|
This is the main entry point for filesystem-based discovery.
|
|
|
|
**Args:**
|
|
- `root`: Root directory to scan.
|
|
|
|
**Returns:**
|
|
- DiscoveryResult with components and any failed files.
|
|
|
|
|
|
## Classes
|
|
|
|
### `DiscoveryResult` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/fs/discovery.py#L28" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
Result of filesystem discovery.
|
|
|