Skip to main content

ResourceHandler

Trait ResourceHandler 

Source
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§

Source

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().

Source

fn read_resource( &self, uri: &str, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<ResourceContents>, McpError>> + Send

Read a resource by URI.

Provided Methods§

Source

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.

Source

fn subscribe( &self, _uri: &str, _ctx: &Context<'_>, ) -> impl Future<Output = Result<bool, McpError>> + Send

Subscribe to resource updates.

Returns true if the subscription was successful.

Source

fn unsubscribe( &self, _uri: &str, _ctx: &Context<'_>, ) -> impl Future<Output = Result<bool, McpError>> + Send

Unsubscribe from resource updates.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> ResourceHandler for Arc<T>
where T: ResourceHandler,

Source§

fn list_resources( &self, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<Resource>, McpError>> + Send

Source§

fn list_resource_templates( &self, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<ResourceTemplate>, McpError>> + Send

Source§

fn read_resource( &self, uri: &str, ctx: &Context<'_>, ) -> impl Future<Output = Result<Vec<ResourceContents>, McpError>> + Send

Source§

fn subscribe( &self, uri: &str, ctx: &Context<'_>, ) -> impl Future<Output = Result<bool, McpError>> + Send

Source§

fn unsubscribe( &self, uri: &str, ctx: &Context<'_>, ) -> impl Future<Output = Result<bool, McpError>> + Send

Implementors§