rustdoc-mcp 0.4.0

mcp server for rustdocs
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
use crate::{
    filter::Filter,
    state::RustdocTools,
    tools::{GetItem, ListCrates, Search, SetWorkingDirectory},
    verbosity::Verbosity,
};
use mcplease::traits::Tool;
use std::path::PathBuf;

/// Get the path to our test crate (fast to build, minimal dependencies)
fn get_test_crate_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../tests/test-crate")
}

/// Create a test state with isolated session
fn create_test_state() -> RustdocTools {
    let mut state = RustdocTools::new(None)
        .expect("Failed to create state")
        .with_default_session_id("test");

    SetWorkingDirectory {
        path: get_test_crate_path().to_string_lossy().to_string(),
    }
    .execute(&mut state)
    .unwrap();

    state
}

#[test]
fn test_get_crate_root() {
    let mut state = create_test_state();

    // Get the crate root
    let tool = GetItem {
        name: "crate".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_show_docs_vs_hide_docs_comparison() {
    let mut state = create_test_state();

    // First, get TestStruct with docs shown (default)
    let tool_with_docs = GetItem {
        name: "crate::TestStruct".to_string(),
        ..Default::default()
    };

    let result_with_docs = tool_with_docs
        .execute(&mut state)
        .expect("Tool execution failed");

    // Then get TestStruct with docs hidden
    let tool_no_docs = GetItem {
        name: "crate::TestStruct".to_string(),
        verbosity: Some(Verbosity::Minimal),
        ..Default::default()
    };

    let result_no_docs = tool_no_docs
        .execute(&mut state)
        .expect("Tool execution failed");

    // Verify the difference
    assert!(result_with_docs.len() > result_no_docs.len());

    // Both should contain the struct signature
    assert!(result_with_docs.contains("struct TestStruct"));
    assert!(result_no_docs.contains("struct TestStruct"));

    println!("=== WITH DOCS ({} chars) ===", result_with_docs.len());
    println!("{result_with_docs}");
    println!("\n=== WITHOUT DOCS ({} chars) ===", result_no_docs.len());
    println!("{result_no_docs}");
}

#[test]
fn test_verbosity_minimal() {
    let mut state = create_test_state();

    // Get the crate root with documentation hidden
    let tool = GetItem {
        name: "crate".to_string(),
        verbosity: Some(Verbosity::Minimal),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    // The result should not contain documentation text
    assert!(!result.contains("Documentation:"));

    // But should still contain structure information
    assert!(result.contains("Item: test_crate"));
    assert!(
        result.contains("Structs:") || result.contains("Enums:") || result.contains("Functions:")
    );

    insta::assert_snapshot!(result);
}

#[test]
fn test_fuzzy_matching_tool_execute() {
    let mut state = create_test_state();

    // Try to access a trait method with a typo - should find TestTrait methods
    let tool = GetItem {
        name: "crate::TestStruct::test_metod".to_string(), // typo: should suggest "test_method"
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}
#[test]
fn test_fuzzy_matching_trait_methods() {
    let mut state = create_test_state();

    // Try to access a trait method that should be available via impl
    // This tests whether we collect trait implementation methods
    let tool = GetItem {
        name: "crate::TestStruct::cute".to_string(), // Should suggest "clone" from Clone trait
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    // Should contain suggestions from trait implementations
    assert!(result.contains("Did you mean"));
    // Should suggest trait methods that are actually available
    // TestStruct implements Clone, so "clone" should be suggested for "cute"

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_struct_details() {
    let mut state = create_test_state();

    // Get TestStruct details
    let tool = GetItem {
        name: "crate::TestStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_struct_with_source() {
    let mut state = create_test_state();

    // Get TestStruct details with source
    let tool = GetItem {
        name: "crate::TestStruct".to_string(),
        include_source: Some(true),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");
    let test_crate_dir = state.get_context(None).unwrap().unwrap();

    // Normalize project path in source output
    let normalized_result = result.replace(
        &test_crate_dir.to_string_lossy().to_string(),
        "/TEST_CRATE_ROOT",
    );
    insta::assert_snapshot!(normalized_result);
}

#[test]
fn test_get_function_details() {
    let mut state = create_test_state();

    // Get test_function details with source
    let tool = GetItem {
        name: "crate::test_function".to_string(),
        include_source: Some(true),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");
    let test_crate_dir = state.get_context(None).unwrap().unwrap();

    // Normalize project path in source output
    let normalized_result = result.replace(
        &test_crate_dir.to_string_lossy().to_string(),
        "/TEST_CRATE_ROOT",
    );
    insta::assert_snapshot!(normalized_result);
}

#[test]
fn test_get_submodule() {
    let mut state = create_test_state();

    // Get submodule listing
    let tool = GetItem {
        name: "crate::submodule".to_string(),
        include_source: None,
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_enum_details() {
    let mut state = create_test_state();

    // Get TestEnum from submodule
    let tool = GetItem {
        name: "crate::submodule::TestEnum".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_generic_struct() {
    let mut state = create_test_state();

    // Get GenericStruct details
    let tool = GetItem {
        name: "crate::GenericStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_generic_function() {
    let mut state = create_test_state();

    // Get generic_function details
    let tool = GetItem {
        name: "crate::generic_function".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_constants() {
    let mut state = create_test_state();

    // Get constant
    let tool = GetItem {
        name: "crate::TEST_CONSTANT".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_struct_with_private_fields() {
    let mut state = create_test_state();

    // Get GenericStruct to see hidden field indicator
    let tool = GetItem {
        name: "crate::GenericStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_fuzzy_matching_suggestions() {
    let mut state = create_test_state();

    // Try to get a non-existent item that should trigger fuzzy suggestions
    let tool = GetItem {
        name: "crate::TestStruct::incrementCount".to_string(), // typo: should be increment_count
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    // Should contain suggestions
    assert!(result.contains("Did you mean"));
    assert!(result.contains("increment_count"));

    insta::assert_snapshot!(result);
}
#[test]
fn test_nonexistent_item() {
    let mut state = create_test_state();

    // Try to get a nonexistent item
    let tool = GetItem {
        name: "crate::DoesNotExist".to_string(),
        include_source: None,
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_unit_struct() {
    let mut state = create_test_state();

    // Get unit struct details
    let tool = GetItem {
        name: "crate::UnitStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_tuple_struct() {
    let mut state = create_test_state();

    // Get tuple struct details
    let tool = GetItem {
        name: "crate::TupleStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_generic_enum() {
    let mut state = create_test_state();

    // Get generic enum details
    let tool = GetItem {
        name: "crate::GenericEnum".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_trait_details() {
    let mut state = create_test_state();

    // Get TestTrait details
    let tool = GetItem {
        name: "crate::TestTrait".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_recursive_module_listing() {
    let mut state = create_test_state();

    // Get recursive listing of the crate root
    let tool = GetItem {
        name: "crate".to_string(),
        recursive: Some(true),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_recursive_submodule_listing() {
    let mut state = create_test_state();

    // Get recursive listing of a submodule
    let tool = GetItem {
        name: "crate::submodule".to_string(),
        recursive: Some(true),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_recursive_filtering() {
    let mut state = create_test_state();

    // Get recursive listing with struct filter only
    let tool = GetItem {
        name: "crate".to_string(),
        recursive: Some(true),
        filter: Some(vec![Filter::Struct]),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_non_recursive_filtering() {
    let mut state = create_test_state();
    // Get non-recursive listing with struct filter
    let tool = GetItem {
        name: "crate".to_string(),
        filter: Some(vec![Filter::Struct]),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_recursive_multiple_filters() {
    let mut state = create_test_state();

    // Get recursive listing with function and trait filters
    let tool = GetItem {
        name: "crate".to_string(),
        recursive: Some(true),
        filter: Some(vec![Filter::Function, Filter::Trait]),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn test_get_std_vec() {
    let mut state = create_test_state();

    // Get the root of the std crate
    let tool_std_root = GetItem {
        name: "std".to_string(),
        ..Default::default()
    };
    let result_std_root = tool_std_root
        .execute(&mut state)
        .expect("Tool execution failed for std root");
    insta::assert_snapshot!(result_std_root);

    // Get std::collections::HashMap
    let tool_std_collections_hashmap = GetItem {
        name: "std::collections::HashMap".to_string(),
        ..Default::default()
    };
    let result_std_collections_hashmap = tool_std_collections_hashmap
        .execute(&mut state)
        .expect("Tool execution failed for std::collections::HashMap");
    insta::assert_snapshot!(result_std_collections_hashmap);

    // Get std::vec::Vec
    let tool_std_vec_vec = GetItem {
        name: "std::vec::Vec".to_string(),
        ..Default::default()
    };
    let result_std_vec_vec = tool_std_vec_vec
        .execute(&mut state)
        .expect("Tool execution failed for std::vec::Vec");
    insta::assert_snapshot!(result_std_vec_vec);
}
#[test]
fn test_get_item_with_normalized_crate_name() {
    let mut state = create_test_state();

    // Get an item from the test-crate using a hyphen in the name
    let tool = GetItem {
        name: "test-crate::TestStruct".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}
#[test]
fn test_get_complex_trait_details() {
    let mut state = create_test_state();

    // Get ComplexTrait details
    let tool = GetItem {
        name: "crate::ComplexTrait".to_string(),
        ..Default::default()
    };

    let result = tool.execute(&mut state).expect("Tool execution failed");

    insta::assert_snapshot!(result);
}

#[test]
fn tools_doesnt_panic() {
    use crate::tools::Tools;
    use mcplease::traits::AsToolsList;
    Tools::tools_list();
}

#[test]
fn list_crates() {
    let mut state = create_test_state();
    let result = ListCrates::default().execute(&mut state).unwrap();
    insta::assert_snapshot!(result);
}

#[test]
fn search() {
    let mut state = create_test_state();
    let result = Search {
        crate_name: "crate".into(),
        query: "trigger line-based truncation".into(),
        limit: None,
    }
    .execute(&mut state)
    .unwrap();
    insta::assert_snapshot!(result);
}

#[test]
fn search_2() {
    let mut state = create_test_state();
    let result = Search {
        crate_name: "crate".into(),
        query: "generic struct".into(),
        limit: None,
    }
    .execute(&mut state)
    .unwrap();
    insta::assert_snapshot!(result);
}