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.