SamplingHandler

Trait SamplingHandler 

Source
pub trait SamplingHandler:
    Send
    + Sync
    + Debug {
    // Required method
    fn handle_create_message<'life0, 'async_trait>(
        &'life0 self,
        request: CreateMessageRequest,
    ) -> Pin<Box<dyn Future<Output = Result<CreateMessageResult, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Handler for server-initiated sampling requests

Implement this trait to handle sampling requests from MCP servers. The handler receives a CreateMessageRequest and must return a response with the generated content.

§Examples

use turbomcp_client::sampling::{SamplingHandler, ProductionSamplingHandler, LLMBackendConfig, LLMProvider};
use turbomcp_protocol::types::{CreateMessageRequest, CreateMessageResult};

let config = LLMBackendConfig {
    provider: LLMProvider::OpenAI {
        api_key: std::env::var("OPENAI_API_KEY")?,
        base_url: None,
        organization: None,
    },
    default_model: Some("gpt-4".to_string()),
    timeout_seconds: 30,
    max_retries: 3,
};

let handler = ProductionSamplingHandler::new(config)?;
// Use handler for MCP sampling requests

Required Methods§

Source

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

Handle a sampling/createMessage request from the server

Implementors§