TurboMCP Client
Robust MCP client implementation with intelligent connection management, automatic error recovery, and comprehensive capability negotiation.
Overview
turbomcp-client
provides a production-ready client for Model Context Protocol servers. It handles all client-side concerns including connection management, request correlation, error recovery, capability negotiation, and session lifecycle management across all transport protocols.
Key Features
🔌 Multi-Transport Connection Management
- All transport protocols - STDIO, HTTP/SSE, WebSocket, TCP, Unix sockets
- Connection pooling - Efficient connection reuse and management
- Health monitoring - Automatic connection health checks and recovery
- Load balancing - Multiple server connection with failover support
🔄 Intelligent Error Recovery
- Auto-retry with backoff - Configurable retry logic with exponential backoff
- Circuit breaker integration - Prevents cascade failures with automatic recovery
- Graceful degradation - Fallback mechanisms when servers are unavailable
- Error classification - Smart handling of temporary vs permanent failures
📞 Request Correlation & Management
- Automatic ID generation - UUID-based request correlation
- Request/response matching - Efficient correlation with timeout handling
- Concurrent requests - Multiple outstanding requests with proper ordering
- Request cancellation - Proper cleanup of cancelled or timed-out requests
🤝 Capability Negotiation
- Server discovery - Automatic server capability detection
- Feature matching - Client/server capability compatibility checking
- Version negotiation - Protocol version compatibility handling
- Extension support - Custom capability extensions and fallbacks
📊 Session Lifecycle Management
- Connection state tracking - Proper session initialization and cleanup
- Heartbeat monitoring - Keep-alive and connection validation
- Reconnection logic - Intelligent reconnection with state preservation
- Session persistence - Optional session state persistence across connections
Architecture
┌─────────────────────────────────────────────┐
│ TurboMCP Client │
├─────────────────────────────────────────────┤
│ Connection Management │
│ ├── Multi-transport support │
│ ├── Connection pooling │
│ ├── Health monitoring │
│ └── Load balancing │
├─────────────────────────────────────────────┤
│ Request Processing │
│ ├── ID generation and correlation │
│ ├── Concurrent request handling │
│ ├── Response timeout management │
│ └── Request cancellation │
├─────────────────────────────────────────────┤
│ Error Recovery & Resilience │
│ ├── Exponential backoff retry │
│ ├── Circuit breaker pattern │
│ ├── Graceful degradation │
│ └── Error classification │
├─────────────────────────────────────────────┤
│ Capability & Session Management │
│ ├── Server capability discovery │
│ ├── Protocol negotiation │
│ ├── Session initialization │
│ └── State synchronization │
└─────────────────────────────────────────────┘
Client Builder
Basic Client Setup
use ;
// Simple STDIO client
let client = new
.transport
.connect.await?;
// Query available tools
let tools = client.list_tools.await?;
for tool in tools
Production Client Configuration
use ;
let client = new
.name
.version
// Multi-transport with failover
.transport
.fallback_transport
// Connection management
.connection_pool
// Error recovery
.retry_config
.circuit_breaker
// Health monitoring
.health_checks
.connect.await?;
Transport Configuration
STDIO Transport
For local process communication:
use ;
// Direct STDIO connection
let transport = stdio;
// Child process management
let transport = stdio_with_command;
let client = new
.transport
.connect.await?;
HTTP/SSE Transport
For web-based servers:
use ;
let transport = http
.with_config;
let client = new
.transport
.connect.await?;
WebSocket Transport
For real-time communication:
use ;
let transport = websocket
.with_config;
let client = new
.transport
.connect.await?;
TCP Transport
For network socket communication:
use ;
let transport = tcp
.with_config; // 64KB
let client = new
.transport
.connect.await?;
Tool Interaction
Listing and Calling Tools
use Client;
// List available tools
let tools = client.list_tools.await?;
println!;
for tool in &tools
// Call a specific tool
let result = client.call_tool.await?;
println!;
Concurrent Tool Calls
use try_join;
// Execute multiple tools concurrently
let = try_join!?;
println!;
println!;
println!;
Resource Management
Reading Resources
// List available resources
let resources = client.list_resources.await?;
for resource in &resources
// Read specific resources
let file_content = client.read_resource.await?;
println!;
// Read multiple resources concurrently
let contents = client.read_resources_concurrent.await?;
Resource Subscriptions
use StreamExt;
// Subscribe to resource updates
let mut updates = client.subscribe_to_resource.await?;
while let Some = updates.next.await
Error Handling & Recovery
Retry Configuration
use ;
let retry_config = new
.max_attempts
.strategy
.retryable_errors;
let client = new
.retry_config
.transport
.connect.await?;
Circuit Breaker
use ;
let circuit_config = new
.failure_threshold
.recovery_timeout
.half_open_max_calls
.success_threshold;
let client = new
.circuit_breaker
.transport
.connect.await?;
Error Classification
use ;
match client.call_tool.await
Capability Negotiation
Client Capabilities
use ;
let capabilities = ClientCapabilities ;
let client = new
.capabilities
.transport
.connect.await?;
// Check negotiated capabilities
let server_capabilities = client.server_capabilities.await?;
if server_capabilities.tools.is_some
if server_capabilities.resources.is_some
Capability-Aware Operations
// Check capabilities before making requests
if client.supports_tool_calls.await? else
if client.supports_resource_subscriptions.await? else
Session Management
Connection Lifecycle
use ;
// Monitor connection state
client.on_state_change;
// Graceful shutdown
ctrl_c.await?;
println!;
client.shutdown.await?;
Session Persistence
use ;
let session_store = file;
let session_config = new
.persist_session
.session_timeout // 1 hour
.heartbeat_interval;
let client = new
.session_store
.session_config
.transport
.connect.await?;
// Session is automatically restored on reconnection
Integration Examples
With TurboMCP Framework
Client functionality integrates seamlessly with server-side code:
use *;
Standalone Client Application
use ;
async
Feature Flags
Feature | Description | Default |
---|---|---|
http |
Enable HTTP/SSE transport | ✅ |
websocket |
Enable WebSocket transport | ✅ |
tcp |
Enable TCP transport | ✅ |
unix |
Enable Unix socket transport | ✅ |
tls |
Enable TLS/SSL support | ✅ |
compression |
Enable compression support | ✅ |
session-persistence |
Enable session state persistence | ❌ |
metrics |
Enable client-side metrics | ✅ |
Performance Characteristics
Benchmarks
Operation | Latency (avg) | Throughput | Memory Usage |
---|---|---|---|
Tool Call (STDIO) | 2ms | 25k req/s | 5MB |
Tool Call (HTTP) | 10ms | 10k req/s | 8MB |
Tool Call (WebSocket) | 5ms | 15k req/s | 6MB |
Resource Read | 3ms | 20k req/s | 4MB |
Concurrent Requests (10) | 8ms | 12k req/s | 12MB |
Optimization Features
- 🚀 Connection Pooling - Reuse connections for better performance
- 📦 Request Pipelining - Multiple concurrent requests per connection
- 🗜️ Compression - Automatic request/response compression
- ⚡ Caching - Smart caching of capabilities and resource metadata
Development
Building
# Build with all features
# Build specific transports only
# Build minimal client (STDIO only)
Testing
# Run client tests
# Test with different transports
# Integration tests with real servers
# Test error recovery and circuit breaker
Related Crates
- turbomcp - Main framework (uses this crate)
- turbomcp-core - Core types and utilities
- turbomcp-transport - Transport layer
- turbomcp-protocol - MCP protocol implementation
External Resources
- MCP Client Specification - Official client implementation guidelines
- Circuit Breaker Pattern - Fault tolerance pattern
- Connection Pooling - Connection management patterns
License
Licensed under the MIT License.
Part of the TurboMCP high-performance Rust SDK for the Model Context Protocol.