Skip to content

Commit 2d2a5e0

Browse files
committed
fix: replace println! with eprintln! in all MCP server examples for stdio transport compliance
1 parent 1294a66 commit 2d2a5e0

11 files changed

+259
-259
lines changed

src/examples/example_01_hello_world.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
152152
// Initialize logging to help with debugging
153153
tracing_subscriber::fmt::init();
154154

155-
println!("🚀 Starting Hello World MCP Server");
156-
println!("📝 Available tools: greeting");
157-
println!("💡 Send JSON-RPC messages via stdin");
158-
println!("📋 Example: {{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}}");
159-
println!();
155+
eprintln!("🚀 Starting Hello World MCP Server");
156+
eprintln!("📝 Available tools: greeting");
157+
eprintln!("💡 Send JSON-RPC messages via stdin");
158+
eprintln!("📋 Example: {{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}}");
159+
eprintln!();
160160

161161
// Create our server handler instance
162162
let server = HelloWorldServer::new();
@@ -204,6 +204,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
204204
}
205205
}
206206

207-
println!("👋 Hello World server shutting down");
207+
eprintln!("👋 Hello World server shutting down");
208208
Ok(())
209209
}

src/examples/example_02_calculator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ impl CalculatorServer {
204204
async fn main() -> Result<(), Box<dyn std::error::Error>> {
205205
tracing_subscriber::fmt::init();
206206

207-
println!("🧮 Starting Calculator MCP Server");
208-
println!("📝 Available tools: calculator");
209-
println!("💡 Send JSON-RPC messages via stdin");
210-
println!("📋 Example: {{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{{\"name\":\"calculator\",\"arguments\":{{\"operation\":\"add\",\"a\":5,\"b\":3}}}}}}");
211-
println!();
207+
eprintln!("🧮 Starting Calculator MCP Server");
208+
eprintln!("📝 Available tools: calculator");
209+
eprintln!("💡 Send JSON-RPC messages via stdin");
210+
eprintln!("📋 Example: {{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{{\"name\":\"calculator\",\"arguments\":{{\"operation\":\"add\",\"a\":5,\"b\":3}}}}}}");
211+
eprintln!();
212212

213213
let server = CalculatorServer::new();
214214

@@ -254,7 +254,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
254254
}
255255
}
256256

257-
println!("🧮 Calculator server shutting down");
257+
eprintln!("🧮 Calculator server shutting down");
258258
Ok(())
259259
}
260260

