codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
//! Consent-gated external MCP imports.
//!
//! Discovery can scan `~/.claude.json`, project `.mcp.json`, and marketplace
//! manifests, but **nothing is connected until the user approves**. Provenance
//! (source path + content hash) is shown before import. `enabled=false` and
//! `disabled=true` on a source entry are hard blocks — those candidates never
//! become managed connectors even after a blanket approval.
//!
//! Design (Kimi session_a75a393a-a984-4f35-98d0-b78cfbdcf23f): keep discovery
//! pure and independent of the TUI; merge approved servers through the same
//! config write path as `/mcp add`.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};

use super::{McpConfig, McpServerConfig};

/// Where an import candidate came from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExternalMcpSourceKind {
    ClaudeJson,
    ProjectMcpJson,
    Marketplace,
}

impl ExternalMcpSourceKind {
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::ClaudeJson => "claude.json",
            Self::ProjectMcpJson => ".mcp.json",
            Self::Marketplace => "marketplace",
        }
    }
}

/// One discovered server before consent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImportCandidate {
    pub name: String,
    pub source_kind: ExternalMcpSourceKind,
    pub source_path: PathBuf,
    /// Hex sha256 of the raw source file (or marketplace entry blob).
    pub content_hash: String,
    pub summary: String,
    /// When true the entry is present but must never connect.
    pub hard_blocked: bool,
    pub block_reason: Option<String>,
    pub server: McpServerConfig,
}

/// User decision for one candidate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ImportDecision {
    Approve,
    Decline,
    Skip,
}

/// Durable consent / decline record keyed by source path + hash.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ImportConsentStore {
    #[serde(default)]
    pub entries: HashMap<String, ConsentEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsentEntry {
    pub source_path: String,
    pub content_hash: String,
    pub decision: ImportDecision,
    pub decided_at_unix: u64,
    pub servers: Vec<String>,
}

fn consent_key(path: &Path, hash: &str) -> String {
    format!("{}::{hash}", path.display())
}

/// Discover candidates from well-known external locations. Never connects.
pub fn discover_external_sources(
    home: &Path,
    workspace: &Path,
    marketplace_paths: &[PathBuf],
) -> Vec<ImportCandidate> {
    let mut out = Vec::new();
    let claude = home.join(".claude.json");
    if claude.is_file() {
        out.extend(discover_from_json_file(
            &claude,
            ExternalMcpSourceKind::ClaudeJson,
        ));
    }
    let project_mcp = workspace.join(".mcp.json");
    if project_mcp.is_file() {
        out.extend(discover_from_json_file(
            &project_mcp,
            ExternalMcpSourceKind::ProjectMcpJson,
        ));
    }
    for path in marketplace_paths {
        if path.is_file() {
            out.extend(discover_from_json_file(
                path,
                ExternalMcpSourceKind::Marketplace,
            ));
        }
    }
    out
}

fn discover_from_json_file(path: &Path, kind: ExternalMcpSourceKind) -> Vec<ImportCandidate> {
    let Ok(raw) = fs::read(path) else {
        return Vec::new();
    };
    let hash = hex_sha256(&raw);
    let Ok(value) = serde_json::from_slice::<Value>(&raw) else {
        return Vec::new();
    };
    let servers = extract_servers_map(&value);
    let mut out = Vec::with_capacity(servers.len());
    for (name, cfg_value) in servers {
        let Ok(server) = serde_json::from_value::<McpServerConfig>(cfg_value.clone()) else {
            continue;
        };
        let hard_blocked = !server.is_enabled();
        let summary = server_summary(&name, &server);
        out.push(ImportCandidate {
            name,
            source_kind: kind.clone(),
            source_path: path.to_path_buf(),
            content_hash: hash.clone(),
            summary,
            hard_blocked,
            block_reason: hard_blocked.then(|| {
                "enabled=false (or disabled=true) is a hard block — will not import".to_string()
            }),
            server,
        });
    }
    out
}

fn extract_servers_map(value: &Value) -> Vec<(String, Value)> {
    // Claude / team: { "mcpServers": { name: {...} } }
    // Marketplace catalog: { "servers": { name: {...} } } or array of {name, ...}
    if let Some(map) = value
        .get("mcpServers")
        .or_else(|| value.get("servers"))
        .and_then(|v| v.as_object())
    {
        return map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
    }
    if let Some(arr) = value.as_array() {
        let mut out = Vec::new();
        for item in arr {
            let Some(name) = item.get("name").and_then(|v| v.as_str()) else {
                continue;
            };
            out.push((name.to_string(), item.clone()));
        }
        return out;
    }
    Vec::new()
}

fn server_summary(name: &str, server: &McpServerConfig) -> String {
    if let Some(url) = server.url.as_deref() {
        return format!("{name} — http {url}");
    }
    if let Some(cmd) = server.command.as_deref() {
        let args = server.args.join(" ");
        if args.is_empty() {
            return format!("{name} — stdio {cmd}");
        }
        return format!("{name} — stdio {cmd} {args}");
    }
    format!("{name} — (incomplete server config)")
}

