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>
impl<T: Transport> Client<T>
Sourcepub fn new(transport: T) -> Self
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);
Sourcepub fn with_capabilities(transport: T, capabilities: ClientCapabilities) -> Self
pub fn with_capabilities(transport: T, capabilities: ClientCapabilities) -> Self
Create a new client with custom capabilities
§Arguments
transport
- The transport implementation to usecapabilities
- 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);
Sourcepub async fn initialize(&mut self) -> Result<InitializeResult>
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);
Sourcepub async fn list_tools(&mut self) -> Result<Vec<String>>
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);
}
Sourcepub async fn call_tool(
&mut self,
name: &str,
arguments: Option<HashMap<String, Value>>,
) -> Result<Value>
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 callarguments
- 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);
Sourcepub async fn list_resources(&mut self) -> Result<Vec<String>>
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);
}