Skip to main content

Client

Struct Client 

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

The MCP client struct, managing transport, requests, and responses. This client is suitable for connecting to an MCP-compliant server to send requests, receive responses, and handle notifications.

Implementations§

Source§

impl Client

Source

pub fn new(transport: Arc<dyn Transport>) -> Self

Creates a new MCP client with the given transport. This does not perform initialization. You typically call client.initialize(...) next.

Source

pub async fn initialize( &self, implementation: Implementation, capabilities: ClientCapabilities, ) -> Result<InitializeResult, Error>

Initializes the client by sending an “initialize” request containing:

  • client implementation info
  • client capabilities
  • protocol version

On success, updates the client’s server_capabilities field and sends an initialized notification to the server.

Source

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

Sends a request to the server with the given method and optional parameters, then waits up to 30 seconds for a matching response.

§Errors

Returns an error if the transport fails, the server returns an error, or no response is received within 30 seconds.

Source

pub async fn notify( &self, method: &str, params: Option<Value>, ) -> Result<(), Error>

Sends a notification to the server using the given method and optional parameters. Notifications do not expect a response from the server.

Source

pub async fn capabilities(&self) -> Option<ServerCapabilities>

Returns the cached server capabilities if the client has already initialized.

Source

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

Shuts down the client by closing the transport. This does not send a server shutdown request.

Source

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

Lists available tools on the server by calling tools/list.

Source

pub async fn call_tool( &self, name: &str, arguments: Value, ) -> Result<CallToolResult, Error>

Calls a tool on the server by name, passing the specified arguments as JSON. If the returned CallToolResult has is_error set to true, this method converts it into an Error::Other.

Source

pub async fn get_tool(&self, name: &str) -> Result<Option<Tool>, Error>

Retrieves a single tool from the server by name, returning Some(tool) if found, or None otherwise.

Source

pub async fn read_resource( &self, uri: &str, ) -> Result<ReadResourceResult, Error>

Reads a resource by URI from the server, calling resources/read.

Source

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

Lists resources by calling resources/list on the server.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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