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
//! Resource Metadata and Content Type Tests
//!
//! Tests for resource metadata handling, MIME type validation, content encoding,
//! and various data formats across different transport layers.

use {
    anyhow::Result,
    async_trait::async_trait,
    futures_util::{SinkExt, StreamExt},
    serde_json::{json, Value},
    std::{sync::Arc, time::Duration},
    tokio_tungstenite::{connect_async, tungstenite::Message},
    solidmcp::{
        framework::{McpServerBuilder, ResourceProvider},
        handler::{ResourceContent, ResourceInfo},
    },
};

mod mcp_test_helpers;
use mcp_test_helpers::*;

/// Resource provider that tests various metadata and content types
#[derive(Debug)]
struct MetadataTestResourceProvider;

#[async_trait]
impl ResourceProvider<()> for MetadataTestResourceProvider {
    async fn list_resources(&self, _context: Arc<()>) -> Result<Vec<ResourceInfo>> {
        Ok(vec![
            // Text formats
            ResourceInfo {
                uri: "data://text/plain".to_string(),
                name: "plain_text".to_string(),
                description: Some("Plain text content".to_string()),
                mime_type: Some("text/plain".to_string()),
            },
            ResourceInfo {
                uri: "data://text/markdown".to_string(),
                name: "markdown_content".to_string(),
                description: Some("Markdown formatted content".to_string()),
                mime_type: Some("text/markdown".to_string()),
            },
            ResourceInfo {
                uri: "data://text/html".to_string(),
                name: "html_content".to_string(),
                description: Some("HTML formatted content".to_string()),
                mime_type: Some("text/html".to_string()),
            },
            // Structured data formats
            ResourceInfo {
                uri: "data://application/json".to_string(),
                name: "json_data".to_string(),
                description: Some("JSON structured data".to_string()),
                mime_type: Some("application/json".to_string()),
            },
            ResourceInfo {
                uri: "data://application/xml".to_string(),
                name: "xml_data".to_string(),
                description: Some("XML structured data".to_string()),
                mime_type: Some("application/xml".to_string()),
            },
            ResourceInfo {
                uri: "data://text/csv".to_string(),
                name: "csv_data".to_string(),
                description: Some("CSV tabular data".to_string()),
                mime_type: Some("text/csv".to_string()),
            },
            // Binary-like content represented as text
            ResourceInfo {
                uri: "data://application/base64".to_string(),
                name: "base64_data".to_string(),
                description: Some("Base64 encoded data".to_string()),
                mime_type: Some("application/octet-stream".to_string()),
            },
            // Resource without MIME type
            ResourceInfo {
                uri: "data://unknown/format".to_string(),
                name: "unknown_format".to_string(),
                description: Some("Content with no specified MIME type".to_string()),
                mime_type: None,
            },
            // Resource without description
            ResourceInfo {
                uri: "data://minimal/info".to_string(),
                name: "minimal_info".to_string(),
                description: None,
                mime_type: Some("text/plain".to_string()),
            },
            // Large content resource
            ResourceInfo {
                uri: "data://large/content".to_string(),
                name: "large_content".to_string(),
                description: Some("Large content for size testing".to_string()),
                mime_type: Some("text/plain".to_string()),
            },
        ])
    }

