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
impl Client
Sourcepub fn new(transport: Arc<dyn Transport>) -> Self
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.
Sourcepub async fn initialize(
&self,
implementation: Implementation,
capabilities: ClientCapabilities,
) -> Result<InitializeResult, Error>
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.
Sourcepub async fn request(
&self,
method: &str,
params: Option<Value>,
) -> Result<Value, Error>
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.
Sourcepub async fn notify(
&self,
method: &str,
params: Option<Value>,
) -> Result<(), Error>
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.
Sourcepub async fn capabilities(&self) -> Option<ServerCapabilities>
pub async fn capabilities(&self) -> Option<ServerCapabilities>
Returns the cached server capabilities if the client has already initialized.
Sourcepub async fn shutdown(&self) -> Result<(), Error>
pub async fn shutdown(&self) -> Result<(), Error>
Shuts down the client by closing the transport. This does not send a server shutdown request.
Sourcepub async fn list_tools(&self) -> Result<ListToolsResult, Error>
pub async fn list_tools(&self) -> Result<ListToolsResult, Error>
Lists available tools on the server by calling tools/list.
Sourcepub async fn call_tool(
&self,
name: &str,
arguments: Value,
) -> Result<CallToolResult, Error>
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.
Sourcepub async fn get_tool(&self, name: &str) -> Result<Option<Tool>, Error>
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.
Sourcepub async fn read_resource(
&self,
uri: &str,
) -> Result<ReadResourceResult, Error>
pub async fn read_resource( &self, uri: &str, ) -> Result<ReadResourceResult, Error>
Reads a resource by URI from the server, calling resources/read.
Sourcepub async fn list_resources(&self) -> Result<ListResourcesResult, Error>
pub async fn list_resources(&self) -> Result<ListResourcesResult, Error>
Lists resources by calling resources/list on the server.