Skip to main content

Module sampling

Module sampling 

Source
Expand description

Sampling API for server-initiated LLM requests.

This module allows MCP servers to request LLM completions from clients that support the sampling capability. This enables agentic workflows where the server can leverage the client’s LLM for complex tasks.

§Example

use mcp_kit::server::sampling::{SamplingRequest, SamplingClient};

async fn agentic_tool(client: impl SamplingClient) -> Result<String, Error> {
    let request = SamplingRequest::new()
        .add_user_message("Analyze this data and suggest improvements")
        .max_tokens(1000);
     
    let response = client.create_message(request).await?;
    Ok(response.content.text().unwrap_or_default())
}

Structs§

ChannelSamplingClient
Channel-based sampling client implementation.
NoOpSamplingClient
No-op sampling client for when client doesn’t support sampling.
SamplingRequestBuilder
Builder for creating sampling requests.

Traits§

SamplingClient
A client for making sampling requests to the MCP client.