Skip to main content

fastmcp.resources.template

Resource template functionality.

Functions

extract_query_params

Extract query parameter names from RFC 6570 {?param1,param2} syntax.

build_regex

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 group names so that matched groups are valid Python identifiers. Returns None if the template produces an invalid regex (e.g. parameter names with leading digits or duplicates from a remote server).

match_uri_template

Match URI against template and extract both path and query parameters. Supports RFC 6570 URI templates:
  • Path params: {var}, {var*}
  • Query params: {?var1,var2}

expand_uri_template

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}

Classes

ResourceTemplate

A template for dynamically creating resources. Methods:

resolve_security

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

set_default_mime_type

Set default MIME type if not provided.

matches

Check if URI matches template and extract parameters.

read

Read the resource content.

convert_result

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. The template’s own mime_type is forwarded so that reads match the MIME type the template advertises in resources/templates/list.

create_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

Convert the resource template to an SDKResourceTemplate.

from_mcp_template

Creates a FastMCP ResourceTemplate from a raw MCP ResourceTemplate object.

key

The globally unique lookup key for this template.

get_span_attributes

FunctionResourceTemplate

A template for dynamically creating resources. Methods:

create_resource

Create a resource from the template with the given parameters.

read

Read the resource content.

from_function

Create a template from a function.