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
//! MCP Tools Implementation
//!
//! Defines available MCP tools and their implementations.
use {
anyhow::Result,
serde_json::{json, Value},
tokio::fs,
tracing::{debug, error, info},
};
pub struct McpTools;
impl McpTools {
/// Get list of available tools with their schemas
pub fn get_tools_list() -> Value {
Self::get_tools_list_for_version(None)
}
/// Get list of available tools with protocol version-specific schemas
pub fn get_tools_list_for_version(protocol_version: Option<&str>) -> Value {
info!(
"📋 MCP tools list requested for protocol version: {:?}",
protocol_version
);
// Both protocol versions use the same schema format for tools
// The main difference is in the initialization response, not the tools list
let tools = vec![
json!({
"name": "echo",
"description": "Echo back the input message",
"input_schema": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "Message to echo"
}
},
"required": ["message"]
},
"output_schema": {
"type": "object",
"properties": {
"content": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["text"]
},
"text": {
"type": "string"
}
},
"required": ["type", "text"]
}
}
},
"required": ["content"]
}
}),
json!({
"name": "read_file",
"description": "Read contents of a file",
"input_schema": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to read"
}
},
"required": ["file_path"]
},
"output_schema": {
"type": "object",
"properties": {
"content": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["text"]
},
"text": {
"type": "string"
}
},
"required": ["type", "text"]
}
}
},
"required": ["content"]
}
}),
];
info!(
"📋 Returning {} available tools for protocol version {:?}",
tools.len(),
protocol_version
);
json!({ "tools": tools })
}
/// Execute a tool by name
pub async fn execute_tool(tool_name: &str, arguments: Value) -> Result<Value> {
let result = match tool_name {
"echo" => Self::handle_echo(arguments).await?,
"read_file" => Self::handle_read_file(arguments).await?,
_ => {
error!("Unknown tool: {}", tool_name);
return Err(anyhow::anyhow!("Unknown tool: {}", tool_name));
}
};
debug!(
"\u{1F6E0}\u{FE0F} Tool '{}' executed successfully",
tool_name
);
// Return structured data directly for programmatic consumption
// instead of stringifying it into human-readable text
Ok(json!({
"content": [
{
"type": "text",
"text": Self::format_result_summary(&result, tool_name)
}
],
"data": result // Structured data for programmatic access
}))
}
/// Format a human-readable summary of the result while preserving structured data
fn format_result_summary(result: &Value, tool_name: &str) -> String {
match tool_name {
"echo" => {
if let Some(message) = result.get("echo").and_then(|v| v.as_str()) {
format!("Echo: {}", message)
} else {
"Echo completed".to_string()
}
},
"read_file" => {
if let Some(error) = result.get("error") {
format!("File read error: {}", error)
} else if let Some(path) = result.get("file_path").and_then(|v| v.as_str()) {
if let Some(content) = result.get("content").and_then(|v| v.as_str()) {
let len = content.len();
format!("Successfully read file '{}' ({} bytes)", path, len)
} else {
format!("Read file: {}", path)
}
} else {
"File read completed".to_string()
}
},
_ => {
// For search results or other tools, try to extract meaningful info
if let Some(query) = result.get("query").and_then(|v| v.as_str()) {
if let Some(results) = result.get("results").and_then(|v| v.as_array()) {
format!("Found {} results for query '{}'", results.len(), query)
} else {
format!("Search completed for query '{}'", query)
}
} else {
format!("Tool '{}' completed successfully", tool_name)
}
}
}
}
/// Echo handler for MCP
async fn handle_echo(params: Value) -> Result<Value> {
let message = params["message"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Missing required parameter 'message'"))?
.to_string();
debug!("🔊 Echo request received: '{}'", message);
let response = json!({ "echo": message });
info!("🔊 Echo response sent: '{}'", message);
Ok(response)
}
/// Read file handler for MCP with error logging
async fn handle_read_file(params: Value) -> Result<Value> {
let file_path = params["file_path"].as_str().unwrap_or("");
debug!("📖 Reading file: {}", file_path);
match fs::read_to_string(file_path).await {
Ok(content) => {
let content_length = content.len();
info!(
"📖 Successfully read file '{}' ({} bytes)",
file_path, content_length
);
Ok(json!({ "file_path": file_path, "content": content }))
}
Err(e) => {
error!("📖 Failed to read file '{}': {}", file_path, e);
Ok(json!({
"file_path": file_path,
"content": format!("[ERROR] {}", e),
"error": e.to_string(),
"error_kind": format!("{:?}", e.kind())
}))
}
}
}
}