nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! MCP server configuration management — global and project-level configs.
//!
//! This module provides types and functions for managing MCP server configurations
//! at both global (~/.nika/mcp.yaml) and project (.nika/mcp.yaml) levels.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────────┐
//! │  MCP CONFIG HIERARCHY (lowest wins)                                         │
//! ├─────────────────────────────────────────────────────────────────────────────┤
//! │                                                                             │
//! │  Global:   ~/.nika/mcp.yaml        ← User's personal MCP servers            │
//! │              ↑                                                              │
//! │  Project:  ./.nika/mcp.yaml        ← Project-specific servers               │
//! │              ↑                                                              │
//! │  Workflow: (inline in .nika.yaml)  ← Workflow-specific servers              │
//! │                                                                             │
//! │  Resolution: project overrides global, workflow overrides both              │
//! │                                                                             │
//! └─────────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Usage
//!
//! ```rust,ignore
//! use nika::core::mcp_config::{McpConfig, McpServer, load_global_config, save_global_config};
//!
//! // Load existing config (or create empty)
//! let mut config = load_global_config().unwrap_or_default();
//!
//! // Add a server
//! config.servers.insert("neo4j".to_string(), McpServer {
//!     command: "npx".to_string(),
//!     args: vec!["-y".to_string(), "@neo4j/mcp-neo4j".to_string()],
//!     env: Default::default(),
//!     description: Some("Neo4j graph database".to_string()),
//!     enabled: true,
//!     source: Some(McpSource::Global),
//! });
//!
//! // Save
//! save_global_config(&config)?;
//! ```

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

/// MCP configuration file structure.
///
/// Contains a version and a map of server names to their configurations.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct McpConfig {
    /// Config file version (currently 1)
    #[serde(default = "default_version")]
    pub version: u32,

    /// Map of server names to their configurations
    #[serde(default)]
    pub servers: HashMap<String, McpServer>,
}

fn default_version() -> u32 {
    1
}

/// MCP server configuration.
///
/// Defines how to start and configure an MCP server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct McpServer {
    /// Command to run (e.g., "npx", "node", "python")
    pub command: String,

    /// Command arguments (e.g., `["-y", "@neo4j/mcp-neo4j"]`)
    #[serde(default)]
    pub args: Vec<String>,

    /// Environment variables for the server
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// Human-readable description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Whether this server is enabled
    #[serde(default = "default_enabled")]
    pub enabled: bool,

    /// Source of this configuration (for merged configs)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<McpSource>,
}

fn default_enabled() -> bool {
    true
}

/// Source of an MCP server configuration.
///
/// Used to track where a server definition came from when configs are merged.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum McpSource {
    /// Global config (~/.nika/mcp.yaml)
    Global,
    /// Project config (.nika/mcp.yaml)
    Project,
    /// Workflow config (inline in .nika.yaml)
    Workflow,
}

impl std::fmt::Display for McpSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            McpSource::Global => write!(f, "global"),
            McpSource::Project => write!(f, "project"),
            McpSource::Workflow => write!(f, "workflow"),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// PATH HELPERS
// ═══════════════════════════════════════════════════════════════════════════════

/// Get the global MCP config directory (~/.nika/).
///
/// Creates the directory if it doesn't exist.
pub fn global_config_dir() -> Option<PathBuf> {
    dirs::home_dir().map(|h| h.join(".nika"))
}

/// Get the global MCP config file path (~/.nika/mcp.yaml).
pub fn global_config_path() -> Option<PathBuf> {
    global_config_dir().map(|d| d.join("mcp.yaml"))
}

/// Get the project MCP config file path (.nika/mcp.yaml).
///
/// Searches upward from current directory for a .nika directory.
pub fn project_config_path() -> Option<PathBuf> {
    find_project_root().map(|r| r.join(".nika").join("mcp.yaml"))
}

/// Get the project config path relative to a specific directory.
pub fn project_config_path_from(dir: &Path) -> PathBuf {
    dir.join(".nika").join("mcp.yaml")
}

