pub trait SamplingHandler: Send + Sync {
// Required method
fn create_message(
&self,
request: CreateMessageRequest,
ctx: &Context<'_>,
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send;
}Expand description
Handler for sampling requests (server-initiated LLM calls).
Implement this trait to allow the server to request LLM completions from the client. This enables agentic workflows where servers can leverage the client’s AI capabilities.
§Example
use mcpkit_server::{SamplingHandler, Context};
use mcpkit_core::types::sampling::{CreateMessageRequest, CreateMessageResult};
use mcpkit_core::error::McpError;
struct MyServer;
impl SamplingHandler for MyServer {
async fn create_message(
&self,
request: CreateMessageRequest,
ctx: &Context<'_>,
) -> Result<CreateMessageResult, McpError> {
// The client will handle this request and invoke the LLM
ctx.peer().create_message(request).await
}
}§Note
Sampling is a client-side capability. The server sends a sampling request to the client, which then invokes the LLM and returns the result. The handler implementation typically just forwards the request through the peer interface.
Required Methods§
Sourcefn create_message(
&self,
request: CreateMessageRequest,
ctx: &Context<'_>,
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send
fn create_message( &self, request: CreateMessageRequest, ctx: &Context<'_>, ) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send
Request the client to create an LLM message.
This allows the server to leverage the client’s AI capabilities for generating completions, answering questions, or performing other LLM-based operations.
§Arguments
request- The sampling request specifying messages, model preferences, etc.ctx- The request context providing access to the peer connection.
§Returns
The result of the LLM completion, including the generated content and metadata about the completion.
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.