solidmcp 0.4.0

A high-level Rust toolkit for building Model Context Protocol (MCP) servers with type safety and minimal boilerplate. Supports tools, resources, and prompts with automatic JSON schema generation.
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# SolidMCP Test Patterns Reference

## Overview

This reference documents reusable test patterns established during TDD implementation of SolidMCP's test suite. These patterns ensure consistency and reduce boilerplate across tests.

## Core Test Helpers

### Server Setup Pattern

```rust
use mcp_test_helpers::TestServerBuilder;

// Basic server
let (server, port) = TestServerBuilder::new()
    .build()
    .await;

// Server with tools
let (server, port) = TestServerBuilder::new()
    .with_tool("echo", "Echo tool", |msg: String| async move {
        Ok(json!({ "echoed": msg }))
    })
    .with_tool("math", "Math tool", |args: MathArgs| async move {
        Ok(json!({ "result": args.a + args.b }))
    })
    .build()
    .await;

// Server with context
struct AppContext {
    database: Database,
}

let (server, port) = TestServerBuilder::new()
    .with_context(AppContext { database })
    .build()
    .await;
```

### HTTP Client Setup

```rust
let client = reqwest::Client::builder()
    .timeout(Duration::from_secs(5))
    .build()
    .unwrap();
```

### Session Management Patterns

```rust
// Create new session
async fn create_session(client: &Client, port: u16) -> String {
    let response = client
        .post(&format!("http://localhost:{}/", port))
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "clientInfo": {
                    "name": "test-client",
                    "version": "1.0.0"
                }
            },
            "id": 1
        }))
        .send()
        .await
        .unwrap();
    
    extract_session_cookie(&response)
}

// Extract cookie from response
fn extract_session_cookie(response: &Response) -> String {
    response
        .headers()
        .get("set-cookie")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string()
}

// Initialize existing session
async fn initialize_session(
    client: &Client,
    port: u16,
    session_cookie: &str,
    client_name: &str
) -> Value {
    let response = client
        .post(&format!("http://localhost:{}/", port))
        .header("Cookie", session_cookie)
        .json(&init_request(client_name))
        .send()
        .await
        .unwrap();
    
    response.json().await.unwrap()
}
```

## Common Request Patterns

### Initialize Request

```rust
fn init_request(client_name: &str) -> Value {
    json!({
        "jsonrpc": "2.0",
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-06-18",
            "capabilities": {},
            "clientInfo": {
                "name": client_name,
                "version": "1.0.0"
            }
        },
        "id": 1
    })
}
```

### Tool Call Request

```rust
fn tool_call_request(tool_name: &str, arguments: Value) -> Value {
    json!({
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": {
            "name": tool_name,
            "arguments": arguments
        },
        "id": 2
    })
}

// Helper function
async fn call_tool(
    client: &Client,
    port: u16,
    session_cookie: &str,
    tool_name: &str,
    arguments: Value
) -> Value {
    let response = client
        .post(&format!("http://localhost:{}/", port))
        .header("Cookie", session_cookie)
        .json(&tool_call_request(tool_name, arguments))
        .send()
        .await
        .unwrap();
    
    response.json().await.unwrap()
}
```

### List Tools Request

```rust
fn list_tools_request() -> Value {
    json!({
        "jsonrpc": "2.0",
        "method": "tools/list",
        "params": {},
        "id": 3
    })
}
```

## Error Validation Patterns

### JSON-RPC Error Validation

```rust
fn assert_json_rpc_error(response: &Value, expected_code: i32) {
    assert!(response["error"].is_object());
    let code = response["error"]["code"].as_i64().unwrap();
    assert_eq!(code, expected_code as i64);
    assert!(response["error"]["message"].is_string());
}

// Common error codes
const PARSE_ERROR: i32 = -32700;
const INVALID_REQUEST: i32 = -32600;
const METHOD_NOT_FOUND: i32 = -32601;
const INVALID_PARAMS: i32 = -32602;
const INTERNAL_ERROR: i32 = -32603;
```

### HTTP Error Validation

