Module handlers

Module handlers 

Source
Expand description

Handler traits for bidirectional communication in MCP client

This module provides handler traits and registration mechanisms for processing server-initiated requests. The MCP protocol is bidirectional, meaning servers can also send requests to clients for various purposes like elicitation, logging, and resource updates.

§Handler Types

  • ElicitationHandler: Handle user input requests from servers
  • LogHandler: Route server log messages to client logging systems
  • ResourceUpdateHandler: Handle notifications when resources change

§Usage

use turbomcp_client::handlers::{ElicitationHandler, ElicitationRequest, ElicitationResponse, ElicitationAction, HandlerError};
use async_trait::async_trait;

// Implement elicitation handler
#[derive(Debug)]
struct MyElicitationHandler;

#[async_trait]
impl ElicitationHandler for MyElicitationHandler {
    async fn handle_elicitation(
        &self,
        request: ElicitationRequest,
    ) -> Result<ElicitationResponse, HandlerError> {
        // Display the prompt to the user
        eprintln!("\n{}", request.message());
        eprintln!("---");

        // Access the typed schema (not serde_json::Value!)
        let mut content = std::collections::HashMap::new();
        for (field_name, field_def) in &request.schema().properties {
            eprint!("{}: ", field_name);

            let mut input = String::new();
            std::io::stdin().read_line(&mut input)
                .map_err(|e| HandlerError::Generic {
                    message: e.to_string()
                })?;

            let input = input.trim();

            // Parse input based on field type (from typed schema!)
            use turbomcp_protocol::types::PrimitiveSchemaDefinition;
            let value: serde_json::Value = match field_def {
                PrimitiveSchemaDefinition::Boolean { .. } => {
                    serde_json::json!(input == "true" || input == "yes" || input == "1")
                }
                PrimitiveSchemaDefinition::Number { .. } | PrimitiveSchemaDefinition::Integer { .. } => {
                    input.parse::<f64>()
                        .map(|n| serde_json::json!(n))
                        .unwrap_or_else(|_| serde_json::json!(input))
                }
                _ => serde_json::json!(input),
            };

            content.insert(field_name.clone(), value);
        }

        Ok(ElicitationResponse::accept(content))
    }
}

Structs§

CancelledNotification
Cancellation notification
DeclineElicitationHandler
Default elicitation handler that declines all requests
ElicitationRequest
Ergonomic wrapper around protocol ElicitRequest with request ID
ElicitationResponse
Elicitation response builder
HandlerRegistry
Registry for managing client-side handlers
LoggingCancellationHandler
Default cancellation handler that logs cancellation notifications
LoggingNotification
Logging notification
LoggingPromptListChangedHandler
Default prompt list changed handler that logs changes
LoggingResourceListChangedHandler
Default resource list changed handler that logs changes
LoggingResourceUpdateHandler
Default resource update handler that logs changes
LoggingToolListChangedHandler
Default tool list changed handler that logs changes
ResourceUpdatedNotification
Resource updated notification
TracingLogHandler
Default log handler that routes server logs to tracing

Enums§

ElicitationAction
Elicitation action taken by user
HandlerError
Errors that can occur during handler operations

Traits§

CancellationHandler
Cancellation handler for processing cancellation notifications
ElicitationHandler
Handler for server-initiated elicitation requests
LogHandler
Handler for server log messages
PromptListChangedHandler
Handler for prompt list changes
ResourceListChangedHandler
Handler for resource list changes
ResourceUpdateHandler
Handler for resource update notifications
RootsHandler
Roots handler for responding to server requests for filesystem roots
ToolListChangedHandler
Handler for tool list changes

Type Aliases§

HandlerResult