Skip to content

Commit 0c13e6b

Browse files
committed
refactor: use loops for logging and progress notifications in conformance server
Address PR review comments by refactoring repetitive code blocks: - Fix test_tool_with_logging: wrap in proper match arm and use loop over messages - Refactor test_tool_with_progress: replace 3 identical if-let blocks with a loop over [(0.0, "Starting"), (50.0, "Halfway"), (100.0, "Complete")]
1 parent e851122 commit 0c13e6b

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

conformance/src/bin/server.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,29 @@ impl ServerHandler for ConformanceServer {
289289
meta: None,
290290
}),
291291

292-
for msg in ["Tool execution started", "Tool processing data", "Tool execution completed"] {
293-
let _ = cx.peer.notify_logging_message(LoggingMessageNotificationParam {
294-
level: LoggingLevel::Info,
295-
logger: Some("conformance-server".into()),
296-
data: json!(msg),
297-
}).await;
298-
tokio::time::sleep(Duration::from_millis(50)).await;
292+
"test_tool_with_logging" => {
293+
for msg in [
294+
"Tool execution started",
295+
"Tool processing data",
296+
"Tool execution completed",
297+
] {
298+
let _ = cx
299+
.peer
300+
.notify_logging_message(LoggingMessageNotificationParam {
301+
level: LoggingLevel::Info,
302+
logger: Some("conformance-server".into()),
303+
data: json!(msg),
304+
})
305+
.await;
306+
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
307+
}
308+
309+
Ok(CallToolResult {
310+
content: vec![Content::text("Logging test completed")],
311+
structured_content: None,
312+
is_error: None,
313+
meta: None,
314+
})
299315
}
300316

301317
"test_error_handling" => Ok(CallToolResult {
@@ -310,44 +326,21 @@ impl ServerHandler for ConformanceServer {
310326
"test_tool_with_progress" => {
311327
let progress_token = cx.meta.get_progress_token();
312328

313-
if let Some(token) = &progress_token {
314-
let _ = cx
315-
.peer
316-
.notify_progress(ProgressNotificationParam {
317-
progress_token: token.clone(),
318-
progress: 0.0,
319-
total: Some(100.0),
320-
message: Some("Starting".into()),
321-
})
322-
.await;
323-
}
324-
325-
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
326-
327-
if let Some(token) = &progress_token {
328-
let _ = cx
329-
.peer
330-
.notify_progress(ProgressNotificationParam {
331-
progress_token: token.clone(),
332-
progress: 50.0,
333-
total: Some(100.0),
334-
message: Some("Halfway".into()),
335-
})
336-
.await;
337-
}
338-
339-
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
340-
341-
if let Some(token) = &progress_token {
342-
let _ = cx
343-
.peer
344-
.notify_progress(ProgressNotificationParam {
345-
progress_token: token.clone(),
346-
progress: 100.0,
347-
total: Some(100.0),
348-
message: Some("Complete".into()),
349-
})
350-
.await;
329+
for (progress, message) in
330+
[(0.0, "Starting"), (50.0, "Halfway"), (100.0, "Complete")]
331+
{
332+
if let Some(token) = &progress_token {
333+
let _ = cx
334+
.peer
335+
.notify_progress(ProgressNotificationParam {
336+
progress_token: token.clone(),
337+
progress,
338+
total: Some(100.0),
339+
message: Some(message.into()),
340+
})
341+
.await;
342+
}
343+
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
351344
}
352345

353346
Ok(CallToolResult {

0 commit comments

Comments
 (0)