/// Find the project root by searching for .nika, .git, or nika.yaml.
fn find_project_root() -> Option<PathBuf> {
    let cwd = std::env::current_dir().ok()?;
    let mut current = cwd.as_path();

    loop {
        // Check for .nika directory
        if current.join(".nika").is_dir() {
            return Some(current.to_path_buf());
        }
        // Check for .git directory (common project root indicator)
        if current.join(".git").exists() {
            return Some(current.to_path_buf());
        }
        // Check for nika.yaml (project manifest)
        if current.join("nika.yaml").exists() {
            return Some(current.to_path_buf());
        }

        current = current.parent()?;
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// LOAD / SAVE
// ═══════════════════════════════════════════════════════════════════════════════

/// Load the global MCP config (~/.nika/mcp.yaml).
///
/// Returns `None` if the file doesn't exist, `Err` on parse errors.
pub fn load_global_config() -> Result<Option<McpConfig>, McpConfigError> {
    load_config_from_path(global_config_path())
}

/// Load the project MCP config (.nika/mcp.yaml).
///
/// Returns `None` if the file doesn't exist, `Err` on parse errors.
pub fn load_project_config() -> Result<Option<McpConfig>, McpConfigError> {
    load_config_from_path(project_config_path())
}

/// Load an MCP config from a specific path.
pub fn load_config_from_path(path: Option<PathBuf>) -> Result<Option<McpConfig>, McpConfigError> {
    let path = match path {
        Some(p) => p,
        None => return Ok(None),
    };

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

    let content = std::fs::read_to_string(&path).map_err(|e| McpConfigError::Io {
        path: path.clone(),
        source: e,
    })?;

    let config: McpConfig = serde_yaml::from_str(&content).map_err(|e| McpConfigError::Parse {
        path: path.clone(),
        message: e.to_string(),
    })?;

    Ok(Some(config))
}

/// Save the global MCP config (~/.nika/mcp.yaml).
///
/// Creates the directory if it doesn't exist.
pub fn save_global_config(config: &McpConfig) -> Result<(), McpConfigError> {
    let path = global_config_path().ok_or(McpConfigError::NoHomeDir)?;
    save_config_to_path(config, &path)
}

/// Save the project MCP config (.nika/mcp.yaml).
///
/// Creates the directory if it doesn't exist.
pub fn save_project_config(config: &McpConfig) -> Result<(), McpConfigError> {
    let path = project_config_path().ok_or(McpConfigError::NoProjectRoot)?;
    save_config_to_path(config, &path)
}

/// Save an MCP config to a specific path.
pub fn save_config_to_path(config: &McpConfig, path: &Path) -> Result<(), McpConfigError> {
    // Create parent directory if needed
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| McpConfigError::Io {
            path: parent.to_path_buf(),
            source: e,
        })?;
    }

    let content =
        serde_yaml::to_string(config).map_err(|e| McpConfigError::Serialize(e.to_string()))?;
    std::fs::write(path, content).map_err(|e| McpConfigError::Io {
        path: path.to_path_buf(),
        source: e,
    })?;

    Ok(())
}

// ═══════════════════════════════════════════════════════════════════════════════
// MERGE
// ═══════════════════════════════════════════════════════════════════════════════

/// Merge multiple configs with precedence: later configs override earlier ones.
///
/// The `source` field is set on each server to indicate its origin.
pub fn merge_configs(configs: Vec<(McpConfig, McpSource)>) -> McpConfig {
    let mut merged = McpConfig::default();

    for (config, source) in configs {
        for (name, mut server) in config.servers {
            server.source = Some(source);
            merged.servers.insert(name, server);
        }
    }

    merged
}

/// Load and merge global + project configs.
///
/// Project config overrides global config.
pub fn load_merged_config() -> Result<McpConfig, McpConfigError> {
    let mut configs = Vec::new();

    // Load global config (if exists)
    if let Some(global) = load_global_config()? {
        configs.push((global, McpSource::Global));
    }

    // Load project config (if exists)
    if let Some(project) = load_project_config()? {
        configs.push((project, McpSource::Project));
    }

    Ok(merge_configs(configs))
}

