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
impl Client
Sourcepub fn stdio_with_cx(
command: &str,
args: &[&str],
cx: Cx,
) -> Result<Client, McpError>
pub fn stdio_with_cx( command: &str, args: &[&str], cx: Cx, ) -> Result<Client, McpError>
Creates a client with a provided Cx for cancellation support.
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Creates a new client builder.
Sourcepub fn ensure_initialized(&mut self) -> Result<(), McpError>
pub fn ensure_initialized(&mut self) -> Result<(), McpError>
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.
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Returns whether the client has been initialized.
Sourcepub fn server_info(&self) -> &ServerInfo
pub fn server_info(&self) -> &ServerInfo
Returns the server info after initialization.
Sourcepub fn server_capabilities(&self) -> &ServerCapabilities
pub fn server_capabilities(&self) -> &ServerCapabilities
Returns the server capabilities after initialization.
Sourcepub fn protocol_version(&self) -> &str
pub fn protocol_version(&self) -> &str
Returns the protocol version negotiated during initialization.
Sourcepub fn cancel_request(
&mut self,
request_id: impl Into<RequestId>,
reason: Option<String>,
await_cleanup: bool,
) -> Result<(), McpError>
pub fn cancel_request( &mut self, request_id: impl Into<RequestId>, reason: Option<String>, await_cleanup: bool, ) -> Result<(), McpError>
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.
Sourcepub fn call_tool(
&mut self,
name: &str,
arguments: Value,
) -> Result<Vec<Content>, McpError>
pub fn call_tool( &mut self, name: &str, arguments: Value, ) -> Result<Vec<Content>, McpError>
Sourcepub fn call_tool_with_progress(
&mut self,
name: &str,
arguments: Value,
on_progress: &mut dyn FnMut(f64, Option<f64>, Option<&str>),
) -> Result<Vec<Content>, McpError>
pub fn call_tool_with_progress( &mut self, name: &str, arguments: Value, on_progress: &mut dyn FnMut(f64, Option<f64>, Option<&str>), ) -> Result<Vec<Content>, McpError>
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 callarguments- The tool arguments as JSONon_progress- Callback invoked for each progress notification
§Errors
Returns an error if the tool call fails.
Sourcepub fn list_resource_templates(
&mut self,
) -> Result<Vec<ResourceTemplate>, McpError>
pub fn list_resource_templates( &mut self, ) -> Result<Vec<ResourceTemplate>, McpError>
Sourcepub fn read_resource(
&mut self,
uri: &str,
) -> Result<Vec<ResourceContent>, McpError>
pub fn read_resource( &mut self, uri: &str, ) -> Result<Vec<ResourceContent>, McpError>
Sourcepub fn get_prompt(
&mut self,
name: &str,
arguments: HashMap<String, String>,
) -> Result<Vec<PromptMessage>, McpError>
pub fn get_prompt( &mut self, name: &str, arguments: HashMap<String, String>, ) -> Result<Vec<PromptMessage>, McpError>
Sourcepub fn list_tasks(
&mut self,
status: Option<TaskStatus>,
cursor: Option<&str>,
limit: Option<u32>,
) -> Result<ListTasksResult, McpError>
pub fn list_tasks( &mut self, status: Option<TaskStatus>, cursor: Option<&str>, limit: Option<u32>, ) -> Result<ListTasksResult, McpError>
Sourcepub fn list_tasks_all(
&mut self,
status: Option<TaskStatus>,
) -> Result<Vec<TaskInfo>, McpError>
pub fn list_tasks_all( &mut self, status: Option<TaskStatus>, ) -> Result<Vec<TaskInfo>, McpError>
Lists all tasks by following pagination cursors until exhaustion.
§Errors
Returns an error if any request fails.
Sourcepub fn cancel_task_with_reason(
&mut self,
task_id: &str,
reason: Option<&str>,
) -> Result<TaskInfo, McpError>
pub fn cancel_task_with_reason( &mut self, task_id: &str, reason: Option<&str>, ) -> Result<TaskInfo, McpError>
Sourcepub fn wait_for_task(
&mut self,
task_id: &str,
poll_interval: Duration,
) -> Result<TaskResult, McpError>
pub fn wait_for_task( &mut self, task_id: &str, poll_interval: Duration, ) -> Result<TaskResult, McpError>
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 forpoll_interval- Duration between poll requests
§Errors
Returns an error if the task fails, is cancelled, or the request fails.
Sourcepub fn wait_for_task_with_progress<F>(
&mut self,
task_id: &str,
poll_interval: Duration,
on_progress: F,
) -> Result<TaskResult, McpError>
pub fn wait_for_task_with_progress<F>( &mut self, task_id: &str, poll_interval: Duration, on_progress: F, ) -> Result<TaskResult, McpError>
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 forpoll_interval- Duration between poll requestson_progress- Callback invoked with progress updates
§Errors
Returns an error if the task fails, is cancelled, or the request fails.
Trait Implementations§
Source§impl ProxyBackend for Client
impl ProxyBackend for Client
Source§fn list_resource_templates(&mut self) -> Result<Vec<ResourceTemplate>, McpError>
fn list_resource_templates(&mut self) -> Result<Vec<ResourceTemplate>, McpError>
Source§fn call_tool(
&mut self,
name: &str,
arguments: Value,
) -> Result<Vec<Content>, McpError>
fn call_tool( &mut self, name: &str, arguments: Value, ) -> Result<Vec<Content>, McpError>
Source§fn call_tool_with_progress(
&mut self,
name: &str,
arguments: Value,
on_progress: &mut dyn FnMut(f64, Option<f64>, Option<String>),
) -> Result<Vec<Content>, McpError>
fn call_tool_with_progress( &mut self, name: &str, arguments: Value, on_progress: &mut dyn FnMut(f64, Option<f64>, Option<String>), ) -> Result<Vec<Content>, McpError>
Source§fn read_resource(&mut self, uri: &str) -> Result<Vec<ResourceContent>, McpError>
fn read_resource(&mut self, uri: &str) -> Result<Vec<ResourceContent>, McpError>
Auto Trait Implementations§
impl !Freeze for Client
impl !RefUnwindSafe for Client
impl !UnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§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: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more