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
//! Simple MCP server example
use mcpr::constants::LATEST_PROTOCOL_VERSION;
use mcpr::error::MCPError;
use mcpr::schema::{
Implementation, InitializeResult, JSONRPCError, JSONRPCMessage, JSONRPCResponse,
ServerCapabilities,
};
use mcpr::transport::{stdio::StdioTransport, Transport};
use serde_json::Value;
fn main() -> Result<(), MCPError> {
// Create a stdio transport
let mut transport = StdioTransport::new();
// Wait for initialize request
let message: JSONRPCMessage = transport.receive()?;
match message {
JSONRPCMessage::Request(request) => {
if request.method == "initialize" {
// Handle initialize request
let result = InitializeResult {
protocol_version: LATEST_PROTOCOL_VERSION.to_string(),
capabilities: ServerCapabilities {
experimental: None,
logging: Some(Value::Object(serde_json::Map::new())),
prompts: None,
resources: None,
tools: None,
},
server_info: Implementation {
name: "simple-server".to_string(),
version: "0.1.0".to_string(),
},
instructions: Some("This is a simple MCP server example.".to_string()),
};
// Send initialize response
let response =
JSONRPCResponse::new(request.id, serde_json::to_value(result).unwrap());
transport.send(&response)?;
println!("Server initialized");
// Main message loop
loop {
let message: JSONRPCMessage = match transport.receive() {
Ok(msg) => msg,
Err(e) => {
eprintln!("Error receiving message: {}", e);
break;
}
};
match message {
JSONRPCMessage::Request(request) => {
// Handle request
match request.method.as_str() {
"ping" => {
// Send empty response
let response = JSONRPCResponse::new(
request.id,
Value::Object(serde_json::Map::new()),
);
transport.send(&response)?;
}
_ => {
// Method not found
let error = JSONRPCError::new(
request.id,
-32601,
format!("Method not found: {}", request.method),
None,
);
transport.send(&error)?;
}
}
}
JSONRPCMessage::Notification(notification) => {
// Handle notification
match notification.method.as_str() {
"notifications/initialized" => {
println!("Client initialized");
}
_ => {
println!("Received notification: {}", notification.method);
}
}
}
_ => {
// Ignore other message types
}
}
}
} else {
// Not an initialize request
let error = JSONRPCError::new(
request.id,
-32600,
"Expected initialize request".to_string(),
None,
);
transport.send(&error)?;
}
}
_ => {
// Not a request
eprintln!("Expected initialize request, got something else");
}
}
Ok(())
}