    async fn read_resource(&self, uri: &str, _context: Arc<()>) -> Result<ResourceContent> {
        match uri {
            "data://text/plain" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("text/plain".to_string()),
                content: "This is plain text content with special characters: äöü, 日本語, 🚀".to_string(),
            }),
            "data://text/markdown" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("text/markdown".to_string()),
                content: "# Markdown Content\n\n## Features\n\n- **Bold text**\n- *Italic text*\n- [Link](https://example.com)\n- `Code snippet`\n\n```rust\nfn hello() {\n    println!(\"Hello, world!\");\n}\n```".to_string(),
            }),
            "data://text/html" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("text/html".to_string()),
                content: "<!DOCTYPE html>\n<html>\n<head><title>Test</title></head>\n<body>\n<h1>HTML Content</h1>\n<p>This is <strong>HTML</strong> with <em>formatting</em>.</p>\n<a href=\"https://example.com\">Link</a>\n</body>\n</html>".to_string(),
            }),
            "data://application/json" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("application/json".to_string()),
                content: json!({
                    "name": "Test Data",
                    "version": "1.0.0",
                    "features": ["json", "parsing", "validation"],
                    "metadata": {
                        "created": "2025-01-31T00:00:00Z",
                        "author": "test"
                    },
                    "numbers": [1, 2, 3.14, -42],
                    "boolean": true,
                    "null_value": null
                }).to_string(),
            }),
            "data://application/xml" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("application/xml".to_string()),
                content: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n  <item id=\"1\">\n    <name>Test Item</name>\n    <value>42</value>\n    <active>true</active>\n  </item>\n  <item id=\"2\">\n    <name>Another Item</name>\n    <value>3.14</value>\n    <active>false</active>\n  </item>\n</root>".to_string(),
            }),
            "data://text/csv" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("text/csv".to_string()),
                content: "name,age,city,score\nAlice,30,New York,95.5\nBob,25,London,87.2\nCharlie,35,Tokyo,92.1\nDiana,28,\"San Francisco\",89.7".to_string(),
            }),
            "data://application/base64" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("application/octet-stream".to_string()),
                content: "SGVsbG8sIFdvcmxkISBUaGlzIGlzIGEgYmFzZTY0IGVuY29kZWQgbWVzc2FnZS4gSXQgY29udGFpbnMgc3BlY2lhbCBjaGFyYWN0ZXJzOiDDpMO2w7wsIOaXpeacrOiqniwg8J+agA==".to_string(),
            }),
            "data://unknown/format" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: None,
                content: "This content has no specified MIME type and could be anything.".to_string(),
            }),
            "data://minimal/info" => Ok(ResourceContent {
                uri: uri.to_string(),
                mime_type: Some("text/plain".to_string()),
                content: "Minimal resource info.".to_string(),
            }),
            "data://large/content" => {
                // Generate large content (about 50KB)
                let mut large_content = String::new();
                for i in 0..1000 {
                    large_content.push_str(&format!("Line {}: This is a long line of text to test large content handling in the resource system. It contains enough text to make the overall content size significant for testing purposes.\n", i + 1));
                }
                Ok(ResourceContent {
                    uri: uri.to_string(),
                    mime_type: Some("text/plain".to_string()),
                    content: large_content,
                })
            },
            _ => Err(anyhow::anyhow!("Resource not found: {}", uri)),
        }
    }
}

/// Create test server with metadata provider
async fn create_metadata_test_server() -> Result<solidmcp::McpServer> {
    McpServerBuilder::new((), "metadata-test-server", "1.0.0")
        .with_resource_provider(Box::new(MetadataTestResourceProvider))
        .build()
        .await
}

/// Test various MIME types and content formats
#[tokio::test]
async fn test_mime_types_and_formats() -> Result<()> {
    init_test_tracing();

    with_metadata_test_server("mime_types_test", |server| async move {
        let (ws_stream, _) = connect_async(&server.ws_url()).await?;
        let (mut write, mut read) = ws_stream.split();

        // Initialize
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        });

        write.send(Message::Text(init_request.to_string().into())).await?;
        receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;

        // Test different content types
        let test_cases = vec![
            ("data://text/plain", "text/plain", "special characters"),
            ("data://text/markdown", "text/markdown", "# Markdown"),
            ("data://text/html", "text/html", "<!DOCTYPE html>"),
            ("data://application/json", "application/json", "\"name\""),
            ("data://application/xml", "application/xml", "<?xml version"),
            ("data://text/csv", "text/csv", "name,age,city"),
        ];

        for (uri, expected_mime, content_check) in test_cases {
            let read_request = json!({
                "jsonrpc": "2.0",
                "id": 2,
                "method": "resources/read",
                "params": {
                    "uri": uri
                }
            });

            write.send(Message::Text(read_request.to_string().into())).await?;
            let response = receive_ws_message(&mut read, Duration::from_secs(5)).await
                .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
            let parsed: Value = serde_json::from_str(&response)?;

            assert!(parsed["result"].is_object());
            let content = &parsed["result"]["contents"][0];
            assert_eq!(content["uri"], uri);
            assert_eq!(content["mimeType"], expected_mime);
            assert!(content["text"].as_str().unwrap().contains(content_check));
        }

        Ok(())
    }).await
}

