json-mcp-server 0.1.0

A high-performance Model Context Protocol (MCP) server for comprehensive JSON file operations optimized for LLM interactions
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
use json_mcp_server::json_tools::JsonToolsHandler;
use json_mcp_server::mcp::{
    protocol::ToolCall,
    server::{MCPServer, ToolHandler},
};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper to create temporary test files
struct TestEnvironment {
    _temp_dir: TempDir,
    temp_path: PathBuf,
}

impl TestEnvironment {
    fn new() -> Self {
        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().to_path_buf();
        Self {
            _temp_dir: temp_dir,
            temp_path,
        }
    }

    fn create_json_file(&self, name: &str, content: &str) -> PathBuf {
        let file_path = self.temp_path.join(name);
        fs::write(&file_path, content).unwrap();
        file_path
    }

    fn read_json_file(&self, name: &str) -> String {
        let file_path = self.temp_path.join(name);
        fs::read_to_string(file_path).unwrap()
    }
}

fn create_args(pairs: &[(&str, Value)]) -> HashMap<String, Value> {
    pairs.iter()
        .map(|(k, v)| (k.to_string(), v.clone()))
        .collect()
}

async fn call_tool(handler: &JsonToolsHandler, name: &str, args: HashMap<String, Value>) -> Result<String, String> {
    let result = handler.call_tool(ToolCall {
        name: name.to_string(),
        arguments: args,
    }).await;

    match result {
        Ok(tool_result) => {
            if tool_result.is_error.unwrap_or(false) {
                Err(tool_result.content.get(0).map(|c| c.text.clone()).unwrap_or_default())
            } else {
                Ok(tool_result.content.get(0).map(|c| c.text.clone()).unwrap_or_default())
            }
        }
        Err(e) => Err(e.to_string())
    }
}

#[tokio::test]
async fn test_complete_json_workflow() {
    let env = TestEnvironment::new();
    let handler = JsonToolsHandler::new();

    // Test data
    let initial_data = json!({
        "users": [
            {"id": 1, "name": "Alice", "age": 30, "active": true},
            {"id": 2, "name": "Bob", "age": 25, "active": false},
            {"id": 3, "name": "Charlie", "age": 35, "active": true}
        ],
        "metadata": {
            "total": 3,
            "created": "2025-01-01"
        }
    });

    // 1. Write initial data
    let file_path = env.temp_path.join("users.json");
    let write_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", initial_data.clone()),
    ]);

    let result = call_tool(&handler, "json-write", write_args).await;
    assert!(result.is_ok(), "Failed to write initial data: {:?}", result);

    // 2. Validate the file
    let validate_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-validate", validate_args).await;
    assert!(result.is_ok(), "Validation failed: {:?}", result);
    assert!(result.unwrap().contains("is valid"));

    // 3. Read the entire file
    let read_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-read", read_args).await;
    assert!(result.is_ok(), "Failed to read file: {:?}", result);
    let content = result.unwrap();
    assert!(content.contains("Alice"));
    assert!(content.contains("Bob"));
    assert!(content.contains("Charlie"));

    // 4. Query active users only
    let query_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("query", json!("$.users[?(@.active == true)].name")),
    ]);

    let result = call_tool(&handler, "json-query", query_args).await;
    assert!(result.is_ok(), "Query failed: {:?}", result);
    let query_result = result.unwrap();
    assert!(query_result.contains("Alice"));
    assert!(query_result.contains("Charlie"));
    assert!(!query_result.contains("Bob")); // Bob is inactive

    // 5. Add new user via merge
    let new_user_data = json!({
        "users": [
            {"id": 4, "name": "Diana", "age": 28, "active": true}
        ]
    });

    let merge_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", new_user_data),
        ("mode", json!("merge")),
    ]);

    let result = call_tool(&handler, "json-write", merge_args).await;
    assert!(result.is_ok(), "Merge failed: {:?}", result);

    // 6. Verify the merge worked
    let final_read_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("query", json!("$.users[*].name")),
    ]);

    let result = call_tool(&handler, "json-read", final_read_args).await;
    assert!(result.is_ok(), "Final read failed: {:?}", result);
    let final_content = result.unwrap();
    assert!(final_content.contains("Diana"));
}

