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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! Model Context Protocol (MCP) Client Implementation
//!
//! This crate provides a comprehensive client implementation for the Model Context Protocol (MCP),
//! enabling AI assistants to connect to and interact with external tools and resources through
//! standardized server interfaces.
//!
//! # Overview
//!
//! The MCP client supports multiple transport protocols and provides automatic tool discovery,
//! registration, and execution. It seamlessly integrates with tool calling systems,
//! allowing MCP tools to be used alongside built-in tools.
//!
//! # Features
//!
//! - **Multiple Transport Protocols**: HTTP, WebSocket, and Process-based connections
//! - **Automatic Tool Discovery**: Discovers and registers tools from connected MCP servers
//! - **Bearer Token Authentication**: Supports authentication for secured MCP servers
//! - **Concurrent Tool Execution**: Handles multiple tool calls efficiently
//! - **Resource Access**: Access to MCP server resources like files and data
//! - **Tool Naming Prefix**: Avoid conflicts with customizable tool name prefixes
//!
//! # Transport Protocols
//!
//! ## HTTP Transport
//!
//! For MCP servers accessible via HTTP endpoints with JSON-RPC over HTTP.
//! Supports both regular JSON responses and Server-Sent Events (SSE).
//!
//! ## WebSocket Transport
//!
//! For real-time bidirectional communication with MCP servers over WebSocket.
//! Ideal for interactive applications requiring low-latency tool calls.
//!
//! ## Process Transport
//!
//! For local MCP servers running as separate processes, communicating via stdin/stdout
//! using JSON-RPC messages.
//!
//! # Example Usage
//!
//! ## Simple Configuration
//!
//! ```rust,no_run
//! use mistralrs_mcp::{McpClientConfig, McpServerConfig, McpServerSource, McpClient};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Simple configuration with minimal settings
//! // Most fields use sensible defaults (enabled=true, UUID for id/prefix, no timeouts)
//! let config = McpClientConfig {
//! servers: vec![
//! McpServerConfig {
//! name: "Hugging Face MCP Server".to_string(),
//! source: McpServerSource::Http {
//! url: "https://hf.co/mcp".to_string(),
//! timeout_secs: None,
//! headers: None,
//! },
//! bearer_token: Some("hf_xxx".to_string()),
//! ..Default::default()
//! },
//! ],
//! ..Default::default()
//! };
//!
//! // Initialize MCP client
//! let mut client = McpClient::new(config);
//! client.initialize().await?;
//!
//! // Get tool callbacks for integration with model builder
//! let tool_callbacks = client.get_tool_callbacks_with_tools();
//! println!("Registered {} MCP tools", tool_callbacks.len());
//!
//! Ok(())
//! }
//! ```
//!
//! ## Advanced Configuration
//!
//! ```rust,no_run
//! use mistralrs_mcp::{McpClientConfig, McpServerConfig, McpServerSource, McpClient};
//! use std::collections::HashMap;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Configure MCP client with multiple servers and custom settings
//! let config = McpClientConfig {
//! servers: vec![
//! // HTTP server with Bearer token
//! McpServerConfig {
//! id: "web_search".to_string(),
//! name: "Web Search MCP".to_string(),
//! source: McpServerSource::Http {
//! url: "https://api.example.com/mcp".to_string(),
//! timeout_secs: Some(30),
//! headers: None,
//! },
//! enabled: true,
//! tool_prefix: Some("web".to_string()),
//! resources: None,
//! bearer_token: Some("your-api-token".to_string()),
//! },
//! // WebSocket server
//! McpServerConfig {
//! id: "realtime_data".to_string(),
//! name: "Real-time Data MCP".to_string(),
//! source: McpServerSource::WebSocket {
//! url: "wss://realtime.example.com/mcp".to_string(),
//! timeout_secs: Some(60),
//! headers: None,
//! },
//! enabled: true,
//! tool_prefix: Some("rt".to_string()),
//! resources: None,
//! bearer_token: Some("ws-token".to_string()),
//! },
//! // Process-based server
//! McpServerConfig {
//! id: "filesystem".to_string(),
//! name: "Filesystem MCP".to_string(),
//! source: McpServerSource::Process {
//! command: "mcp-server-filesystem".to_string(),
//! args: vec!["--root".to_string(), "/tmp".to_string()],
//! work_dir: None,
//! env: None,
//! },
//! enabled: true,
//! tool_prefix: Some("fs".to_string()),
//! resources: Some(vec!["file://**".to_string()]),
//! bearer_token: None,
//! },
//! ],
//! auto_register_tools: true,
//! tool_timeout_secs: Some(30),
//! max_concurrent_calls: Some(5),
//! };
//!
//! // Initialize MCP client
//! let mut client = McpClient::new(config);
//! client.initialize().await?;
//!
//! // Get tool callbacks for integration with model builder
//! let tool_callbacks = client.get_tool_callbacks_with_tools();
//! println!("Registered {} MCP tools", tool_callbacks.len());
//!
//! Ok(())
//! }
//! ```
pub use ;
pub use ;
pub use McpToolResult;
pub use rust_mcp_schema;
use ;
use HashMap;
use Uuid;
/// Supported MCP server transport sources
///
/// Defines the different ways to connect to MCP servers, each optimized for
/// specific use cases and deployment scenarios.
/// Configuration for MCP client integration
///
/// This structure defines how the MCP client should connect to and manage
/// multiple MCP servers, including authentication, tool registration, and
/// execution policies.
/// Configuration for an individual MCP server
///
/// Defines connection parameters, authentication, and tool management
/// settings for a single MCP server instance.
/// Information about a tool discovered from an MCP server