rmcp-memex 0.3.6

RAG/memory MCP server with LanceDB vector storage
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Host detection module for MCP server configurations.
//!
//! Scans known locations for MCP host configurations (Codex, Cursor, Claude Desktop, JetBrains).
//! Also provides config writing functionality for the wizard.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

// =============================================================================
// HOST TYPES (previously from rmcp-common)
// =============================================================================

/// Supported MCP host application kinds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum HostKind {
    /// Codex CLI (uses TOML config)
    Codex,
    /// Cursor editor (uses JSON config)
    Cursor,
    /// VS Code with MCP extension (uses JSON config)
    VSCode,
    /// Claude Desktop application (uses JSON config)
    Claude,
    /// JetBrains IDEs with MCP plugin (uses JSON config)
    JetBrains,
    /// Unknown or custom host
    Unknown,
}

impl HostKind {
    /// Returns a lowercase label for the host kind.
    pub fn as_label(&self) -> &'static str {
        match self {
            HostKind::Codex => "codex",
            HostKind::Cursor => "cursor",
            HostKind::VSCode => "vscode",
            HostKind::Claude => "claude",
            HostKind::JetBrains => "jetbrains",
            HostKind::Unknown => "unknown",
        }
    }

    /// Returns a human-readable display name for the host kind.
    pub fn display_name(&self) -> &'static str {
        match self {
            HostKind::Codex => "Codex CLI",
            HostKind::Cursor => "Cursor",
            HostKind::VSCode => "VS Code",
            HostKind::Claude => "Claude Desktop",
            HostKind::JetBrains => "JetBrains IDEs",
            HostKind::Unknown => "Unknown",
        }
    }
}

/// Configuration file format for MCP hosts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HostFormat {
    /// TOML format (used by Codex)
    Toml,
    /// JSON format (used by most other hosts)
    Json,
}

// =============================================================================
// MCP SERVER ENTRIES
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpServerEntry {
    pub name: String,
    pub command: String,
    pub args: Vec<String>,
    pub env: HashMap<String, String>,
}

#[derive(Debug, Clone)]
pub struct HostDetection {
    pub kind: HostKind,
    pub path: PathBuf,
    pub format: HostFormat,
    pub exists: bool,
    pub has_rmcp_memex: bool,
    pub servers: Vec<McpServerEntry>,
}

impl HostDetection {
    pub fn status_icon(&self) -> &'static str {
        if !self.exists {
            "[ ]"
        } else if self.has_rmcp_memex {
            "[x]"
        } else {
            "[~]"
        }
    }

    pub fn status_text(&self) -> &'static str {
        if !self.exists {
            "Not found"
        } else if self.has_rmcp_memex {
            "Configured"
        } else {
            "Detected (no rmcp_memex)"
        }
    }
}

fn home_dir() -> Option<PathBuf> {
    std::env::var("HOME")
        .or_else(|_| std::env::var("USERPROFILE"))
        .ok()
        .map(PathBuf::from)
}

/// Extended host kind that includes hosts not in rmcp-common
/// (ClaudeCode and Junie are specific to rmcp-memex wizard)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExtendedHostKind {
    /// Standard hosts from rmcp-common
    Standard(HostKind),
    /// Claude Code CLI (~/.claude.json)
    ClaudeCode,
    /// Junie AI (~/.junie/mcp.json)
    Junie,
}

impl ExtendedHostKind {
    pub fn display_name(&self) -> &'static str {
        match self {
            ExtendedHostKind::Standard(k) => k.display_name(),
            ExtendedHostKind::ClaudeCode => "Claude Code",
            ExtendedHostKind::Junie => "Junie",
        }
    }
}

