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
//! Capability Negotiation Unit Tests
//!
//! Tests for MCP capability negotiation and feature detection

#[cfg(test)]
mod tests {
    use crate::protocol_impl::McpProtocolHandlerImpl;
    use serde_json::json;

    /// Test basic capability negotiation
    #[tokio::test]
    async fn test_basic_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize with basic capabilities
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {
                        "listChanged": true
                    },
                    "resources": {
                        "subscribe": false,
                        "listChanged": false
                    }
                },
                "clientInfo": {
                    "name": "test-client",
                    "version": "1.0.0"
                }
            }
        });

        let result = handler.handle_message(init_request).await.unwrap();
        assert_eq!(result["jsonrpc"], "2.0");
        assert_eq!(result["id"], 1);

        let capabilities = &result["result"]["capabilities"];
        assert!(capabilities.is_object());

        // Server should advertise its capabilities
        if let Some(tools) = capabilities.get("tools") {
            assert!(tools.is_object());
        }
    }

    /// Test capability intersection (what both client and server support)
    #[tokio::test]
    async fn test_capability_intersection() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Client requests capabilities server doesn't support
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {
                        "listChanged": true
                    },
                    "prompts": {
                        "listChanged": true
                    },
                    "resources": {
                        "subscribe": true,
                        "listChanged": true
                    },
                    "logging": {}
                }
            }
        });

        let result = handler.handle_message(init_request).await.unwrap();
        let server_caps = &result["result"]["capabilities"];

        // Server should only advertise what it actually supports
        // Built-in handler supports tools but may not support all advanced features
        assert!(server_caps.get("tools").is_some());
    }

    /// Test version negotiation
    #[tokio::test]
    async fn test_version_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Test with exact supported version
        let supported_version = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });

        let result = handler.handle_message(supported_version).await.unwrap();
        assert_eq!(result["result"]["protocolVersion"], "2025-06-18");

        // Test with another supported version
        let mut handler2 = McpProtocolHandlerImpl::new();
        let different_version = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-03-26"
            }
        });

        let result = handler2.handle_message(different_version).await.unwrap();
        assert_eq!(result["result"]["protocolVersion"], "2025-03-26");
    }

    /// Test client info handling
    #[tokio::test]
    async fn test_client_info_handling() {
        let mut handler = McpProtocolHandlerImpl::new();

        let init_with_client_info = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "clientInfo": {
                    "name": "Claude Desktop",
                    "version": "0.7.1"
                }
            }
        });

        let result = handler.handle_message(init_with_client_info).await.unwrap();
        assert!(result["result"]["serverInfo"].is_object());

        let server_info = &result["result"]["serverInfo"];
        assert!(server_info["name"].is_string());
        assert!(server_info["version"].is_string());
    }

    /// Test tools capability negotiation
    #[tokio::test]
    async fn test_tools_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize with tools capability
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {
                        "listChanged": true
                    }
                }
            }
        });

        let result = handler.handle_message(init).await.unwrap();
        let capabilities = &result["result"]["capabilities"];

        // Should include tools capability
        assert!(capabilities.get("tools").is_some());

        // Should be able to list tools after negotiation
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list",
            "params": {}
        });

        let tools_result = handler.handle_message(tools_request).await.unwrap();
        assert!(tools_result["result"]["tools"].is_array());
    }

    /// Test resources capability negotiation
    #[tokio::test]
    async fn test_resources_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize requesting resources capability
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "resources": {
                        "subscribe": true,
                        "listChanged": false
                    }
                }
            }
        });

        let _result = handler.handle_message(init).await.unwrap();

        // Built-in handler may not support resources, but should respond gracefully
        let resources_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "resources/list",
            "params": {}
        });

        let resources_result = handler.handle_message(resources_request).await.unwrap();
        // Should either succeed with empty list or fail gracefully
        assert!(
            resources_result.get("result").is_some() || resources_result.get("error").is_some()
        );
    }

    /// Test prompts capability negotiation
    #[tokio::test]
    async fn test_prompts_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "prompts": {
                        "listChanged": true
                    }
                }
            }
        });

        let _result = handler.handle_message(init).await.unwrap();

        // Test prompts/list
        let prompts_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "prompts/list",
            "params": {}
        });

        let prompts_result = handler.handle_message(prompts_request).await.unwrap();
        // Should either succeed or fail gracefully
        assert!(prompts_result.get("result").is_some() || prompts_result.get("error").is_some());
    }

    /// Test logging capability negotiation
    #[tokio::test]
    async fn test_logging_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "logging": {}
                }
            }
        });

        let result = handler.handle_message(init).await.unwrap();

        // Test logging notification
        let log_notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/message",
            "params": {
                "level": "info",
                "message": "Test log message"
            }
        });

        let log_result = handler.handle_message(log_notification).await.unwrap();
        // Notifications should be processed successfully
        assert!(log_result.as_object().unwrap().is_empty() || log_result.get("id").is_some());
    }

    /// Test sampling capability negotiation
    #[tokio::test]
    async fn test_sampling_capability_negotiation() {
        let mut handler = McpProtocolHandlerImpl::new();

        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "sampling": {}
                }
            }
        });

        let result = handler.handle_message(init).await.unwrap();

        // Test sampling request (may not be supported by built-in handler)
        let sampling_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "sampling/createMessage",
            "params": {
                "messages": [
                    {
                        "role": "user",
                        "content": {
                            "type": "text",
                            "text": "Hello"
                        }
                    }
                ]
            }
        });

        let sampling_result = handler.handle_message(sampling_request).await.unwrap();
        // Should fail gracefully if not supported
        assert!(sampling_result.get("error").is_some() || sampling_result.get("result").is_some());
    }

    /// Test empty capabilities
    #[tokio::test]
    async fn test_empty_capabilities() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize with no capabilities
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });

        let result = handler.handle_message(init).await.unwrap();
        assert!(result["result"]["capabilities"].is_object());

        // Should still be able to use basic functionality
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list",
            "params": {}
        });

        let tools_result = handler.handle_message(tools_request).await.unwrap();
        assert!(tools_result.get("result").is_some());
    }

    /// Test missing capabilities field
    #[tokio::test]
    async fn test_missing_capabilities_field() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize without capabilities field
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });

        let result = handler.handle_message(init).await.unwrap();
        assert!(result["result"]["capabilities"].is_object());

        // Should work with default capabilities
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list",
            "params": {}
        });

        let tools_result = handler.handle_message(tools_request).await.unwrap();
        assert!(tools_result.get("result").is_some());
    }

    /// Test capability-dependent method availability
    #[tokio::test]
    async fn test_capability_dependent_methods() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize with minimal capabilities
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {}
                }
            }
        });

        handler.handle_message(init).await.unwrap();

        // Methods that should work with basic tools capability
        let valid_methods = vec!["tools/list", "tools/call"];

        for method in valid_methods {
            let request = json!({
                "jsonrpc": "2.0",
                "id": 2,
                "method": method,
                "params": {}
            });

            let result = handler.handle_message(request).await.unwrap();
            // Should not return "method not found" error
            if let Some(error) = result.get("error") {
                let code = error["code"].as_i64().unwrap_or(0);
                assert_ne!(
                    code, -32601,
                    "Method {method} should be available with tools capability"
                );
            }
        }
    }

    /// Test progressive capability discovery
    #[tokio::test]
    async fn test_progressive_capability_discovery() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Start with basic capabilities
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {
                        "listChanged": false
                    }
                }
            }
        });

        let result = handler.handle_message(init).await.unwrap();
        let server_caps = &result["result"]["capabilities"];

        // Check what server actually supports
        let supports_tools = server_caps.get("tools").is_some();
        let supports_resources = server_caps.get("resources").is_some();
        let supports_prompts = server_caps.get("prompts").is_some();

        // Verify tools work if advertised
        if supports_tools {
            let tools_request = json!({
                "jsonrpc": "2.0",
                "id": 2,
                "method": "tools/list",
                "params": {}
            });

            let tools_result = handler.handle_message(tools_request).await.unwrap();
            assert!(tools_result.get("result").is_some());
        }

        // Test other capabilities only if advertised
        if supports_resources {
            let resources_request = json!({
                "jsonrpc": "2.0",
                "id": 3,
                "method": "resources/list",
                "params": {}
            });

            let resources_result = handler.handle_message(resources_request).await.unwrap();
            assert!(resources_result.get("result").is_some());
        }

        if supports_prompts {
            let prompts_request = json!({
                "jsonrpc": "2.0",
                "id": 4,
                "method": "prompts/list",
                "params": {}
            });

            let prompts_result = handler.handle_message(prompts_request).await.unwrap();
            assert!(prompts_result.get("result").is_some());
        }
    }
}