```rust
async fn assert_http_error(response: Response, expected_status: u16) {
    assert_eq!(response.status(), expected_status);
    
    // For JSON-RPC over HTTP, errors often return 200 with error in body
    if response.status() == 200 {
        let body: Value = response.json().await.unwrap();
        assert!(body["error"].is_object());
    }
}
```

## Concurrent Testing Patterns

### Parallel Operations

```rust
use futures::future::join_all;

#[tokio::test]
async fn test_concurrent_operations() {
    let (server, port) = TestServerBuilder::new().build().await;
    let client = Client::new();
    
    // Spawn concurrent tasks
    let handles: Vec<_> = (0..10)
        .map(|i| {
            let client = client.clone();
            tokio::spawn(async move {
                let session = create_session(&client, port).await;
                initialize_session(&client, port, &session, &format!("client-{}", i)).await
            })
        })
        .collect();
    
    // Wait for all to complete
    let results = join_all(handles).await;
    
    // Verify all succeeded
    for result in results {
        assert!(result.is_ok());
    }
}
```

### Race Condition Testing

```rust
use std::sync::Arc;
use tokio::sync::Barrier;

#[tokio::test]
async fn test_race_conditions() {
    let (server, port) = TestServerBuilder::new().build().await;
    let barrier = Arc::new(Barrier::new(5));
    
    let handles: Vec<_> = (0..5)
        .map(|_| {
            let barrier = barrier.clone();
            tokio::spawn(async move {
                // Synchronize start
                barrier.wait().await;
                
                // Perform operation
                // ...
            })
        })
        .collect();
    
    join_all(handles).await;
}
```

## Edge Case Testing Patterns

### Malformed Input Testing

```rust
#[tokio::test]
async fn test_malformed_inputs() {
    let test_cases = vec![
        ("empty body", ""),
        ("invalid json", "{not valid json}"),
        ("json array", "[1, 2, 3]"),
        ("json string", "\"just a string\""),
        ("json number", "42"),
        ("json null", "null"),
        ("truncated", "{\"jsonrpc\": \"2.0\", \"meth"),
    ];
    
    for (name, body) in test_cases {
        let response = client
            .post(&url)
            .header("Content-Type", "application/json")
            .body(body)
            .send()
            .await
            .unwrap();
        
        // Should not panic, should return error
        assert!(
            response.status() == 400 || 
            response.json::<Value>().await.unwrap()["error"].is_object(),
            "Failed for case: {}", name
        );
    }
}
```

### Large Payload Testing

```rust
fn generate_large_payload(size_mb: usize) -> String {
    let size_bytes = size_mb * 1024 * 1024;
    let padding = "x".repeat(size_bytes);
    
    json!({
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": {
            "name": "test",
            "arguments": { "data": padding }
        },
        "id": 1
    }).to_string()
}

#[tokio::test]
async fn test_oversized_payload() {
    let large_payload = generate_large_payload(3); // 3MB
    
    let response = client
        .post(&url)
        .header("Content-Type", "application/json")
        .body(large_payload)
        .send()
        .await;
    
    // Should handle gracefully, not panic
    assert!(response.is_ok() || response.is_err());
}
```

## Transport Detection Patterns

### Header Variation Testing

```rust
#[tokio::test]
async fn test_header_variations() {
    let header_cases = vec![
        // (Accept header, Upgrade header, expected transport)
        ("application/json", None, "http"),
        ("*/*", Some("websocket"), "websocket"),
        ("application/json, text/html", None, "http"),
        ("text/html, application/json", None, "http"),
        ("", None, "http"), // Empty accept
    ];
    
    for (accept, upgrade, expected) in header_cases {
        let mut req = client.post(&url);
        
        if !accept.is_empty() {
            req = req.header("Accept", accept);
        }
        
        if let Some(upgrade_val) = upgrade {
            req = req.header("Upgrade", upgrade_val)
                     .header("Connection", "upgrade");
        }
        
        let response = req.json(&init_request("test")).send().await.unwrap();
        
        // Verify correct transport was selected
        verify_transport_response(&response, expected);
    }
}
```

## Test Data Builders

### Request Builder Pattern

