1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! # 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.
//!
//! [](https://crates.io/crates/turul-mcp-client)
//! [](https://docs.rs/turul-mcp-client)
//! [](https://github.com/aussierobots/turul-mcp-framework/blob/main/LICENSE)
//!
//! ## Features
//!
//! - **Multi-transport**: HTTP and Server-Sent Events (SSE), with stdio planned
//! - **Bilingual Protocol**: Speaks both MCP 2026-07-28 (stateless core) and
//! 2025-11-25; by default the client negotiates the spec per connection
//! - **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
//!
//! ```toml
//! [dependencies]
//! turul-mcp-client = "0.4"
//! tokio = { version = "1.0", features = ["full"] }
//! ```
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! 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)
//!
//! The Streamable HTTP transport sends each MCP request as an independent HTTP
//! POST. There is no persistent connection. The client negotiates the spec per
//! connection: on a 2026-07-28 connection it is stateless (no session id; each
//! request carries `_meta` and the `MCP-Protocol-Version: 2026-07-28` header). On
//! a connection locked to 2025-11-25, session continuity is maintained via the
//! `Mcp-Session-Id` header the server returns during initialization and the client
//! includes on subsequent requests.
//!
//! `transport.connect()` only marks the transport as logically ready; it performs
//! no network I/O. The first real validation happens when `McpClient::connect()`
//! probes `server/discover`. On a 2026-07-28 server discovery answers statelessly;
//! on a 2025-locked connection the client falls back to the `initialize` POST
//! followed by `notifications/initialized`. If the server is unreachable or rejects
//! the probe, the error surfaces there.
//!
//! ```rust,no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use turul_mcp_client::transport::HttpTransport;
//!
//! let transport = HttpTransport::new("http://localhost:8080/mcp")?;
//! # Ok(())
//! # }
//! ```
//!
//! ### SSE Transport (HTTP+SSE, deprecated — SEP-2596)
//!
//! For servers using the pre-2025-03-26 SSE-based protocol. The HTTP+SSE
//! transport is deprecated upstream ("new implementations SHOULD NOT adopt
//! it") — use [`HttpTransport`](transport::HttpTransport) unless you must
//! talk to an unmigrated ≤ 2024-11-05 server. Like the HTTP transport,
//! `connect()` is a no-op marker — the SSE subscription is established
//! lazily during message exchange.
//!
//! ```rust,no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #![allow(deprecated)]
//! use turul_mcp_client::transport::SseTransport;
//!
//! let transport = SseTransport::new("http://localhost:8080/mcp")?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Future Transports
//!
//! Stdio transports are planned for future releases:
//!
//! ```text
//! // Coming soon:
//! // StdioTransport::new("./mcp-server-executable")
//! ```
//!
//! ## Common Operations
//!
//! ### Tool Execution
//!
//! ```rust,no_run
//! # use turul_mcp_client::prelude::*;
//! # async fn example(client: &McpClient) -> Result<(), Box<dyn std::error::Error>> {
//! // 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);
//! # Ok(())
//! # }
//! ```
//!
//! ### Resource Access
//!
//! ```rust,no_run
//! # use turul_mcp_client::prelude::*;
//! # async fn example(client: &McpClient) -> Result<(), Box<dyn std::error::Error>> {
//! // List available resources
//! let resources = client.list_resources().await?;
//!
//! // Discover dynamic URI templates
//! let templates = client.list_resource_templates().await?;
//!
//! // Read a specific resource
//! let content = client.read_resource("file://config.json").await?;
//! println!("Resource content: {:?}", content);
//! # Ok(())
//! # }
//! ```
//!
//! ### Prompt Templates
//!
//! ```rust,no_run
//! # use turul_mcp_client::prelude::*;
//! # async fn example(client: &McpClient) -> Result<(), Box<dyn std::error::Error>> {
//! // 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?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Configuration
//!
//! The client supports extensive configuration:
//!
//! ```rust,no_run
//! # use turul_mcp_client::prelude::*;
//!
//! // Create a client with default configuration
//! let client = McpClientBuilder::new()
//! .build();
//! ```
//!
//! ## Real-time Streaming
//!
//! For real-time notifications and progress updates:
//!
//! ```rust,no_run
//! # use turul_mcp_client::prelude::*;
//! # async fn example(client: &McpClient) -> Result<(), Box<dyn std::error::Error>> {
//! // Get available tools from server
//! let tools = client.list_tools().await?;
//! println!("Available tools: {}", tools.len());
//! # Ok(())
//! # }
//! ```
//!
//! ## Examples
//!
//! **Complete examples available at:**
//! [github.com/aussierobots/turul-mcp-framework/tree/main/examples](https://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`](https://crates.io/crates/turul-mcp-server) - Build MCP servers
//! - [`turul-mcp-protocol`](https://crates.io/crates/turul-mcp-protocol) - Protocol types
//! - [`turul-mcp-derive`](https://crates.io/crates/turul-mcp-derive) - Macros for tools/resources
// Spec-coexistence features are mutually exclusive. Bilingual (default) links
// both protocol crates; narrowing to one requires --no-default-features.
compile_error!;
compile_error!;
pub
// Re-export main types
pub use ToolCallOutcome;
/// High-level MCP client with session management and automatic reconnection
pub use ;
/// Client configuration types for timeouts, retries, and connection parameters
pub use ;
/// Client-specific error types and result aliases for error handling
pub use ;
/// Session management types for tracking connection state and statistics
pub use ;
/// Per-connection MCP wire-version negotiation
pub use McpVersion;
// Re-export transport types
/// Transport layer abstractions for different MCP connection types
pub use ;
// Re-export protocol types for convenience
/// Core MCP protocol types and message structures
pub use *;