lifecycle_client/
lifecycle_client.rs1use mcprotocol_rs::{
2 transport::{ClientTransportFactory, TransportConfig, TransportType},
3 ClientCapabilities, ImplementationInfo, Message, Method, Notification, Request, RequestId,
4 Result, PROTOCOL_VERSION,
5};
6use serde_json::json;
7use std::{collections::HashSet, env};
8use tokio;
9
10#[tokio::main]
11async fn main() -> Result<()> {
12 let mut session_ids = HashSet::new();
15
16 let server_path = env::current_dir()?.join("target/debug/examples/lifecycle_server");
19
20 let config = TransportConfig {
23 transport_type: TransportType::Stdio {
24 server_path: Some(server_path.to_str().unwrap().to_string()),
25 server_args: None,
26 },
27 parameters: None,
28 };
29
30 let factory = ClientTransportFactory;
33 let mut client = factory.create(config)?;
34
35 eprintln!("Client starting...");
36
37 client.initialize().await?;
40
41 let init_request = Request::new(
44 Method::Initialize,
45 Some(json!({
46 "protocolVersion": PROTOCOL_VERSION,
47 "capabilities": ClientCapabilities {
48 roots: None,
49 sampling: None,
50 experimental: None,
51 },
52 "clientInfo": ImplementationInfo {
53 name: "Example Client".to_string(),
54 version: "1.0.0".to_string(),
55 }
56 })),
57 RequestId::Number(1),
58 );
59
60 if !init_request.validate_id_uniqueness(&mut session_ids) {
63 eprintln!("Request ID has already been used in this session");
64 return Ok(());
65 }
66
67 eprintln!("Sending initialize request...");
68 client.send(Message::Request(init_request)).await?;
69
70 match client.receive().await {
73 Ok(message) => {
74 match message {
75 Message::Response(response) => {
76 if response.error.is_some() {
77 eprintln!("Initialization failed: {:?}", response.error);
78 return Ok(());
79 }
80
81 if let Some(result) = response.result {
82 let server_version = result
85 .get("protocolVersion")
86 .and_then(|v| v.as_str())
87 .unwrap_or("unknown");
88
89 if server_version != PROTOCOL_VERSION {
90 eprintln!(
91 "Protocol version mismatch: expected {}, got {}",
92 PROTOCOL_VERSION, server_version
93 );
94 return Ok(());
95 }
96
97 eprintln!("Server initialized with version: {}", server_version);
98
99 let init_notification = Notification::new(Method::Initialized, None);
102 client
103 .send(Message::Notification(init_notification))
104 .await?;
105 eprintln!("Sent initialized notification");
106
107 tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
110
111 eprintln!("Sending shutdown request...");
114 let shutdown_request =
115 Request::new(Method::Shutdown, None, RequestId::Number(2));
116
117 if !shutdown_request.validate_id_uniqueness(&mut session_ids) {
120 eprintln!("Request ID has already been used in this session");
121 return Ok(());
122 }
123
124 client.send(Message::Request(shutdown_request)).await?;
125
126 match client.receive().await {
129 Ok(message) => {
130 match message {
131 Message::Response(response) => {
132 if response.error.is_some() {
133 eprintln!("Shutdown failed: {:?}", response.error);
134 return Ok(());
135 }
136
137 eprintln!("Sending exit notification...");
140 let exit_notification =
141 Notification::new(Method::Exit, None);
142 client
143 .send(Message::Notification(exit_notification))
144 .await?;
145 }
146 _ => eprintln!("Unexpected response type"),
147 }
148 }
149 Err(e) => {
150 eprintln!("Error receiving response: {}", e);
151 return Ok(());
152 }
153 }
154 }
155 }
156 _ => eprintln!("Unexpected message type"),
157 }
158 }
159 Err(e) => {
160 eprintln!("Error receiving response: {}", e);
161 return Ok(());
162 }
163 }
164
165 client.close().await?;
168 eprintln!("Client stopped");
169 Ok(())
170}