From 50fadfb81bb4c4f3d389b37fb1c3276e556045fe Mon Sep 17 00:00:00 2001 From: Sandro Hanea <40202887+sandrohanea@users.noreply.github.com> Date: Sat, 29 Mar 2025 17:34:32 +0100 Subject: [PATCH] [Example] Small fix on incorrect counter logic and improve prompt handling (#63) The counter decrement in `get_value` was removed to prevent unintended changes to the counter state. Additionally, `get_prompt` was updated to parse and validate the `message` argument properly, ensuring better error handling and formatting. Co-authored-by: sandrohanea --- examples/servers/src/common/counter.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/servers/src/common/counter.rs b/examples/servers/src/common/counter.rs index a47be63..dbdfb42 100644 --- a/examples/servers/src/common/counter.rs +++ b/examples/servers/src/common/counter.rs @@ -49,8 +49,7 @@ impl Counter { #[tool(description = "Get the current counter value")] async fn get_value(&self) -> Result { - let mut counter = self.counter.lock().await; - *counter -= 1; + let counter = self.counter.lock().await; Ok(CallToolResult::success(vec![Content::text( counter.to_string(), )])) @@ -159,12 +158,20 @@ impl ServerHandler for Counter { async fn get_prompt( &self, - GetPromptRequestParam { name, arguments: _ }: GetPromptRequestParam, + GetPromptRequestParam { name, arguments }: GetPromptRequestParam, _: RequestContext, ) -> Result { match name.as_str() { "example_prompt" => { - let prompt = "This is an example prompt with your message here: '{message}'"; + let message = arguments + .and_then( + |json| + json.get("message") + ?.as_str() + .map(|s| s.to_string())) + .ok_or_else(|| McpError::invalid_params("No message provided to example_prompt", None))?; + + let prompt = format!("This is an example prompt with your message here: '{message}'"); Ok(GetPromptResult { description: None, messages: vec![PromptMessage {