Expand description
§MCP Client Library
Production-ready Rust client for Model Context Protocol (MCP) servers.
Connect to MCP servers with full protocol compliance, multiple transport options, and automatic session management. Supports both synchronous and streaming operations with comprehensive error handling and recovery mechanisms.
§Features
- Multi-transport: HTTP, Server-Sent Events (SSE), WebSocket (planned), stdio (planned)
- Full Protocol: Complete MCP 2025-06-18 specification support
- High Performance: Built on Tokio with async/await throughout
- Session Management: Automatic connection handling and recovery
- Real-time Streaming: SSE support for progress and notifications
- Error Handling: Comprehensive error types with automatic retry
- Configurable: Timeouts, retries, connection pooling
§Installation
[dependencies]
turul-mcp-client = "0.2"
tokio = { version = "1.0", features = ["full"] }§Quick Start
use turul_mcp_client::{McpClient, McpClientBuilder, transport::HttpTransport};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let transport = HttpTransport::new("http://localhost:8080/mcp")?;
let client = McpClientBuilder::new()
.with_transport(Box::new(transport))
.build();
client.connect().await?;
let tools = client.list_tools().await?;
println!("Available tools: {:?}", tools);
Ok(())
}§Transport Types
§HTTP Transport (Streamable HTTP)
use turul_mcp_client::transport::HttpTransport;
let transport = HttpTransport::new("http://localhost:8080/mcp")?;§SSE Transport (HTTP+SSE)
use turul_mcp_client::transport::SseTransport;
let transport = SseTransport::new("http://localhost:8080/mcp")?;§SSE Transport (Real-time)
use turul_mcp_client::transport::SseTransport;
let transport = SseTransport::new("http://localhost:8080/mcp")?;§Future Transports
WebSocket and Stdio transports are planned for future releases:
// Coming soon:
// WebSocketTransport::new("ws://localhost:8080/mcp")
// StdioTransport::new("./mcp-server-executable")§Common Operations
§Tool Execution
// List available tools
let tools = client.list_tools().await?;
println!("Available tools: {:?}", tools);
// Execute a tool
let result = client.call_tool("calculator", serde_json::json!({
"operation": "add",
"a": 5,
"b": 3
})).await?;
println!("Result: {:?}", result);§Resource Access
// List available resources
let resources = client.list_resources().await?;
// Read a specific resource
let content = client.read_resource("file://config.json").await?;
println!("Resource content: {:?}", content);§Prompt Templates
// List available prompts
let prompts = client.list_prompts().await?;
// Get a prompt with arguments
let prompt = client.get_prompt("code_review", Some(serde_json::json!({
"language": "rust",
"code": "fn main() { println!(\"Hello!\"); }"
}))).await?;§Configuration
The client supports extensive configuration:
// Create a client with default configuration
let client = McpClientBuilder::new()
.build();§Real-time Streaming
For real-time notifications and progress updates:
// Get available tools from server
let tools = client.list_tools().await?;
println!("Available tools: {}", tools.len());§Examples
Complete examples available at: github.com/aussierobots/turul-mcp-framework/tree/main/examples
- Basic Client - Simple tool execution
- Streaming Client - Real-time notifications
- HTTP Client - Production HTTP integration
- Retry Logic - Error handling and recovery
- Monitoring - Connection health and metrics
§Related Crates
turul-mcp-server- Build MCP serversturul-mcp-protocol- Protocol typesturul-mcp-derive- Macros for tools/resources
Re-exports§
pub use client::McpClient;pub use client::McpClientBuilder;pub use config::ClientConfig;pub use config::RetryConfig;pub use config::TimeoutConfig;pub use error::McpClientError;pub use error::McpClientResult;pub use session::SessionInfo;pub use session::SessionManager;pub use session::SessionState;pub use transport::Transport;pub use transport::TransportType;
Modules§
- client
- Main MCP client implementation
- completion
- Core MCP protocol types and message structures MCP Completion Protocol Types
- config
- Configuration types for MCP client
- content
- Core MCP protocol types and message structures Content types for MCP 2025-06-18 specification
- elicitation
- Core MCP protocol types and message structures MCP Elicitation Protocol Types
- error
- Error types for MCP client operations
- initialize
- Core MCP protocol types and message structures MCP Initialize Protocol Types
- json_
rpc - Core MCP protocol types and message structures JSON-RPC 2.0 Implementation for MCP 2025-06-18
- logging
- Core MCP protocol types and message structures MCP Logging Protocol Types
- meta
- Core MCP protocol types and message structures _meta Field Support for MCP 2025-06-18
- notifications
- Core MCP protocol types and message structures MCP Notifications Protocol Types
- param_
extraction - Core MCP protocol types and message structures Parameter extraction utilities for MCP protocol
- ping
- Core MCP protocol types and message structures MCP Ping Protocol Types
- prelude
- MCP Client Prelude
- prompts
- Core MCP protocol types and message structures MCP Prompts Protocol Types
- resources
- Core MCP protocol types and message structures MCP Resources Protocol Types
- roots
- Core MCP protocol types and message structures MCP Roots Protocol Types
- sampling
- Core MCP protocol types and message structures MCP Sampling Protocol Types
- schema
- Core MCP protocol types and message structures JSON Schema Support for MCP
- session
- Session management for MCP client
- streaming
- Streaming support for MCP client
- tools
- Core MCP protocol types and message structures MCP Tools Protocol Types
- traits
- Core MCP protocol types and message structures Traits for JSON-RPC types as per MCP specification (2025-06-18)
- transport
- Transport layer for MCP client
- version
- Core MCP protocol types and message structures MCP Protocol Version Support
Macros§
- client_
error - Convenience macro for creating generic errors
- impl_
serde_ extractor - Core MCP protocol types and message structures Macro to implement SerdeParamExtractor for any Params type that implements Deserialize
Structs§
- Annotations
- Core MCP protocol types and message structures Generic annotations structure (matches TypeScript Annotations) Used across all MCP types that support client annotations
- Blob
Resource Contents - Core MCP protocol types and message structures Binary resource contents (matches TypeScript BlobResourceContents exactly)
- Call
Tool Request - Core MCP protocol types and message structures Complete tools/call request (matches TypeScript CallToolRequest interface)
- Call
Tool Result - Core MCP protocol types and message structures Result for tools/call (per MCP spec)
- Cancelled
Notification - Core MCP protocol types and message structures Method: “notifications/cancelled”
- Client
Capabilities - Core MCP protocol types and message structures Capabilities that a client may support
- Elicit
Create Params - Core MCP protocol types and message structures Parameters for elicitation/create request (per MCP spec)
- Elicit
Create Request - Core MCP protocol types and message structures Complete elicitation/create request (matches TypeScript ElicitRequest interface)
- Elicit
Result - Core MCP protocol types and message structures The client’s response to an elicitation request (per MCP spec)
- Elicitation
Builder - Core MCP protocol types and message structures Builder for creating common elicitation patterns
- Elicitation
Schema - Core MCP protocol types and message structures Restricted schema for elicitation (only primitive types, no nesting) - per MCP spec
- Empty
Params - Core MCP protocol types and message structures
- Empty
Result - Core MCP protocol types and message structures Empty result for successful operations (per MCP spec)
- GetPrompt
Request - Core MCP protocol types and message structures Complete prompts/get request (matches TypeScript GetPromptRequest interface)
- GetPrompt
Result - Core MCP protocol types and message structures Result for prompts/get (per MCP spec)
- Implementation
- Core MCP protocol types and message structures Describes the name and version of an MCP implementation
- Initialize
Request - Core MCP protocol types and message structures Parameters for initialize request
- Initialize
Result - Core MCP protocol types and message structures Result payload for initialize (per MCP spec)
- Initialized
Notification - Core MCP protocol types and message structures Method: “notifications/initialized”
- Json
RpcError - Core MCP protocol types and message structures JSON-RPC 2.0 error object
- Json
RpcNotification - Core MCP protocol types and message structures A JSON-RPC 2.0 notification (no response expected)
- Json
RpcRequest - Core MCP protocol types and message structures A standard JSON-RPC 2.0 request
- Json
RpcResponse - Core MCP protocol types and message structures A standard JSON-RPC 2.0 response
- List
Prompts Request - Core MCP protocol types and message structures Complete prompts/list request (matches TypeScript ListPromptsRequest interface)
- List
Prompts Result - Core MCP protocol types and message structures Result for prompts/list (per MCP spec)
- List
Resources Request - Core MCP protocol types and message structures Complete resources/list request (matches TypeScript ListResourcesRequest interface)
- List
Resources Result - Core MCP protocol types and message structures Result for resources/list (per MCP spec)
- List
Tools Request - Core MCP protocol types and message structures Complete tools/list request (matches TypeScript ListToolsRequest interface)
- List
Tools Result - Core MCP protocol types and message structures Result for tools/list (per MCP spec) - extends PaginatedResult
- Logging
Message Notification - Core MCP protocol types and message structures Method: “notifications/message”
- Logging
Message Notification Params - Core MCP protocol types and message structures
- Meta
- Core MCP protocol types and message structures Structured _meta field for MCP 2025-06-18
- Meta
Cursor - Core MCP protocol types and message structures Cursor for pagination support
- Notification
- Core MCP protocol types and message structures Base notification structure following MCP TypeScript specification
- Notification
Params - Core MCP protocol types and message structures Base notification parameters that can include _meta
- Paginated
Response - Core MCP protocol types and message structures Helper for pagination responses
- Ping
Request - Core MCP protocol types and message structures Request for ping (per MCP spec)
- Progress
Notification - Core MCP protocol types and message structures Method: “notifications/progress”
- Progress
Notification Params - Core MCP protocol types and message structures
- Progress
Response - Core MCP protocol types and message structures Helper for progress responses
- Progress
Token - Core MCP protocol types and message structures Progress token for tracking long-running operations
- Prompt
- Core MCP protocol types and message structures A prompt descriptor (matches TypeScript Prompt interface exactly)
- Prompt
Argument - Core MCP protocol types and message structures Argument definition for prompts (extends BaseMetadata per MCP spec)
- Prompt
List Changed Notification - Core MCP protocol types and message structures Method: “notifications/prompts/listChanged” (per MCP spec)
- Prompt
Message - Core MCP protocol types and message structures Message content for prompts (matches MCP PromptMessage interface exactly)
- Read
Resource Request - Core MCP protocol types and message structures Complete resources/read request (matches TypeScript ReadResourceRequest interface)
- Read
Resource Result - Core MCP protocol types and message structures Result for resources/read (per MCP spec)
- Request
Params - Core MCP protocol types and message structures
JSON-RPC
paramsobject with optional_metaand method-specific arguments - Resource
- Core MCP protocol types and message structures A resource descriptor (matches TypeScript Resource interface) Resource extends BaseMetadata, so it includes name and title fields
- Resource
List Changed Notification - Core MCP protocol types and message structures Method: “notifications/resources/listChanged” (per MCP spec)
- Resource
Reference - Core MCP protocol types and message structures Resource reference for resource links (matches TypeScript Resource interface)
- Resource
Subscription - Core MCP protocol types and message structures Resource subscription parameters
- Resource
Updated Notification - Core MCP protocol types and message structures Method: “notifications/resources/updated”
- Resource
Updated Notification Params - Core MCP protocol types and message structures
- Result
With Meta - Core MCP protocol types and message structures A generic result wrapper that combines data with optional _meta information
- Roots
List Changed Notification - Core MCP protocol types and message structures Method: “notifications/roots/listChanged” (per MCP spec)
- Server
Capabilities - Core MCP protocol types and message structures Capabilities that a server may support
- Subscribe
Request - Core MCP protocol types and message structures Complete resources/subscribe request (per MCP spec)
- Text
Resource Contents - Core MCP protocol types and message structures Text resource contents (matches TypeScript TextResourceContents exactly)
- Tool
- Core MCP protocol types and message structures Tool definition
- Tool
List Changed Notification - Core MCP protocol types and message structures Method: “notifications/tools/listChanged” (per MCP spec)
- Tool
Schema - Core MCP protocol types and message structures JSON Schema definition for tool input/output (matches TypeScript spec exactly) Must be an object with type: “object”, properties, and required fields
- Unsubscribe
Request - Core MCP protocol types and message structures Complete resources/unsubscribe request (per MCP spec)
Enums§
- Content
Block - Core MCP protocol types and message structures Content block union type matching MCP 2025-06-18 specification exactly
- Elicit
Action - Core MCP protocol types and message structures User action in response to elicitation
- Json
RpcMessage - Core MCP protocol types and message structures Unified JSON-RPC message type
- Json
Schema - Core MCP protocol types and message structures A JSON Schema definition
- Legacy
Request Params - Core MCP protocol types and message structures Parameters for a JSON-RPC request
- McpError
- Core MCP protocol types and message structures MCP-specific errors
- McpVersion
- Core MCP protocol types and message structures Supported MCP protocol versions
- Primitive
Schema Definition - Core MCP protocol types and message structures Restricted schema definitions that only allow primitive types without nested objects or arrays (per MCP spec).
- Request
Id - Core MCP protocol types and message structures A uniquely identifying ID for a JSON-RPC request. Can be a string or a number, but never null.
- Resource
Content - Core MCP protocol types and message structures Union type for resource contents (matches TypeScript union)
- Resource
Contents - Core MCP protocol types and message structures Resource contents union type (matches TypeScript TextResourceContents | BlobResourceContents)
- Response
Result - Core MCP protocol types and message structures Result data for a JSON-RPC response
- String
Format - Core MCP protocol types and message structures String format constraints
Constants§
- CURRENT_
VERSION - Core MCP protocol types and message structures The current MCP protocol version implemented by this crate
- MCP_
VERSION - Core MCP protocol types and message structures The MCP protocol version implemented by this crate
Traits§
- HasData
- Core MCP protocol types and message structures
- HasData
Param - Core MCP protocol types and message structures
- HasMeta
- Core MCP protocol types and message structures
- HasMeta
Param - Core MCP protocol types and message structures
- HasProgress
Token Param - Core MCP protocol types and message structures
- Json
RpcNotification Trait - Core MCP protocol types and message structures
- Json
RpcRequest Trait - Core MCP protocol types and message structures
- Json
RpcResponse Trait - Core MCP protocol types and message structures
- Params
- Core MCP protocol types and message structures
- RpcResult
- Core MCP protocol types and message structures
- With
Meta - Core MCP protocol types and message structures Trait for types that can include _meta fields
Type Aliases§
- McpResult
- Core MCP protocol types and message structures Common result type for MCP operations
- Tool
Result - Core MCP protocol types and message structures Tool result type - an alias for ContentBlock to maintain backward compatibility while ensuring MCP 2025-06-18 specification compliance