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, progress reporting, logging, and resource updates.
§Handler Types
- ElicitationHandler: Handle user input requests from servers
- ProgressHandler: Process progress notifications from long-running operations
- 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.prompt);
eprintln!("---");
// For each field in the schema, collect user input
let mut content = std::collections::HashMap::new();
if let Some(properties) = request.schema.get("properties") {
if let Some(props) = properties.as_object() {
for (field_name, field_schema) in props {
let field_type = field_schema.get("type")
.and_then(|v| v.as_str())
.unwrap_or("string");
eprint!("{} ({}): ", field_name, field_type);
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
let value: serde_json::Value = match field_type {
"boolean" => serde_json::json!(input == "true" || input == "yes" || input == "1"),
"number" | "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 {
action: ElicitationAction::Accept,
content: Some(serde_json::to_value(content).unwrap()),
})
}
}Structs§
- Decline
Elicitation Handler - Default elicitation handler that declines all requests
- Elicitation
Request - Request structure for elicitation operations
- Elicitation
Response - Response structure for elicitation operations (MCP-compliant)
- Handler
Registry - Registry for managing client-side handlers
- LogMessage
- Log message from the server
- Logging
Progress Handler - Default progress handler that logs progress to tracing
- Logging
Resource Update Handler - Default resource update handler that logs changes
- Progress
Notification - Progress notification from server operations
- Resource
Update Notification - Resource update notification
- Tracing
LogHandler - Default log handler that routes server logs to tracing
Enums§
- Elicitation
Action - Elicitation response action indicating user’s choice
- Handler
Error - Errors that can occur during handler operations
- Resource
Change Type - Types of resource changes
Traits§
- Elicitation
Handler - Handler for server-initiated elicitation requests
- LogHandler
- Handler for server log messages
- Progress
Handler - Handler for server progress notifications
- Resource
Update Handler - Handler for resource update notifications
- Roots
Handler - Roots handler for responding to server requests for filesystem roots