#[tokio::test]
async fn test_all_write_modes() {
    let env = TestEnvironment::new();
    let handler = JsonToolsHandler::new();

    // Test replace mode (default)
    let file_path = env.temp_path.join("write_test.json");
    let initial_data = json!({"name": "test", "value": 1});

    let replace_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", initial_data),
        ("mode", json!("replace")),
    ]);

    let result = call_tool(&handler, "json-write", replace_args).await;
    assert!(result.is_ok());

    // Test merge mode
    let merge_data = json!({"extra": "field", "value": 2});
    let merge_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", merge_data),
        ("mode", json!("merge")),
    ]);

    let result = call_tool(&handler, "json-write", merge_args).await;
    assert!(result.is_ok());

    // Verify merge result
    let read_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-read", read_args).await;
    assert!(result.is_ok());
    let content = result.unwrap();
    assert!(content.contains("test")); // Original name
    assert!(content.contains("extra")); // Merged field
    assert!(content.contains("\"value\": 2")); // Updated value

    // Test append mode with array
    let array_file = env.temp_path.join("array_test.json");
    let initial_array = json!([1, 2, 3]);

    let array_args = create_args(&[
        ("file_path", json!(array_file.to_string_lossy())),
        ("data", initial_array),
    ]);

    let result = call_tool(&handler, "json-write", array_args).await;
    assert!(result.is_ok());

    // Append to array
    let append_data = json!([4, 5]);
    let append_args = create_args(&[
        ("file_path", json!(array_file.to_string_lossy())),
        ("data", append_data),
        ("mode", json!("append")),
    ]);

    let result = call_tool(&handler, "json-write", append_args).await;
    assert!(result.is_ok());

    // Verify append
    let read_array_args = create_args(&[
        ("file_path", json!(array_file.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-read", read_array_args).await;
    assert!(result.is_ok());
    let array_content = result.unwrap();
    assert!(array_content.contains("1"));
    assert!(array_content.contains("5")); // Should contain both original and appended
}

#[tokio::test]
async fn test_complex_jsonpath_queries() {
    let env = TestEnvironment::new();
    let handler = JsonToolsHandler::new();

    // Create complex test data
    let complex_data = json!({
        "store": {
            "book": [
                {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
                },
                {
                    "category": "fiction",
                    "author": "Evelyn Waugh", 
                    "title": "Sword of Honour",
                    "price": 12.99
                },
                {
                    "category": "fiction",
                    "author": "Herman Melville",
                    "title": "Moby Dick",
                    "price": 8.99
                }
            ],
            "bicycle": {
                "color": "red",
                "price": 19.95
            }
        }
    });

    let file_path = env.temp_path.join("bookstore.json");
    let write_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", complex_data),
    ]);

    let result = call_tool(&handler, "json-write", write_args).await;
    assert!(result.is_ok());

    // Test various JSONPath queries
    let test_cases = vec![
        // All book titles
        ("$.store.book[*].title", vec!["Sayings of the Century", "Sword of Honour", "Moby Dick"]),
        // Books under $10
        ("$.store.book[?(@.price < 10)].title", vec!["Sayings of the Century", "Moby Dick"]),
        // Fiction books
        ("$.store.book[?(@.category == 'fiction')].author", vec!["Evelyn Waugh", "Herman Melville"]),
        // All prices in the store
        ("$.store..price", vec!["8.95", "12.99", "8.99", "19.95"]),
    ];

    for (query, expected_content) in test_cases {
        let query_args = create_args(&[
            ("file_path", json!(file_path.to_string_lossy())),
            ("query", json!(query)),
        ]);

        let result = call_tool(&handler, "json-query", query_args).await;
        assert!(result.is_ok(), "Query '{}' failed: {:?}", query, result);
        
        let query_result = result.unwrap();
        for expected in expected_content {
            assert!(query_result.contains(expected), 
                   "Query '{}' result should contain '{}', but got: {}", 
                   query, expected, query_result);
        }
    }
}

#[tokio::test]
async fn test_error_handling() {
    let env = TestEnvironment::new();
    let handler = JsonToolsHandler::new();

    // Test missing file
    let missing_file = env.temp_path.join("nonexistent.json");
    let read_args = create_args(&[
        ("file_path", json!(missing_file.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-read", read_args).await;
    assert!(result.is_err(), "Should fail for missing file");

    // Test invalid JSON
    let invalid_file = env.create_json_file("invalid.json", "{invalid json");
    let validate_args = create_args(&[
        ("file_path", json!(invalid_file.to_string_lossy())),
    ]);

    let result = call_tool(&handler, "json-validate", validate_args).await;
    assert!(result.is_err(), "Should fail for invalid JSON");

    // Test missing required parameters
    let empty_args = HashMap::new();
    let result = call_tool(&handler, "json-read", empty_args).await;
    assert!(result.is_err(), "Should fail without file_path");
    assert!(result.unwrap_err().contains("file_path is required"));

    // Test invalid JSONPath
    let valid_file = env.create_json_file("valid.json", r#"{"test": "data"}"#);
    let invalid_query_args = create_args(&[
        ("file_path", json!(valid_file.to_string_lossy())),
        ("query", json!("$[invalid")), // Invalid JSONPath syntax
    ]);

    let result = call_tool(&handler, "json-query", invalid_query_args).await;
    assert!(result.is_err(), "Should fail for invalid JSONPath");
}

#[tokio::test]
async fn test_large_file_simulation() {
    let env = TestEnvironment::new();
    let handler = JsonToolsHandler::new();

    // Create a larger JSON structure
    let mut large_data = json!({
        "records": [],
        "metadata": {
            "total": 1000,
            "generated": "2025-01-01"
        }
    });

    // Add 1000 records
    for i in 0..1000 {
        large_data["records"].as_array_mut().unwrap().push(json!({
            "id": i,
            "name": format!("User {}", i),
            "score": i * 10,
            "active": i % 2 == 0
        }));
    }

    let file_path = env.temp_path.join("large_data.json");
    let write_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("data", large_data),
    ]);

    let result = call_tool(&handler, "json-write", write_args).await;
    assert!(result.is_ok(), "Failed to write large file");

    // Test reading with limit
    let limited_read_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("query", json!("$.records[*].id")),
        ("limit", json!(10)),
    ]);

    let result = call_tool(&handler, "json-read", limited_read_args).await;
    assert!(result.is_ok(), "Failed to read with limit");
    
    // Test reading with offset
    let offset_read_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("query", json!("$.records[*].id")),
        ("limit", json!(5)),
        ("offset", json!(10)),
    ]);

    let result = call_tool(&handler, "json-read", offset_read_args).await;
    assert!(result.is_ok(), "Failed to read with offset");

    // Test high-score query
    let high_score_args = create_args(&[
        ("file_path", json!(file_path.to_string_lossy())),
        ("query", json!("$.records[?(@.score > 5000)].name")),
    ]);

    let result = call_tool(&handler, "json-query", high_score_args).await;
    assert!(result.is_ok(), "Failed high score query");
    let high_score_result = result.unwrap();
    assert!(high_score_result.contains("User 50")); // Score would be 500, so this shouldn't match
}

