mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-24 14:34:17 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
196 lines
7.2 KiB
Text
196 lines
7.2 KiB
Text
---
|
|
title: base
|
|
sidebarTitle: base
|
|
---
|
|
|
|
# `fastmcp.resources.base`
|
|
|
|
|
|
Base classes and interfaces for FastMCP resources.
|
|
|
|
## Functions
|
|
|
|
### `convert_raw_to_resource_result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L270" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
convert_raw_to_resource_result(raw_value: Any) -> ResourceResult
|
|
```
|
|
|
|
|
|
Wrap a user function's return value in a ResourceResult.
|
|
|
|
Shared by `Resource` and `ResourceTemplate` so both honor the MIME type
|
|
the component declares in listings. A component that advertises
|
|
`text/csv` must not serve `text/plain` on read.
|
|
|
|
**Args:**
|
|
- `raw_value`: The value returned by the user's function.
|
|
- `mime_type`: The component's declared MIME type, forwarded to content items.
|
|
- `meta`: Component-level meta (e.g. `ui` metadata for MCP Apps CSP/permissions)
|
|
propagated to each content item.
|
|
|
|
|
|
## Classes
|
|
|
|
### `ResourceContent` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L34" 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/fastmcp_slim/fastmcp/resources/base.py#L89" 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/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L116" 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/fastmcp_slim/fastmcp/resources/base.py#L197" 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
|
|
|
|
|
|
### `InputRequiredResourceResult` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L213" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
The full result of a single multi-round-trip resource read (SEP-2322).
|
|
|
|
`InputRequiredResult` is a result type, not a `tools/call` feature: any
|
|
request may resolve to one. When a resource or resource template returns an
|
|
`InputRequiredResult` from its body to ask the client for input, that ask is
|
|
the legitimate result of this `resources/read` — so FastMCP wraps it in this
|
|
`ResourceResult` subclass, mirroring `InputRequiredToolResult` and
|
|
`InputRequiredPromptResult`, and it flows through the middleware chain as an
|
|
ordinary return value.
|
|
|
|
Invariant: the wrapped `InputRequiredResult` is never serialized as resource
|
|
contents. `contents` is always empty; the wire handler (`_on_read_resource`)
|
|
reads `.input_required` and returns it to the runner.
|
|
|
|
|
|
### `Resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L334" 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/fastmcp_slim/fastmcp/resources/base.py#L359" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(cls, fn: Callable[..., Any], uri: str | AnyUrl) -> FunctionResource
|
|
```
|
|
|
|
#### `set_default_mime_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L396" 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/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L403" 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/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L413" 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/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L425" 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. 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/fastmcp_slim/fastmcp/resources/base.py#L457" 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/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L480" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
key(self) -> str
|
|
```
|
|
|
|
The globally unique lookup key for this resource.
|
|
|
|
|
|
#### `get_span_attributes` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/base.py#L485" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_span_attributes(self) -> dict[str, Any]
|
|
```
|