fn hex_sha256(bytes: &[u8]) -> String {
    let digest = Sha256::digest(bytes);
    digest.iter().map(|b| format!("{b:02x}")).collect()
}

/// Load consent store from disk (missing file → empty).
pub fn load_consent_store(path: &Path) -> ImportConsentStore {
    let Ok(raw) = fs::read_to_string(path) else {
        return ImportConsentStore::default();
    };
    serde_json::from_str(&raw).unwrap_or_default()
}

/// Persist consent store atomically-ish (write then rename best-effort).
pub fn save_consent_store(path: &Path, store: &ImportConsentStore) -> std::io::Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    let raw = serde_json::to_string_pretty(store)
        .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
    let tmp = path.with_extension("json.tmp");
    fs::write(&tmp, raw)?;
    fs::rename(tmp, path)?;
    Ok(())
}

/// Filter candidates that still need a user decision for this content hash.
#[allow(dead_code)] // used by future selector UI + unit tests
pub fn candidates_needing_consent(
    candidates: &[ImportCandidate],
    store: &ImportConsentStore,
) -> Vec<ImportCandidate> {
    candidates
        .iter()
        .filter(|c| {
            let key = consent_key(&c.source_path, &c.content_hash);
            match store.entries.get(&key) {
                Some(entry) if entry.decision == ImportDecision::Decline => false,
                Some(entry) if entry.decision == ImportDecision::Approve => {
                    // Re-prompt only when the specific server was not part of
                    // the prior approval list (partial approval).
                    !entry.servers.iter().any(|s| s == &c.name)
                }
                _ => true,
            }
        })
        .cloned()
        .collect()
}

/// Apply approvals: returns servers to merge into user mcp.json.
/// Hard-blocked candidates are never returned even if decision is Approve.
pub fn apply_approved(
    candidates: &[ImportCandidate],
    decisions: &HashMap<String, ImportDecision>,
) -> Vec<(String, McpServerConfig, ImportCandidate)> {
    let mut out = Vec::new();
    for candidate in candidates {
        let decision = decisions
            .get(&candidate.name)
            .copied()
            .unwrap_or(ImportDecision::Skip);
        if decision != ImportDecision::Approve {
            continue;
        }
        if candidate.hard_blocked {
            continue;
        }
        out.push((
            candidate.name.clone(),
            candidate.server.clone(),
            candidate.clone(),
        ));
    }
    out
}

/// Record decisions in the consent store (including declines).
pub fn record_decisions(
    store: &mut ImportConsentStore,
    candidates: &[ImportCandidate],
    decisions: &HashMap<String, ImportDecision>,
    now_unix: u64,
) {
    // Group by source path + hash so one file approval is one entry.
    let mut by_source: HashMap<(PathBuf, String), Vec<(&ImportCandidate, ImportDecision)>> =
        HashMap::new();
    for candidate in candidates {
        let decision = decisions
            .get(&candidate.name)
            .copied()
            .unwrap_or(ImportDecision::Skip);
        if decision == ImportDecision::Skip {
            continue;
        }
        by_source
            .entry((
                candidate.source_path.clone(),
                candidate.content_hash.clone(),
            ))
            .or_default()
            .push((candidate, decision));
    }
    for ((path, hash), group) in by_source {
        // If any approval exists, record Approve with the approved names;
        // pure decline groups record Decline.
        let any_approve = group.iter().any(|(_, d)| *d == ImportDecision::Approve);
        let decision = if any_approve {
            ImportDecision::Approve
        } else {
            ImportDecision::Decline
        };
        let servers: Vec<String> = group
            .iter()
            .filter(|(_, d)| *d == ImportDecision::Approve)
            .filter(|(c, _)| !c.hard_blocked)
            .map(|(c, _)| c.name.clone())
            .collect();
        let key = consent_key(&path, &hash);
        store.entries.insert(
            key,
            ConsentEntry {
                source_path: path.display().to_string(),
                content_hash: hash,
                decision,
                decided_at_unix: now_unix,
                servers,
            },
        );
    }
}

/// Merge approved servers into an existing McpConfig. Does not touch
/// hard-blocked entries. Returns names that were newly inserted.
pub fn merge_approved_into_config(
    config: &mut McpConfig,
    approved: &[(String, McpServerConfig, ImportCandidate)],
) -> Vec<String> {
    let mut inserted = Vec::new();
    for (name, server, _) in approved {
        if config.servers.contains_key(name) {
            continue;
        }
        // Defense in depth: never insert disabled servers.
        if !server.is_enabled() {
            continue;
        }
        config.servers.insert(name.clone(), server.clone());
        inserted.push(name.clone());
    }
    inserted
}

