ccboard-core 0.12.0

Core library for ccboard - parsers, models, store, watcher
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
//! Parser for Claude Desktop MCP server configuration
//!
//! Parses `~/.claude/claude_desktop_config.json` to extract MCP server definitions.

use anyhow::{Context, Result};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// MCP server configuration from claude_desktop_config.json
#[derive(Debug, Clone, Deserialize)]
pub struct McpConfig {
    /// Map of server name to server configuration
    #[serde(rename = "mcpServers")]
    pub servers: HashMap<String, McpServer>,
}

/// Individual MCP server definition
#[derive(Debug, Clone, Deserialize)]
pub struct McpServer {
    /// Server type ("stdio", "http") - optional field from .mcp.json format
    #[serde(default)]
    #[serde(rename = "type")]
    pub server_type: Option<String>,

    /// Command to execute (e.g., "npx", "node") - required for stdio servers
    #[serde(default)]
    pub command: String,

    /// Arguments to pass to the command - optional for stdio servers
    #[serde(default)]
    pub args: Vec<String>,

    /// Optional environment variables
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// URL for HTTP servers - optional field from .mcp.json format
    #[serde(default)]
    pub url: Option<String>,

    /// Headers for HTTP servers - optional field from .mcp.json format
    #[serde(default)]
    pub headers: Option<HashMap<String, String>>,
}

impl McpServer {
    /// Get display string for this server (command or URL)
    pub fn display_command(&self) -> String {
        if let Some(url) = &self.url {
            // HTTP server
            url.clone()
        } else if !self.command.is_empty() {
            // Stdio server
            if self.args.is_empty() {
                self.command.clone()
            } else {
                format!("{} {}", self.command, self.args.join(" "))
            }
        } else {
            "(unknown)".to_string()
        }
    }

    /// Check if this is an HTTP server
    pub fn is_http(&self) -> bool {
        self.url.is_some()
            || self
                .server_type
                .as_ref()
                .is_some_and(|t| t.to_lowercase() == "http")
    }
}

impl McpConfig {
    /// Load MCP configuration from claude_desktop_config.json and optional .mcp.json
    ///
    /// Merges configuration from:
    /// 1. Global: ~/.claude/claude_desktop_config.json
    /// 2. Project: <project>/.mcp.json (if project_path provided)
    ///
    /// Project config takes precedence for duplicate server names.
    ///
    /// Returns `None` if no config files exist (not an error - MCP is optional).
    /// Returns `Err` only if a file exists but cannot be parsed.
    pub fn load(claude_home: &Path) -> Result<Option<Self>> {
        Self::load_merged(claude_home, None)
    }

    /// Load MCP configuration with optional project-level config
    ///
    /// Merges configuration from:
    /// 1. Global: ~/.claude/claude_desktop_config.json
    /// 2. Project: <project>/.mcp.json (if project_path provided)
    ///
    /// Project config takes precedence for duplicate server names.
    pub fn load_merged(claude_home: &Path, project_path: Option<&Path>) -> Result<Option<Self>> {
        let mut global_config = Self::load_global(claude_home)?;
        let project_config = Self::load_project(project_path)?;

        match (global_config.as_mut(), project_config) {
            (Some(global), Some(project)) => {
                // Merge project servers into global (project takes precedence)
                for (name, server) in project.servers {
                    global.servers.insert(name, server);
                }
                Ok(Some(global.clone()))
            }
            (Some(global), None) => Ok(Some(global.clone())),
            (None, Some(project)) => Ok(Some(project)),
            (None, None) => Ok(None),
        }
    }

    /// Load global MCP config from claude_desktop_config.json
    fn load_global(claude_home: &Path) -> Result<Option<Self>> {
        let config_path = claude_home.join("claude_desktop_config.json");

        if !config_path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(&config_path)
            .context("Failed to read claude_desktop_config.json")?;

        let config: McpConfig =
            serde_json::from_str(&content).context("Failed to parse claude_desktop_config.json")?;

        Ok(Some(config))
    }

