Skip to main content

ClientBuilder

Struct ClientBuilder 

Source
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

Source

pub fn new() -> ClientBuilder

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)
Source

pub fn client_info( self, name: impl Into<String>, version: impl Into<String>, ) -> ClientBuilder

Sets the client name and version.

This information is sent to the server during initialization.

Source

pub fn timeout_ms(self, timeout: u64) -> ClientBuilder

Sets the request timeout in milliseconds.

This affects how long the client waits for responses from the server. Default is 30,000ms (30 seconds).

Source

pub fn max_retries(self, retries: u32) -> ClientBuilder

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).

Source

pub fn retry_delay_ms(self, delay: u64) -> ClientBuilder

Sets the delay between connection retries in milliseconds.

Default is 1,000ms (1 second).

Source

pub fn working_dir(self, path: impl Into<PathBuf>) -> ClientBuilder

Sets the working directory for the subprocess.

If not set, the subprocess inherits the current working directory.

Source

pub fn env( self, key: impl Into<String>, value: impl Into<String>, ) -> ClientBuilder

Adds an environment variable for the subprocess.

Multiple calls to this method accumulate environment variables.

Source

pub fn envs<I, K, V>(self, vars: I) -> ClientBuilder
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

Adds multiple environment variables for the subprocess.

Source

pub fn inherit_env(self, inherit: bool) -> ClientBuilder

Sets whether to inherit the parent process’s environment.

If true (default), the subprocess starts with the parent’s environment plus any variables added via env or envs.

If false, the subprocess starts with only the explicitly set variables.

Source

pub fn capabilities(self, capabilities: ClientCapabilities) -> ClientBuilder

Sets the client capabilities to advertise to the server.

Source

pub fn auto_initialize(self, enabled: bool) -> ClientBuilder

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 here
Source

pub fn connect_stdio( self, command: &str, args: &[&str], ) -> Result<Client, McpError>

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
Source

pub fn connect_stdio_with_cx( self, command: &str, args: &[&str], cx: &Cx, ) -> Result<Client, McpError>

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

Source§

fn clone(&self) -> ClientBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClientBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for ClientBuilder

Source§

fn default() -> ClientBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more