// ═══════════════════════════════════════════════════════════════════════════════
// SERVER MANAGEMENT
// ═══════════════════════════════════════════════════════════════════════════════

/// Create an McpServer from an npm package name.
///
/// Uses `npx -y <package>` as the command.
pub fn server_from_npm_package(package: &str, description: Option<&str>) -> McpServer {
    McpServer {
        command: "npx".to_string(),
        args: vec!["-y".to_string(), package.to_string()],
        env: HashMap::new(),
        description: description.map(|s| s.to_string()),
        enabled: true,
        source: None,
    }
}

/// Add a server to the global config.
///
/// Returns `true` if the server was added, `false` if it already exists.
pub fn add_server_to_global(name: &str, server: McpServer) -> Result<bool, McpConfigError> {
    let mut config = load_global_config()?.unwrap_or_default();

    if config.servers.contains_key(name) {
        return Ok(false);
    }

    config.servers.insert(name.to_string(), server);
    save_global_config(&config)?;
    Ok(true)
}

/// Add a server to the project config.
///
/// Returns `true` if the server was added, `false` if it already exists.
pub fn add_server_to_project(name: &str, server: McpServer) -> Result<bool, McpConfigError> {
    let mut config = load_project_config()?.unwrap_or_default();

    if config.servers.contains_key(name) {
        return Ok(false);
    }

    config.servers.insert(name.to_string(), server);
    save_project_config(&config)?;
    Ok(true)
}

/// Remove a server from the global config.
///
/// Returns `true` if the server was removed, `false` if it didn't exist.
pub fn remove_server_from_global(name: &str) -> Result<bool, McpConfigError> {
    let mut config = match load_global_config()? {
        Some(c) => c,
        None => return Ok(false),
    };

    let removed = config.servers.remove(name).is_some();
    if removed {
        save_global_config(&config)?;
    }
    Ok(removed)
}

/// Remove a server from the project config.
///
/// Returns `true` if the server was removed, `false` if it didn't exist.
pub fn remove_server_from_project(name: &str) -> Result<bool, McpConfigError> {
    let mut config = match load_project_config()? {
        Some(c) => c,
        None => return Ok(false),
    };

    let removed = config.servers.remove(name).is_some();
    if removed {
        save_project_config(&config)?;
    }
    Ok(removed)
}

// ═══════════════════════════════════════════════════════════════════════════════
// ERRORS
// ═══════════════════════════════════════════════════════════════════════════════

/// Errors that can occur when working with MCP configs.
#[derive(Debug)]
pub enum McpConfigError {
    /// IO error (reading/writing files)
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    /// YAML parse error
    Parse { path: PathBuf, message: String },
    /// YAML serialize error
    Serialize(String),
    /// Home directory not found
    NoHomeDir,
    /// Project root not found
    NoProjectRoot,
}

impl std::fmt::Display for McpConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            McpConfigError::Io { path, source } => {
                write!(f, "I/O error for {}: {}", path.display(), source)
            }
            McpConfigError::Parse { path, message } => {
                write!(f, "Parse error in {}: {}", path.display(), message)
            }
            McpConfigError::Serialize(e) => write!(f, "Serialize error: {}", e),
            McpConfigError::NoHomeDir => write!(f, "Home directory not found"),
            McpConfigError::NoProjectRoot => write!(f, "Project root not found"),
        }
    }
}

