forked from paiml/rust-mcp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_sampling_llm.rs
More file actions
78 lines (68 loc) · 2.12 KB
/
14_sampling_llm.rs
File metadata and controls
78 lines (68 loc) · 2.12 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
//! Example of implementing a sampling/LLM server.
use async_trait::async_trait;
use pmcp::{
types::{
capabilities::ServerCapabilities, Content, CreateMessageParams, CreateMessageResult,
SamplingMessageContent, TokenUsage,
},
SamplingHandler, Server,
};
use tracing::info;
struct MockLLM {
model_name: String,
}
#[async_trait]
impl SamplingHandler for MockLLM {
async fn create_message(
&self,
params: CreateMessageParams,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<CreateMessageResult> {
info!(
"Received sampling request with {} messages",
params.messages.len()
);
// In a real implementation, this would call an actual LLM
let response_text = format!(
"This is a mock response to: {}",
params
.messages
.last()
.map(|m| match &m.content {
SamplingMessageContent::Text { text, .. } => text.as_str(),
SamplingMessageContent::Image { .. } => "[image]",
_ => "[other]",
})
.unwrap_or("empty")
);
Ok(
CreateMessageResult::new(Content::text(response_text), &self.model_name)
.with_usage(TokenUsage::new(
params.messages.len() as u32 * 10,
20,
params.messages.len() as u32 * 10 + 20,
))
.with_stop_reason("end_of_text"),
)
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
info!("Creating LLM server");
let server = Server::builder()
.name("mock-llm-server")
.version("1.0.0")
.capabilities({
let mut caps = ServerCapabilities::default();
caps.sampling = Some(Default::default());
caps
})
.sampling(MockLLM {
model_name: "mock-gpt-4".to_string(),
})
.build()?;
info!("Starting server on stdio");
server.run_stdio().await?;
Ok(())
}