mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
214 lines
7.9 KiB
Text
214 lines
7.9 KiB
Text
---
|
|
title: resource
|
|
sidebarTitle: resource
|
|
---
|
|
|
|
# `fastmcp.resources.resource`
|
|
|
|
|
|
Base classes and interfaces for FastMCP resources.
|
|
|
|
## Classes
|
|
|
|
### `ResourceContent` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L35" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
Wrapper for resource content with optional MIME type and metadata.
|
|
|
|
Accepts any value for content - strings and bytes pass through directly,
|
|
other types (dict, list, BaseModel, etc.) are automatically JSON-serialized.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `to_mcp_resource_contents` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L90" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_resource_contents(self, uri: AnyUrl | str) -> mcp.types.TextResourceContents | mcp.types.BlobResourceContents
|
|
```
|
|
|
|
Convert to MCP resource contents type.
|
|
|
|
**Args:**
|
|
- `uri`: The URI of the resource (required by MCP types)
|
|
|
|
**Returns:**
|
|
- TextResourceContents for str content, BlobResourceContents for bytes
|
|
|
|
|
|
### `ResourceResult` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L117" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
Canonical result type for resource reads.
|
|
|
|
Provides explicit control over resource responses: multiple content items,
|
|
per-item MIME types, and metadata at both the item and result level.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `to_mcp_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L192" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_result(self, uri: AnyUrl | str) -> mcp.types.ReadResourceResult
|
|
```
|
|
|
|
Convert to MCP ReadResourceResult.
|
|
|
|
**Args:**
|
|
- `uri`: The URI of the resource (required by MCP types)
|
|
|
|
**Returns:**
|
|
- MCP ReadResourceResult with converted contents
|
|
|
|
|
|
### `Resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L208" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
Base class for all resources.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `from_function` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L229" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(fn: Callable[..., Any], uri: str | AnyUrl, name: str | None = None, title: str | None = None, description: str | None = None, icons: list[Icon] | None = None, mime_type: str | None = None, tags: set[str] | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None, task: bool | TaskConfig | None = None) -> FunctionResource
|
|
```
|
|
|
|
#### `set_default_mime_type` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L258" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
set_default_mime_type(cls, mime_type: str | None) -> str
|
|
```
|
|
|
|
Set default MIME type if not provided.
|
|
|
|
|
|
#### `set_default_name` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L265" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
set_default_name(self) -> Self
|
|
```
|
|
|
|
Set default name from URI if not provided.
|
|
|
|
|
|
#### `read` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L275" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self) -> str | bytes | ResourceResult
|
|
```
|
|
|
|
Read the resource content.
|
|
|
|
Subclasses implement this to return resource data. Supported return types:
|
|
- str: Text content
|
|
- bytes: Binary content
|
|
- ResourceResult: Full control over contents and result-level meta
|
|
|
|
|
|
#### `convert_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L287" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
convert_result(self, raw_value: Any) -> ResourceResult
|
|
```
|
|
|
|
Convert a raw result to ResourceResult.
|
|
|
|
This is used in two contexts:
|
|
1. In _read() to convert user function return values to ResourceResult
|
|
2. In tasks_result_handler() to convert Docket task results to ResourceResult
|
|
|
|
Handles ResourceResult passthrough and converts raw values using
|
|
ResourceResult's normalization.
|
|
|
|
|
|
#### `to_mcp_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L343" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_resource(self, **overrides: Any) -> SDKResource
|
|
```
|
|
|
|
Convert the resource to an SDKResource.
|
|
|
|
|
|
#### `key` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L368" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
key(self) -> str
|
|
```
|
|
|
|
The globally unique lookup key for this resource.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L372" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
register_with_docket(self, docket: Docket) -> None
|
|
```
|
|
|
|
Register this resource with docket for background execution.
|
|
|
|
|
|
#### `add_to_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L378" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
add_to_docket(self, docket: Docket, **kwargs: Any) -> Execution
|
|
```
|
|
|
|
Schedule this resource for background execution via docket.
|
|
|
|
**Args:**
|
|
- `docket`: The Docket instance
|
|
- `fn_key`: Function lookup key in Docket registry (defaults to self.key)
|
|
- `task_key`: Redis storage key for the result
|
|
- `**kwargs`: Additional kwargs passed to docket.add()
|
|
|
|
|
|
### `FunctionResource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L400" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
A resource that defers data loading by wrapping a function.
|
|
|
|
The function is only called when the resource is read, allowing for lazy loading
|
|
of potentially expensive data. This is particularly useful when listing resources,
|
|
as the function won't be called until the resource is actually accessed.
|
|
|
|
The function can return:
|
|
- str for text content (default)
|
|
- bytes for binary content
|
|
- other types will be converted to JSON
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `from_function` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L416" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(cls, fn: Callable[..., Any], uri: str | AnyUrl, name: str | None = None, title: str | None = None, description: str | None = None, icons: list[Icon] | None = None, mime_type: str | None = None, tags: set[str] | None = None, annotations: Annotations | None = None, meta: dict[str, Any] | None = None, task: bool | TaskConfig | None = None) -> FunctionResource
|
|
```
|
|
|
|
Create a FunctionResource from a function.
|
|
|
|
|
|
#### `read` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L462" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self) -> str | bytes | ResourceResult
|
|
```
|
|
|
|
Read the resource by calling the wrapped function.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/resource.py#L478" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
register_with_docket(self, docket: Docket) -> None
|
|
```
|
|
|
|
Register this resource with docket for background execution.
|
|
|
|
FunctionResource registers the underlying function, which has the user's
|
|
Depends parameters for docket to resolve.
|
|
|