[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 <me@sandro.rocks>
This commit is contained in:
Sandro Hanea 2025-03-29 17:34:32 +01:00 committed by GitHub
parent 0a2f13a1f4
commit 50fadfb81b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -49,8 +49,7 @@ impl Counter {
#[tool(description = "Get the current counter value")]
async fn get_value(&self) -> Result<CallToolResult, McpError> {
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<RoleServer>,
) -> Result<GetPromptResult, McpError> {
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 {