pub struct ResourceTemplate {
pub uri_template: String,
pub name: String,
pub title: Option<String>,
pub description: Option<String>,
pub mime_type: Option<String>,
pub icons: Option<Vec<ToolIcon>>,
pub annotations: Option<ContentAnnotations>,
/* private fields */
}Expand description
A parameterized resource template
Resource templates use URI template syntax (RFC 6570) to match multiple URIs and extract variable values. This allows servers to expose dynamic resources like file systems or database records.
§Example
use tower_mcp::resource::ResourceTemplateBuilder;
use tower_mcp::protocol::{ReadResourceResult, ResourceContent};
use std::collections::HashMap;
let template = ResourceTemplateBuilder::new("file:///{path}")
.name("Project Files")
.handler(|uri: String, vars: HashMap<String, String>| async move {
let path = vars.get("path").unwrap_or(&String::new()).clone();
Ok(ReadResourceResult {
contents: vec![ResourceContent {
uri,
mime_type: Some("text/plain".to_string()),
text: Some(format!("Contents of {}", path)),
blob: None,
meta: None,
}],
meta: None,
..Default::default()
})
});Fields§
§uri_template: StringThe URI template pattern (e.g., file:///{path})
name: StringHuman-readable name
title: Option<String>Human-readable title for display purposes
description: Option<String>Optional description
mime_type: Option<String>Optional MIME type hint
icons: Option<Vec<ToolIcon>>Optional icons for display in user interfaces
annotations: Option<ContentAnnotations>Optional annotations (audience, priority hints)
Implementations§
Source§impl ResourceTemplate
impl ResourceTemplate
Sourcepub fn builder(uri_template: impl Into<String>) -> ResourceTemplateBuilder
pub fn builder(uri_template: impl Into<String>) -> ResourceTemplateBuilder
Create a new resource template builder
Sourcepub fn definition(&self) -> ResourceTemplateDefinition
pub fn definition(&self) -> ResourceTemplateDefinition
Get the template definition for resources/templates/list
Sourcepub fn match_uri(&self, uri: &str) -> Option<HashMap<String, String>>
pub fn match_uri(&self, uri: &str) -> Option<HashMap<String, String>>
Check if a URI matches this template and extract variables
Returns Some(HashMap) with extracted variables if the URI matches,
None if it doesn’t match.
Sourcepub fn read(
&self,
uri: &str,
variables: HashMap<String, String>,
) -> BoxFuture<'_, Result<ReadResourceResult>>
pub fn read( &self, uri: &str, variables: HashMap<String, String>, ) -> BoxFuture<'_, Result<ReadResourceResult>>
Read a resource at the given URI using this template’s handler
§Arguments
uri- The actual URI being readvariables- Variables extracted from matching the URI against the template
Sourcepub fn read_outcome_with_context(
&self,
ctx: RequestContext,
uri: &str,
variables: HashMap<String, String>,
) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>>
pub fn read_outcome_with_context( &self, ctx: RequestContext, uri: &str, variables: HashMap<String, String>, ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>>
Read a matched resource while preserving an SEP-2322 continuation.