pub struct ClientBuilder { /* private fields */ }Expand description
Builder for configuring and creating MCP clients
Provides a fluent interface for configuring client options before creation. The enhanced builder pattern supports comprehensive configuration including:
- Protocol capabilities
- Plugin registration
- Handler registration
- Connection settings
- Resilience configuration
§Examples
Basic usage:
use turbomcp_client::ClientBuilder;
use turbomcp_transport::stdio::StdioTransport;
let client = ClientBuilder::new()
.with_tools(true)
.with_prompts(true)
.with_resources(false)
.build(StdioTransport::new());Advanced configuration:
use turbomcp_client::{ClientBuilder, ConnectionConfig};
use turbomcp_client::plugins::{MetricsPlugin, PluginConfig};
use turbomcp_transport::stdio::StdioTransport;
use std::sync::Arc;
let client = ClientBuilder::new()
.with_tools(true)
.with_prompts(true)
.with_resources(true)
.with_sampling(true)
.with_connection_config(ConnectionConfig {
timeout_ms: 60_000,
max_retries: 5,
retry_delay_ms: 2_000,
keepalive_ms: 30_000,
})
.with_plugin(Arc::new(MetricsPlugin::new(PluginConfig::Metrics)))
.build(StdioTransport::new())
.await?;Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn new() -> ClientBuilder
pub fn new() -> ClientBuilder
Create a new client builder
Returns a new builder with default configuration.
Sourcepub fn with_tools(self, enabled: bool) -> ClientBuilder
pub fn with_tools(self, enabled: bool) -> ClientBuilder
Sourcepub fn with_prompts(self, enabled: bool) -> ClientBuilder
pub fn with_prompts(self, enabled: bool) -> ClientBuilder
Sourcepub fn with_resources(self, enabled: bool) -> ClientBuilder
pub fn with_resources(self, enabled: bool) -> ClientBuilder
Sourcepub fn with_sampling(self, enabled: bool) -> ClientBuilder
pub fn with_sampling(self, enabled: bool) -> ClientBuilder
Sourcepub fn with_max_concurrent_handlers(self, limit: usize) -> ClientBuilder
pub fn with_max_concurrent_handlers(self, limit: usize) -> ClientBuilder
Set maximum concurrent request/notification handlers
This limits how many server-initiated requests/notifications can be processed simultaneously. Provides automatic backpressure when the limit is reached.
§Arguments
limit- Maximum concurrent handlers (default: 100)
§Tuning Guide
- Low-resource clients: 50
- Standard clients: 100 (default)
- High-performance: 200-500
- Maximum recommended: 1000
§Example
use turbomcp_client::ClientBuilder;
let builder = ClientBuilder::new()
.with_max_concurrent_handlers(200);Sourcepub fn with_capabilities(
self,
capabilities: ClientCapabilities,
) -> ClientBuilder
pub fn with_capabilities( self, capabilities: ClientCapabilities, ) -> ClientBuilder
Sourcepub fn with_connection_config(self, config: ConnectionConfig) -> ClientBuilder
pub fn with_connection_config(self, config: ConnectionConfig) -> ClientBuilder
Sourcepub fn with_timeout(self, timeout_ms: u64) -> ClientBuilder
pub fn with_timeout(self, timeout_ms: u64) -> ClientBuilder
Sourcepub fn with_max_retries(self, max_retries: u32) -> ClientBuilder
pub fn with_max_retries(self, max_retries: u32) -> ClientBuilder
Sourcepub fn with_retry_delay(self, delay_ms: u64) -> ClientBuilder
pub fn with_retry_delay(self, delay_ms: u64) -> ClientBuilder
Sourcepub fn with_keepalive(self, interval_ms: u64) -> ClientBuilder
pub fn with_keepalive(self, interval_ms: u64) -> ClientBuilder
Sourcepub fn enable_resilience(self) -> ClientBuilder
pub fn enable_resilience(self) -> ClientBuilder
Enable resilient transport with circuit breaker, retry, and health checking
When enabled, the transport layer will automatically:
- Retry failed operations with exponential backoff
- Use circuit breaker pattern to prevent cascade failures
- Perform periodic health checks
- Deduplicate messages
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::stdio::StdioTransport;
let client = ClientBuilder::new()
.enable_resilience()
.build(StdioTransport::new());Sourcepub fn with_retry_config(self, config: RetryConfig) -> ClientBuilder
pub fn with_retry_config(self, config: RetryConfig) -> ClientBuilder
Configure retry behavior for resilient transport
§Arguments
config- Retry configuration
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::resilience::RetryConfig;
use turbomcp_transport::stdio::StdioTransport;
use std::time::Duration;
let client = ClientBuilder::new()
.enable_resilience()
.with_retry_config(RetryConfig {
max_attempts: 5,
base_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(30),
backoff_multiplier: 2.0,
jitter_factor: 0.1,
retry_on_connection_error: true,
retry_on_timeout: true,
custom_retry_conditions: Vec::new(),
})
.build(StdioTransport::new());Sourcepub fn with_circuit_breaker_config(
self,
config: CircuitBreakerConfig,
) -> ClientBuilder
pub fn with_circuit_breaker_config( self, config: CircuitBreakerConfig, ) -> ClientBuilder
Configure circuit breaker for resilient transport
§Arguments
config- Circuit breaker configuration
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::resilience::CircuitBreakerConfig;
use turbomcp_transport::stdio::StdioTransport;
use std::time::Duration;
let client = ClientBuilder::new()
.enable_resilience()
.with_circuit_breaker_config(CircuitBreakerConfig {
failure_threshold: 5,
success_threshold: 2,
timeout: Duration::from_secs(60),
rolling_window_size: 100,
minimum_requests: 10,
})
.build(StdioTransport::new());Sourcepub fn with_health_check_config(
self,
config: HealthCheckConfig,
) -> ClientBuilder
pub fn with_health_check_config( self, config: HealthCheckConfig, ) -> ClientBuilder
Configure health checking for resilient transport
§Arguments
config- Health check configuration
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::resilience::HealthCheckConfig;
use turbomcp_transport::stdio::StdioTransport;
use std::time::Duration;
let client = ClientBuilder::new()
.enable_resilience()
.with_health_check_config(HealthCheckConfig {
interval: Duration::from_secs(30),
timeout: Duration::from_secs(5),
failure_threshold: 3,
success_threshold: 1,
custom_check: None,
})
.build(StdioTransport::new());Sourcepub fn with_plugin(self, plugin: Arc<dyn ClientPlugin>) -> ClientBuilder
pub fn with_plugin(self, plugin: Arc<dyn ClientPlugin>) -> ClientBuilder
Register a plugin with the client
Plugins provide middleware functionality for request/response processing, metrics collection, retry logic, caching, and other cross-cutting concerns.
§Arguments
plugin- The plugin implementation
§Examples
use turbomcp_client::{ClientBuilder, ConnectionConfig};
use turbomcp_client::plugins::{MetricsPlugin, RetryPlugin, PluginConfig, RetryConfig};
use std::sync::Arc;
let client = ClientBuilder::new()
.with_plugin(Arc::new(MetricsPlugin::new(PluginConfig::Metrics)))
.with_plugin(Arc::new(RetryPlugin::new(PluginConfig::Retry(RetryConfig {
max_retries: 5,
base_delay_ms: 1000,
max_delay_ms: 30000,
backoff_multiplier: 2.0,
retry_on_timeout: true,
retry_on_connection_error: true,
}))));Sourcepub fn with_plugins(self, plugins: Vec<Arc<dyn ClientPlugin>>) -> ClientBuilder
pub fn with_plugins(self, plugins: Vec<Arc<dyn ClientPlugin>>) -> ClientBuilder
Sourcepub fn with_elicitation_handler(
self,
handler: Arc<dyn ElicitationHandler>,
) -> ClientBuilder
pub fn with_elicitation_handler( self, handler: Arc<dyn ElicitationHandler>, ) -> ClientBuilder
Register an elicitation handler for processing user input requests
§Arguments
handler- The elicitation handler implementation
Sourcepub fn with_log_handler(self, handler: Arc<dyn LogHandler>) -> ClientBuilder
pub fn with_log_handler(self, handler: Arc<dyn LogHandler>) -> ClientBuilder
Register a log handler for processing server log messages
§Arguments
handler- The log handler implementation
Sourcepub fn with_resource_update_handler(
self,
handler: Arc<dyn ResourceUpdateHandler>,
) -> ClientBuilder
pub fn with_resource_update_handler( self, handler: Arc<dyn ResourceUpdateHandler>, ) -> ClientBuilder
Register a resource update handler for processing resource change notifications
§Arguments
handler- The resource update handler implementation
Sourcepub async fn build<T>(self, transport: T) -> Result<Client<T>, Box<Error>>where
T: Transport + 'static,
pub async fn build<T>(self, transport: T) -> Result<Client<T>, Box<Error>>where
T: Transport + 'static,
Build a client with the configured options
Creates a new client instance with all the configured options. The client will be initialized with the registered plugins, handlers, and providers.
§Arguments
transport- The transport to use for the client
§Returns
Returns a configured Client instance wrapped in a Result for async setup.
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::stdio::StdioTransport;
let client = ClientBuilder::new()
.with_tools(true)
.with_prompts(true)
.build(StdioTransport::new())
.await?;Sourcepub async fn build_resilient<T>(
self,
transport: T,
) -> Result<Client<TurboTransport>, Box<Error>>where
T: Transport + 'static,
pub async fn build_resilient<T>(
self,
transport: T,
) -> Result<Client<TurboTransport>, Box<Error>>where
T: Transport + 'static,
Build a client with resilient transport (circuit breaker, retry, health checking)
When resilience features are enabled via enable_resilience() or any resilience
configuration method, this wraps the transport in a TurboTransport that provides:
- Automatic retry with exponential backoff
- Circuit breaker pattern for fast failure
- Health checking and monitoring
- Message deduplication
§Arguments
transport- The base transport to wrap with resilience features
§Returns
Returns a configured Client<TurboTransport> instance.
§Errors
Returns an error if plugin initialization fails.
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::stdio::StdioTransport;
use turbomcp_transport::resilience::{RetryConfig, CircuitBreakerConfig, HealthCheckConfig};
use std::time::Duration;
let client = ClientBuilder::new()
.with_retry_config(RetryConfig {
max_attempts: 5,
base_delay: Duration::from_millis(200),
..Default::default()
})
.with_circuit_breaker_config(CircuitBreakerConfig {
failure_threshold: 3,
timeout: Duration::from_secs(30),
..Default::default()
})
.with_health_check_config(HealthCheckConfig {
interval: Duration::from_secs(15),
timeout: Duration::from_secs(5),
..Default::default()
})
.build_resilient(StdioTransport::new())
.await?;Sourcepub fn build_sync<T>(self, transport: T) -> Client<T>where
T: Transport + 'static,
pub fn build_sync<T>(self, transport: T) -> Client<T>where
T: Transport + 'static,
Build a client synchronously with basic configuration only
This is a convenience method for simple use cases where no async setup
is required. For advanced features like plugins, use build() instead.
§Arguments
transport- The transport to use for the client
§Returns
Returns a configured Client instance.
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_transport::stdio::StdioTransport;
let client = ClientBuilder::new()
.with_tools(true)
.build_sync(StdioTransport::new());Sourcepub fn capabilities(&self) -> &ClientCapabilities
pub fn capabilities(&self) -> &ClientCapabilities
Get the current capabilities configuration
Sourcepub fn connection_config(&self) -> &ConnectionConfig
pub fn connection_config(&self) -> &ConnectionConfig
Get the current connection configuration
Sourcepub fn plugin_count(&self) -> usize
pub fn plugin_count(&self) -> usize
Get the number of registered plugins
Sourcepub fn has_handlers(&self) -> bool
pub fn has_handlers(&self) -> bool
Check if any handlers are registered
Trait Implementations§
Source§impl Debug for ClientBuilder
impl Debug for ClientBuilder
Source§impl Default for ClientBuilder
impl Default for ClientBuilder
Source§fn default() -> ClientBuilder
fn default() -> ClientBuilder
Auto Trait Implementations§
impl Freeze for ClientBuilder
impl !RefUnwindSafe for ClientBuilder
impl Send for ClientBuilder
impl Sync for ClientBuilder
impl Unpin for ClientBuilder
impl !UnwindSafe for ClientBuilder
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more