pub trait ResourceHandler: Send + Sync {
// Required methods
fn list_resources(
&self,
ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<Resource>, McpError>> + Send;
fn read_resource(
&self,
uri: &str,
ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<ResourceContents>, McpError>> + Send;
// Provided methods
fn list_resource_templates(
&self,
_ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<ResourceTemplate>, McpError>> + Send { ... }
fn subscribe(
&self,
_uri: &str,
_ctx: &Context<'_>,
) -> impl Future<Output = Result<bool, McpError>> + Send { ... }
fn unsubscribe(
&self,
_uri: &str,
_ctx: &Context<'_>,
) -> impl Future<Output = Result<bool, McpError>> + Send { ... }
}Expand description
Handler for resource-related operations.
Implement this trait to expose resources that AI assistants can read.
Required Methods§
Sourcefn list_resources(
&self,
ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<Resource>, McpError>> + Send
fn list_resources( &self, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<Resource>, McpError>> + Send
List all available static resources.
This returns resources with fixed URIs. For dynamic resources with
parameterized URIs (e.g., file://{path}), use list_resource_templates().
Sourcefn read_resource(
&self,
uri: &str,
ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<ResourceContents>, McpError>> + Send
fn read_resource( &self, uri: &str, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<ResourceContents>, McpError>> + Send
Read a resource by URI.
Provided Methods§
Sourcefn list_resource_templates(
&self,
_ctx: &Context<'_>,
) -> impl Future<Output = Result<Vec<ResourceTemplate>, McpError>> + Send
fn list_resource_templates( &self, _ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<ResourceTemplate>, McpError>> + Send
List all available resource templates.
Resource templates describe dynamic resources with parameterized URIs.
For example, a template with URI file://{path} allows clients to
construct URIs like file:///etc/hosts to read specific files.
The default implementation returns an empty list.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.