#[tokio::test]
async fn test_help_system_comprehensive() {
    let handler = JsonToolsHandler::new();
    
    let help_topics = vec![
        ("", "JSON MCP Server Help"), // Default topic content
        ("overview", "JSON MCP Server Help"),
        ("reading", "Reading JSON Files"),
        ("writing", "Writing JSON Files"),
        ("querying", "Querying JSON with JSONPath"),
        ("streaming", "Streaming Large JSON Files"),
        ("examples", "Practical JSON Tool Examples"),
        ("tools", "Individual Tool Help"),
    ];

    for (topic, expected_content) in help_topics {
        let args = if topic.is_empty() {
            HashMap::new()
        } else {
            create_args(&[("topic", json!(topic))])
        };

        let result = call_tool(&handler, "json-help", args).await;
        assert!(result.is_ok(), "Help topic '{}' failed: {:?}", topic, result);
        
        let help_content = result.unwrap();
        assert!(help_content.contains(expected_content), 
               "Help topic '{}' should contain '{}', but got: {}", 
               topic, expected_content, help_content);
    }
}

#[tokio::test]
async fn test_mcp_protocol_integration() {
    let handler = JsonToolsHandler::new();
    let mut server = MCPServer::new(handler);
    
    // Test tool registration
    let result = server.register_tools().await;
    assert!(result.is_ok(), "Failed to register tools");

    // Test tools/list method
    let tools_request = r#"{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}"#;
    let response = server.handle_request(tools_request).await;
    assert!(response.is_ok(), "tools/list failed");
    
    let response_str = response.unwrap();
    assert!(response_str.contains("json-read"));
    assert!(response_str.contains("json-write"));
    assert!(response_str.contains("json-query"));
    assert!(response_str.contains("json-validate"));
    assert!(response_str.contains("json-help"));

    // Test actual tool call through MCP
    let env = TestEnvironment::new();
    let test_file = env.create_json_file("mcp_test.json", r#"{"test": "mcp integration"}"#);
    
    let tool_call_request = format!(
        r#"{{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {{"name": "json-validate", "arguments": {{"file_path": "{}"}}}}}}"#,
        test_file.to_string_lossy().replace('\\', "\\\\")
    );
    
    let response = server.handle_request(&tool_call_request).await;
    assert!(response.is_ok(), "MCP tool call failed");
    
    let response_str = response.unwrap();
    assert!(response_str.contains("is valid"));
}