src/examples/example_03_text_processor.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ impl TextProcessorServer {
164164
async fn main() -> Result<(), Box<dyn std::error::Error>> {
165165
tracing_subscriber::fmt::init();
166166

167-
println!("📝 Starting Text Processor MCP Server");
168-
println!("🛠️ Available tools: transform_text, analyze_text");
169-
println!("💡 Send JSON-RPC messages via stdin");
167+
eprintln!("📝 Starting Text Processor MCP Server");
168+
eprintln!("🛠️ Available tools: transform_text, analyze_text");
169+
eprintln!("💡 Send JSON-RPC messages via stdin");
170170

171171
// Simple demo mode for testing
172172
let server = TextProcessorServer::new();
173173

174174
// Demo usage
175-
println!("\n🧪 Running demo transformations:");
175+
eprintln!("\n🧪 Running demo transformations:");
176176

177177
let demo_text = "hello world";
178178
let transform_args = serde_json::json!({
@@ -183,12 +183,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
183183
match server.call_tool("transform_text", transform_args) {
184184
Ok(result) => {
185185
let response: TextResponse = serde_json::from_value(result).unwrap();
186-
println!(
186+
eprintln!(
187187
"✅ Transform '{}' to uppercase: '{}'",
188188
demo_text, response.result
189189
);
190190
}
191-
Err(e) => println!("❌ Transform failed: {}", e),
191+
Err(e) => eprintln!("❌ Transform failed: {}", e),
192192
}
193193

194194
let analyze_args = serde_json::json!({
@@ -198,15 +198,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
198198
match server.call_tool("analyze_text", analyze_args) {
199199
Ok(result) => {
200200
let response: TextAnalysisResponse = serde_json::from_value(result).unwrap();
201-
println!(
201+
eprintln!(
202202
"✅ Analysis of '{}': {} words, {} chars",
203203
demo_text, response.word_count, response.character_count
204204
);
205205
}
206-
Err(e) => println!("❌ Analysis failed: {}", e),
206+
Err(e) => eprintln!("❌ Analysis failed: {}", e),
207207
}
208208

209-
println!("\n🎉 Text processor demo completed");
209+
eprintln!("\n🎉 Text processor demo completed");
210210
Ok(())
211211
}
212212

src/examples/example_04_simple_client.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ impl SimpleMcpClient {
4444

4545
// Simulate connecting to an MCP server
4646
pub async fn connect(&self) -> Result<(), String> {
47-
println!("🔗 Connecting to MCP server: {}", self.server_url);
47+
eprintln!("🔗 Connecting to MCP server: {}", self.server_url);
4848

4949
// In a real implementation, this would establish a connection
5050
// For this demo, we'll just simulate success
5151
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
5252

53-
println!("✅ Connected successfully!");
53+
eprintln!("✅ Connected successfully!");
5454
Ok(())
5555
}
5656

5757
// Simulate listing available tools from the server
5858
pub async fn list_tools(&self) -> Result<Vec<ToolInfo>, String> {
59-
println!("🔍 Discovering available tools...");
59+
eprintln!("🔍 Discovering available tools...");
6060

6161
// Simulate network delay
6262
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
@@ -101,17 +101,17 @@ impl SimpleMcpClient {
101101
},
102102
];
103103

104-
println!("📋 Found {} tools", tools.len());
104+
eprintln!("📋 Found {} tools", tools.len());
105105
for tool in &tools {
106-
println!(" - {}: {}", tool.name, tool.description);
106+
eprintln!(" - {}: {}", tool.name, tool.description);
107107
}
108108

109109
Ok(tools)
110110
}
111111

112112
// Simulate calling a tool on the server
113113
pub async fn call_tool(&self, request: ToolCallRequest) -> Result<ToolCallResponse, String> {
114-
println!("🔧 Calling tool: {}", request.tool_name);
114+
eprintln!("🔧 Calling tool: {}", request.tool_name);
115115

116116
// Simulate network delay
117117
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
@@ -216,8 +216,8 @@ impl SimpleMcpClient {
216216

217217
// Demonstrate a complete client workflow
218218
pub async fn demonstrate_client_workflow(&self) -> Result<(), String> {
219-
println!("🚀 Starting MCP Client Demonstration");
220-
println!("====================================");
219+
eprintln!("🚀 Starting MCP Client Demonstration");
220+
eprintln!("====================================");
221221

222222
// Step 1: Connect to server
223223
self.connect().await?;
@@ -226,7 +226,7 @@ impl SimpleMcpClient {
226226
let tools = self.list_tools().await?;
227227

228228
// Step 3: Call each tool with sample data
229-
println!("\n🧪 Testing tools with sample data:");
229+
eprintln!("\n🧪 Testing tools with sample data:");
230230

231231
// Test greeting tool
232232
if tools.iter().any(|t| t.name == "greeting") {
@@ -241,16 +241,16 @@ impl SimpleMcpClient {
241241
result: Some(result),
242242
..
243243
} => {
244-
println!("✅ Greeting result: {}", result);
244+
eprintln!("✅ Greeting result: {}", result);
245245
}
246246
ToolCallResponse {
247247
success: false,
248248
error: Some(err),
249249
..
250250
} => {
251-
println!("❌ Greeting failed: {}", err);
251+
eprintln!("❌ Greeting failed: {}", err);
252252
}
253-
_ => println!("⚠️ Unexpected greeting response"),
253+
_ => eprintln!("⚠️ Unexpected greeting response"),
254254
}
255255
}
256256

@@ -271,16 +271,16 @@ impl SimpleMcpClient {
271271
result: Some(result),
272272
..
273273
} => {
274-
println!("✅ Calculator result: {}", result);
274+
eprintln!("✅ Calculator result: {}", result);
275275
}
276276
ToolCallResponse {
277277
success: false,
278278
error: Some(err),
279279
..
280280
} => {
281-
println!("❌ Calculator failed: {}", err);
281+
eprintln!("❌ Calculator failed: {}", err);
282282
}
283-
_ => println!("⚠️ Unexpected calculator response"),
283+
_ => eprintln!("⚠️ Unexpected calculator response"),
284284
}
285285
}
286286

@@ -300,20 +300,20 @@ impl SimpleMcpClient {
300300
result: Some(result),
301301
..
302302
} => {
303-
println!("✅ Text transform result: {}", result);
303+
eprintln!("✅ Text transform result: {}", result);
304304
}
305305
ToolCallResponse {
306306
success: false,
307307
error: Some(err),
308308
..
309309
} => {
310-
println!("❌ Text transform failed: {}", err);
310+
eprintln!("❌ Text transform failed: {}", err);
311311
}
312-
_ => println!("⚠️ Unexpected text transform response"),
312+
_ => eprintln!("⚠️ Unexpected text transform response"),
313313
}
314314
}
315315

316-
println!("\n🎉 Client demonstration completed successfully!");
316+
eprintln!("\n🎉 Client demonstration completed successfully!");
317317
Ok(())
318318
}
319319
}

src/examples/example_05_resource_provider.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,28 +301,28 @@ impl ResourceProviderServer {
301301
async fn main() -> Result<(), Box<dyn std::error::Error>> {
302302
tracing_subscriber::fmt::init();
303303

304-
println!("📚 Starting Resource Provider MCP Server");
305-
println!("🗂️ Sample documents with search capabilities loaded");
306-
println!();
304+
eprintln!("📚 Starting Resource Provider MCP Server");
305+
eprintln!("🗂️ Sample documents with search capabilities loaded");
306+
eprintln!();
307307

308308
let server = ResourceProviderServer::new();
309309

310310
// Demonstrate resource functionality
311-
println!("🧪 Demonstrating resource functionality:");
311+
eprintln!("🧪 Demonstrating resource functionality:");
312312

313313
// List resources
314314
let resources = server.list_resources();
315-
println!("📋 Available resources ({} total):", resources.len());
315+
eprintln!("📋 Available resources ({} total):", resources.len());
316316
for resource in &resources {
317-
println!(
317+
eprintln!(
318318
" - {} ({})",
319319
resource.uri,
320320
resource.name.as_deref().unwrap_or("Unnamed")
321321
);
322322
}
323323

324324
// Demonstrate search functionality
325-
println!("\n🔍 Search demonstration:");
325+
eprintln!("\n🔍 Search demonstration:");
326326
let search_args = serde_json::json!({
327327
"query": "Rust",
328328
"limit": 3
@@ -331,38 +331,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
331331
match server.call_tool("search_documents", search_args) {
332332
Ok(result) => {
333333
let response: SearchResponse = serde_json::from_value(result).unwrap();
334-
println!(
334+
eprintln!(
335335
"✅ Found {} documents matching 'Rust':",
336336
response.total_count
337337
);
338338
for doc in response.matches {
339-
println!(" - {}: {} [{}]", doc.id, doc.title, doc.tags.join(", "));
339+
eprintln!(" - {}: {} [{}]", doc.id, doc.title, doc.tags.join(", "));
340340
}
341341
}
342-
Err(e) => println!("❌ Search failed: {}", e),
342+
Err(e) => eprintln!("❌ Search failed: {}", e),
343343
}
344344

345345
// Demonstrate resource reading
346-
println!("\n📖 Resource reading demonstration:");
346+
eprintln!("\n📖 Resource reading demonstration:");
347347
match server.read_resource("document://doc1") {
348348
Ok(content) => {
349-
println!("✅ Successfully read document://doc1");
349+
eprintln!("✅ Successfully read document://doc1");
350350
let text = content
351351
.get("contents")
352352
.and_then(|c| c.as_array())
353353
.and_then(|arr| arr.first())
354354
.and_then(|item| item.get("text"))
355355
.and_then(|t| t.as_str())
356356
.unwrap_or("No content");
357-
println!(
357+
eprintln!(
358358
"📄 Content preview: {}...",
359359
&text[..std::cmp::min(text.len(), 100)]
360360
);
361361
}
362-
Err(e) => println!("❌ Read failed: {}", e),
362+
Err(e) => eprintln!("❌ Read failed: {}", e),
363363
}
364364

365-
println!("\n🎉 Resource provider demonstration completed!");
365+
eprintln!("\n🎉 Resource provider demonstration completed!");
366366
Ok(())
367367
}
368368

0 commit comments

Comments
 (0)