TurboMCP Client
MCP client with complete MCP 2025-06-18 specification support and plugin middleware system.
Table of Contents
- Overview
- Supported Transports
- Quick Start
- Transport Configuration
- Advanced Features
- Plugin Middleware
- Sampling Handler Integration
- Handler Registration
- Error Handling
- Production Deployment
Overview
turbomcp-client provides a comprehensive MCP client implementation with:
- ✅ Full MCP 2025-06-18 compliance - All server and client features
- ✅ Bidirectional communication - Server-initiated requests (sampling, elicitation)
- ✅ Plugin middleware - Extensible request/response processing
- ✅ Sampling protocol support - Handle server-initiated sampling requests
- ✅ Transport agnostic - Works with STDIO, TCP, Unix, WebSocket transports
- ✅ Thread-safe sharing - Client is cheaply cloneable via Arc for concurrent async tasks
Supported Transports
| Transport | Status | Feature Flag | Use Case |
|---|---|---|---|
| STDIO | ✅ Full | default | Local process communication |
| HTTP/SSE | ✅ Client | http |
HTTP/SSE client transport |
| TCP | ✅ Full | tcp |
Network socket communication |
| Unix | ✅ Full | unix |
Fast local IPC |
| WebSocket | ✅ Full | websocket |
Real-time bidirectional |
Version 2.1.0: HTTP/SSE client transport with
Client::connect_http()convenience API, OAuth 2.1 support, and universal proxy compatibility.
Quick Start
Basic Client (STDIO)
use Client;
use StdioTransport;
async
HTTP Client (One-Liner)
use Client;
async
TCP/Unix Clients
// TCP
let client = connect_tcp.await?;
// Unix socket
let client = connect_unix.await?;
With ClientBuilder
use ClientBuilder;
use StdioTransport;
let client = new
.with_tools
.with_prompts
.with_resources
.with_sampling
.build
.await?;
Cloning Client for Concurrent Usage
use Client;
use StdioTransport;
// Create client (cheaply cloneable via Arc)
let client = new;
// Initialize once
client.initialize.await?;
// Clone for multiple async tasks - this is cheap (just Arc clone)
let client1 = client.clone;
let client2 = client.clone;
let handle1 = spawn;
let handle2 = spawn;
let = try_join!?;
Transport Configuration
STDIO Transport (Default)
use StdioTransport;
// Direct STDIO
let transport = new;
let mut client = new;
HTTP Transport (New in 2.0!)
use Client;
// One-liner - connects and initializes automatically
let client = connect_http.await?;
Or with custom configuration:
use Client;
use Duration;
let client = connect_http_with.await?;
TCP Transport
use Client;
// One-liner - connects and initializes automatically
let client = connect_tcp.await?;
Or using transport directly:
use TcpTransport;
use SocketAddr;
let server_addr: SocketAddr = "127.0.0.1:8765".parse?;
let bind_addr: SocketAddr = "0.0.0.0:0".parse?; // Any available port
let transport = new_client;
let mut client = new;
client.initialize.await?;
Unix Socket Transport
use Client;
// One-liner - connects and initializes automatically
let client = connect_unix.await?;
Or using transport directly:
use UnixTransport;
use PathBuf;
let transport = new_client;
let mut client = new;
client.initialize.await?;
WebSocket Transport
use ;
let config = WebSocketBidirectionalConfig ;
let transport = new.await?;
let mut client = new;
Advanced Features
Robust Transport with Retry & Circuit Breaker
use ClientBuilder;
use StdioTransport;
// Configure retry and health checking
let client = new
.with_max_retries // Configure retry attempts
.with_retry_delay // Retry delay in milliseconds
.with_keepalive // Keepalive interval
.build
.await?;
Additional Configuration Options
use ClientBuilder;
use StdioTransport;
use Duration;
let client = new
// Configure capabilities needed from the server
.with_tools
.with_prompts
.with_resources
// Configure timeouts and retries
.with_timeout // 30 second timeout
.with_max_retries // Retry up to 3 times
.with_retry_delay // 100ms delay between retries
.with_keepalive // 30 second keepalive
// Build the client
.build
.await?;
Plugin Middleware
use ClientBuilder;
use ;
use Arc;
let client = new
.with_plugin
.build
.await?;
Sampling Handler Integration
Handle server-initiated sampling requests by implementing a custom sampling handler:
use SamplingHandler;
use ;
use async_trait;
use Arc;
// Register the handler
let handler = new;
client.set_sampling_handler;
Note: TurboMCP provides the sampling protocol infrastructure. You implement your own LLM integration (OpenAI SDK, Anthropic SDK, local models, etc.) as needed for your use case.
Handler Registration
use ;
use async_trait;
use Arc;
;
let client = new
.with_elicitation_handler
.build
.await?;
MCP Operations
Tools
// List available tools
let tools = client.list_tools.await?;
for tool in &tools
// List tool names only
let names = client.list_tool_names.await?;
// Call a tool
use HashMap;
let mut args = new;
args.insert;
let result = client.call_tool.await?;
Prompts
use PromptInput;
// List prompts
let prompts = client.list_prompts.await?;
// Get prompt with arguments
let prompt_args = PromptInput ;
let result = client.get_prompt.await?;
println!;
for message in result.messages
Resources
// List resources
let resources = client.list_resources.await?;
// Read a resource
let content = client.read_resource.await?;
// List resource templates
let templates = client.list_resource_templates.await?;
Completions
use CompletionContext;
// Complete a prompt argument
let completions = client.complete_prompt.await?;
for value in completions.completion.values
// Complete with context
let mut context_args = new;
context_args.insert;
let context = CompletionContext ;
let completions = client.complete_prompt.await?;
Subscriptions
use LogLevel;
// Subscribe to resource updates
client.subscribe.await?;
// Set logging level
client.set_log_level.await?;
// Unsubscribe
client.unsubscribe.await?;
Health Monitoring
// Send ping to check connection
let ping_result = client.ping.await?;
println!;
Bidirectional Communication
Processing Server-Initiated Requests
use Client;
use StdioTransport;
// Create and initialize client
let client = new;
client.initialize.await?;
// Message processing is automatic! The MessageDispatcher runs in the background.
// No need for manual message loops - just use the client directly.
// Perform operations - bidirectional communication works automatically
let tools = client.list_tools.await?;
Error Handling
use Error;
match client.call_tool.await
Examples
See the examples/ directory for complete working examples:
sampling_client.rs- Client with server-initiated sampling protocolelicitation_interactive_client.rs- Interactive elicitation handling
Run examples:
Feature Flags
| Feature | Description | Status |
|---|---|---|
default |
STDIO transport only | ✅ |
tcp |
TCP transport | ✅ |
unix |
Unix socket transport | ✅ |
websocket |
WebSocket transport | ✅ |
http |
HTTP/SSE client transport | ✅ |
Enable features in Cargo.toml:
[]
= { = "2.0.4", = ["tcp", "websocket"] }
Architecture
┌─────────────────────────────────────────────┐
│ Application Code │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ Client API (Clone-able) │
│ ├── initialize(), list_tools(), etc. │
│ ├── Handler Registry (elicitation, etc.) │
│ └── Plugin Registry (metrics, etc.) │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ Protocol Layer (JSON-RPC) │
│ ├── Request/Response correlation │
│ ├── Bidirectional message routing │
│ └── Capability negotiation │
└─────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────┐
│ Transport Layer │
│ ├── STDIO, TCP, Unix, WebSocket │
│ ├── RobustTransport (retry, circuit) │
│ └── Connection management │
└─────────────────────────────────────────────┘
Development
Building
# Build with default features (STDIO only)
# Build with all transport features
# Build with robustness features
Testing
# Run unit tests
# Run with specific features
# Run examples
Related Crates
- turbomcp - Main framework with server macros
- turbomcp-protocol - Protocol types and core utilities
- turbomcp-transport - Transport implementations
Resources
- MCP Specification - Official protocol docs
- MCP 2025-06-18 Spec - Current version
- TurboMCP Documentation - Framework docs
Roadmap
Version 2.0.4 Features
- HTTP/SSE Client Transport - Client-side HTTP/SSE with
Client::connect_http() - Convenience Constructors - One-liner client creation for all transports
- Ergonomic Config Builders - Simplified configuration APIs
Planned Features
- Connection Pool Management - Multi-server connection pooling
- Session Persistence - Automatic state preservation across reconnects
- Roots Handler - Complete filesystem roots implementation
- Progress Reporting - Client-side progress emission
- Batch Requests - Send multiple requests in single message
License
Licensed under the MIT License.
Part of the TurboMCP Rust SDK for the Model Context Protocol.