Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284---title: templatesidebarTitle: template---
# `fastmcp.resources.template`
Resource template functionality.
## Functions
### `extract_query_params` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L40" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonextract_query_params(uri_template: str) -> set[str]```
Extract query parameter names from RFC 6570 `{?param1,param2}` syntax.
The explode modifier is stripped, so `{?tags*}` yields `{"tags"}`. Use`extract_exploded_query_params` to find which names carried it.
### `extract_exploded_query_params` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L88" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonextract_exploded_query_params(uri_template: str) -> set[str]```
Extract query parameter names declared with the RFC 6570 explode modifier.
`{?tags*}` marks `tags` as repeatable — `?tags=a&tags=b` collects into alist rather than collapsing to the first value.
### `encode_literal` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L106" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonencode_literal(text: str) -> str```
Percent-encode literal template text per RFC 6570 section 3.1.
A template may be written with characters the URI grammar does not allow —a non-ASCII `ucschar` (`file:///docs/café/`) or a space — and section 3.1requires those to be UTF-8 percent-encoded in the expanded URI. Reservedand unreserved characters are structural and stay as written, and existing`%XX` triplets pass through so an already-encoded literal is not encodedtwice.
A resource URI reaches the server as an `AnyUrl`, which percent-encodes it,so the encoded form is what matching has to line up with.
### `build_regex` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L160" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonbuild_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)
Hyphens in parameter names are normalized to underscores in regex groupnames so that matched groups are valid Python identifiers.
Returns None if the template produces an invalid regex (e.g. parameternames with leading digits or duplicates from a remote server).
### `match_uri_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L197" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonmatch_uri_template(uri: str, uri_template: str) -> dict[str, Any] | 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}`, `{?list*}` (repeated keys)
`list_params` names non-exploded query params that hold lists. Per RFC 6570section 3.2.8 their value is comma-joined, with literal commas separatingitems and `%2C` inside an item, so they are split before decoding.
`regex` is the template's compiled path pattern when the caller alreadyholds it; otherwise it is built from `uri_template` through a bounded cache.
### `expand_uri_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L272" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonexpand_uri_template(uri_template: str, params: dict[str, Any]) -> str```
Expand a URI template with parameters — inverse of `match_uri_template`.
Supports the same RFC 6570 subset:- Path params: `{var}`, `{var*}`- Query params: `{?var1,var2}`
### `forward_uri` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L344" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonforward_uri(uri_template: str, params: dict[str, Any], uri: str) -> str```
Build the URI a forwarding layer (mount or proxy) sends to the server behind it.
The path is expanded from `uri_template` with `params`, but the query stringis carried over from the incoming `uri` byte for byte. Re-expanding it woulddecode and re-encode values, and the forwarding layer does not know which`{?name}` params hold comma-joined lists, so `?tags=a%2Cb,c` would arrive asone item instead of two.
## Classes
### `ResourceTemplate` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L359" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
A template for dynamically creating resources.
**Methods:**
#### `resolve_security` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L396" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonresolve_security(self, server_default: ResourceSecurity | None) -> ResourceSecurity | None```
Resolve the effective security policy for this template.
A per-component ``security`` overrides the server default.``INHERIT_SECURITY`` (the field default) inherits ``server_default``;an explicit ``None`` disables screening for this template.
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L413" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_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, auth: AuthCheck | list[AuthCheck] | None = None, security: ResourceSecurity | None | InheritSecurity = INHERIT_SECURITY) -> FunctionResourceTemplate```
#### `set_default_mime_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L446" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset_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/fastmcp_slim/fastmcp/resources/template.py#L452" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonmatches(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/fastmcp_slim/fastmcp/resources/template.py#L478" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonread(self, arguments: dict[str, Any]) -> str | bytes | ResourceResult```
Read the resource content.
#### `convert_result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L484" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonconvert_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 ResourceResult2. In tasks_result_handler() to convert Docket task results to ResourceResult
Handles ResourceResult passthrough and converts raw values usingResourceResult's normalization. The template's own ``mime_type`` isforwarded so that reads match the MIME type the template advertisesin ``resources/templates/list``.
#### `create_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L511" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncreate_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/fastmcp_slim/fastmcp/resources/template.py#L522" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonto_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/fastmcp_slim/fastmcp/resources/template.py#L542" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_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/fastmcp_slim/fastmcp/resources/template.py#L555" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonkey(self) -> str```
The globally unique lookup key for this template.
#### `get_span_attributes` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L560" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget_span_attributes(self) -> dict[str, Any]```
### `FunctionResourceTemplate` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L567" 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/fastmcp_slim/fastmcp/resources/template.py#L582" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncreate_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/fastmcp_slim/fastmcp/resources/template.py#L604" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonread(self, arguments: dict[str, Any]) -> str | bytes | ResourceResult```
Read the resource content.
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/template.py#L644" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_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, auth: AuthCheck | list[AuthCheck] | None = None, security: ResourceSecurity | None | InheritSecurity = INHERIT_SECURITY) -> FunctionResourceTemplate```
Create a template from a function.