ServerToClientRequests

Trait ServerToClientRequests 

Source
pub trait ServerToClientRequests:
    Send
    + Sync
    + Debug {
    // Required methods
    fn create_message(
        &self,
        request: CreateMessageRequest,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<CreateMessageResult, Error>>;
    fn elicit(
        &self,
        request: ElicitRequest,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<ElicitResult, Error>>;
    fn list_roots(
        &self,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<ListRootsResult, Error>>;
}
Expand description

Trait for server-to-client requests (sampling, elicitation, roots)

This trait provides a type-safe interface for servers to make requests to clients, enabling bidirectional MCP communication patterns. All methods accept a RequestContext parameter to enable proper context propagation for tracing, attribution, and auditing.

§Design Rationale

This trait uses typed request/response structures instead of serde_json::Value to provide:

  • Type safety: Compile-time validation of request/response structures
  • Performance: Zero-cost abstraction with no intermediate serialization
  • Context propagation: Full support for distributed tracing and user attribution
  • Better errors: Structured error types instead of generic Box<dyn Error>

§Breaking Change (v2.0.0)

This trait was renamed from ServerCapabilities to ServerToClientRequests and redesigned to fix fundamental architecture issues:

  • Old: fn create_message(&self, request: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>>
  • New: fn create_message(&self, request: CreateMessageRequest, ctx: RequestContext) -> Result<CreateMessageResult, ServerError>

Required Methods§

Source

fn create_message( &self, request: CreateMessageRequest, ctx: RequestContext, ) -> BoxFuture<'_, Result<CreateMessageResult, Error>>

Send a sampling/createMessage request to the client

This method allows server tools to request LLM sampling from the client. The client is responsible for:

  • Selecting an appropriate model based on preferences
  • Making the LLM API call
  • Returning the generated response
§Arguments
  • request - The sampling request with messages, model preferences, and parameters
  • ctx - Request context for tracing, user attribution, and metadata propagation
§Errors

Returns an error if:

  • The client does not support sampling
  • The transport layer fails
  • The client returns an error response
  • The LLM request fails
§Example
use turbomcp_protocol::context::capabilities::ServerToClientRequests;
use turbomcp_protocol::RequestContext;

async fn example(capabilities: &dyn ServerToClientRequests) {
    let request = CreateMessageRequest {
        messages: vec![SamplingMessage {
            role: Role::User,
            content: Content::Text(TextContent {
                text: "What is 2+2?".to_string(),
                annotations: None,
                meta: None,
            }),
            metadata: None,
        }],
        model_preferences: None,
        system_prompt: None,
        include_context: None,
        temperature: None,
        max_tokens: 100,
        stop_sequences: None,
        _meta: None,
    };

    let ctx = RequestContext::new();
    let result = capabilities.create_message(request, ctx).await;
}
Source

fn elicit( &self, request: ElicitRequest, ctx: RequestContext, ) -> BoxFuture<'_, Result<ElicitResult, Error>>

Send an elicitation request to the client for user input

This method allows server tools to request structured input from users through the client’s UI. The client is responsible for presenting the elicitation prompt and collecting the user’s response according to the requested schema.

§Arguments
  • request - The elicitation request with prompt and optional schema
  • ctx - Request context for tracing, user attribution, and metadata propagation
§Errors

Returns an error if:

  • The client does not support elicitation
  • The transport layer fails
  • The user declines or cancels the request
  • The client returns an error response
Source

fn list_roots( &self, ctx: RequestContext, ) -> BoxFuture<'_, Result<ListRootsResult, Error>>

List client’s root capabilities

This method allows servers to discover which directories or files the client has granted access to. Roots define the filesystem boundaries for resource access.

§Arguments
  • ctx - Request context for tracing, user attribution, and metadata propagation
§Errors

Returns an error if:

  • The client does not support roots
  • The transport layer fails
  • The client returns an error response

Implementors§