Client

Struct Client 

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

MCP client for communicating with servers

The Client struct provides a beautiful, ergonomic interface for interacting with MCP servers. It handles all protocol complexity internally, exposing only clean, type-safe methods.

§Type Parameters

  • T - The transport implementation used for communication

§Examples

use turbomcp_client::Client;
use turbomcp_transport::stdio::StdioTransport;

let transport = StdioTransport::new();
let mut client = Client::new(transport);

// Initialize and start using the client
client.initialize().await?;

Implementations§

Source§

impl<T: Transport> Client<T>

Source

pub fn new(transport: T) -> Self

Create a new client with the specified transport

Creates a new MCP client instance with default capabilities. The client must be initialized before use by calling initialize().

§Arguments
  • transport - The transport implementation to use for communication
§Examples
use turbomcp_client::Client;
use turbomcp_transport::stdio::StdioTransport;

let transport = StdioTransport::new();
let client = Client::new(transport);
Source

pub fn with_capabilities(transport: T, capabilities: ClientCapabilities) -> Self

Create a new client with custom capabilities

§Arguments
  • transport - The transport implementation to use
  • capabilities - The client capabilities to negotiate
§Examples
use turbomcp_client::{Client, ClientCapabilities};
use turbomcp_transport::stdio::StdioTransport;

let capabilities = ClientCapabilities {
    tools: true,
    prompts: true,
    resources: false,
    sampling: false,
};

let transport = StdioTransport::new();
let client = Client::with_capabilities(transport, capabilities);
Source

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

Initialize the connection with the MCP server

Performs the initialization handshake with the server, negotiating capabilities and establishing the protocol version. This method must be called before any other operations can be performed.

§Returns

Returns an InitializeResult containing server information and negotiated capabilities.

§Errors

Returns an error if:

  • The transport connection fails
  • The server rejects the initialization request
  • Protocol negotiation fails
§Examples
let mut client = Client::new(StdioTransport::new());

let result = client.initialize().await?;
println!("Server: {} v{}", result.server_info.name, result.server_info.version);
Source

pub async fn list_tools(&mut self) -> Result<Vec<String>>

List available tools from the server

Retrieves the list of tools that the server provides. Tools are functions that can be called to perform specific operations on the server.

§Returns

Returns a vector of tool names available on the server.

§Errors

Returns an error if:

  • The client is not initialized
  • The server doesn’t support tools
  • The request fails
§Examples
let mut client = Client::new(StdioTransport::new());
client.initialize().await?;

let tools = client.list_tools().await?;
for tool in tools {
    println!("Available tool: {}", tool);
}
Source

pub async fn call_tool( &mut self, name: &str, arguments: Option<HashMap<String, Value>>, ) -> Result<Value>

Call a tool on the server

Executes a tool on the server with the provided arguments.

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

Returns the result of the tool execution.

§Examples
let mut client = Client::new(StdioTransport::new());
client.initialize().await?;

let mut args = HashMap::new();
args.insert("input".to_string(), serde_json::json!("test"));

let result = client.call_tool("my_tool", Some(args)).await?;
println!("Tool result: {:?}", result);
Source

pub async fn list_resources(&mut self) -> Result<Vec<String>>

List available resources from the server

§Examples
let mut client = Client::new(StdioTransport::new());
client.initialize().await?;

let resources = client.list_resources().await?;
for resource in resources {
    println!("Available resource: {}", resource);
}

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Client<T>

§

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

§

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>
where T: UnwindSafe,

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 = 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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