impl std::error::Error for McpConfigError {}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

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

    #[test]
    fn test_default_config() {
        let config = McpConfig::default();
        assert_eq!(config.version, 0); // serde default is 0, not 1 (default_version only for deserialize)
        assert!(config.servers.is_empty());
    }

    #[test]
    fn test_server_from_npm_package() {
        let server = server_from_npm_package("@neo4j/mcp-neo4j", Some("Neo4j database"));
        assert_eq!(server.command, "npx");
        assert_eq!(server.args, vec!["-y", "@neo4j/mcp-neo4j"]);
        assert_eq!(server.description, Some("Neo4j database".to_string()));
        assert!(server.enabled);
        assert!(server.source.is_none());
    }

    #[test]
    fn test_mcp_source_display() {
        assert_eq!(McpSource::Global.to_string(), "global");
        assert_eq!(McpSource::Project.to_string(), "project");
        assert_eq!(McpSource::Workflow.to_string(), "workflow");
    }

    #[test]
    fn test_merge_configs() {
        let global = McpConfig {
            version: 1,
            servers: {
                let mut m = HashMap::new();
                m.insert(
                    "neo4j".to_string(),
                    McpServer {
                        command: "npx".to_string(),
                        args: vec!["-y".to_string(), "@neo4j/mcp-neo4j".to_string()],
                        env: HashMap::new(),
                        description: Some("Global neo4j".to_string()),
                        enabled: true,
                        source: None,
                    },
                );
                m.insert(
                    "github".to_string(),
                    McpServer {
                        command: "npx".to_string(),
                        args: vec![
                            "-y".to_string(),
                            "@modelcontextprotocol/server-github".to_string(),
                        ],
                        env: HashMap::new(),
                        description: Some("GitHub".to_string()),
                        enabled: true,
                        source: None,
                    },
                );
                m
            },
        };

        let project = McpConfig {
            version: 1,
            servers: {
                let mut m = HashMap::new();
                m.insert(
                    "neo4j".to_string(),
                    McpServer {
                        command: "npx".to_string(),
                        args: vec!["-y".to_string(), "@neo4j/mcp-neo4j".to_string()],
                        env: {
                            let mut e = HashMap::new();
                            e.insert("NEO4J_URI".to_string(), "bolt://project:7687".to_string());
                            e
                        },
                        description: Some("Project neo4j".to_string()),
                        enabled: true,
                        source: None,
                    },
                );
                m
            },
        };

        let merged = merge_configs(vec![
            (global, McpSource::Global),
            (project, McpSource::Project),
        ]);

        // Project neo4j should override global neo4j
        assert_eq!(merged.servers.len(), 2);
        let neo4j = merged.servers.get("neo4j").unwrap();
        assert_eq!(neo4j.description, Some("Project neo4j".to_string()));
        assert_eq!(neo4j.source, Some(McpSource::Project));
        assert!(neo4j.env.contains_key("NEO4J_URI"));

        // GitHub should remain from global
        let github = merged.servers.get("github").unwrap();
        assert_eq!(github.source, Some(McpSource::Global));
    }

    #[test]
    fn test_serialize_deserialize_roundtrip() {
        let config = McpConfig {
            version: 1,
            servers: {
                let mut m = HashMap::new();
                m.insert(
                    "test".to_string(),
                    McpServer {
                        command: "npx".to_string(),
                        args: vec!["-y".to_string(), "test-package".to_string()],
                        env: {
                            let mut e = HashMap::new();
                            e.insert("KEY".to_string(), "value".to_string());
                            e
                        },
                        description: Some("Test server".to_string()),
                        enabled: true,
                        source: Some(McpSource::Global),
                    },
                );
                m
            },
        };

        let yaml = serde_yaml::to_string(&config).unwrap();
        let parsed: McpConfig = serde_yaml::from_str(&yaml).unwrap();

        assert_eq!(config.version, parsed.version);
        assert_eq!(config.servers.len(), parsed.servers.len());
        assert_eq!(
            config.servers.get("test").unwrap().command,
            parsed.servers.get("test").unwrap().command
        );
    }

    #[test]
    fn test_global_config_path() {
        let path = global_config_path();
        // Should be Some on most systems
        if let Some(p) = path {
            assert!(p.ends_with(".nika/mcp.yaml") || p.ends_with(".nika\\mcp.yaml"));
        }
    }

    #[test]
    fn test_load_nonexistent_config() {
        // Loading a nonexistent path should return Ok(None)
        let result = load_config_from_path(Some(PathBuf::from("/nonexistent/path/mcp.yaml")));
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }
}