pub struct ResourceBuilder { /* private fields */ }Expand description
Builder for creating resources with a fluent API
§Example
use tower_mcp::resource::ResourceBuilder;
use tower_mcp::protocol::{ReadResourceResult, ResourceContent};
let resource = ResourceBuilder::new("file:///config.json")
.name("Configuration")
.description("Application configuration file")
.mime_type("application/json")
.handler(|| async {
Ok(ReadResourceResult {
contents: vec![ResourceContent {
uri: "file:///config.json".to_string(),
mime_type: Some("application/json".to_string()),
text: Some(r#"{"setting": "value"}"#.to_string()),
blob: None,
meta: None,
}],
meta: None,
})
})
.build();
assert_eq!(resource.uri, "file:///config.json");Implementations§
Source§impl ResourceBuilder
impl ResourceBuilder
Sourcepub fn title(self, title: impl Into<String>) -> Self
pub fn title(self, title: impl Into<String>) -> Self
Set a human-readable title for the resource
Sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set the resource description
Sourcepub fn icon_with_meta(
self,
src: impl Into<String>,
mime_type: Option<String>,
sizes: Option<Vec<String>>,
) -> Self
pub fn icon_with_meta( self, src: impl Into<String>, mime_type: Option<String>, sizes: Option<Vec<String>>, ) -> Self
Add an icon with metadata
Sourcepub fn annotations(self, annotations: ContentAnnotations) -> Self
pub fn annotations(self, annotations: ContentAnnotations) -> Self
Set annotations (audience, priority hints) for this resource
Sourcepub fn handler<F, Fut>(self, handler: F) -> ResourceBuilderWithHandler<F>
pub fn handler<F, Fut>(self, handler: F) -> ResourceBuilderWithHandler<F>
Set the handler function for reading the resource.
Returns a [ResourceBuilderWithHandler] that can be used to apply
middleware layers via .layer() or build the resource directly via .build().
§Sharing State
Capture an Arc in the closure to share state across handler
invocations or with other parts of your application:
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_mcp::resource::ResourceBuilder;
use tower_mcp::protocol::{ReadResourceResult, ResourceContent};
let db = Arc::new(RwLock::new(vec!["initial".to_string()]));
let db_clone = Arc::clone(&db);
let resource = ResourceBuilder::new("app://entries")
.name("Entries")
.handler(move || {
let db = Arc::clone(&db_clone);
async move {
let entries = db.read().await;
Ok(ReadResourceResult {
contents: vec![ResourceContent {
uri: "app://entries".to_string(),
mime_type: Some("text/plain".to_string()),
text: Some(entries.join("\n")),
blob: None,
meta: None,
}],
meta: None,
})
}
})
.build();Sourcepub fn handler_with_context<F, Fut>(
self,
handler: F,
) -> ResourceBuilderWithContextHandler<F>where
F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
pub fn handler_with_context<F, Fut>(
self,
handler: F,
) -> ResourceBuilderWithContextHandler<F>where
F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
Set a context-aware handler for reading the resource.
The handler receives a RequestContext for progress reporting and
cancellation checking.
Returns a [ResourceBuilderWithContextHandler] that can be used to apply
middleware layers via .layer() or build the resource directly via .build().