Struct Client

Source
pub struct Client<T: Transport> { /* private fields */ }
Expand description

An MCP client for connecting to MCP servers and invoking their tools.

The Client provides a high-level API for interacting with MCP servers, including initialization, tool discovery, and tool invocation.

Implementations§

Source§

impl<T: Transport> Client<T>

Source

pub fn builder(transport: T) -> ClientBuilder<T>

Creates a new client builder.

§Arguments
  • transport - The transport to use for communication with the server
§Returns

A ClientBuilder for configuring and building the client

Source

pub fn set_protocol_version(self, protocol_version: ProtocolVersion) -> Self

Sets the protocol version for the client.

§Arguments
  • protocol_version - The protocol version to use
§Returns

The modified client instance

Source

pub async fn open(&self) -> Result<()>

Opens the transport connection.

§Returns

A Result indicating success or failure

Source

pub async fn initialize(&self) -> Result<InitializeResponse>

Initializes the connection with the MCP server.

This sends the initialize request to the server, negotiates protocol version and capabilities, and establishes the session.

§Returns

A Result containing the server’s initialization response if successful

Source

pub async fn assert_initialized(&self) -> Result<(), Error>

Checks if the client has been initialized.

§Returns

A Result indicating if the client is initialized

Source

pub async fn request( &self, method: &str, params: Option<Value>, options: RequestOptions, ) -> Result<Value>

Sends a request to the server.

§Arguments
  • method - The method name
  • params - Optional parameters for the request
  • options - Request options (like timeout)
§Returns

A Result containing the server’s response if successful

Source

pub async fn list_tools( &self, cursor: Option<String>, request_options: Option<RequestOptions>, ) -> Result<ToolsListResponse>

Lists tools available on the server.

§Arguments
  • cursor - Optional pagination cursor
  • request_options - Optional request options
§Returns

A Result containing the list of tools if successful

Source

pub async fn call_tool( &self, name: &str, arguements: Option<Value>, ) -> Result<CallToolResponse>

Calls a tool on the server.

§Arguments
  • name - The name of the tool to call
  • arguments - Optional arguments for the tool
§Returns

A Result containing the tool’s response if successful

Source

pub async fn list_resources( &self, cursor: Option<String>, request_options: Option<RequestOptions>, ) -> Result<ResourcesListResponse>

Lists resources available on the server.

§Arguments
  • cursor - Optional pagination cursor
  • request_options - Optional request options
§Returns

A Result containing the list of resources if successful

Source

pub async fn read_resource(&self, uri: Url) -> Result<Resource>

Reads a resource from the server.

§Arguments
  • uri - The URI of the resource to read
§Returns

A Result containing the resource if successful

Source

pub async fn subscribe_to_resource(&self, uri: Url) -> Result<()>

Source

pub async fn unsubscribe_to_resource(&self, uri: Url) -> Result<()>

Trait Implementations§

Source§

impl<T: Clone + Transport> Clone for Client<T>

Source§

fn clone(&self) -> Client<T>

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

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Client<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Client<T>

§

impl<T> Send for Client<T>

§

impl<T> Sync for Client<T>

§

impl<T> Unpin for Client<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Client<T>

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: 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> 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T