    /// Load project-level MCP config from .mcp.json
    fn load_project(project_path: Option<&Path>) -> Result<Option<Self>> {
        let Some(project) = project_path else {
            return Ok(None);
        };

        let config_path = project.join(".mcp.json");

        if !config_path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(&config_path).context("Failed to read .mcp.json")?;

        let config: McpConfig =
            serde_json::from_str(&content).context("Failed to parse .mcp.json")?;

        Ok(Some(config))
    }

    /// Get a formatted command string for display
    pub fn command_display(&self, name: &str) -> Option<String> {
        self.servers
            .get(name)
            .map(|server| server.display_command())
    }

    /// Check if a server has environment variables configured
    pub fn has_env(&self, name: &str) -> bool {
        self.servers
            .get(name)
            .map(|s| !s.env.is_empty())
            .unwrap_or(false)
    }
}

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

    #[test]
    fn test_parse_mcp_config() {
        let json = r#"{
            "mcpServers": {
                "playwright": {
                    "command": "npx",
                    "args": ["-y", "@modelcontextprotocol/server-playwright"]
                },
                "serena": {
                    "command": "npx",
                    "args": ["-y", "@serenaai/serena-mcp"],
                    "env": {
                        "SERENA_PROJECT_PATH": "/path/to/project"
                    }
                }
            }
        }"#;

        let config: McpConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.servers.len(), 2);
        assert!(config.servers.contains_key("playwright"));
        assert!(config.servers.contains_key("serena"));

        let playwright = &config.servers["playwright"];
        assert_eq!(playwright.command, "npx");
        assert_eq!(playwright.args.len(), 2);
        assert!(playwright.env.is_empty());

        let serena = &config.servers["serena"];
        assert_eq!(serena.env.len(), 1);
    }

    #[test]
    fn test_command_display() {
        let json = r#"{
            "mcpServers": {
                "test": {
                    "command": "node",
                    "args": ["server.js", "--port", "3000"]
                }
            }
        }"#;

        let config: McpConfig = serde_json::from_str(json).unwrap();
        let display = config.command_display("test").unwrap();
        assert_eq!(display, "node server.js --port 3000");
    }

    // Edge cases added for TDD compliance
    #[test]
    fn test_empty_config_parses_with_no_servers() {
        let json = r#"{"mcpServers": {}}"#;
        let config: McpConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.servers.len(), 0);
    }

    #[test]
    fn test_invalid_json_returns_error() {
        let invalid_json = r#"{ invalid json }"#;
        let result: Result<McpConfig, _> = serde_json::from_str(invalid_json);
        assert!(result.is_err());
    }

    #[test]
    fn test_missing_mcpservers_field_returns_error() {
        let json = r#"{"other": "field"}"#;
        let result: Result<McpConfig, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_http_server_without_command_is_valid() {
        // HTTP servers don't need command field
        let json = r#"{
            "mcpServers": {
                "http-server": {
                    "type": "http",
                    "url": "https://example.com/mcp"
                }
            }
        }"#;
        let result: Result<McpConfig, _> = serde_json::from_str(json);
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.servers.len(), 1);
        let server = &config.servers["http-server"];
        assert!(server.is_http());
        assert_eq!(server.url.as_deref(), Some("https://example.com/mcp"));
    }

    #[test]
    fn test_command_display_returns_none_for_nonexistent_server() {
        let json = r#"{"mcpServers": {}}"#;
        let config: McpConfig = serde_json::from_str(json).unwrap();
        assert!(config.command_display("nonexistent").is_none());
    }

    #[test]
    fn test_has_env_returns_false_for_server_without_env() {
        let json = r#"{
            "mcpServers": {
                "no-env": {
                    "command": "test",
                    "args": []
                }
            }
        }"#;
        let config: McpConfig = serde_json::from_str(json).unwrap();
        assert!(!config.has_env("no-env"));
    }

    #[test]
    fn test_has_env_returns_false_for_nonexistent_server() {
        let json = r#"{"mcpServers": {}}"#;
        let config: McpConfig = serde_json::from_str(json).unwrap();
        assert!(!config.has_env("nonexistent"));
    }

    #[test]
    fn test_load_returns_none_for_missing_file() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let result = McpConfig::load(temp_dir.path()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_load_returns_error_for_invalid_json_file() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("claude_desktop_config.json");
        fs::write(&config_path, "{ invalid json }").unwrap();

        let result = McpConfig::load(temp_dir.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_load_merged_combines_global_and_project_configs() {
        use std::fs;
        use tempfile::TempDir;

        // Setup global config
        let claude_home = TempDir::new().unwrap();
        let global_config = r#"{
            "mcpServers": {
                "playwright": {
                    "command": "npx",
                    "args": ["-y", "@modelcontextprotocol/server-playwright"]
                },
                "serena": {
                    "command": "npx",
                    "args": ["-y", "@serenaai/serena-mcp"]
                }
            }
        }"#;
        fs::write(
            claude_home.path().join("claude_desktop_config.json"),
            global_config,
        )
        .unwrap();

        // Setup project config
        let project_dir = TempDir::new().unwrap();
        let project_config = r#"{
            "mcpServers": {
                "postgres-staging": {
                    "command": "bash",
                    "args": ["script.sh"]
                },
                "serena": {
                    "command": "uvx",
                    "args": ["--from", "git+https://github.com/oraios/serena"]
                }
            }
        }"#;
        fs::write(project_dir.path().join(".mcp.json"), project_config).unwrap();

        // Load merged config
        let merged = McpConfig::load_merged(claude_home.path(), Some(project_dir.path()))
            .unwrap()
            .unwrap();

        // Should have 3 servers total (playwright from global, postgres-staging from project, serena overridden by project)
        assert_eq!(merged.servers.len(), 3);
        assert!(merged.servers.contains_key("playwright"));
        assert!(merged.servers.contains_key("postgres-staging"));
        assert!(merged.servers.contains_key("serena"));

        // Verify serena was overridden by project config
        let serena = &merged.servers["serena"];
        assert_eq!(serena.command, "uvx");
        assert_eq!(serena.args[0], "--from");
    }

    #[test]
    fn test_load_merged_returns_global_only_when_no_project() {
        use std::fs;
        use tempfile::TempDir;

        let claude_home = TempDir::new().unwrap();
        let global_config = r#"{
            "mcpServers": {
                "playwright": {
                    "command": "npx",
                    "args": ["-y", "@modelcontextprotocol/server-playwright"]
                }
            }
        }"#;
        fs::write(
            claude_home.path().join("claude_desktop_config.json"),
            global_config,
        )
        .unwrap();

        let merged = McpConfig::load_merged(claude_home.path(), None)
            .unwrap()
            .unwrap();

        assert_eq!(merged.servers.len(), 1);
        assert!(merged.servers.contains_key("playwright"));
    }

    #[test]
    fn test_load_merged_returns_project_only_when_no_global() {
        use std::fs;
        use tempfile::TempDir;

        let claude_home = TempDir::new().unwrap(); // No global config
        let project_dir = TempDir::new().unwrap();
        let project_config = r#"{
            "mcpServers": {
                "postgres": {
                    "command": "bash",
                    "args": ["script.sh"]
                }
            }
        }"#;
        fs::write(project_dir.path().join(".mcp.json"), project_config).unwrap();

        let merged = McpConfig::load_merged(claude_home.path(), Some(project_dir.path()))
            .unwrap()
            .unwrap();

        assert_eq!(merged.servers.len(), 1);
        assert!(merged.servers.contains_key("postgres"));
    }
}