Client implementation for the MCP SDK.
This crate provides the client-side implementation for the Model Context Protocol. It includes a fluent client API, server discovery, and connection management.
Overview
The MCP client allows AI applications to:
- Connect to MCP servers via various transports
- Discover and invoke tools
- Read resources
- Get prompts
- Track long-running tasks
Example
use mcpkit_client::{Client, ClientBuilder};
use mcpkit_transport::SpawnedTransport;
#[tokio::main]
async fn main() -> Result<(), mcpkit_core::error::McpError> {
// Spawn an MCP server as a subprocess and connect via stdio
let transport = SpawnedTransport::spawn("my-mcp-server", &[] as &[&str]).await?;
let client = ClientBuilder::new()
.name("my-client")
.version("1.0.0")
.build(transport)
.await?;
// List available tools
let tools = client.list_tools().await?;
for tool in &tools {
println!("Tool: {}", tool.name);
}
// Call a tool
let result = client.call_tool("add", serde_json::json!({
"a": 1,
"b": 2
})).await?;
Ok(())
}
Client Handler
For handling server-initiated requests (sampling, elicitation), implement
the [ClientHandler] trait:
use ClientHandler;
use ;
use McpError;
;