terraphim_mcp_server 1.0.0

Model Context Protocol (MCP) server for Terraphim AI
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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
use anyhow::Result;
use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::TokioChildProcess};
use serde_json::json;
use serial_test::serial;
use terraphim_automata::builder::{Logseq, ThesaurusBuilder};
use terraphim_config::{
    ConfigBuilder, Haystack, KnowledgeGraph, KnowledgeGraphLocal, Role, ServiceType,
};
use terraphim_persistence::DeviceStorage;
use terraphim_persistence::Persistable;
use terraphim_test_utils::set_env_var;
use terraphim_types::{KnowledgeGraphInputType, RelevanceFunction};
use tokio::process::Command;

// Additional imports for thesaurus building
use terraphim_automata::AutomataPath;

/// Create a comprehensive test configuration with the "Terraphim Engineer" role
/// that uses local KG files and builds thesaurus from local markdown files
async fn create_autocomplete_test_config() -> Result<String> {
    // Use memory-only persistence to avoid RocksDB filesystem issues in CI
    set_env_var("TERRAPHIM_PROFILE_MEMORY_TYPE", "memory");
    let log_dir = tempfile::Builder::new()
        .prefix("terraphim-logs")
        .tempdir()?
        .keep();
    std::fs::create_dir_all(&log_dir)?;
    set_env_var("TERRAPHIM_LOG_DIR", log_dir.to_string_lossy().to_string());
    // Force persistence layer to use memory-only device settings
    let _ = DeviceStorage::init_memory_only().await;
    let current_dir = std::env::current_dir()?;
    let project_root = current_dir.parent().unwrap().parent().unwrap();
    let docs_src_path = project_root.join("docs/src");
    let kg_path = docs_src_path.join("kg");

    // Verify paths exist
    if !kg_path.exists() {
        panic!("Knowledge graph directory not found: {:?}", kg_path);
    }
    if !kg_path.join("terraphim-graph.md").exists() {
        panic!("terraphim-graph.md not found in kg directory");
    }

    println!("๐Ÿ”ง Building thesaurus from local KG files: {:?}", kg_path);

    // Build thesaurus using Logseq builder
    let logseq_builder = Logseq::default();
    let mut thesaurus = logseq_builder
        .build("Terraphim Engineer".to_string(), kg_path.clone())
        .await?;

    println!(
        "โœ… Built thesaurus with {} entries from local KG",
        thesaurus.len()
    );

    // Save thesaurus to persistence layer
    thesaurus.save().await?;
    println!("โœ… Saved thesaurus to persistence layer");

    // Reload thesaurus from persistence to get canonical version
    thesaurus = thesaurus.load().await?;

    // Create automata path pointing to the persisted thesaurus
    let temp_dir = std::env::temp_dir();
    let thesaurus_path = temp_dir.join("terraphim_engineer_autocomplete_thesaurus.json");

    // Write thesaurus to temp file for automata path
    let thesaurus_json = serde_json::to_string_pretty(&thesaurus)?;
    tokio::fs::write(&thesaurus_path, thesaurus_json).await?;

    let automata_path = AutomataPath::Local(thesaurus_path.clone());
    println!("โœ… Set automata_path to: {:?}", thesaurus_path);

    let terraphim_engineer_role = Role {
        shortname: Some("Terraphim Engineer".to_string()),
        name: terraphim_types::RoleName::new("Terraphim Engineer"),
        relevance_function: RelevanceFunction::TerraphimGraph,
        terraphim_it: true,
        theme: "lumen".to_string(),
        kg: Some(KnowledgeGraph {
            automata_path: Some(automata_path),
            knowledge_graph_local: Some(KnowledgeGraphLocal {
                input_type: KnowledgeGraphInputType::Markdown,
                path: kg_path,
            }),
            public: true,
            publish: true,
        }),
        haystacks: vec![Haystack {
            location: docs_src_path.to_string_lossy().to_string(),
            service: ServiceType::Ripgrep,
            read_only: true,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
            fetch_content: false,
        }],
        llm_enabled: false,
        llm_api_key: None,
        llm_model: None,
        llm_auto_summarize: false,
        llm_chat_enabled: false,
        llm_chat_system_prompt: None,
        llm_chat_model: None,
        llm_context_window: Some(4096),
        llm_router_enabled: false,
        llm_router_config: None,
        extra: ahash::AHashMap::new(),
    };

    let mut config = ConfigBuilder::new()
        .global_shortcut("Ctrl+Shift+T")
        .add_role("Terraphim Engineer", terraphim_engineer_role)
        .build()?;

    // Set the selected role
    config.selected_role = terraphim_types::RoleName::new("Terraphim Engineer");

    Ok(serde_json::to_string_pretty(&config)?)
}