/// Human-readable provenance block for the selector / status panel.
pub fn format_candidates_for_display(candidates: &[ImportCandidate]) -> String {
    if candidates.is_empty() {
        return "No external MCP sources found (or all already decided for current content)."
            .to_string();
    }
    let mut lines = vec![
        "External MCP import candidates (nothing is installed until you approve):".to_string(),
        String::new(),
    ];
    for (idx, c) in candidates.iter().enumerate() {
        let status = if c.hard_blocked { "BLOCKED" } else { "pending" };
        lines.push(format!(
            "  {}. [{}] {} — provenance: {} ({})",
            idx + 1,
            status,
            c.summary,
            c.source_kind.as_str(),
            c.source_path.display()
        ));
        lines.push(format!(
            "     content_hash: {}",
            &c.content_hash[..12.min(c.content_hash.len())]
        ));
        if let Some(reason) = &c.block_reason {
            lines.push(format!("     {reason}"));
        }
    }
    lines.push(String::new());
    lines.push(
        "Approve one: /mcp import approve <name> · Decline source: /mcp import decline <name> · List: /mcp import"
            .to_string(),
    );
    lines.join("\n")
}

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

    fn write_claude_json(dir: &Path, body: &str) -> PathBuf {
        let path = dir.join(".claude.json");
        fs::write(&path, body).unwrap();
        path
    }

    #[test]
    fn disabled_imported_server_never_merges() {
        let dir = tempdir().unwrap();
        let body = r#"{
            "mcpServers": {
                "ok": { "command": "npx", "args": ["-y", "good"], "enabled": true },
                "blocked": { "command": "npx", "args": ["-y", "bad"], "enabled": false }
            }
        }"#;
        write_claude_json(dir.path(), body);
        let candidates = discover_external_sources(dir.path(), dir.path(), &[]);
        assert_eq!(candidates.len(), 2);
        let blocked = candidates.iter().find(|c| c.name == "blocked").unwrap();
        assert!(blocked.hard_blocked);

        let mut decisions = HashMap::new();
        decisions.insert("ok".into(), ImportDecision::Approve);
        decisions.insert("blocked".into(), ImportDecision::Approve);
        let approved = apply_approved(&candidates, &decisions);
        assert_eq!(approved.len(), 1);
        assert_eq!(approved[0].0, "ok");

        let mut config = McpConfig::default();
        let inserted = merge_approved_into_config(&mut config, &approved);
        assert_eq!(inserted, vec!["ok".to_string()]);
        assert!(!config.servers.contains_key("blocked"));
        assert!(config.servers["ok"].is_enabled());
    }

    #[test]
    fn declined_consent_skips_reprompt_until_hash_changes() {
        let dir = tempdir().unwrap();
        let path = write_claude_json(
            dir.path(),
            r#"{"mcpServers":{"x":{"command":"echo","enabled":true}}}"#,
        );
        let candidates = discover_from_json_file(&path, ExternalMcpSourceKind::ClaudeJson);
        let mut store = ImportConsentStore::default();
        let mut decisions = HashMap::new();
        decisions.insert("x".into(), ImportDecision::Decline);
        record_decisions(&mut store, &candidates, &decisions, 1);
        let needing = candidates_needing_consent(&candidates, &store);
        assert!(needing.is_empty(), "declined should not re-prompt");

        // Content change → new hash → re-prompt.
        fs::write(
            &path,
            r#"{"mcpServers":{"x":{"command":"echo","args":["changed"],"enabled":true}}}"#,
        )
        .unwrap();
        let refreshed = discover_from_json_file(&path, ExternalMcpSourceKind::ClaudeJson);
        let needing = candidates_needing_consent(&refreshed, &store);
        assert_eq!(needing.len(), 1);
    }

    #[test]
    fn provenance_display_includes_source_and_hash() {
        let dir = tempdir().unwrap();
        write_claude_json(
            dir.path(),
            r#"{"mcpServers":{"hf":{"url":"https://example.com/mcp","enabled":true}}}"#,
        );
        let candidates = discover_external_sources(dir.path(), dir.path(), &[]);
        let text = format_candidates_for_display(&candidates);
        assert!(text.contains("provenance:"));
        assert!(text.contains("claude.json"));
        assert!(text.contains("content_hash:"));
        assert!(text.contains("nothing is installed until you approve"));
    }

    #[test]
    fn project_mcp_json_and_marketplace_are_discovered() {
        let home = tempdir().unwrap();
        let workspace = tempdir().unwrap();
        fs::write(
            workspace.path().join(".mcp.json"),
            r#"{"mcpServers":{"team":{"command":"uvx","args":["team-mcp"]}}}"#,
        )
        .unwrap();
        let market = home.path().join("market.json");
        fs::write(
            &market,
            r#"{"servers":{"shop":{"url":"https://market.example/mcp"}}}"#,
        )
        .unwrap();
        let candidates = discover_external_sources(home.path(), workspace.path(), &[market]);
        let names: Vec<_> = candidates.iter().map(|c| c.name.as_str()).collect();
        assert!(names.contains(&"team"));
        assert!(names.contains(&"shop"));
    }
}