fn get_host_config_path(kind: HostKind) -> Option<(PathBuf, HostFormat)> {
    let home = home_dir()?;

    match kind {
        HostKind::Codex => Some((home.join(".codex/config.toml"), HostFormat::Toml)),
        HostKind::Cursor => {
            #[cfg(target_os = "macos")]
            let path = home.join(
                "Library/Application Support/Cursor/User/globalStorage/cursor.mcp/config.json",
            );
            #[cfg(target_os = "linux")]
            let path = home.join(".config/Cursor/User/globalStorage/cursor.mcp/config.json");
            #[cfg(target_os = "windows")]
            let path =
                home.join("AppData/Roaming/Cursor/User/globalStorage/cursor.mcp/config.json");
            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            let path = home.join(".config/Cursor/config.json");
            Some((path, HostFormat::Json))
        }
        HostKind::Claude => {
            #[cfg(target_os = "macos")]
            let path = home.join("Library/Application Support/Claude/claude_desktop_config.json");
            #[cfg(target_os = "linux")]
            let path = home.join(".config/Claude/claude_desktop_config.json");
            #[cfg(target_os = "windows")]
            let path = home.join("AppData/Roaming/Claude/claude_desktop_config.json");
            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            let path = home.join(".config/Claude/claude_desktop_config.json");
            Some((path, HostFormat::Json))
        }
        HostKind::JetBrains => {
            // JetBrains uses a common MCP config location
            #[cfg(target_os = "macos")]
            let path = home.join("Library/Application Support/JetBrains/mcp.json");
            #[cfg(target_os = "linux")]
            let path = home.join(".config/JetBrains/mcp.json");
            #[cfg(target_os = "windows")]
            let path = home.join("AppData/Roaming/JetBrains/mcp.json");
            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            let path = home.join(".config/JetBrains/mcp.json");
            Some((path, HostFormat::Json))
        }
        HostKind::VSCode => {
            #[cfg(target_os = "macos")]
            let path = home.join("Library/Application Support/Code/User/globalStorage/anthropic.claude-vscode/settings/cline_mcp_settings.json");
            #[cfg(target_os = "linux")]
            let path = home.join(".config/Code/User/globalStorage/anthropic.claude-vscode/settings/cline_mcp_settings.json");
            #[cfg(target_os = "windows")]
            let path = home.join("AppData/Roaming/Code/User/globalStorage/anthropic.claude-vscode/settings/cline_mcp_settings.json");
            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            let path = home.join(".config/Code/cline_mcp_settings.json");
            Some((path, HostFormat::Json))
        }
        HostKind::Unknown => None,
    }
}

/// Get config path for extended host kinds (including ClaudeCode and Junie)
pub fn get_extended_host_config_path(kind: ExtendedHostKind) -> Option<(PathBuf, HostFormat)> {
    let home = home_dir()?;

    match kind {
        ExtendedHostKind::Standard(k) => get_host_config_path(k),
        ExtendedHostKind::ClaudeCode => Some((home.join(".claude.json"), HostFormat::Json)),
        ExtendedHostKind::Junie => Some((home.join(".junie/mcp.json"), HostFormat::Json)),
    }
}

fn parse_toml_mcp_servers(content: &str) -> Vec<McpServerEntry> {
    let mut servers = Vec::new();

    if let Ok(value) = content.parse::<toml::Value>()
        && let Some(mcp_servers) = value.get("mcp_servers").and_then(|v| v.as_table())
    {
        for (name, config) in mcp_servers {
            let command = config
                .get("command")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let args = config
                .get("args")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();

            let env = config
                .get("env")
                .and_then(|v| v.as_table())
                .map(|t| {
                    t.iter()
                        .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
                        .collect()
                })
                .unwrap_or_default();

            servers.push(McpServerEntry {
                name: name.clone(),
                command,
                args,
                env,
            });
        }
    }

    servers
}

fn parse_json_mcp_servers(content: &str) -> Vec<McpServerEntry> {
    let mut servers = Vec::new();

    if let Ok(value) = serde_json::from_str::<serde_json::Value>(content) {
        let mcp_servers = value.get("mcpServers").or_else(|| value.get("mcp_servers"));

        if let Some(mcp_obj) = mcp_servers.and_then(|v| v.as_object()) {
            for (name, config) in mcp_obj {
                let command = config
                    .get("command")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();

                let args = config
                    .get("args")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();

                let env = config
                    .get("env")
                    .and_then(|v| v.as_object())
                    .map(|obj| {
                        obj.iter()
                            .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
                            .collect()
                    })
                    .unwrap_or_default();

                servers.push(McpServerEntry {
                    name: name.clone(),
                    command,
                    args,
                    env,
                });
            }
        }
    }

    servers
}

fn detect_single_host(kind: HostKind) -> Option<HostDetection> {
    let (path, format) = get_host_config_path(kind)?;
    let exists = path.exists();

    let (has_rmcp_memex, servers) = if exists {
        if let Ok(content) = std::fs::read_to_string(&path) {
            let servers = match format {
                HostFormat::Toml => parse_toml_mcp_servers(&content),
                HostFormat::Json => parse_json_mcp_servers(&content),
            };
            let has_rmcp = servers
                .iter()
                .any(|s| s.name.contains("rmcp_memex") || s.command.contains("rmcp_memex"));
            (has_rmcp, servers)
        } else {
            (false, Vec::new())
        }
    } else {
        (false, Vec::new())
    };

    Some(HostDetection {
        kind,
        path,
        format,
        exists,
        has_rmcp_memex,
        servers,
    })
}