/// Start the MCP server as a subprocess and return the transport
async fn start_mcp_server() -> Result<TokioChildProcess> {
    let mut cmd = Command::new("cargo");
    cmd.arg("run").arg("--bin").arg("terraphim_mcp_server");

    if std::env::var_os("CI").is_some() {
        cmd.arg("--features").arg("zlob");
        cmd.arg("--release");
    }

    cmd.stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());

    let transport = TokioChildProcess::new(cmd)?;
    Ok(transport)
}

/// Test building autocomplete index for Terraphim Engineer role
#[tokio::test]
#[serial]
async fn test_build_autocomplete_index_terraphim_engineer() -> Result<()> {
    println!("๐Ÿงช Testing autocomplete index building for Terraphim Engineer role");

    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // First update the configuration to use our test config
    let update_config_args = json!({
        "config_str": config_json
    });

    let update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    println!("Config update result: {:?}", update_result);
    assert!(update_result.is_error != Some(true));

    // Now build the autocomplete index for Terraphim Engineer role
    let build_index_args = json!({
        "role": "Terraphim Engineer"
    });

    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    println!("Build index result: {:?}", build_result);

    // Verify the build was successful
    assert!(build_result.is_error != Some(true));

    if let Some(content) = build_result.content.first() {
        let content_text = content.as_text().unwrap().text.clone();
        assert!(content_text.contains("Autocomplete index built successfully"));
        assert!(content_text.contains("Terraphim Engineer"));
        println!("โœ… Successfully built autocomplete index: {}", content_text);
    } else {
        panic!("No content returned from build_autocomplete_index");
    }

    Ok(())
}