/// Test resource metadata completeness
#[tokio::test]
async fn test_resource_metadata() -> Result<()> {
    init_test_tracing();

    with_metadata_test_server("metadata_test", |server| async move {
        let (ws_stream, _) = connect_async(&server.ws_url()).await?;
        let (mut write, mut read) = ws_stream.split();

        // Initialize
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        });

        write.send(Message::Text(init_request.to_string().into())).await?;
        receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;

        // List resources to check metadata
        let list_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "resources/list",
            "params": {}
        });

        write.send(Message::Text(list_request.to_string().into())).await?;
        let response = receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
        let parsed: Value = serde_json::from_str(&response)?;

        let resources = parsed["result"]["resources"].as_array().unwrap();
        assert_eq!(resources.len(), 10);

        // Find and verify specific resources
        let json_resource = resources.iter()
            .find(|r| r["name"] == "json_data")
            .unwrap();
        assert_eq!(json_resource["uri"], "data://application/json");
        assert_eq!(json_resource["mimeType"], "application/json");
        assert_eq!(json_resource["description"], "JSON structured data");

        // Check resource without description
        let minimal_resource = resources.iter()
            .find(|r| r["name"] == "minimal_info")
            .unwrap();
        assert_eq!(minimal_resource["uri"], "data://minimal/info");
        assert_eq!(minimal_resource["mimeType"], "text/plain");
        assert!(minimal_resource["description"].is_null());

        // Check resource without MIME type
        let unknown_resource = resources.iter()
            .find(|r| r["name"] == "unknown_format")
            .unwrap();
        assert_eq!(unknown_resource["uri"], "data://unknown/format");
        assert!(unknown_resource["mimeType"].is_null());

        Ok(())
    }).await
}

/// Test large content handling
#[tokio::test]
async fn test_large_content_handling() -> Result<()> {
    init_test_tracing();

    with_metadata_test_server("large_content_test", |server| async move {
        let (ws_stream, _) = connect_async(&server.ws_url()).await?;
        let (mut write, mut read) = ws_stream.split();

        // Initialize
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        });

        write.send(Message::Text(init_request.to_string().into())).await?;
        receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;

        // Request large content
        let read_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "resources/read",
            "params": {
                "uri": "data://large/content"
            }
        });

        write.send(Message::Text(read_request.to_string().into())).await?;
        let response = receive_ws_message(&mut read, Duration::from_secs(10)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
        let parsed: Value = serde_json::from_str(&response)?;

        assert!(parsed["result"].is_object());
        let content = &parsed["result"]["contents"][0];
        assert_eq!(content["uri"], "data://large/content");
        assert_eq!(content["mimeType"], "text/plain");
        
        let text_content = content["text"].as_str().unwrap();
        assert!(text_content.len() > 40000); // Should be around 50KB
        assert!(text_content.contains("Line 1:"));
        assert!(text_content.contains("Line 1000:"));

        Ok(())
    }).await
}

/// Test content with Unicode and special characters
#[tokio::test]
async fn test_unicode_content() -> Result<()> {
    init_test_tracing();

    with_metadata_test_server("unicode_test", |server| async move {
        let (ws_stream, _) = connect_async(&server.ws_url()).await?;
        let (mut write, mut read) = ws_stream.split();

        // Initialize
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        });

        write.send(Message::Text(init_request.to_string().into())).await?;
        receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;

        // Request content with Unicode
        let read_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "resources/read",
            "params": {
                "uri": "data://text/plain"
            }
        });

        write.send(Message::Text(read_request.to_string().into())).await?;
        let response = receive_ws_message(&mut read, Duration::from_secs(5)).await
            .map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
        let parsed: Value = serde_json::from_str(&response)?;

        let content = &parsed["result"]["contents"][0];
        let text = content["text"].as_str().unwrap();
        
        // Verify Unicode characters are preserved
        assert!(text.contains("äöü"));
        assert!(text.contains("日本語"));
        assert!(text.contains("🚀"));

        Ok(())
    }).await
}

// Helper function to create metadata test server
async fn start_metadata_test_server() -> Result<u16> {
    let port = find_available_port().await
        .map_err(|e| anyhow::anyhow!("Failed to find port: {}", e))?;
    let mut server = create_metadata_test_server().await?;
    
    tokio::spawn(async move {
        if let Err(e) = server.start(port).await {
            eprintln!("Metadata test server error: {e}");
        }
    });

    tokio::time::sleep(Duration::from_millis(100)).await;
    Ok(port)
}

// Custom test helper for metadata tests
async fn with_metadata_test_server<F, Fut, T>(
    test_name: &str,
    test_fn: F,
) -> Result<T>
where
    F: FnOnce(McpTestServer) -> Fut,
    Fut: std::future::Future<Output = Result<T>>,
{
    tracing::info!("🚀 Starting MCP metadata test server for: {}", test_name);

    let port = start_metadata_test_server().await?;
    let server = McpTestServer {
        port,
        server_handle: tokio::spawn(async {}),
    };

    tracing::info!("✅ MCP metadata test server started on port {}", server.port);
    let result = test_fn(server).await;
    tracing::info!("🛑 Stopping MCP metadata test server for: {}", test_name);

    result
}