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
use crate::{
protocol::{Protocol, ProtocolBuilder, RequestOptions},
transport::{Transport, TransportError},
types::{
ClientCapabilities, Implementation, InitializeRequest, InitializeResponse,
LATEST_PROTOCOL_VERSION,
},
};
use tracing::debug;
type Result<T> = std::result::Result<T, TransportError>;
/// A client for interacting with an MCP server.
#[derive(Clone)]
pub struct Client<T: Transport> {
protocol: Protocol<T>,
}
impl<T: Transport> Client<T> {
/// Creates a new `ClientBuilder` with the given transport.
pub fn builder(transport: T) -> ClientBuilder<T> {
ClientBuilder::new(transport)
}
/// Initializes the client with the server.
///
/// This method sends an initialization request to the server and processes the response.
/// It also sends a notification that the client has been initialized.
///
/// # Arguments
///
/// * `client_info` - Information about the client implementation.
///
/// # Returns
///
/// Returns the server's response to the initialization request.
pub async fn initialize(&self, client_info: Implementation) -> Result<InitializeResponse> {
let request = InitializeRequest {
protocol_version: LATEST_PROTOCOL_VERSION.to_string(),
capabilities: ClientCapabilities::default(),
client_info,
};
let response = self
.request(
"initialize",
Some(serde_json::to_value(request).map_err(TransportError::Json)?),
RequestOptions::default(),
)
.await?;
let response: InitializeResponse = serde_json::from_value(response)
.map_err(TransportError::Json)?;
if response.protocol_version != LATEST_PROTOCOL_VERSION {
return Err(TransportError::new(
crate::transport::TransportErrorCode::InvalidMessage,
format!("Unsupported protocol version: {}", response.protocol_version),
));
}
debug!(
"Initialized with protocol version: {}",
response.protocol_version
);
self.protocol
.notify("notifications/initialized", None)
.await?;
Ok(response)
}
/// Sends a request to the server.
///
/// # Arguments
///
/// * `method` - The method name for the request.
/// * `params` - Optional parameters for the request.
/// * `options` - Request options.
///
/// # Returns
///
/// Returns the server's response as a JSON value.
pub async fn request(
&self,
method: &str,
params: Option<serde_json::Value>,
options: RequestOptions,
) -> Result<serde_json::Value> {
let response = self.protocol.request(method, params, options).await?;
response.result.ok_or_else(|| {
TransportError::new(
crate::transport::TransportErrorCode::InvalidMessage,
format!("Request failed: {:?}", response.error),
)
})
}
/// Starts listening for messages from the server.
///
/// This method should be called to begin processing incoming messages.
pub async fn start(&self) -> Result<()> {
self.protocol.listen().await
}
}
/// A builder for creating `Client` instances.
pub struct ClientBuilder<T: Transport> {
protocol: ProtocolBuilder<T>,
}
impl<T: Transport> ClientBuilder<T> {
/// Creates a new `ClientBuilder` with the given transport.
pub fn new(transport: T) -> Self {
Self {
protocol: ProtocolBuilder::new(transport),
}
}
/// Builds and returns a new `Client` instance.
pub fn build(self) -> Client<T> {
Client {
protocol: self.protocol.build(),
}
}
}