1use crate::{
2 protocol::{Protocol, ProtocolBuilder, RequestOptions},
3 transport::Transport,
4 types::{
5 ClientCapabilities, Implementation, InitializeRequest, InitializeResponse,
6 LATEST_PROTOCOL_VERSION,
7 },
8};
9
10use anyhow::Result;
11use tracing::debug;
12
13#[derive(Clone)]
14pub struct Client<T: Transport> {
15 protocol: Protocol<T>,
16}
17
18impl<T: Transport> Client<T> {
19 pub fn builder(transport: T) -> ClientBuilder<T> {
20 ClientBuilder::new(transport)
21 }
22
23 pub async fn initialize(&self, client_info: Implementation) -> Result<InitializeResponse> {
24 let request = InitializeRequest {
25 protocol_version: LATEST_PROTOCOL_VERSION.to_string(),
26 capabilities: ClientCapabilities::default(),
27 client_info,
28 };
29 let response = self
30 .request(
31 "initialize",
32 Some(serde_json::to_value(request)?),
33 RequestOptions::default(),
34 )
35 .await?;
36 let response: InitializeResponse = serde_json::from_value(response)
37 .map_err(|e| anyhow::anyhow!("Failed to parse response: {}", e))?;
38
39 if response.protocol_version != LATEST_PROTOCOL_VERSION {
40 return Err(anyhow::anyhow!(
41 "Unsupported protocol version: {}",
42 response.protocol_version
43 ));
44 }
45
46 debug!(
47 "Initialized with protocol version: {}",
48 response.protocol_version
49 );
50 self.protocol
51 .notify("notifications/initialized", None)
52 .await?;
53 Ok(response)
54 }
55
56 pub async fn request(
57 &self,
58 method: &str,
59 params: Option<serde_json::Value>,
60 options: RequestOptions,
61 ) -> Result<serde_json::Value> {
62 let response = self.protocol.request(method, params, options).await?;
63 response
64 .result
65 .ok_or_else(|| anyhow::anyhow!("Request failed: {:?}", response.error))
66 }
67
68 pub async fn start(&self) -> Result<()> {
69 self.protocol.listen().await
70 }
71}
72
73pub struct ClientBuilder<T: Transport> {
74 protocol: ProtocolBuilder<T>,
75}
76
77impl<T: Transport> ClientBuilder<T> {
78 pub fn new(transport: T) -> Self {
79 Self {
80 protocol: ProtocolBuilder::new(transport),
81 }
82 }
83
84 pub fn build(self) -> Client<T> {
85 Client {
86 protocol: self.protocol.build(),
87 }
88 }
89}