forked from modelcontextprotocol/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.rs
More file actions
400 lines (357 loc) · 13.4 KB
/
counter.rs
File metadata and controls
400 lines (357 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#![allow(dead_code)]
use std::{any::Any, sync::Arc};
use rmcp::{
ErrorData as McpError, RoleServer, ServerHandler,
handler::server::{
router::{prompt::PromptRouter, tool::ToolRouter},
wrapper::Parameters,
},
model::*,
prompt, prompt_handler, prompt_router, schemars,
service::RequestContext,
task_handler,
task_manager::{OperationProcessor, OperationResultTransport},
tool, tool_handler, tool_router,
};
use serde_json::json;
use tokio::sync::Mutex;
struct ToolCallOperationResult {
id: String,
result: Result<CallToolResult, McpError>,
}
impl OperationResultTransport for ToolCallOperationResult {
fn operation_id(&self) -> &String {
&self.id
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
pub struct StructRequest {
pub a: i32,
pub b: i32,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct ExamplePromptArgs {
/// A message to put in the prompt
pub message: String,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct CounterAnalysisArgs {
/// The target value you're trying to reach
pub goal: i32,
/// Preferred strategy: 'fast' or 'careful'
#[serde(skip_serializing_if = "Option::is_none")]
pub strategy: Option<String>,
}
#[derive(Clone)]
pub struct Counter {
counter: Arc<Mutex<i32>>,
tool_router: ToolRouter<Counter>,
prompt_router: PromptRouter<Counter>,
processor: Arc<Mutex<OperationProcessor>>,
}
#[tool_router]
impl Counter {
#[allow(dead_code)]
pub fn new() -> Self {
Self {
counter: Arc::new(Mutex::new(0)),
tool_router: Self::tool_router(),
prompt_router: Self::prompt_router(),
processor: Arc::new(Mutex::new(OperationProcessor::new())),
}
}
fn _create_resource_text(&self, uri: &str, name: &str) -> Resource {
RawResource::new(uri, name.to_string()).no_annotation()
}
#[tool(description = "Increment the counter by 1")]
async fn increment(&self) -> Result<CallToolResult, McpError> {
let mut counter = self.counter.lock().await;
*counter += 1;
Ok(CallToolResult::success(vec![Content::text(
counter.to_string(),
)]))
}
#[tool(description = "Decrement the counter by 1")]
async fn decrement(&self) -> Result<CallToolResult, McpError> {
let mut counter = self.counter.lock().await;
*counter -= 1;
Ok(CallToolResult::success(vec![Content::text(
counter.to_string(),
)]))
}
#[tool(description = "Get the current counter value")]
async fn get_value(&self) -> Result<CallToolResult, McpError> {
let counter = self.counter.lock().await;
Ok(CallToolResult::success(vec![Content::text(
counter.to_string(),
)]))
}
#[tool(
description = "Long running task example",
execution(task_support = "optional")
)]
async fn long_task(&self) -> Result<CallToolResult, McpError> {
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
Ok(CallToolResult::success(vec![Content::text(
"Long task completed",
)]))
}
#[tool(description = "Say hello to the client")]
fn say_hello(&self) -> Result<CallToolResult, McpError> {
Ok(CallToolResult::success(vec![Content::text("hello")]))
}
#[tool(description = "Repeat what you say")]
fn echo(&self, Parameters(object): Parameters<JsonObject>) -> Result<CallToolResult, McpError> {
Ok(CallToolResult::success(vec![Content::text(
serde_json::Value::Object(object).to_string(),
)]))
}
#[tool(description = "Calculate the sum of two numbers")]
fn sum(
&self,
Parameters(StructRequest { a, b }): Parameters<StructRequest>,
) -> Result<CallToolResult, McpError> {
Ok(CallToolResult::success(vec![Content::text(
(a + b).to_string(),
)]))
}
/// Returns the `Mcp-Session-Id` of the current session (streamable HTTP only).
#[tool(description = "Get the session ID for this connection")]
fn get_session_id(&self, ctx: RequestContext<RoleServer>) -> Result<CallToolResult, McpError> {
let session_id = ctx
.extensions
.get::<axum::http::request::Parts>()
.and_then(|parts| parts.headers.get("mcp-session-id"))
.map(|v| v.to_str().unwrap_or("(non-ascii)").to_owned());
match session_id {
Some(id) => Ok(CallToolResult::success(vec![Content::text(id)])),
None => Ok(CallToolResult::success(vec![Content::text(
"no session (not running over streamable HTTP?)",
)])),
}
}
}
#[prompt_router]
impl Counter {
/// This is an example prompt that takes one required argument, message
#[prompt(
name = "example_prompt",
meta = Meta(rmcp::object!({"meta_key": "meta_value"}))
)]
async fn example_prompt(
&self,
Parameters(args): Parameters<ExamplePromptArgs>,
_ctx: RequestContext<RoleServer>,
) -> Result<Vec<PromptMessage>, McpError> {
let prompt = format!(
"This is an example prompt with your message here: '{}'",
args.message
);
Ok(vec![PromptMessage {
role: PromptMessageRole::User,
content: PromptMessageContent::text(prompt),
}])
}
/// Analyze the current counter value and suggest next steps
#[prompt(name = "counter_analysis")]
async fn counter_analysis(
&self,
Parameters(args): Parameters<CounterAnalysisArgs>,
_ctx: RequestContext<RoleServer>,
) -> Result<GetPromptResult, McpError> {
let strategy = args.strategy.unwrap_or_else(|| "careful".to_string());
let current_value = *self.counter.lock().await;
let difference = args.goal - current_value;
let messages = vec![
PromptMessage::new_text(
PromptMessageRole::Assistant,
"I'll analyze the counter situation and suggest the best approach.",
),
PromptMessage::new_text(
PromptMessageRole::User,
format!(
"Current counter value: {}\nGoal value: {}\nDifference: {}\nStrategy preference: {}\n\nPlease analyze the situation and suggest the best approach to reach the goal.",
current_value, args.goal, difference, strategy
),
),
];
Ok(GetPromptResult {
description: Some(format!(
"Counter analysis for reaching {} from {}",
args.goal, current_value
)),
messages,
})
}
}
#[tool_handler(meta = Meta(rmcp::object!({"tool_meta_key": "tool_meta_value"})))]
#[prompt_handler(meta = Meta(rmcp::object!({"router_meta_key": "router_meta_value"})))]
#[task_handler]
impl ServerHandler for Counter {
fn get_info(&self) -> ServerInfo {
ServerInfo {
protocol_version: ProtocolVersion::V_2024_11_05,
capabilities: ServerCapabilities::builder()
.enable_prompts()
.enable_resources()
.enable_tools()
.build(),
server_info: Implementation::from_build_env(),
instructions: Some("This server provides counter tools and prompts. Tools: increment, decrement, get_value, say_hello, echo, sum. Prompts: example_prompt (takes a message), counter_analysis (analyzes counter state with a goal).".to_string()),
}
}
async fn list_resources(
&self,
_request: Option<PaginatedRequestParams>,
_: RequestContext<RoleServer>,
) -> Result<ListResourcesResult, McpError> {
Ok(ListResourcesResult {
resources: vec![
self._create_resource_text("str:////Users/to/some/path/", "cwd"),
self._create_resource_text("memo://insights", "memo-name"),
],
next_cursor: None,
meta: None,
})
}
async fn read_resource(
&self,
ReadResourceRequestParams { meta: _, uri }: ReadResourceRequestParams,
_: RequestContext<RoleServer>,
) -> Result<ReadResourceResult, McpError> {
match uri.as_str() {
"str:////Users/to/some/path/" => {
let cwd = "/Users/to/some/path/";
Ok(ReadResourceResult {
contents: vec![ResourceContents::text(cwd, uri)],
})
}
"memo://insights" => {
let memo = "Business Intelligence Memo\n\nAnalysis has revealed 5 key insights ...";
Ok(ReadResourceResult {
contents: vec![ResourceContents::text(memo, uri)],
})
}
_ => Err(McpError::resource_not_found(
"resource_not_found",
Some(json!({
"uri": uri
})),
)),
}
}
async fn list_resource_templates(
&self,
_request: Option<PaginatedRequestParams>,
_: RequestContext<RoleServer>,
) -> Result<ListResourceTemplatesResult, McpError> {
Ok(ListResourceTemplatesResult {
next_cursor: None,
resource_templates: Vec::new(),
meta: None,
})
}
async fn initialize(
&self,
_request: InitializeRequestParams,
context: RequestContext<RoleServer>,
) -> Result<InitializeResult, McpError> {
if let Some(http_request_part) = context.extensions.get::<axum::http::request::Parts>() {
let initialize_headers = &http_request_part.headers;
let initialize_uri = &http_request_part.uri;
tracing::info!(?initialize_headers, %initialize_uri, "initialize from http server");
}
Ok(self.get_info())
}
}
#[cfg(test)]
mod tests {
use rmcp::{ClientHandler, ServiceExt};
use tokio::time::Duration;
use super::*;
#[derive(Default, Clone)]
struct TestClient;
impl ClientHandler for TestClient {}
#[tokio::test]
async fn test_prompt_attributes_generated() {
// Verify that the prompt macros generate the expected attributes
let example_attr = Counter::example_prompt_prompt_attr();
assert_eq!(example_attr.name, "example_prompt");
assert!(example_attr.description.is_some());
assert!(example_attr.arguments.is_some());
let args = example_attr.arguments.unwrap();
assert_eq!(args.len(), 1);
assert_eq!(args[0].name, "message");
assert_eq!(args[0].required, Some(true));
let analysis_attr = Counter::counter_analysis_prompt_attr();
assert_eq!(analysis_attr.name, "counter_analysis");
assert!(analysis_attr.description.is_some());
assert!(analysis_attr.arguments.is_some());
let args = analysis_attr.arguments.unwrap();
assert_eq!(args.len(), 2);
assert_eq!(args[0].name, "goal");
assert_eq!(args[0].required, Some(true));
assert_eq!(args[1].name, "strategy");
assert_eq!(args[1].required, Some(false));
}
#[tokio::test]
async fn test_prompt_router_has_routes() {
let router = Counter::prompt_router();
assert!(router.has_route("example_prompt"));
assert!(router.has_route("counter_analysis"));
let prompts = router.list_all();
assert_eq!(prompts.len(), 2);
}
#[tokio::test]
async fn test_client_enqueues_long_task() -> anyhow::Result<()> {
let counter = Counter::new();
let processor = counter.processor.clone();
let client = TestClient::default();
let (server_transport, client_transport) = tokio::io::duplex(4096);
let server_handle = tokio::spawn(async move {
let service = counter.serve(server_transport).await?;
service.waiting().await?;
anyhow::Ok(())
});
let client_service = client.serve(client_transport).await?;
let mut task_meta = serde_json::Map::new();
task_meta.insert(
"source".into(),
serde_json::Value::String("integration-test".into()),
);
let params = CallToolRequestParams {
meta: None,
name: "long_task".into(),
arguments: None,
task: Some(task_meta),
};
let response = client_service
.send_request(ClientRequest::CallToolRequest(Request::new(params.clone())))
.await?;
let ServerResult::CreateTaskResult(info) = response else {
panic!("expected task creation result, got {response:?}");
};
let task = info.task;
assert_eq!(task.status, TaskStatus::Working);
// task list should show the task
let tasks = client_service
.send_request(ClientRequest::ListTasksRequest(
RequestOptionalParam::default(),
))
.await
.unwrap();
let ServerResult::ListTasksResult(listed) = tasks else {
panic!("expected list tasks result, got {tasks:?}");
};
assert_eq!(listed.tasks[0].task_id, task.task_id);
tokio::time::sleep(Duration::from_millis(50)).await;
let running = processor.lock().await.running_task_count();
assert_eq!(running, 1);
client_service.cancel().await?;
let _ = server_handle.await;
Ok(())
}
}