Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

An MCP client instance.

Clients are built using ClientBuilder and can connect to servers via various transports (stdio subprocess, SSE, WebSocket).

Implementations§

Source§

impl Client

Source

pub fn stdio(command: &str, args: &[&str]) -> McpResult<Self>

Creates a client connecting to a subprocess via stdio.

§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 start or initialization fails.

Source

pub fn stdio_with_cx(command: &str, args: &[&str], cx: Cx) -> McpResult<Self>

Creates a client with a provided Cx for cancellation support.

Source

pub fn builder() -> ClientBuilder

Creates a new client builder.

Source

pub fn ensure_initialized(&mut self) -> McpResult<()>

Ensures the client is initialized.

In auto-initialize mode, this performs the initialization handshake on first call. In normal mode, this is a no-op since the client is already initialized.

Since this method takes &mut self, Rust’s borrowing rules guarantee exclusive access, so no additional synchronization is needed.

§Errors

Returns an error if initialization fails.

Source

pub fn is_initialized(&self) -> bool

Returns whether the client has been initialized.

Source

pub fn server_info(&self) -> &ServerInfo

Returns the server info after initialization.

Source

pub fn server_capabilities(&self) -> &ServerCapabilities

Returns the server capabilities after initialization.

Source

pub fn protocol_version(&self) -> &str

Returns the protocol version negotiated during initialization.

Source

pub fn cancel_request( &mut self, request_id: impl Into<RequestId>, reason: Option<String>, await_cleanup: bool, ) -> McpResult<()>

Sends a cancellation notification for a previously issued request.

Set await_cleanup to request that the server wait for any cleanup before acknowledging completion (best-effort; server-dependent).

§Errors

Returns an error if the notification cannot be sent.

Source

pub fn list_tools(&mut self) -> McpResult<Vec<Tool>>

Lists available tools.

§Errors

Returns an error if the request fails.

Source

pub fn call_tool( &mut self, name: &str, arguments: Value, ) -> McpResult<Vec<Content>>

Calls a tool with the given arguments.

§Errors

Returns an error if the tool call fails.

Source

pub fn call_tool_with_progress( &mut self, name: &str, arguments: Value, on_progress: ProgressCallback<'_>, ) -> McpResult<Vec<Content>>

Calls a tool with progress callback support.

This method allows you to receive progress notifications during tool execution. The callback is invoked for each progress notification received from the server.

§Arguments
  • name - The tool name to call
  • arguments - The tool arguments as JSON
  • on_progress - Callback invoked for each progress notification
§Errors

Returns an error if the tool call fails.

Source

pub fn list_resources(&mut self) -> McpResult<Vec<Resource>>

Lists available resources.

§Errors

Returns an error if the request fails.

Source

pub fn list_resource_templates(&mut self) -> McpResult<Vec<ResourceTemplate>>

Lists available resource templates.

§Errors

Returns an error if the request fails.

Source

pub fn set_log_level(&mut self, level: LogLevel) -> McpResult<()>

Sets the server log level (if supported).

§Errors

Returns an error if the request fails.

Source

pub fn read_resource(&mut self, uri: &str) -> McpResult<Vec<ResourceContent>>

Reads a resource by URI.

§Errors

Returns an error if the resource cannot be read.

Source

pub fn list_prompts(&mut self) -> McpResult<Vec<Prompt>>

Lists available prompts.

§Errors

Returns an error if the request fails.

Source

pub fn get_prompt( &mut self, name: &str, arguments: HashMap<String, String>, ) -> McpResult<Vec<PromptMessage>>

Gets a prompt with the given arguments.

§Errors

Returns an error if the prompt cannot be retrieved.

Source

pub fn submit_task( &mut self, task_type: &str, input: Value, ) -> McpResult<TaskInfo>

Submits a background task for execution.

§Arguments
  • task_type - The type of task to execute (e.g., “data_export”, “batch_process”)
  • input - Task parameters as JSON
§Errors

Returns an error if the server doesn’t support tasks or the request fails.

Source

pub fn list_tasks( &mut self, status: Option<TaskStatus>, cursor: Option<&str>, limit: Option<u32>, ) -> McpResult<ListTasksResult>

Lists tasks with optional status filter.

§Arguments
  • status - Optional filter by task status
  • cursor - Optional pagination cursor from previous response
§Errors

Returns an error if the server doesn’t support tasks or the request fails.

Source

pub fn list_tasks_all( &mut self, status: Option<TaskStatus>, ) -> McpResult<Vec<TaskInfo>>

Lists all tasks by following pagination cursors until exhaustion.

§Errors

Returns an error if any request fails.

Source

pub fn get_task(&mut self, task_id: &str) -> McpResult<GetTaskResult>

Gets detailed information about a specific task.

§Arguments
  • task_id - The task ID to retrieve
§Errors

Returns an error if the task is not found or the request fails.

Source

pub fn cancel_task(&mut self, task_id: &str) -> McpResult<TaskInfo>

Cancels a running or pending task.

§Arguments
  • task_id - The task ID to cancel
§Errors

Returns an error if the task cannot be cancelled or is already complete.

Source

pub fn cancel_task_with_reason( &mut self, task_id: &str, reason: Option<&str>, ) -> McpResult<TaskInfo>

Cancels a running or pending task with an optional reason.

§Arguments
  • task_id - The task ID to cancel
  • reason - Optional reason for the cancellation
§Errors

Returns an error if the task cannot be cancelled or is already complete.

Source

pub fn wait_for_task( &mut self, task_id: &str, poll_interval: Duration, ) -> McpResult<TaskResult>

Waits for a task to complete by polling.

This method polls the server at the specified interval until the task reaches a terminal state (completed, failed, or cancelled).

§Arguments
  • task_id - The task ID to wait for
  • poll_interval - Duration between poll requests
§Errors

Returns an error if the task fails, is cancelled, or the request fails.

Source

pub fn wait_for_task_with_progress<F>( &mut self, task_id: &str, poll_interval: Duration, on_progress: F, ) -> McpResult<TaskResult>
where F: FnMut(f64, Option<&str>),

Waits for a task with progress callback.

Similar to wait_for_task but also provides progress information via callback.

§Arguments
  • task_id - The task ID to wait for
  • poll_interval - Duration between poll requests
  • on_progress - Callback invoked with progress updates
§Errors

Returns an error if the task fails, is cancelled, or the request fails.

Source

pub fn close(self)

Closes the client connection.

Trait Implementations§

Source§

impl Drop for Client

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> 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, 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