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>
233 lines
8.6 KiB
Text
233 lines
8.6 KiB
Text
---
|
|
title: template
|
|
sidebarTitle: template
|
|
---
|
|
|
|
# `fastmcp.resources.template`
|
|
|
|
|
|
Resource template functionality.
|
|
|
|
## Functions
|
|
|
|
### `extract_query_params` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L32" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L40" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
build_regex(template: str) -> re.Pattern
|
|
```
|
|
|
|
|
|
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)
|
|
|
|
|
|
### `match_uri_template` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L66" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L97" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L119" 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, 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) -> FunctionResourceTemplate
|
|
```
|
|
|
|
#### `set_default_mime_type` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L148" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L154" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L158" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self, arguments: dict[str, Any]) -> str | bytes
|
|
```
|
|
|
|
Read the resource content.
|
|
|
|
|
|
#### `convert_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L164" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
convert_result(self, raw_value: Any) -> ResourceContent
|
|
```
|
|
|
|
Convert a raw return value to ResourceContent.
|
|
|
|
Handles ResourceContent passthrough and converts raw values using mime_type.
|
|
|
|
|
|
#### `create_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L205" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L216" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L238" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L251" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L255" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L261" 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()
|
|
|
|
|
|
### `FunctionResourceTemplate` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L285" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L315" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L333" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
read(self, arguments: dict[str, Any]) -> str | bytes
|
|
```
|
|
|
|
Read the resource content.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L364" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L374" 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/jlowin/fastmcp/blob/main/src/fastmcp/resources/template.py#L400" 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, 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) -> FunctionResourceTemplate
|
|
```
|
|
|
|
Create a template from a function.
|
|
|