docs/python-sdk/fastmcp-resources-base.mdx
fastmcp.resources.baseBase classes and interfaces for FastMCP resources.
ResourceContent <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L39" 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/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L94" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
ResourceResult <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L121" 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/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L202" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
Resource <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L218" 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/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L243" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>from_function(cls, fn: Callable[..., Any], uri: str | AnyUrl) -> FunctionResource
set_default_mime_type <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L282" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L289" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_default_name(self) -> Self
Set default name from URI if not provided.
read <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L299" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L311" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>convert_result(self, raw_value: Any) -> ResourceResult
Convert a raw result to ResourceResult.
This is used in two contexts:
Handles ResourceResult passthrough and converts raw values using
ResourceResult's normalization. When the raw value is a plain
string or bytes, the resource's own mime_type is forwarded so
that ui:// resources (and others with non-default MIME types)
don't fall back to text/plain.
The resource's component-level meta (e.g. ui metadata for
MCP Apps CSP/permissions) is propagated to each content item so
that hosts can read it from the resources/read response.
to_mcp_resource <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L405" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>to_mcp_resource(self, **overrides: Any) -> SDKResource
Convert the resource to an SDKResource.
key <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L428" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>key(self) -> str
The globally unique lookup key for this resource.
register_with_docket <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L433" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>register_with_docket(self, docket: Docket) -> None
Register this resource with docket for background execution.
add_to_docket <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L439" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>add_to_docket(self, docket: Docket, **kwargs: Any) -> Execution
Schedule this resource for background execution via docket.
Args:
docket: The Docket instancefn_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()get_span_attributes <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/base.py#L460" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_span_attributes(self) -> dict[str, Any]