Skip to content

Commit 50fadfb

Browse files
sandrohaneasandrohanea
andauthored
[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 <[email protected]>
1 parent 0a2f13a commit 50fadfb

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

examples/servers/src/common/counter.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ impl Counter {
4949

5050
#[tool(description = "Get the current counter value")]
5151
async fn get_value(&self) -> Result<CallToolResult, McpError> {
52-
let mut counter = self.counter.lock().await;
53-
*counter -= 1;
52+
let counter = self.counter.lock().await;
5453
Ok(CallToolResult::success(vec![Content::text(
5554
counter.to_string(),
5655
)]))
@@ -159,12 +158,20 @@ impl ServerHandler for Counter {
159158

160159
async fn get_prompt(
161160
&self,
162-
GetPromptRequestParam { name, arguments: _ }: GetPromptRequestParam,
161+
GetPromptRequestParam { name, arguments }: GetPromptRequestParam,
163162
_: RequestContext<RoleServer>,
164163
) -> Result<GetPromptResult, McpError> {
165164
match name.as_str() {
166165
"example_prompt" => {
167-
let prompt = "This is an example prompt with your message here: '{message}'";
166+
let message = arguments
167+
.and_then(
168+
|json|
169+
json.get("message")
170+
?.as_str()
171+
.map(|s| s.to_string()))
172+
.ok_or_else(|| McpError::invalid_params("No message provided to example_prompt", None))?;
173+
174+
let prompt = format!("This is an example prompt with your message here: '{message}'");
168175
Ok(GetPromptResult {
169176
description: None,
170177
messages: vec![PromptMessage {

0 commit comments

Comments
 (0)