/// Detect all known MCP host configurations.
pub fn detect_hosts() -> Vec<HostDetection> {
    let kinds = [
        HostKind::Codex,
        HostKind::Cursor,
        HostKind::Claude,
        HostKind::JetBrains,
        HostKind::VSCode,
    ];

    kinds
        .iter()
        .filter_map(|&k| detect_single_host(k))
        .collect()
}

/// Generate a config snippet for an extended host kind.
pub fn generate_extended_snippet(
    kind: ExtendedHostKind,
    binary_path: &str,
    db_path: &str,
) -> String {
    match get_extended_host_config_path(kind) {
        Some((_, HostFormat::Toml)) => {
            format!(
                r#"[mcp_servers.rmcp_memex]
command = "{}"
args = ["serve", "--db-path", "{}", "--log-level", "info"]
"#,
                binary_path, db_path
            )
        }
        Some((_, HostFormat::Json)) => {
            format!(
                r#"{{
  "mcpServers": {{
    "rmcp_memex": {{
      "command": "{}",
      "args": ["serve", "--db-path", "{}", "--log-level", "info"]
    }}
  }}
}}"#,
                binary_path, db_path
            )
        }
        None => String::new(),
    }
}

/// Result of writing a host config
#[derive(Debug)]
pub struct WriteResult {
    pub host_name: String,
    pub config_path: PathBuf,
    pub backup_path: Option<PathBuf>,
    pub created: bool,
}

/// Generate a backup timestamp
fn backup_timestamp() -> String {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    format!("{}", secs)
}

/// Create a backup of an existing config file
fn create_backup(path: &Path) -> Result<PathBuf> {
    use crate::path_utils::{validate_read_path, validate_write_path};

    // Validate source path is safe to read
    let safe_src = validate_read_path(path).with_context(|| {
        format!(
            "Cannot backup: source path validation failed for {}",
            path.display()
        )
    })?;

    let backup_path = PathBuf::from(format!("{}.bak.{}", safe_src.display(), backup_timestamp()));

    // Validate backup destination is safe to write
    let safe_dst = validate_write_path(&backup_path).with_context(|| {
        format!(
            "Cannot backup: destination path validation failed for {}",
            backup_path.display()
        )
    })?;

    // Path is validated by validate_read_path/validate_write_path above
    std::fs::copy(&safe_src, &safe_dst) // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
        .with_context(|| format!("Failed to create backup of {}", safe_src.display()))?;
    Ok(safe_dst)
}

/// Merge rmcp_memex server entry into existing JSON config
fn merge_json_config(existing_content: &str, binary_path: &str, db_path: &str) -> Result<String> {
    let mut config: serde_json::Value = if existing_content.trim().is_empty() {
        serde_json::json!({})
    } else {
        serde_json::from_str(existing_content)
            .with_context(|| "Failed to parse existing JSON config")?
    };

    // Ensure mcpServers object exists
    if config.get("mcpServers").is_none() {
        config["mcpServers"] = serde_json::json!({});
    }

    // Add or update rmcp_memex entry
    config["mcpServers"]["rmcp_memex"] = serde_json::json!({
        "command": binary_path,
        "args": ["serve", "--db-path", db_path, "--log-level", "info"],
        "description": "RAG memory with vector search"
    });

    serde_json::to_string_pretty(&config).with_context(|| "Failed to serialize JSON config")
}

