mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 23:29:10 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
247 lines
9.5 KiB
Text
247 lines
9.5 KiB
Text
---
|
|
title: template
|
|
sidebarTitle: template
|
|
---
|
|
|
|
# `fastmcp.resources.template`
|
|
|
|
|
|
Resource template functionality.
|
|
|
|
## Functions
|
|
|
|
### `extract_query_params` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L39" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
extract_query_params(uri_template: str) -> set[str]
|
|
```
|
|
|
|
|
|
Extract query parameter names from RFC 6570 `{?param1,param2}` syntax.
|
|
|
|
|
|
### `build_regex` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L47" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
build_regex(template: str) -> re.Pattern[str] | None
|
|
```
|
|
|
|
|
|
Build regex pattern for URI template, handling RFC 6570 syntax.
|
|
|
|
Supports:
|
|
- `{var}` - simple path parameter
|
|
- `{var*}` - wildcard path parameter (captures multiple segments)
|
|
- `{?var1,var2}` - query parameters (ignored in path matching)
|
|
|
|
Returns None if the template produces an invalid regex (e.g. parameter
|
|
names with hyphens, leading digits, or duplicates from a remote server).
|
|
|
|
|
|
### `match_uri_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L79" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
match_uri_template(uri: str, uri_template: str) -> dict[str, str] | None
|
|
```
|
|
|
|
|
|
Match URI against template and extract both path and query parameters.
|
|
|
|
Supports RFC 6570 URI templates:
|
|
- Path params: `{var}`, `{var*}`
|
|
- Query params: `{?var1,var2}`
|
|
|
|
|
|
## Classes
|
|
|
|
### `ResourceTemplate` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L112" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
A template for dynamically creating resources.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L139" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(fn: Callable[..., Any], uri_template: str, name: str | None = None, version: str | int | 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, auth: AuthCheck | list[AuthCheck] | None = None) -> FunctionResourceTemplate
|
|
```
|
|
|
|
#### `set_default_mime_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L172" 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.
|
|
|
|
|
|
#### `matches` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L178" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
matches(self, uri: str) -> dict[str, Any] | None
|
|
```
|
|
|
|
Check if URI matches template and extract parameters.
|
|
|
|
|
|
#### `read` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L182" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self, arguments: dict[str, Any]) -> str | bytes | ResourceResult
|
|
```
|
|
|
|
Read the resource content.
|
|
|
|
|
|
#### `convert_result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L188" 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.
|
|
|
|
|
|
#### `create_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L252" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
create_resource(self, uri: str, params: dict[str, Any]) -> Resource
|
|
```
|
|
|
|
Create a resource from the template with the given parameters.
|
|
|
|
The base implementation does not support background tasks.
|
|
Use FunctionResourceTemplate for task support.
|
|
|
|
|
|
#### `to_mcp_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L263" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_template(self, **overrides: Any) -> SDKResourceTemplate
|
|
```
|
|
|
|
Convert the resource template to an SDKResourceTemplate.
|
|
|
|
|
|
#### `from_mcp_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L283" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_mcp_template(cls, mcp_template: SDKResourceTemplate) -> ResourceTemplate
|
|
```
|
|
|
|
Creates a FastMCP ResourceTemplate from a raw MCP ResourceTemplate object.
|
|
|
|
|
|
#### `key` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L296" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
key(self) -> str
|
|
```
|
|
|
|
The globally unique lookup key for this template.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L301" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
register_with_docket(self, docket: Docket) -> None
|
|
```
|
|
|
|
Register this template with docket for background execution.
|
|
|
|
|
|
#### `add_to_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L307" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
add_to_docket(self, docket: Docket, params: dict[str, Any], **kwargs: Any) -> Execution
|
|
```
|
|
|
|
Schedule this template for background execution via docket.
|
|
|
|
**Args:**
|
|
- `docket`: The Docket instance
|
|
- `params`: Template parameters
|
|
- `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()
|
|
|
|
|
|
#### `get_span_attributes` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L330" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_span_attributes(self) -> dict[str, Any]
|
|
```
|
|
|
|
### `FunctionResourceTemplate` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L337" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
A template for dynamically creating resources.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `create_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L383" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
create_resource(self, uri: str, params: dict[str, Any]) -> Resource
|
|
```
|
|
|
|
Create a resource from the template with the given parameters.
|
|
|
|
|
|
#### `read` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L402" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self, arguments: dict[str, Any]) -> str | bytes | ResourceResult
|
|
```
|
|
|
|
Read the resource content.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L441" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
register_with_docket(self, docket: Docket) -> None
|
|
```
|
|
|
|
Register this template with docket for background execution.
|
|
|
|
FunctionResourceTemplate registers the underlying function, which has the
|
|
user's Depends parameters for docket to resolve.
|
|
|
|
|
|
#### `add_to_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L451" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
add_to_docket(self, docket: Docket, params: dict[str, Any], **kwargs: Any) -> Execution
|
|
```
|
|
|
|
Schedule this template for background execution via docket.
|
|
|
|
FunctionResourceTemplate splats the params dict since .fn expects **kwargs.
|
|
|
|
**Args:**
|
|
- `docket`: The Docket instance
|
|
- `params`: Template parameters
|
|
- `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()
|
|
|
|
|
|
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/resources/template.py#L477" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(cls, fn: Callable[..., Any], uri_template: str, name: str | None = None, version: str | int | 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, auth: AuthCheck | list[AuthCheck] | None = None) -> FunctionResourceTemplate
|
|
```
|
|
|
|
Create a template from a function.
|
|
|