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
//! JSON-RPC 2.0 Compliance Tests
//!
//! Tests to ensure strict compliance with JSON-RPC 2.0 specification

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

    /// Test that all responses include required JSON-RPC 2.0 fields
    #[tokio::test]
    async fn test_response_format_compliance() {
        let mut handler = McpProtocolHandlerImpl::new();

        let request = json!({
            "jsonrpc": "2.0",
            "id": 12345,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });

        let response = handler.handle_message(request).await.unwrap();

        // JSON-RPC 2.0 required fields
        assert_eq!(response["jsonrpc"], "2.0");
        assert_eq!(response["id"], 12345);

        // Must have either result or error, but not both
        let has_result = response.get("result").is_some();
        let has_error = response.get("error").is_some();
        assert!(has_result || has_error);
        assert!(!(has_result && has_error));
    }

    /// Test JSON-RPC 2.0 error object format
    #[tokio::test]
    async fn test_error_object_compliance() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Send an invalid request to trigger an error
        let invalid_request = json!({
            "jsonrpc": "2.0",
            "id": "test-error",
            "method": "unknown/method",
            "params": {}
        });

        let response = handler.handle_message(invalid_request).await.unwrap();

        assert_eq!(response["jsonrpc"], "2.0");
        assert_eq!(response["id"], "test-error");
        assert!(response.get("error").is_some());

        let error = &response["error"];

        // JSON-RPC 2.0 error object requirements
        assert!(error.get("code").is_some());
        assert!(error.get("message").is_some());

        // Code must be integer
        assert!(error["code"].is_i64());

        // Message must be string
        assert!(error["message"].is_string());

        // Data field is optional but if present should be structured
        if let Some(data) = error.get("data") {
            assert!(data.is_object() || data.is_array() || data.is_string());
        }
    }

    /// Test standard JSON-RPC 2.0 error codes
    #[tokio::test]
    async fn test_standard_error_codes() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Test -32601 Method not found
        let method_not_found = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "completely/unknown/method",
            "params": {}
        });

        let response = handler.handle_message(method_not_found).await.unwrap();
        if let Some(error) = response.get("error") {
            let code = error["code"].as_i64().unwrap();
            assert_eq!(code, -32601);
        }

        // Test -32600 Invalid Request (malformed request)
        let invalid_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": 123, // Method should be string
            "params": {}
        });

        let response = handler.handle_message(invalid_request).await;
        // Should either fail or return -32600
        if let Ok(response) = response {
            if let Some(error) = response.get("error") {
                let code = error["code"].as_i64().unwrap();
                assert!(code == -32600 || code == -32602); // Invalid Request or Invalid params
            }
        }
    }

    /// Test notification handling (no response expected)
    #[tokio::test]
    async fn test_notification_compliance() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Notifications have no ID field
        let notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/cancel",
            "params": {
                "requestId": "some-request"
            }
        });

        let response = handler.handle_message(notification).await.unwrap();

        // For notifications, response should be empty or have null ID
        if !response.as_object().unwrap().is_empty() {
            assert_eq!(response.get("id"), Some(&Value::Null));
        }
    }

    /// Test ID preservation and types
    #[tokio::test]
    async fn test_id_preservation() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Test different ID types that JSON-RPC 2.0 supports
        let test_cases = vec![
            (json!(42), json!(42)),                   // Number
            (json!("string-id"), json!("string-id")), // String
            (json!(null), json!(null)),               // Null (notification)
        ];

        for (request_id, expected_id) in test_cases {
            let request = json!({
                "jsonrpc": "2.0",
                "id": request_id,
                "method": "initialize",
                "params": {
                    "protocolVersion": "2025-06-18"
                }
            });

            let response = handler.handle_message(request).await.unwrap();
            assert_eq!(response["id"], expected_id);

            // Create new handler for each test to avoid "already initialized" error
            handler = McpProtocolHandlerImpl::new();
        }
    }

    /// Test parameter handling compliance
    #[tokio::test]
    async fn test_parameter_compliance() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Initialize first
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });
        handler.handle_message(init).await.unwrap();

        // Test with structured parameters (object)
        let with_object_params = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list",
            "params": {}
        });

        let response = handler.handle_message(with_object_params).await.unwrap();
        assert!(response.get("result").is_some() || response.get("error").is_some());

        // Test with array parameters (less common in MCP)
        let with_array_params = json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/list",
            "params": []
        });

        let response = handler.handle_message(with_array_params).await.unwrap();
        // Should handle gracefully (may convert to object or error)
        assert!(response.get("result").is_some() || response.get("error").is_some());

        // Test with no params field (should be treated as empty object)
        let without_params = json!({
            "jsonrpc": "2.0",
            "id": 4,
            "method": "tools/list"
        });

        let response = handler.handle_message(without_params).await.unwrap();
        assert!(response.get("result").is_some() || response.get("error").is_some());
    }

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

        // Request without jsonrpc field
        let without_version = json!({
            "id": 1,
            "method": "initialize",
            "params": {}
        });

        let result = handler.handle_message(without_version).await;
        // Should either return an error response or fail with an error
        match result {
            Ok(response) => {
                assert!(response.get("error").is_some());
            }
            Err(_) => {
                // Also acceptable - malformed JSON-RPC can fail entirely
            }
        }

        // Request with wrong version
        let wrong_version = json!({
            "jsonrpc": "1.0",
            "id": 2,
            "method": "initialize",
            "params": {}
        });

        let result = handler.handle_message(wrong_version).await;
        // Should either return an error response or fail with an error
        match result {
            Ok(response) => {
                assert!(response.get("error").is_some());
            }
            Err(_) => {
                // Also acceptable - malformed JSON-RPC can fail entirely
            }
        }

        // Request with non-string version
        let invalid_version = json!({
            "jsonrpc": 2.0,
            "id": 3,
            "method": "initialize",
            "params": {}
        });

        let result = handler.handle_message(invalid_version).await;
        // Should either return an error response or fail with an error
        match result {
            Ok(response) => {
                assert!(response.get("error").is_some());
            }
            Err(_) => {
                // Also acceptable - malformed JSON-RPC can fail entirely
            }
        }
    }

    /// Test handling of extra fields (should be ignored)
    #[tokio::test]
    async fn test_extra_fields_ignored() {
        let mut handler = McpProtocolHandlerImpl::new();

        let request_with_extras = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            },
            "extra_field": "should be ignored",
            "another_extra": 42,
            "nested_extra": {
                "foo": "bar"
            }
        });

        let response = handler.handle_message(request_with_extras).await.unwrap();
        assert_eq!(response["jsonrpc"], "2.0");
        assert_eq!(response["id"], 1);
        assert!(response.get("result").is_some());

        // Response should not include extra fields
        assert!(response.get("extra_field").is_none());
        assert!(response.get("another_extra").is_none());
        assert!(response.get("nested_extra").is_none());
    }

    /// Test batch request rejection (not supported by MCP)
    #[tokio::test]
    async fn test_batch_request_rejection() {
        let mut handler = McpProtocolHandlerImpl::new();

        // JSON-RPC 2.0 batch request (array of requests)
        let batch_request = json!([
            {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "initialize",
                "params": {}
            },
            {
                "jsonrpc": "2.0",
                "id": 2,
                "method": "tools/list",
                "params": {}
            }
        ]);

        // Should fail to parse as single request or return appropriate error
        let result = handler.handle_message(batch_request).await;
        // MCP doesn't support batch requests, so this should fail
        assert!(result.is_err());
    }

    /// Test large number handling (JSON-RPC allows arbitrary precision)
    #[tokio::test]
    async fn test_large_number_handling() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Test with large integer ID
        let large_id_request = json!({
            "jsonrpc": "2.0",
            "id": 9007199254740991i64, // Max safe integer in JavaScript
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });

        let response = handler.handle_message(large_id_request).await.unwrap();
        assert_eq!(response["id"], 9007199254740991i64);

        // Test with floating point ID (valid in JSON-RPC)
        let mut handler2 = McpProtocolHandlerImpl::new();
        let float_id_request = json!({
            "jsonrpc": "2.0",
            "id": 123.456,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18"
            }
        });

        let response = handler2.handle_message(float_id_request).await.unwrap();
        assert_eq!(response["id"], 123.456);
    }

    /// Test Unicode and special character handling
    #[tokio::test]
    async fn test_unicode_handling() {
        let mut handler = McpProtocolHandlerImpl::new();

        // Test with Unicode in ID and method
        let unicode_request = json!({
            "jsonrpc": "2.0",
            "id": "测试-тест-🔥",
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "clientInfo": {
                    "name": "测试客户端",
                    "version": "1.0.0-α"
                }
            }
        });

        let response = handler.handle_message(unicode_request).await.unwrap();
        assert_eq!(response["id"], "测试-тест-🔥");
        assert!(response.get("result").is_some());
    }

    /// Test response must be valid JSON
    #[tokio::test]
    async fn test_response_json_validity() {
        let mut handler = McpProtocolHandlerImpl::new();

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

        let response = handler.handle_message(request).await.unwrap();

        // Response should be serializable back to valid JSON
        let json_string = serde_json::to_string(&response).unwrap();
        let parsed_back: Value = serde_json::from_str(&json_string).unwrap();
        assert_eq!(response, parsed_back);
    }

    /// Test that response time is reasonable (performance aspect)
    #[tokio::test]
    async fn test_response_timing() {
        let mut handler = McpProtocolHandlerImpl::new();

        let start = std::time::Instant::now();

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

        let _response = handler.handle_message(request).await.unwrap();

        let elapsed = start.elapsed();

        // Should respond quickly (less than 1 second for simple operations)
        assert!(elapsed.as_secs() < 1, "Response took too long: {elapsed:?}");
    }
}