/// Test fuzzy autocomplete search using Jaro-Winkler algorithm with knowledge graph terms
#[tokio::test]
#[serial]
#[ignore = "Test expects specific autocomplete results that may not be available"]
async fn test_fuzzy_autocomplete_search_kg_terms() -> Result<()> {
    println!("๐Ÿงช Testing fuzzy autocomplete search with knowledge graph terms");

    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Update configuration
    let update_config_args = json!({"config_str": config_json});
    let _update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    // Build autocomplete index
    let build_index_args = json!({"role": "Terraphim Engineer"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    assert!(build_result.is_error != Some(true));

    // Test various knowledge graph terms
    let test_queries = vec![
        ("terrapi", vec!["terraphim-graph"]), // Partial match
        ("graph", vec!["terraphim-graph", "graph embeddings"]), // Common term
        ("embedd", vec!["graph embeddings"]), // Partial embedding
        ("hayst", vec!["haystack"]),          // Haystack term
        ("servic", vec!["service"]),          // Service term
        ("machine", vec![]),                  // Should not match KG terms
    ];

    for (query, expected_terms) in test_queries {
        println!("๐Ÿ” Testing query: '{}'", query);

        let search_args = json!({
            "query": query,
            "similarity": 0.6,
            "limit": 10
        });

        let search_result = service
            .call_tool(CallToolRequestParam {
                name: "fuzzy_autocomplete_search".into(),
                arguments: Some(search_args.as_object().unwrap().clone()),
            })
            .await?;

        println!("Search result for '{}': {:?}", query, search_result);
        assert!(search_result.is_error != Some(true));

        // Check if expected terms are found
        if let Some(summary_content) = search_result.content.first() {
            let summary_text = summary_content.as_text().unwrap().text.clone();
            println!("Summary: {}", summary_text);

            // For terms that should match, verify suggestions are returned
            if !expected_terms.is_empty() {
                assert!(summary_text.contains("Found"));
                assert!(!summary_text.contains("Found 0"));

                // Check individual suggestions
                for (i, content) in search_result.content.iter().skip(1).enumerate() {
                    let suggestion_text = content.as_text().unwrap().text.clone();
                    println!("Suggestion {}: {}", i + 1, suggestion_text);

                    // Verify suggestions contain expected terms
                    let suggestion_contains_expected = expected_terms.iter().any(|expected| {
                        suggestion_text
                            .to_lowercase()
                            .contains(&expected.to_lowercase())
                    });

                    if !suggestion_contains_expected {
                        println!(
                            "โš ๏ธ  Suggestion '{}' doesn't contain expected terms: {:?}",
                            suggestion_text, expected_terms
                        );
                    }
                }
            } else {
                // For queries that shouldn't match, verify few or no results
                println!("Query '{}' expected to have no matches", query);
            }
        }
    }

    println!("โœ… Fuzzy autocomplete search testing completed");
    Ok(())
}

/// Test Levenshtein autocomplete search for comparison
#[tokio::test]
#[serial]
async fn test_levenshtein_autocomplete_search_kg_terms() -> Result<()> {
    println!("๐Ÿงช Testing Levenshtein autocomplete search with knowledge graph terms");

    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Setup (config update and index building)
    let update_config_args = json!({"config_str": config_json});
    let _update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    let build_index_args = json!({"role": "Terraphim Engineer"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    assert!(build_result.is_error != Some(true));

    // Test Levenshtein algorithm with various edit distances
    let test_cases = vec![
        ("graph", 1),   // Exact match
        ("grap", 1),    // 1 edit distance
        ("graff", 2),   // 2 edit distance
        ("terrapi", 2), // For "terraphim"
    ];

    for (query, max_edit_distance) in test_cases {
        println!(
            "๐Ÿ” Testing Levenshtein query: '{}' with max edit distance: {}",
            query, max_edit_distance
        );

        let search_args = json!({
            "query": query,
            "max_edit_distance": max_edit_distance,
            "limit": 10
        });

        let search_result = service
            .call_tool(CallToolRequestParam {
                name: "fuzzy_autocomplete_search_levenshtein".into(),
                arguments: Some(search_args.as_object().unwrap().clone()),
            })
            .await?;

        println!(
            "Levenshtein search result for '{}': {:?}",
            query, search_result
        );
        assert!(search_result.is_error != Some(true));

        if let Some(summary_content) = search_result.content.first() {
            let summary_text = summary_content.as_text().unwrap().text.clone();
            println!("Levenshtein summary: {}", summary_text);

            // Print all suggestions for analysis
            for (i, content) in search_result.content.iter().skip(1).enumerate() {
                let suggestion_text = content.as_text().unwrap().text.clone();
                println!("Levenshtein suggestion {}: {}", i + 1, suggestion_text);
            }
        }
    }

    println!("โœ… Levenshtein autocomplete search testing completed");
    Ok(())
}

/// Test algorithm comparison: Jaro-Winkler vs Levenshtein
#[tokio::test]
#[serial]
async fn test_autocomplete_algorithm_comparison() -> Result<()> {
    println!("๐Ÿงช Testing autocomplete algorithm comparison: Jaro-Winkler vs Levenshtein");

    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Setup
    let update_config_args = json!({"config_str": config_json});
    let _update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    let build_index_args = json!({"role": "Terraphim Engineer"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    assert!(build_result.is_error != Some(true));

    // Compare algorithms on the same queries
    let comparison_queries = vec!["terrapi", "graff", "embedd", "servic"];

    for query in comparison_queries {
        println!("\n๐Ÿ”„ Comparing algorithms for query: '{}'", query);

        // Test Jaro-Winkler
        let jw_args = json!({
            "query": query,
            "similarity": 0.6,
            "limit": 5
        });

        let jw_result = service
            .call_tool(CallToolRequestParam {
                name: "fuzzy_autocomplete_search".into(),
                arguments: Some(jw_args.as_object().unwrap().clone()),
            })
            .await?;

        // Test Levenshtein
        let lev_args = json!({
            "query": query,
            "max_edit_distance": 2,
            "limit": 5
        });

        let lev_result = service
            .call_tool(CallToolRequestParam {
                name: "fuzzy_autocomplete_search_levenshtein".into(),
                arguments: Some(lev_args.as_object().unwrap().clone()),
            })
            .await?;

        // Compare results
        println!(
            "๐Ÿ“Š Jaro-Winkler results for '{}': {} items",
            query,
            jw_result.content.len().saturating_sub(1)
        );
        println!(
            "๐Ÿ“Š Levenshtein results for '{}': {} items",
            query,
            lev_result.content.len().saturating_sub(1)
        );

        // Print summary comparison
        if let (Some(jw_summary), Some(lev_summary)) =
            (jw_result.content.first(), lev_result.content.first())
        {
            println!("๐Ÿ” JW: {}", jw_summary.as_text().unwrap().text);
            println!("๐Ÿ” LEV: {}", lev_summary.as_text().unwrap().text);
        }
    }

    println!("โœ… Algorithm comparison testing completed");
    Ok(())
}

/// Test new autocomplete_terms tool (prefix + fuzzy)
#[tokio::test]
#[serial]
#[ignore = "Test expects specific autocomplete results that may not be available"]
async fn test_autocomplete_terms_tool() -> Result<()> {
    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Update configuration
    let update_config_args = json!({"config_str": config_json});
    service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    // Build index
    let build_index_args = json!({"role": "Terraphim Engineer"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;
    assert!(build_result.is_error != Some(true));

    // Call autocomplete_terms: expect graph expansion to include synonyms of same concept id
    let ac_args = json!({"query": "terraph", "limit": 8});
    let ac_result = service
        .call_tool(CallToolRequestParam {
            name: "autocomplete_terms".into(),
            arguments: Some(ac_args.as_object().unwrap().clone()),
        })
        .await?;
    assert!(ac_result.is_error != Some(true));
    if let Some(summary) = ac_result.content.first() {
        let text = summary.as_text().unwrap().text.clone();
        assert!(text.contains("Found"));
    }
    // Verify that related terms like "graph" or "graph embeddings" appear via concept expansion
    let all_texts: Vec<String> = ac_result
        .content
        .iter()
        .skip(1)
        .filter_map(|c| c.as_text().map(|t| t.text.clone()))
        .collect();
    let has_graph_related = all_texts.iter().any(|t| t.to_lowercase().contains("graph"));
    assert!(
        has_graph_related,
        "Autocomplete should include graph-related synonyms via concept expansion: {:?}",
        all_texts
    );
    Ok(())
}

/// Test new autocomplete_with_snippets tool
#[tokio::test]
#[serial]
async fn test_autocomplete_with_snippets_tool() -> Result<()> {
    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Update configuration and build index
    let update_config_args = json!({"config_str": config_json});
    service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;
    let build_index_args = json!({"role": "Terraphim Engineer"});
    service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    // Call autocomplete_with_snippets
    let ac_args = json!({"query": "graph", "limit": 5});
    let ac_result = service
        .call_tool(CallToolRequestParam {
            name: "autocomplete_with_snippets".into(),
            arguments: Some(ac_args.as_object().unwrap().clone()),
        })
        .await?;
    assert!(ac_result.is_error != Some(true));
    // Should contain a summary and then several text items with " โ€” " separator sometimes
    assert!(!ac_result.content.is_empty());
    let has_snippetish = ac_result
        .content
        .iter()
        .skip(1)
        .any(|c| c.as_text().map(|t| t.text.contains(" โ€” ")).unwrap_or(false));
    if !has_snippetish {
        println!("No snippet separator found; still acceptable depending on dataset");
    }
    Ok(())
}

/// Test error handling for roles without proper knowledge graph configuration
#[tokio::test]
#[serial]
async fn test_autocomplete_error_handling() -> Result<()> {
    println!("๐Ÿงช Testing autocomplete error handling for invalid configurations");

    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Create config with role that doesn't have TerraphimGraph relevance function
    let invalid_role = Role {
        shortname: Some("Invalid Role".to_string()),
        name: terraphim_types::RoleName::new("Invalid Role"),
        relevance_function: RelevanceFunction::TitleScorer, // Wrong relevance function
        terraphim_it: false,
        theme: "spacelab".to_string(),
        kg: None, // No knowledge graph
        haystacks: vec![],
        llm_enabled: false,
        llm_api_key: None,
        llm_model: None,
        llm_auto_summarize: false,
        llm_chat_enabled: false,
        llm_chat_system_prompt: None,
        llm_chat_model: None,
        llm_context_window: Some(4096),
        llm_router_enabled: false,
        llm_router_config: None,
        extra: ahash::AHashMap::new(),
    };

    let invalid_config = ConfigBuilder::new()
        .add_role("Invalid Role", invalid_role)
        .build()?;

    let config_json = serde_json::to_string_pretty(&invalid_config)?;

    // Update configuration with invalid role
    let update_config_args = json!({"config_str": config_json});
    let _update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    // Try to build autocomplete index for invalid role
    let build_index_args = json!({"role": "Invalid Role"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    println!("Build result for invalid role: {:?}", build_result);

    // Should fail with proper error message
    assert!(build_result.is_error == Some(true));

    if let Some(error_content) = build_result.content.first() {
        let error_text = error_content.as_text().unwrap().text.clone();
        assert!(error_text.contains("does not use knowledge graph ranking"));
        println!(
            "โœ… Correct error for invalid relevance function: {}",
            error_text
        );
    }

    // Test autocomplete search without index
    let search_args = json!({
        "query": "test",
        "similarity": 0.6,
        "limit": 5
    });

    let search_result = service
        .call_tool(CallToolRequestParam {
            name: "fuzzy_autocomplete_search".into(),
            arguments: Some(search_args.as_object().unwrap().clone()),
        })
        .await?;

    println!("Search result without index: {:?}", search_result);

    // Should fail with proper error message about missing index
    assert!(search_result.is_error == Some(true));

    if let Some(error_content) = search_result.content.first() {
        let error_text = error_content.as_text().unwrap().text.clone();
        assert!(error_text.contains("Autocomplete index not built"));
        println!("โœ… Correct error for missing index: {}", error_text);
    }

    println!("โœ… Error handling testing completed");
    Ok(())
}

/// Test role-specific autocomplete functionality
#[tokio::test]
#[serial]
async fn test_role_specific_autocomplete() -> Result<()> {
    println!("๐Ÿงช Testing role-specific autocomplete functionality");

    let config_json = create_autocomplete_test_config().await?;
    let transport = start_mcp_server().await?;
    let service = ().serve(transport).await?;

    // Setup configuration
    let update_config_args = json!({"config_str": config_json});
    let _update_result = service
        .call_tool(CallToolRequestParam {
            name: "update_config_tool".into(),
            arguments: Some(update_config_args.as_object().unwrap().clone()),
        })
        .await?;

    // Build index for specific role
    let build_index_args = json!({"role": "Terraphim Engineer"});
    let build_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_args.as_object().unwrap().clone()),
        })
        .await?;

    assert!(build_result.is_error != Some(true));

    // Test that autocomplete works within role context
    let terraphim_specific_queries = vec![
        "terraphim", // Should match terraphim-graph
        "graph",     // Should match various graph terms
        "haystack",  // Should match haystack term
        "service",   // Should match service term
    ];

    for query in terraphim_specific_queries {
        println!("๐Ÿ” Testing role-specific query: '{}'", query);

        let search_args = json!({
            "query": query,
            "similarity": 0.5, // Lower threshold for broader matches
            "limit": 10
        });

        let search_result = service
            .call_tool(CallToolRequestParam {
                name: "fuzzy_autocomplete_search".into(),
                arguments: Some(search_args.as_object().unwrap().clone()),
            })
            .await?;

        assert!(search_result.is_error != Some(true));

        if let Some(summary_content) = search_result.content.first() {
            let summary_text = summary_content.as_text().unwrap().text.clone();
            println!("Role-specific result for '{}': {}", query, summary_text);

            // Should return some autocomplete suggestions
            assert!(summary_text.contains("Found"));
        }
    }

    // Test build without specifying role (should use selected role)
    let build_index_default_args = json!({});
    let build_default_result = service
        .call_tool(CallToolRequestParam {
            name: "build_autocomplete_index".into(),
            arguments: Some(build_index_default_args.as_object().unwrap().clone()),
        })
        .await?;

    assert!(build_default_result.is_error != Some(true));

    if let Some(content) = build_default_result.content.first() {
        let content_text = content.as_text().unwrap().text.clone();
        assert!(content_text.contains("Terraphim Engineer")); // Should use selected role
        println!("โœ… Default role autocomplete build: {}", content_text);
    }

    println!("โœ… Role-specific autocomplete testing completed");
    Ok(())
}