pub struct ClientBuilder { /* private fields */ }Expand description
Builder for configuring an MCP client.
Use this to configure timeout, retry, and spawn options before connecting to an MCP server.
Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new client builder with default settings.
Default configuration:
- Client name: “fastmcp-client”
- Timeout: 30 seconds
- Max retries: 0 (no retries)
- Retry delay: 1 second
- Inherit environment: true
- Auto-initialize: false (initialize immediately on connect)
Sourcepub fn client_info(
self,
name: impl Into<String>,
version: impl Into<String>,
) -> Self
pub fn client_info( self, name: impl Into<String>, version: impl Into<String>, ) -> Self
Sets the client name and version.
This information is sent to the server during initialization.
Sourcepub fn timeout_ms(self, timeout: u64) -> Self
pub fn timeout_ms(self, timeout: u64) -> Self
Sets the request timeout in milliseconds.
This affects how long the client waits for responses from the server. Default is 30,000ms (30 seconds).
Sourcepub fn max_retries(self, retries: u32) -> Self
pub fn max_retries(self, retries: u32) -> Self
Sets the maximum number of connection retries.
When connecting to a server fails, the client will retry up to this many times before returning an error. Default is 0 (no retries).
Sourcepub fn retry_delay_ms(self, delay: u64) -> Self
pub fn retry_delay_ms(self, delay: u64) -> Self
Sets the delay between connection retries in milliseconds.
Default is 1,000ms (1 second).
Sourcepub fn working_dir(self, path: impl Into<PathBuf>) -> Self
pub fn working_dir(self, path: impl Into<PathBuf>) -> Self
Sets the working directory for the subprocess.
If not set, the subprocess inherits the current working directory.
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Adds an environment variable for the subprocess.
Multiple calls to this method accumulate environment variables.
Sourcepub fn envs<I, K, V>(self, vars: I) -> Self
pub fn envs<I, K, V>(self, vars: I) -> Self
Adds multiple environment variables for the subprocess.
Sourcepub fn inherit_env(self, inherit: bool) -> Self
pub fn inherit_env(self, inherit: bool) -> Self
Sourcepub fn capabilities(self, capabilities: ClientCapabilities) -> Self
pub fn capabilities(self, capabilities: ClientCapabilities) -> Self
Sets the client capabilities to advertise to the server.
Sourcepub fn auto_initialize(self, enabled: bool) -> Self
pub fn auto_initialize(self, enabled: bool) -> Self
Enables auto-initialization mode.
When enabled, the client defers the MCP initialization handshake until
the first method call (e.g., list_tools, call_tool). This allows
the subprocess to start immediately without blocking on initialization.
Default is false (initialize immediately on connect).
§Example
let client = ClientBuilder::new()
.auto_initialize(true)
.connect_stdio("uvx", &["my-server"])?;
// Subprocess is running but not yet initialized
// Initialization happens on first use:
let tools = client.list_tools()?; // Initializes hereSourcepub fn connect_stdio(self, command: &str, args: &[&str]) -> McpResult<Client>
pub fn connect_stdio(self, command: &str, args: &[&str]) -> McpResult<Client>
Connects to a server via stdio subprocess.
Spawns the specified command as a subprocess and communicates via stdin/stdout using JSON-RPC over NDJSON framing.
§Arguments
command- The command to run (e.g., “uvx”, “npx”)args- Arguments to pass to the command
§Errors
Returns an error if:
- The subprocess fails to spawn
- The initialization handshake fails
- All retry attempts are exhausted
Sourcepub fn connect_stdio_with_cx(
self,
command: &str,
args: &[&str],
cx: &Cx,
) -> McpResult<Client>
pub fn connect_stdio_with_cx( self, command: &str, args: &[&str], cx: &Cx, ) -> McpResult<Client>
Connects to a server via stdio subprocess with a provided Cx.
Same as connect_stdio but allows providing
a custom capability context for cancellation support.
Trait Implementations§
Source§impl Clone for ClientBuilder
impl Clone for ClientBuilder
Source§fn clone(&self) -> ClientBuilder
fn clone(&self) -> ClientBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more