```rust
struct RequestBuilder {
    method: String,
    params: Value,
    id: Option<u64>,
}

impl RequestBuilder {
    fn new(method: &str) -> Self {
        Self {
            method: method.to_string(),
            params: json!({}),
            id: Some(1),
        }
    }
    
    fn params(mut self, params: Value) -> Self {
        self.params = params;
        self
    }
    
    fn notification(mut self) -> Self {
        self.id = None;
        self
    }
    
    fn build(self) -> Value {
        let mut req = json!({
            "jsonrpc": "2.0",
            "method": self.method,
            "params": self.params,
        });
        
        if let Some(id) = self.id {
            req["id"] = json!(id);
        }
        
        req
    }
}

// Usage
let request = RequestBuilder::new("tools/call")
    .params(json!({ "name": "echo", "arguments": { "msg": "test" } }))
    .build();
```

## Assertion Helpers

### Response Validation

```rust
trait ResponseExt {
    fn assert_success(&self);
    fn assert_error(&self, code: i32);
    fn get_result(&self) -> &Value;
}

impl ResponseExt for Value {
    fn assert_success(&self) {
        assert!(self["result"].is_object() || self["result"].is_array());
        assert!(self["error"].is_null());
    }
    
    fn assert_error(&self, code: i32) {
        assert!(self["error"].is_object());
        assert_eq!(self["error"]["code"], code);
    }
    
    fn get_result(&self) -> &Value {
        self.assert_success();
        &self["result"]
    }
}
```

## Performance Testing Patterns

### Timing Assertions

```rust
use std::time::Instant;

#[tokio::test]
async fn test_performance() {
    let start = Instant::now();
    
    // Perform operation
    let response = call_tool(&client, port, &session, "compute", json!({})).await;
    
    let duration = start.elapsed();
    
    // Assert completes within time limit
    assert!(
        duration.as_millis() < 100,
        "Operation took {}ms, expected <100ms",
        duration.as_millis()
    );
}
```

### Throughput Testing

```rust
#[tokio::test]
async fn test_throughput() {
    let start = Instant::now();
    let operation_count = 1000;
    
    for i in 0..operation_count {
        call_tool(&client, port, &session, "echo", 
            json!({ "msg": format!("test-{}", i) })).await;
    }
    
    let duration = start.elapsed();
    let ops_per_second = operation_count as f64 / duration.as_secs_f64();
    
    println!("Throughput: {:.2} ops/sec", ops_per_second);
    assert!(ops_per_second > 100.0); // Minimum expected throughput
}
```

## Test Organization Best Practices

### Module Structure

```rust
#[cfg(test)]
mod tests {
    use super::*;
    
    mod session_tests {
        use super::*;
        
        #[tokio::test]
        async fn test_session_creation() { /* ... */ }
        
        #[tokio::test]
        async fn test_session_persistence() { /* ... */ }
    }
    
    mod error_handling_tests {
        use super::*;
        
        #[tokio::test]
        async fn test_malformed_request() { /* ... */ }
    }
}
```

### Test Naming Conventions

- `test_<feature>_<scenario>_<expected_outcome>`
- Examples:
  - `test_session_reinitialization_updates_client_info`
  - `test_invalid_json_returns_parse_error`
  - `test_concurrent_requests_maintain_isolation`

## Debugging Helpers

### Request/Response Logging

```rust
async fn debug_request(client: &Client, url: &str, body: Value) -> Value {
    println!("REQUEST: {}", serde_json::to_string_pretty(&body).unwrap());
    
    let response = client
        .post(url)
        .json(&body)
        .send()
        .await
        .unwrap();
    
    let status = response.status();
    let body: Value = response.json().await.unwrap();
    
    println!("RESPONSE ({}): {}", status, serde_json::to_string_pretty(&body).unwrap());
    
    body
}
```

### Test Environment Setup

```rust
fn init_test_logging() {
    let _ = env_logger::builder()
        .filter_level(log::LevelFilter::Debug)
        .is_test(true)
        .try_init();
}

#[tokio::test]
async fn test_with_logging() {
    init_test_logging();
    // Test implementation
}
```

## Conclusion

These patterns provide a comprehensive foundation for writing consistent, maintainable tests for SolidMCP. They emphasize clarity, reusability, and proper error handling while following Rust best practices.