McpSampling

Trait McpSampling 

Source
pub trait McpSampling:
    SamplingDefinition
    + Send
    + Sync {
    // Required method
    fn sample<'life0, 'async_trait>(
        &'life0 self,
        request: CreateMessageRequest,
    ) -> Pin<Box<dyn Future<Output = McpResult<CreateMessageResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn can_handle(&self, _request: &CreateMessageRequest) -> bool { ... }
    fn priority(&self) -> u32 { ... }
    fn validate_request<'life0, 'life1, 'async_trait>(
        &'life0 self,
        request: &'life1 CreateMessageRequest,
    ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

High-level trait for implementing MCP sampling

McpSampling extends SamplingDefinition with execution capabilities. All metadata is provided by the SamplingDefinition trait, ensuring consistency between concrete Sampling structs and dynamic implementations.

Required Methods§

Source

fn sample<'life0, 'async_trait>( &'life0 self, request: CreateMessageRequest, ) -> Pin<Box<dyn Future<Output = McpResult<CreateMessageResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Create a message using the sampling model (per MCP spec)

This method processes the sampling/createMessage request and returns the generated message response.

Provided Methods§

Source

fn can_handle(&self, _request: &CreateMessageRequest) -> bool

Optional: Check if this sampling handler can handle the given request

This allows for conditional sampling based on model preferences, context size, or other factors.

Source

fn priority(&self) -> u32

Optional: Get sampling priority for request routing

Higher priority handlers are tried first when multiple handlers can handle the same request.

Source

fn validate_request<'life0, 'life1, 'async_trait>( &'life0 self, request: &'life1 CreateMessageRequest, ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Optional: Validate the sampling request

This method can perform additional validation beyond basic parameter checks.

Implementors§