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
- LLM provider configuration
- Handler registration
- Connection settings
- Session management
§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_client::llm::{OpenAIProvider, LLMProviderConfig};
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)))
.with_llm_provider("openai", Arc::new(OpenAIProvider::new(LLMProviderConfig {
api_key: std::env::var("OPENAI_API_KEY")?,
model: "gpt-4".to_string(),
..Default::default()
})?))
.build(StdioTransport::new())
.await?;
Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new client builder
Returns a new builder with default configuration.
Sourcepub fn with_tools(self, enabled: bool) -> Self
pub fn with_tools(self, enabled: bool) -> Self
Sourcepub fn with_prompts(self, enabled: bool) -> Self
pub fn with_prompts(self, enabled: bool) -> Self
Sourcepub fn with_resources(self, enabled: bool) -> Self
pub fn with_resources(self, enabled: bool) -> Self
Sourcepub fn with_sampling(self, enabled: bool) -> Self
pub fn with_sampling(self, enabled: bool) -> Self
Sourcepub fn with_capabilities(self, capabilities: ClientCapabilities) -> Self
pub fn with_capabilities(self, capabilities: ClientCapabilities) -> Self
Sourcepub fn with_connection_config(self, config: ConnectionConfig) -> Self
pub fn with_connection_config(self, config: ConnectionConfig) -> Self
Sourcepub fn with_timeout(self, timeout_ms: u64) -> Self
pub fn with_timeout(self, timeout_ms: u64) -> Self
Sourcepub fn with_max_retries(self, max_retries: u32) -> Self
pub fn with_max_retries(self, max_retries: u32) -> Self
Sourcepub fn with_retry_delay(self, delay_ms: u64) -> Self
pub fn with_retry_delay(self, delay_ms: u64) -> Self
Sourcepub fn with_keepalive(self, interval_ms: u64) -> Self
pub fn with_keepalive(self, interval_ms: u64) -> Self
Sourcepub fn with_plugin(self, plugin: Arc<dyn ClientPlugin>) -> Self
pub fn with_plugin(self, plugin: Arc<dyn ClientPlugin>) -> Self
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>>) -> Self
pub fn with_plugins(self, plugins: Vec<Arc<dyn ClientPlugin>>) -> Self
Sourcepub fn with_llm_provider(
self,
name: impl Into<String>,
provider: Arc<dyn LLMProvider>,
) -> Self
pub fn with_llm_provider( self, name: impl Into<String>, provider: Arc<dyn LLMProvider>, ) -> Self
Register an LLM provider
LLM providers handle server-initiated sampling requests by forwarding them to language model services like OpenAI, Anthropic, or local models.
§Arguments
name
- Unique name for the providerprovider
- The LLM provider implementation
§Examples
use turbomcp_client::ClientBuilder;
use turbomcp_client::llm::{OpenAIProvider, AnthropicProvider, LLMProviderConfig};
use std::sync::Arc;
let client = ClientBuilder::new()
.with_llm_provider("openai", Arc::new(OpenAIProvider::new(LLMProviderConfig {
api_key: std::env::var("OPENAI_API_KEY")?,
model: "gpt-4".to_string(),
..Default::default()
})?))
.with_llm_provider("anthropic", Arc::new(AnthropicProvider::new(LLMProviderConfig {
api_key: std::env::var("ANTHROPIC_API_KEY")?,
model: "claude-3-5-sonnet-20241022".to_string(),
..Default::default()
})?));
Sourcepub fn with_llm_providers(
self,
providers: HashMap<String, Arc<dyn LLMProvider>>,
) -> Self
pub fn with_llm_providers( self, providers: HashMap<String, Arc<dyn LLMProvider>>, ) -> Self
Register multiple LLM providers at once
§Arguments
providers
- Map of provider names to implementations
Sourcepub fn with_session_config(self, config: SessionConfig) -> Self
pub fn with_session_config(self, config: SessionConfig) -> Self
Configure session management for conversations
§Arguments
config
- Session configuration for conversation tracking
Sourcepub fn with_elicitation_handler(
self,
handler: Arc<dyn ElicitationHandler>,
) -> Self
pub fn with_elicitation_handler( self, handler: Arc<dyn ElicitationHandler>, ) -> Self
Register an elicitation handler for processing user input requests
§Arguments
handler
- The elicitation handler implementation
Sourcepub fn with_progress_handler(self, handler: Arc<dyn ProgressHandler>) -> Self
pub fn with_progress_handler(self, handler: Arc<dyn ProgressHandler>) -> Self
Register a progress handler for processing operation progress updates
§Arguments
handler
- The progress handler implementation
Sourcepub fn with_log_handler(self, handler: Arc<dyn LogHandler>) -> Self
pub fn with_log_handler(self, handler: Arc<dyn LogHandler>) -> Self
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>,
) -> Self
pub fn with_resource_update_handler( self, handler: Arc<dyn ResourceUpdateHandler>, ) -> Self
Register a resource update handler for processing resource change notifications
§Arguments
handler
- The resource update handler implementation
Sourcepub async fn build<T: Transport>(self, transport: T) -> Result<Client<T>>
pub async fn build<T: Transport>(self, transport: T) -> Result<Client<T>>
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 fn build_sync<T: Transport>(self, transport: T) -> Client<T>
pub fn build_sync<T: Transport>(self, transport: T) -> Client<T>
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 LLM providers, 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 llm_provider_count(&self) -> usize
pub fn llm_provider_count(&self) -> usize
Get the number of registered LLM providers
Sourcepub fn has_handlers(&self) -> bool
pub fn has_handlers(&self) -> bool
Check if any handlers are registered