ghost-link 1.16.0

CLI and API runtime for Ghostlink, a distributed inference fabric for routing and scheduling custom LLM workloads across heterogeneous hardware.
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
//! Configuration for MCP (Model Context Protocol) servers Ghostlink connects to.
//!
//! Mirrors `backend_config::ConfigManager`'s load-as-`toml::Value`-then-extract pattern
//! (rather than the simpler one-shot `FileConfig`) since the GUI needs to toggle
//! `enabled` per server and persist that back to disk.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "transport", rename_all = "lowercase")]
pub enum McpTransport {
    Stdio {
        command: String,
        #[serde(default)]
        args: Vec<String>,
        /// Values written as `"${VAR_NAME}"` are resolved from the host process
        /// environment at connect time — never stored here as literal secrets.
        #[serde(default)]
        env: HashMap<String, String>,
    },
    Http {
        url: String,
        /// Values written as `"${VAR_NAME}"` are resolved from the host process
        /// environment at connect time — never stored here as literal secrets.
        #[serde(default)]
        headers: HashMap<String, String>,
    },
}

fn default_true() -> bool {
    true
}

fn default_timeout_secs() -> u64 {
    30
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpServerConfig {
    pub name: String,
    /// Legacy chat "tool slot" this server backs (web_search, calculator, ...),
    /// or empty for standalone additions (sequential-thinking, Docker gateway, ...).
    #[serde(default)]
    pub slot: String,
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(flatten)]
    pub transport: McpTransport,
    /// Risky tools (terminal, code_execution) require an explicit user approval
    /// round-trip before the tool-calling loop executes them.
    #[serde(default)]
    pub requires_confirmation: bool,
    #[serde(default = "default_timeout_secs")]
    pub timeout_secs: u64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct McpServersFile {
    #[serde(default, rename = "mcp_servers")]
    servers: Vec<McpServerConfig>,
}

#[derive(Debug, Clone)]
pub struct McpConfigManager {
    config_path: PathBuf,
}

impl McpConfigManager {
    pub fn new(config_path: impl AsRef<Path>) -> Self {
        Self {
            config_path: config_path.as_ref().to_path_buf(),
        }
    }

    /// `mcp_servers.toml` is gitignored (the GUI writes per-install enable/disable
    /// toggles back to it — same pattern as `ghostlink.toml`/`ghostlink.example.toml`).
    /// On a fresh checkout it won't exist yet; copy the checked-in
    /// `mcp_servers.example.toml` template so real servers connect out of the box
    /// instead of silently connecting zero tools until someone copies it by hand.
    fn bootstrap_from_example(&self) {
        let example_path = self.config_path.with_file_name("mcp_servers.example.toml");
        if example_path.exists() {
            if let Err(err) = std::fs::copy(&example_path, &self.config_path) {
                tracing::warn!(
                    "mcp: failed to bootstrap {} from {}: {err}",
                    self.config_path.display(),
                    example_path.display()
                );
            }
        }
    }

    pub fn load(&self) -> Result<Vec<McpServerConfig>, String> {
        if !self.config_path.exists() {
            self.bootstrap_from_example();
        }
        if !self.config_path.exists() {
            return Ok(Vec::new());
        }

        let content = std::fs::read_to_string(&self.config_path)
            .map_err(|err| format!("failed to read {}: {err}", self.config_path.display()))?;
        let file: McpServersFile = toml::from_str(&content)
            .map_err(|err| format!("failed to parse {}: {err}", self.config_path.display()))?;

        for server in &file.servers {
            validate_server(server)?;
        }

        Ok(file.servers)
    }

    // Only called from set_enabled() below, which itself has no caller yet
    // (see McpRegistry::set_enabled's doc comment) - kept for that same
    // planned GUI enable/disable toggle rather than deleted.
    #[allow(dead_code)]
    pub fn save(&self, servers: &[McpServerConfig]) -> Result<(), String> {
        for server in servers {
            validate_server(server)?;
        }

        let file = McpServersFile {
            servers: servers.to_vec(),
        };
        let output = toml::to_string_pretty(&file)
            .map_err(|err| format!("failed to serialize mcp_servers.toml: {err}"))?;
        std::fs::write(&self.config_path, output)
            .map_err(|err| format!("failed to write {}: {err}", self.config_path.display()))?;
        Ok(())
    }

    #[allow(dead_code)]
    pub fn set_enabled(&self, name: &str, enabled: bool) -> Result<(), String> {
        let mut servers = self.load()?;
        let server = servers
            .iter_mut()
            .find(|s| s.name == name)
            .ok_or_else(|| format!("no MCP server named '{name}'"))?;
        server.enabled = enabled;
        self.save(&servers)
    }
}

/// Rejects a config whose `env` (stdio) or `headers` (HTTP) map contains a
/// literal-looking secret instead of an `${VAR_NAME}` reference — secrets must come
/// from the host environment, never from `mcp_servers.toml` itself.
fn validate_server(server: &McpServerConfig) -> Result<(), String> {
    match &server.transport {
        McpTransport::Stdio { env, .. } => {
            for (key, value) in env {
                if looks_like_literal_secret(value) {
                    return Err(format!(
                        "mcp server '{}': env var '{}' looks like a literal secret; use \"${{{}}}\" to reference a host environment variable instead",
                        server.name, key, key
                    ));
                }
            }
        }
        McpTransport::Http { headers, .. } => {
            for (key, value) in headers {
                if looks_like_literal_secret(value) {
                    return Err(format!(
                        "mcp server '{}': header '{}' looks like a literal secret; use \"${{{}}}\" to reference a host environment variable instead",
                        server.name, key, key
                    ));
                }
            }
        }
    }
    Ok(())
}

fn looks_like_literal_secret(value: &str) -> bool {
    if value.starts_with("${") && value.ends_with('}') {
        return false;
    }
    // Ordinary config values (URLs, model names, paths) are exempt — this
    // heuristic only targets bare, contiguous, key-shaped tokens.
    if value.starts_with("http://")
        || value.starts_with("https://")
        || value.contains(['/', ':', ' ', '\\'])
    {
        return false;
    }
    value.len() >= 20
        && value.chars().any(|c| c.is_ascii_digit())
        && value.chars().any(|c| c.is_ascii_alphabetic())
}

/// Resolves `${VAR_NAME}` to the host process environment; returns the value
/// unchanged otherwise (used for non-secret values).
pub fn resolve_env_value(raw: &str) -> String {
    if let Some(var_name) = raw.strip_prefix("${").and_then(|s| s.strip_suffix('}')) {
        std::env::var(var_name).unwrap_or_default()
    } else {
        raw.to_string()
    }
}

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

    fn temp_config_path() -> PathBuf {
        let suffix = format!(
            "ghostlink-mcp-{}-{}.toml",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        );
        std::env::temp_dir().join(suffix)
    }

    #[test]
    fn missing_file_yields_empty_list() {
        let manager = McpConfigManager::new(temp_config_path());
        assert!(manager.load().unwrap().is_empty());
    }

    #[test]
    fn missing_config_bootstraps_from_sibling_example() {
        let dir = std::env::temp_dir().join(format!(
            "ghostlink-mcp-bootstrap-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();

        let example_path = dir.join("mcp_servers.example.toml");
        std::fs::write(
            &example_path,
            r#"[[mcp_servers]]
name = "filesystem"
slot = "file_operations"
enabled = true
transport = "stdio"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "./mcp_workspace"]
"#,
        )
        .unwrap();

        let config_path = dir.join("mcp_servers.toml");
        assert!(!config_path.exists());

        let manager = McpConfigManager::new(&config_path);
        let servers = manager.load().unwrap();

        assert_eq!(servers.len(), 1);
        assert_eq!(servers[0].name, "filesystem");
        assert!(
            config_path.exists(),
            "load() should have copied the example into place"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn save_and_load_round_trip() {
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "filesystem".to_string(),
            slot: "file_operations".to_string(),
            enabled: true,
            transport: McpTransport::Stdio {
                command: "npx".to_string(),
                args: vec![
                    "-y".to_string(),
                    "@modelcontextprotocol/server-filesystem".to_string(),
                ],
                env: HashMap::new(),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        manager.save(&servers).unwrap();
        let loaded = manager.load().unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "filesystem");
        assert!(loaded[0].enabled);

        manager.set_enabled("filesystem", false).unwrap();
        let loaded = manager.load().unwrap();
        assert!(!loaded[0].enabled);

        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn literal_secret_is_rejected() {
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "brave-search".to_string(),
            slot: "web_search".to_string(),
            enabled: false,
            transport: McpTransport::Stdio {
                command: "npx".to_string(),
                args: vec![],
                env: HashMap::from([(
                    "BRAVE_API_KEY".to_string(),
                    "BSAxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1234".to_string(),
                )]),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        assert!(manager.save(&servers).is_err());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn literal_secret_in_http_header_is_rejected() {
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "remote-tools".to_string(),
            slot: "".to_string(),
            enabled: false,
            transport: McpTransport::Http {
                url: "https://mcp.example.com/mcp".to_string(),
                headers: HashMap::from([(
                    "X-Api-Key".to_string(),
                    "sk-liveSecretToken1234567890".to_string(),
                )]),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        assert!(manager.save(&servers).is_err());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn env_reference_is_accepted() {
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "brave-search".to_string(),
            slot: "web_search".to_string(),
            enabled: false,
            transport: McpTransport::Stdio {
                command: "npx".to_string(),
                args: vec![],
                env: HashMap::from([("BRAVE_API_KEY".to_string(), "${BRAVE_API_KEY}".to_string())]),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        assert!(manager.save(&servers).is_ok());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn env_reference_in_http_header_is_accepted() {
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "remote-tools".to_string(),
            slot: "".to_string(),
            enabled: false,
            transport: McpTransport::Http {
                url: "https://mcp.example.com/mcp".to_string(),
                headers: HashMap::from([(
                    "X-Api-Key".to_string(),
                    "${REMOTE_TOOLS_API_KEY}".to_string(),
                )]),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        assert!(manager.save(&servers).is_ok());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn url_shaped_value_is_not_flagged_as_a_secret() {
        // Regression test: an env value like an Ollama base URL was previously
        // (wrongly) rejected by the literal-secret heuristic just for being long
        // and containing both letters and digits.
        let path = temp_config_path();
        let manager = McpConfigManager::new(&path);
        let servers = vec![McpServerConfig {
            name: "vision".to_string(),
            slot: "".to_string(),
            enabled: false,
            transport: McpTransport::Stdio {
                command: "mcp-vision".to_string(),
                args: vec![],
                env: HashMap::from([(
                    "OLLAMA_URL".to_string(),
                    "http://127.0.0.1:11434".to_string(),
                )]),
            },
            requires_confirmation: false,
            timeout_secs: 30,
        }];

        assert!(manager.save(&servers).is_ok());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn resolve_env_value_reads_process_env() {
        std::env::set_var("GHOSTLINK_MCP_TEST_VAR", "resolved-value");
        assert_eq!(
            resolve_env_value("${GHOSTLINK_MCP_TEST_VAR}"),
            "resolved-value"
        );
        assert_eq!(resolve_env_value("literal"), "literal");
        std::env::remove_var("GHOSTLINK_MCP_TEST_VAR");
    }
}