-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathsampling_stdio.rs
More file actions
139 lines (128 loc) · 4.86 KB
/
sampling_stdio.rs
File metadata and controls
139 lines (128 loc) · 4.86 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
use std::sync::Arc;
use anyhow::Result;
use rmcp::{
ServerHandler, ServiceExt,
model::*,
service::{RequestContext, RoleServer},
transport::stdio,
};
use tracing_subscriber::{self, EnvFilter};
/// Simple Sampling Demo Server
///
/// This server demonstrates how to request LLM sampling from clients.
/// Run with: cargo run -p mcp-server-examples --example servers_sampling_stdio
#[derive(Clone, Debug, Default)]
pub struct SamplingDemoServer;
impl ServerHandler for SamplingDemoServer {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
.with_instructions(concat!(
"This is a demo server that requests sampling from clients. It provides tools that use LLM capabilities.\n\n",
"IMPORTANT: This server requires a client that supports the 'sampling/createMessage' method. ",
"Without sampling support, the tools will return errors."
))
}
async fn call_tool(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<CallToolResult, ErrorData> {
match request.name.as_ref() {
"ask_llm" => {
// Get the question from arguments
let question = request
.arguments
.as_ref()
.and_then(|args| args.get("question"))
.and_then(|q| q.as_str())
.unwrap_or("Hello LLM");
let response = context
.peer
.create_message(
CreateMessageRequestParams::new(
vec![SamplingMessage::user_text(question)],
150,
)
.with_model_preferences(
ModelPreferences::new()
.with_hints(vec![ModelHint::new("claude")])
.with_cost_priority(0.3)
.with_speed_priority(0.8)
.with_intelligence_priority(0.7),
)
.with_system_prompt("You are a helpful assistant.")
.with_include_context(ContextInclusion::None)
.with_temperature(0.7),
)
.await
.map_err(|e| {
ErrorData::new(
ErrorCode::INTERNAL_ERROR,
format!("Sampling request failed: {}", e),
None,
)
})?;
tracing::debug!("Response: {:?}", response);
Ok(CallToolResult::success(vec![Content::text(format!(
"Question: {}\nAnswer: {}",
question,
response
.message
.content
.first()
.and_then(|c| c.as_text())
.map(|t| &t.text)
.unwrap_or(&"No text response".to_string())
))]))
}
_ => Err(ErrorData::new(
ErrorCode::INTERNAL_ERROR,
format!("Unknown tool: {}", request.name),
None,
)),
}
}
async fn list_tools(
&self,
_request: Option<PaginatedRequestParams>,
_context: RequestContext<RoleServer>,
) -> Result<ListToolsResult, ErrorData> {
Ok(ListToolsResult {
tools: vec![Tool::new(
"ask_llm",
"Ask a question to the LLM through sampling",
Arc::new(
serde_json::from_value(serde_json::json!({
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The question to ask the LLM"
}
},
"required": ["question"]
}))
.unwrap(),
),
)],
meta: None,
next_cursor: None,
})
}
}
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(tracing::Level::INFO.into()))
.with_writer(std::io::stderr)
.with_ansi(false)
.init();
tracing::info!("Starting Sampling Demo Server");
// Create and serve the sampling demo server
let service = SamplingDemoServer.serve(stdio()).await.inspect_err(|e| {
tracing::error!("Serving error: {:?}", e);
})?;
service.waiting().await?;
Ok(())
}