/// Merge rmcp_memex server entry into existing TOML config
fn merge_toml_config(existing_content: &str, binary_path: &str, db_path: &str) -> Result<String> {
    let mut config: toml::Value = if existing_content.trim().is_empty() {
        toml::Value::Table(toml::map::Map::new())
    } else {
        existing_content
            .parse()
            .with_context(|| "Failed to parse existing TOML config")?
    };

    // Ensure mcp_servers table exists
    let table = config.as_table_mut().expect("root must be a table");
    if !table.contains_key("mcp_servers") {
        table.insert(
            "mcp_servers".to_string(),
            toml::Value::Table(toml::map::Map::new()),
        );
    }

    // Add or update rmcp_memex entry
    if let Some(mcp_servers) = table.get_mut("mcp_servers").and_then(|v| v.as_table_mut()) {
        let mut entry = toml::map::Map::new();
        entry.insert(
            "command".to_string(),
            toml::Value::String(binary_path.to_string()),
        );
        entry.insert(
            "args".to_string(),
            toml::Value::Array(vec![
                toml::Value::String("serve".to_string()),
                toml::Value::String("--db-path".to_string()),
                toml::Value::String(db_path.to_string()),
                toml::Value::String("--log-level".to_string()),
                toml::Value::String("info".to_string()),
            ]),
        );
        mcp_servers.insert("rmcp_memex".to_string(), toml::Value::Table(entry));
    }

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

/// Write host config, merging with existing config if present.
/// Creates a backup before modifying existing files.
///
/// # Arguments
/// * `host` - The detected host to write config for
/// * `binary_path` - Path to the rmcp_memex binary
/// * `db_path` - Path to the LanceDB database
///
/// # Returns
/// * `Ok(WriteResult)` with details about the write operation
/// * `Err` if the write fails
pub fn write_host_config(
    host: &HostDetection,
    binary_path: &str,
    db_path: &str,
) -> Result<WriteResult> {
    let host_name = host.kind.display_name().to_string();

    // Ensure parent directory exists
    if let Some(parent) = host.path.parent()
        && !parent.exists()
    {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
    }

    // Create backup if file exists
    let backup_path = if host.exists {
        Some(create_backup(&host.path)?)
    } else {
        None
    };

    use crate::path_utils::{validate_read_path, validate_write_path};

    // Read existing content or use empty string
    let existing_content = if host.exists {
        // Validate path before reading
        let safe_read_path = validate_read_path(&host.path).with_context(|| {
            format!(
                "Cannot read config: path validation failed for {}",
                host.path.display()
            )
        })?;
        // Path is validated by validate_read_path above
        // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
        std::fs::read_to_string(&safe_read_path)
            .with_context(|| format!("Failed to read {}", safe_read_path.display()))?
    } else {
        String::new()
    };

    // Merge config based on format
    let new_content = match host.format {
        HostFormat::Json => merge_json_config(&existing_content, binary_path, db_path)?,
        HostFormat::Toml => merge_toml_config(&existing_content, binary_path, db_path)?,
    };

    // Validate path before writing
    let safe_write_path = validate_write_path(&host.path).with_context(|| {
        format!(
            "Cannot write config: path validation failed for {}",
            host.path.display()
        )
    })?;

    // Write the merged config
    std::fs::write(&safe_write_path, &new_content)
        .with_context(|| format!("Failed to write config to {}", safe_write_path.display()))?;

    Ok(WriteResult {
        host_name,
        config_path: host.path.clone(),
        backup_path,
        created: !host.exists,
    })
}

/// Write config for an extended host kind (including ClaudeCode and Junie)
pub fn write_extended_host_config(
    kind: ExtendedHostKind,
    binary_path: &str,
    db_path: &str,
) -> Result<WriteResult> {
    let (path, format) =
        get_extended_host_config_path(kind).ok_or_else(|| anyhow::anyhow!("Unknown host kind"))?;

    let exists = path.exists();
    let host = HostDetection {
        kind: match kind {
            ExtendedHostKind::Standard(k) => k,
            _ => HostKind::Unknown, // Use Unknown for extended types
        },
        path: path.clone(),
        format,
        exists,
        has_rmcp_memex: false,
        servers: Vec::new(),
    };

    let mut result = write_host_config(&host, binary_path, db_path)?;
    result.host_name = kind.display_name().to_string();
    Ok(result)
}

/// Detect all extended hosts (including ClaudeCode and Junie)
pub fn detect_extended_hosts() -> Vec<(ExtendedHostKind, HostDetection)> {
    let mut results = Vec::new();

    // Standard hosts
    for kind in [
        HostKind::Codex,
        HostKind::Cursor,
        HostKind::Claude,
        HostKind::JetBrains,
        HostKind::VSCode,
    ] {
        if let Some(detection) = detect_single_host(kind) {
            results.push((ExtendedHostKind::Standard(kind), detection));
        }
    }

    // Extended hosts (ClaudeCode, Junie)
    for ext_kind in [ExtendedHostKind::ClaudeCode, ExtendedHostKind::Junie] {
        if let Some((path, format)) = get_extended_host_config_path(ext_kind) {
            let exists = path.exists();
            let (has_rmcp_memex, servers) = if exists {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    let servers = parse_json_mcp_servers(&content);
                    let has_rmcp = servers
                        .iter()
                        .any(|s| s.name.contains("rmcp_memex") || s.command.contains("rmcp_memex"));
                    (has_rmcp, servers)
                } else {
                    (false, Vec::new())
                }
            } else {
                (false, Vec::new())
            };

            results.push((
                ext_kind,
                HostDetection {
                    kind: HostKind::Unknown,
                    path,
                    format,
                    exists,
                    has_rmcp_memex,
                    servers,
                },
            ));
        }
    }

    results
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_toml_mcp_servers() {
        let toml_content = r#"
[mcp_servers.rmcp_memex]
command = "/usr/local/bin/rmcp_memex"
args = ["--db-path", "~/.rmcp/db"]

[mcp_servers.other_server]
command = "other"
"#;
        let servers = parse_toml_mcp_servers(toml_content);
        assert_eq!(servers.len(), 2);
        assert!(servers.iter().any(|s| s.name == "rmcp_memex"));
    }

    #[test]
    fn test_parse_json_mcp_servers() {
        let json_content = r#"{
  "mcpServers": {
    "rmcp_memex": {
      "command": "/usr/local/bin/rmcp_memex",
      "args": ["--db-path", "~/.rmcp/db"]
    }
  }
}"#;
        let servers = parse_json_mcp_servers(json_content);
        assert_eq!(servers.len(), 1);
        assert_eq!(servers[0].name, "rmcp_memex");
    }

    #[test]
    fn test_generate_toml_snippet() {
        let snippet = generate_extended_snippet(
            ExtendedHostKind::Standard(HostKind::Codex),
            "/usr/bin/rmcp_memex",
            "~/.rmcp/db",
        );
        assert!(snippet.contains("[mcp_servers.rmcp_memex]"));
        assert!(snippet.contains("/usr/bin/rmcp_memex"));
    }

    #[test]
    fn test_generate_json_snippet() {
        let snippet = generate_extended_snippet(
            ExtendedHostKind::Standard(HostKind::Claude),
            "/usr/bin/rmcp_memex",
            "~/.rmcp/db",
        );
        assert!(snippet.contains("\"mcpServers\""));
        assert!(snippet.contains("\"rmcp_memex\""));
    }

    #[test]
    fn test_generate_extended_claude_code_snippet() {
        let snippet = generate_extended_snippet(
            ExtendedHostKind::ClaudeCode,
            "/usr/bin/rmcp_memex",
            "~/.rmcp/db",
        );
        assert!(snippet.contains("\"mcpServers\""));
        assert!(snippet.contains("\"rmcp_memex\""));
    }

    #[test]
    fn test_generate_extended_junie_snippet() {
        let snippet =
            generate_extended_snippet(ExtendedHostKind::Junie, "/usr/bin/rmcp_memex", "~/.rmcp/db");
        assert!(snippet.contains("\"mcpServers\""));
        assert!(snippet.contains("\"rmcp_memex\""));
    }

    #[test]
    fn test_merge_json_config_empty() {
        let result = merge_json_config("", "/usr/bin/rmcp_memex", "~/.rmcp/db").unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert!(
            parsed["mcpServers"]["rmcp_memex"]["command"]
                .as_str()
                .unwrap()
                .contains("rmcp_memex")
        );
    }

    #[test]
    fn test_merge_json_config_existing() {
        let existing = r#"{
  "mcpServers": {
    "other_server": {
      "command": "other",
      "args": []
    }
  }
}"#;
        let result = merge_json_config(existing, "/usr/bin/rmcp_memex", "~/.rmcp/db").unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        // Should preserve existing server
        assert!(
            parsed["mcpServers"]["other_server"]["command"]
                .as_str()
                .is_some()
        );
        // Should add rmcp_memex
        assert!(
            parsed["mcpServers"]["rmcp_memex"]["command"]
                .as_str()
                .unwrap()
                .contains("rmcp_memex")
        );
    }

    #[test]
    fn test_merge_toml_config_empty() {
        let result = merge_toml_config("", "/usr/bin/rmcp_memex", "~/.rmcp/db").unwrap();
        assert!(result.contains("[mcp_servers.rmcp_memex]"));
        assert!(result.contains("rmcp_memex"));
    }

    #[test]
    fn test_merge_toml_config_existing() {
        let existing = r#"
[mcp_servers.other_server]
command = "other"
args = []
"#;
        let result = merge_toml_config(existing, "/usr/bin/rmcp_memex", "~/.rmcp/db").unwrap();
        // Should preserve existing server
        assert!(result.contains("other_server"));
        // Should add rmcp_memex
        assert!(result.contains("rmcp_memex"));
    }

    #[test]
    fn test_extended_host_kind_display_names() {
        assert_eq!(
            ExtendedHostKind::Standard(HostKind::Claude).display_name(),
            "Claude Desktop"
        );
        assert_eq!(ExtendedHostKind::ClaudeCode.display_name(), "Claude Code");
        assert_eq!(ExtendedHostKind::Junie.display_name(), "Junie");
    }
}