Trait ResourceProvider

Source
pub trait ResourceProvider<C>: Send + Sync {
    // Required methods
    fn list_resources<'life0, 'async_trait>(
        &'life0 self,
        context: Arc<C>,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<ResourceInfo>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn read_resource<'life0, 'life1, 'async_trait>(
        &'life0 self,
        uri: &'life1 str,
        context: Arc<C>,
    ) -> Pin<Box<dyn Future<Output = Result<ResourceContent>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Trait for providing resources dynamically to MCP clients.

Resource providers allow your server to expose data, files, or other content through URI-based access. Clients can list available resources and read their content on demand.

§Type Parameters

  • C: The application context type

§Examples

struct DatabaseProvider {
    connection_pool: sqlx::Pool<sqlx::Postgres>,
}

#[async_trait]
impl ResourceProvider<AppContext> for DatabaseProvider {
    async fn list_resources(&self, context: Arc<AppContext>) -> Result<Vec<ResourceInfo>> {
        Ok(vec![
            ResourceInfo {
                uri: "db://users".to_string(),
                name: "User Database".to_string(),
                description: Some("All registered users".to_string()),
                mime_type: Some("application/json".to_string()),
            }
        ])
    }

    async fn read_resource(&self, uri: &str, context: Arc<AppContext>) -> Result<ResourceContent> {
        match uri {
            "db://users" => {
                let users = sqlx::query!("SELECT * FROM users")
                    .fetch_all(&self.connection_pool)
                    .await?;
                Ok(ResourceContent {
                    uri: uri.to_string(),
                    mime_type: Some("application/json".to_string()),
                    content: serde_json::to_string_pretty(&users)?,
                })
            }
            _ => Err(anyhow::anyhow!("Resource not found: {}", uri))
        }
    }
}

Required Methods§

Source

fn list_resources<'life0, 'async_trait>( &'life0 self, context: Arc<C>, ) -> Pin<Box<dyn Future<Output = Result<Vec<ResourceInfo>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

List all available resources that this provider can serve.

§Parameters
  • context: Shared application context
§Returns

Result<Vec<ResourceInfo>> - List of available resources or an error

Source

fn read_resource<'life0, 'life1, 'async_trait>( &'life0 self, uri: &'life1 str, context: Arc<C>, ) -> Pin<Box<dyn Future<Output = Result<ResourceContent>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Read the content of a specific resource identified by URI.

§Parameters
  • uri: The unique identifier for the resource to read
  • context: Shared application context
§Returns

Result<ResourceContent> - The resource content or an error if not found

Implementors§