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
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};
fn main() {
let stdin = io::stdin();
let mut stdout = io::stdout();
let reader = stdin.lock();
let mut lines = reader.lines();
while let Some(Ok(line)) = lines.next() {
let request: Value = match serde_json::from_str(&line) {
Ok(r) => r,
Err(_) => continue,
};
let response = match request.get("method").and_then(|m| m.as_str()) {
Some("initialize") => {
json!({
"jsonrpc": "2.0",
"id": request.get("id"),
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"serverInfo": {
"name": "test-server",
"version": "0.1.0"
}
}
})
}
Some("tools/list") => {
json!({
"jsonrpc": "2.0",
"id": request.get("id"),
"result": {
"tools": [
{
"name": "search",
"description": "Search records",
"inputSchema": {
"type": "object",
"properties": {
"q": {
"type": "string",
"description": "Search query"
},
"limit": {
"type": "integer",
"description": "Max results"
}
},
"required": ["q", "limit"]
}
},
{
"name": "fetch",
"description": "Fetch a record by ID",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Record ID"
}
},
"required": ["id"]
}
}
]
}
})
}
Some("tools/call") => {
let tool_name = request
.get("params")
.and_then(|p| p.get("name"))
.and_then(|n| n.as_str())
.unwrap_or("unknown");
let args = request
.get("params")
.and_then(|p| p.get("arguments"))
.cloned()
.unwrap_or(json!({}));
let result = match tool_name {
"search" => {
let q = args.get("q").and_then(|v| v.as_str()).unwrap_or("unknown");
let limit = args.get("limit").and_then(|v| v.as_i64()).unwrap_or(10);
json!({
"results": [
{ "id": "1", "title": format!("Result for '{}'", q), "score": 0.95 },
{ "id": "2", "title": format!("Another result for '{}'", q), "score": 0.87 }
],
"total": 2,
"query": q,
"limit": limit
})
}
"fetch" => {
let id = args.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
json!({
"id": id,
"title": format!("Record {}", id),
"content": "Sample content",
"created": "2026-01-01T00:00:00Z"
})
}
_ => {
json!({
"jsonrpc": "2.0",
"id": request.get("id"),
"error": {
"code": -32601,
"message": "Unknown tool"
}
})
}
};
json!({
"jsonrpc": "2.0",
"id": request.get("id"),
"result": {
"content": [
{
"type": "text",
"text": serde_json::to_string(&result).unwrap()
}
],
"isError": false
}
})
}
_ => {
json!({
"jsonrpc": "2.0",
"id": request.get("id"),
"error": {
"code": -32601,
"message": "Method not found"
}
})
}
};
let response_str = serde_json::to_string(&response).unwrap();
writeln!(stdout, "{}", response_str).unwrap();
}
}