pub struct McpClient { /* private fields */ }Expand description
Manages a single MCP server process with stdio JSON-RPC communication.
I/O handles are stored persistently so that concurrent requests can be serialized through the same connection without consuming the handles.
§Example
let client = McpClient::new(server_config);
client.initialize().await?;
let tools = client.list_tools().await?;
let result = client.call_tool("my_tool", serde_json::json!({"arg": "value"})).await?;
client.shutdown().await?;Implementations§
Source§impl McpClient
impl McpClient
Sourcepub fn new(server: McpServer) -> McpClient
pub fn new(server: McpServer) -> McpClient
Create a new MCP client for the given server configuration.
Does NOT spawn the process yet — call initialize() to start and negotiate.
Sourcepub fn with_timeout(self, timeout: Duration) -> McpClient
pub fn with_timeout(self, timeout: Duration) -> McpClient
Set the request timeout duration.
Sourcepub async fn initialize(&self) -> Result<(), Error>
pub async fn initialize(&self) -> Result<(), Error>
Spawn the MCP server process and establish communication.
Sourcepub async fn is_initialized(&self) -> bool
pub async fn is_initialized(&self) -> bool
Check if the server has been initialized
Sourcepub async fn server_info(&self) -> Option<ServerInfo>
pub async fn server_info(&self) -> Option<ServerInfo>
Get the server info received during initialize
Sourcepub async fn list_tools(&self) -> Result<Vec<McpTool>, Error>
pub async fn list_tools(&self) -> Result<Vec<McpTool>, Error>
List all tools available from this MCP server.
Results are cached and refreshed on refresh_tools.
Sourcepub async fn refresh_tools(&self) -> Result<Vec<McpTool>, Error>
pub async fn refresh_tools(&self) -> Result<Vec<McpTool>, Error>
Force-refresh the tool list from the server.
Sourcepub async fn call_tool(
&self,
tool_name: &str,
arguments: Value,
) -> Result<McpToolCallResult, Error>
pub async fn call_tool( &self, tool_name: &str, arguments: Value, ) -> Result<McpToolCallResult, Error>
Call a tool on this MCP server.
The server must be initialized first.
Sourcepub async fn call_tool_text(
&self,
tool_name: &str,
arguments: Value,
) -> Result<String, Error>
pub async fn call_tool_text( &self, tool_name: &str, arguments: Value, ) -> Result<String, Error>
Call a tool and return the result content as a string.
Returns the first text content block, or an error if no text content.
Sourcepub async fn shutdown(&self) -> Result<(), Error>
pub async fn shutdown(&self) -> Result<(), Error>
Gracefully shutdown the MCP server process.
Drops persistent I/O handles first, then kills the child process.