mcp-valve 2.0.1

Unified MCP CLI - Generic MCP Protocol Client
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
//! # Unified MCP CLI
//!
//! A generic MCP (Model Context Protocol) client that works with any MCP server
//! through configurable server profiles.
//!
//! ## Design Philosophy
//!
//! **This script is a thin wrapper around the MCP protocol.**
//!
//! - Tools are called by providing JSON arguments that conform to the `inputSchema` from `tools/list`
//! - **No validation or field name conversion** occurs in this script
//! - The authoritative source of truth is the MCP server's `inputSchema`
//!
//! **Golden Rule**: If `tools/list` shows a field named `X` in the schema, use `X` exactly as-is in the JSON.
//!
//! ## Overview
//!
//! This tool provides a unified interface to any MCP server. Instead of maintaining
//! separate CLI tools for each MCP server, configure servers in `~/.config/mcp-valve/servers.json`
//! and use this single client.
//!
//! ## Quick Start
//!
//! ```bash
//! # List configured servers
//! mcp-valve list-servers
//!
//! # Start daemon (required before any tool operations)
//! cd /path/to/your/project
//! mcp-valve --server playwright start-daemon --server-args '["--gui"]'
//!
//! # List tools (requires daemon)
//! mcp-valve --server playwright list-tools
//!
//! # Call a tool (requires daemon)
//! mcp-valve --server playwright call browser_navigate --args '{"url":"https://example.com"}'
//!
//! # Check daemon status
//! mcp-valve --server playwright daemon-status
//!
//! # Stop daemon
//! mcp-valve --server playwright stop-daemon
//! ```
//!
//! ## Configuration
//!
//! Config file is searched in order: `--config` flag, `MCP_VALVE_CONFIG` env var,
//! `$XDG_CONFIG_HOME/mcp-valve/servers.json`, `~/.config/mcp-valve/servers.json`,
//! `~/.claude/scripts/mcp-servers.json` (legacy).
//!
//! Example config:
//!
//! ```json
//! {
//!   "playwright": {
//!     "command": ["npx", "@playwright/mcp@latest"],
//!     "default_args": ["--headless"],
//!     "supports_daemon": true,
//!     "description": "Playwright browser automation",
//!     "env": {}
//!   },
//!   "zen": {
//!     "command": ["/Users/yonaka/zen-mcp-server/.zen_venv/bin/python"],
//!     "default_args": ["/Users/yonaka/zen-mcp-server/server.py"],
//!     "supports_daemon": false,
//!     "description": "Zen MCP multi-AI model integration",
//!     "env": {}
//!   }
//! }
//! ```
//!
//! ## Features
//!
//! - ✅ Generic MCP protocol client
//! - ✅ JSON-based server configuration
//! - ✅ Support for any MCP server
//! - ✅ Interactive shell mode
//! - ✅ Server-specific arguments via --server-args
//! - ✅ Daemon mode with persistent state
//! - ✅ Project-aware: displays current working directory for all operations
//!
//! ## Technical Details
//!
//! - **Protocol**: MCP 2025-06-18 (JSON-RPC 2.0)
//! - **Transport**: STDIO / Unix socket (daemon)
//! - **Dependencies**: serde, serde_json, anyhow, clap, nix

#[cfg(not(unix))]
compile_error!("mcp-valve requires a Unix platform (Linux, macOS, BSD)");

use anyhow::{anyhow, Context, Result};
use clap::{Parser, Subcommand};
use nix::sys::signal::{kill, Signal};
use nix::sys::stat::{umask, Mode};
use nix::unistd::{setsid, Pid};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::{UnixListener, UnixStream};
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use std::time::Duration;

// ============================================================================
// Configuration
// ============================================================================

#[derive(Debug, Deserialize, Serialize, Clone)]
struct ServerProfile {
    command: Vec<String>,
    #[serde(default)]
    default_args: Vec<String>,
    #[serde(default)]
    supports_daemon: bool,
    #[serde(default)]
    description: String,
    #[serde(default)]
    env: HashMap<String, String>,
}

#[derive(Debug, Deserialize)]
struct ServerConfig {
    #[serde(flatten)]
    servers: HashMap<String, ServerProfile>,
}

/// Resolves config file path with priority:
/// 1. CLI flag (--config)
/// 2. Environment variable (MCP_VALVE_CONFIG)
/// 3. XDG_CONFIG_HOME/mcp-valve/servers.json
/// 4. ~/.config/mcp-valve/servers.json
/// 5. ~/.claude/scripts/mcp-servers.json (legacy)
fn get_config_path(cli_config: Option<PathBuf>) -> Result<PathBuf> {
    // 1. CLI flag (highest priority)
    if let Some(path) = cli_config {
        return Ok(path);
    }

    // 2. Environment variable
    if let Ok(path) = std::env::var("MCP_VALVE_CONFIG") {
        return Ok(PathBuf::from(path));
    }

    let home = std::env::var("HOME").context("HOME environment variable not set")?;

    // 3. XDG_CONFIG_HOME if set
    if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
        let path = PathBuf::from(xdg).join("mcp-valve/servers.json");
        if path.exists() {
            return Ok(path);
        }
    }

    // 4. XDG default location
    let xdg_default = PathBuf::from(&home).join(".config/mcp-valve/servers.json");
    if xdg_default.exists() {
        return Ok(xdg_default);
    }

    // 5. Legacy fallback
    Ok(PathBuf::from(&home).join(".claude/scripts/mcp-servers.json"))
}

fn load_server_config(cli_config: Option<PathBuf>) -> Result<ServerConfig> {
    let config_path = get_config_path(cli_config)?;

    if !config_path.exists() {
        let home = std::env::var("HOME").unwrap_or_default();
        return Err(anyhow!(
            "Configuration file not found.\n\n\
            Searched locations (in order):\n  \
            1. --config flag or MCP_VALVE_CONFIG env var\n  \
            2. $XDG_CONFIG_HOME/mcp-valve/servers.json\n  \
            3. ~/.config/mcp-valve/servers.json\n  \
            4. ~/.claude/scripts/mcp-servers.json\n\n\
            Create a config file at: {}\n\n\
            Example:\n\
            {{\n  \
              \"server-name\": {{\n    \
                \"command\": [\"npx\", \"@example/mcp-server\"],\n    \
                \"default_args\": [],\n    \
                \"supports_daemon\": true,\n    \
                \"description\": \"Example MCP server\",\n    \
                \"env\": {{}}\n  \
              }}\n\
            }}",
            PathBuf::from(&home).join(".config/mcp-valve/servers.json").display()
        ));
    }

    let config_content = fs::read_to_string(&config_path)
        .with_context(|| format!("Failed to read config: {}", config_path.display()))?;

    let config: ServerConfig = serde_json::from_str(&config_content)
        .with_context(|| format!("Invalid JSON in config: {}", config_path.display()))?;

    Ok(config)
}

// ============================================================================
// CLI Definition
// ============================================================================

#[derive(Parser)]
#[command(name = "mcp-valve")]
#[command(about = "Unified MCP CLI - Generic MCP Protocol Client")]
#[command(version)]
struct Cli {
    /// Server name from config (e.g., playwright, zen)
    #[arg(short, long)]
    server: Option<String>,

    /// Additional server arguments (JSON array, e.g., '["--gui", "--browser", "firefox"]')
    #[arg(long)]
    server_args: Option<String>,

    /// Path to config file (overrides default locations)
    #[arg(short, long, global = true)]
    config: Option<PathBuf>,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// List all configured servers
    ListServers,

    /// Call any MCP tool
    Call {
        /// Tool name (e.g., browser_navigate, chat)
        tool: String,
        /// Arguments as JSON string
        #[arg(short, long, default_value = "{}")]
        args: String,
    },

    /// List all available tools from the server
    ListTools,

    /// Interactive shell mode
    Shell,

    /// Start background daemon (requires supports_daemon: true)
    StartDaemon,

    /// Stop background daemon
    StopDaemon,

    /// Check daemon status
    DaemonStatus,
}

// ============================================================================
// Template Variable Expansion
// ============================================================================

/// Sanitizes server name to prevent path traversal attacks
///
/// Only allows alphanumeric characters, hyphens, and underscores
fn sanitize_server_name(name: &str) -> String {
    name.chars()
        .filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_')
        .collect()
}

/// Expands template variables in argument strings
///
/// Supported variables:
/// - {profile_dir}: .mcp-profile/<server-name> (sanitized)
/// - {pid}: Process ID
/// - {cwd}: Current working directory
///
/// Security: Server names are sanitized to prevent path traversal
fn expand_template_vars(arg: &str, server_name: &str) -> String {
    let safe_server_name = sanitize_server_name(server_name);
    let profile_dir = PathBuf::from(".mcp-profile").join(&safe_server_name);
    let profile_dir_str = profile_dir.to_str().unwrap_or("");
    let pid = std::process::id().to_string();
    let cwd = std::env::current_dir()
        .ok()
        .and_then(|p| p.to_str().map(|s| s.to_string()))
        .unwrap_or_else(|| ".".to_string());

    arg.replace("{profile_dir}", profile_dir_str)
        .replace("{pid}", &pid)
        .replace("{cwd}", &cwd)
}

// ============================================================================
// MCP Client (Generic)
// ============================================================================

struct McpClient {
    child: Child,
    stdin: ChildStdin,
    stdout: BufReader<ChildStdout>,
    request_id: u64,
}

impl McpClient {
    fn start(profile: &ServerProfile, extra_args: Option<Vec<String>>, server_name: &str) -> Result<Self> {
        eprintln!("🚀 Starting MCP server...");

        if profile.command.is_empty() {
            return Err(anyhow!("Server profile has empty command"));
        }

        let mut cmd = Command::new(&profile.command[0]);

        // Add command args (e.g., for npx: "@playwright/mcp@latest")
        if profile.command.len() > 1 {
            cmd.args(&profile.command[1..]);
        }

        // Add args: if --server-args was provided (even if empty), use it to override default_args
        // Otherwise use default_args from profile
        // Template variables are expanded for both default_args and extra_args
        let args_to_use = match extra_args {
            Some(args) => args.iter().map(|arg| expand_template_vars(arg, server_name)).collect(),
            None => profile.default_args.iter().map(|arg| expand_template_vars(arg, server_name)).collect::<Vec<String>>(),
        };
        cmd.args(&args_to_use);

        // Set environment variables
        for (key, value) in &profile.env {
            cmd.env(key, value);
        }

        let mut child = cmd
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit())
            .spawn()
            .with_context(|| format!("Failed to spawn MCP server: {:?}", profile.command))?;

        let stdin = child.stdin.take().unwrap();
        let stdout = BufReader::new(child.stdout.take().unwrap());

        let mut mcp = Self {
            child,
            stdin,
            stdout,
            request_id: 0,
        };

        mcp.initialize()?;
        eprintln!("✅ MCP server ready");
        Ok(mcp)
    }

    fn initialize(&mut self) -> Result<()> {
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": self.next_id(),
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "clientInfo": {
                    "name": "mcp-valve",
                    "version": env!("CARGO_PKG_VERSION")
                }
            }
        });

        self.send_request(&init_request)?;

        let notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized",
            "params": {}
        });

        self.send_notification(&notification)?;
        Ok(())
    }

    fn send_request(&mut self, request: &Value) -> Result<Value> {
        let request_str = serde_json::to_string(request)?;
        writeln!(self.stdin, "{}", request_str)?;
        self.stdin.flush()?;

        let mut line = String::new();
        self.stdout.read_line(&mut line)?;

        let response: Value = serde_json::from_str(line.trim())
            .context("Failed to parse JSON-RPC response")?;

        if let Some(error) = response.get("error") {
            return Err(anyhow!("MCP Error: {}", error));
        }

        Ok(response)
    }

    fn send_notification(&mut self, notification: &Value) -> Result<()> {
        let notif_str = serde_json::to_string(notification)?;
        writeln!(self.stdin, "{}", notif_str)?;
        self.stdin.flush()?;
        Ok(())
    }

    fn next_id(&mut self) -> u64 {
        self.request_id += 1;
        self.request_id
    }

    fn call_tool(&mut self, name: &str, args: Value) -> Result<Value> {
        let request = json!({
            "jsonrpc": "2.0",
            "id": self.next_id(),
            "method": "tools/call",
            "params": {
                "name": name,
                "arguments": args
            }
        });

        let response = match self.send_request(&request) {
            Ok(resp) => resp,
            Err(e) => {
                let error_with_schema = self.format_error_with_schema(name, &e.to_string());
                return Err(anyhow!("{}", error_with_schema));
            }
        };
        let result = response["result"].clone();

        // Check for tool-level errors (isError field in result)
        if let Some(is_error) = result.get("isError").and_then(|v| v.as_bool()) {
            if is_error {
                // Extract error message from content if available
                let error_msg = result
                    .get("content")
                    .and_then(|c| c.as_array())
                    .and_then(|arr| arr.first())
                    .and_then(|item| item.get("text"))
                    .and_then(|t| t.as_str())
                    .unwrap_or("Tool execution failed");

                let error_with_schema =
                    self.format_error_with_schema(name, &format!("Tool Error: {}", error_msg));
                return Err(anyhow!("{}", error_with_schema));
            }
        }

        Ok(result)
    }

    fn list_tools(&mut self) -> Result<Value> {
        let request = json!({
            "jsonrpc": "2.0",
            "id": self.next_id(),
            "method": "tools/list",
            "params": {}
        });

        let response = self.send_request(&request)?;
        Ok(response["result"].clone())
    }

    /// Get the inputSchema for a specific tool
    fn get_tool_schema(&mut self, tool_name: &str) -> Option<Value> {
        self.list_tools()
            .ok()
            .and_then(|result| result.get("tools").cloned())
            .and_then(|tools| tools.as_array().cloned())
            .and_then(|tools| {
                tools
                    .into_iter()
                    .find(|t| t.get("name").and_then(|n| n.as_str()) == Some(tool_name))
            })
            .and_then(|tool| tool.get("inputSchema").cloned())
    }

    /// Format error message with tool schema appended
    fn format_error_with_schema(&mut self, tool_name: &str, error_msg: &str) -> String {
        match self.get_tool_schema(tool_name) {
            Some(schema) => {
                let schema_str = serde_json::to_string_pretty(&schema)
                    .unwrap_or_else(|_| schema.to_string());
                format!(
                    "{}\n\nSchema for tool '{}':\n{}",
                    error_msg, tool_name, schema_str
                )
            }
            None => error_msg.to_string(),
        }
    }
}

impl Drop for McpClient {
    fn drop(&mut self) {
        let _ = self.child.kill();
    }
}

// ============================================================================
// Project Context
// ============================================================================

/// Get the current project path (current working directory)
fn get_project_path() -> String {
    std::env::current_dir()
        .ok()
        .and_then(|p| p.to_str().map(|s| s.to_string()))
        .unwrap_or_else(|| ".".to_string())
}

/// Format error message when daemon is not running
fn daemon_not_running_error(server_name: &str) -> anyhow::Error {
    let project = get_project_path();
    anyhow!(
        "Daemon is not running for project '{}'\n\n\
        Start daemon with:\n  \
        cd {}\n  \
        mcp-valve --server {} start-daemon",
        project, project, server_name
    )
}

// ============================================================================
// Daemon Management
// ============================================================================

struct DaemonManager {
    server_name: String,
    pid_file: PathBuf,
}

impl DaemonManager {
    fn new(server_name: &str) -> Self {
        let safe_server_name = sanitize_server_name(server_name);
        let profile_dir = PathBuf::from(".mcp-profile")
            .join(&safe_server_name);

        // Ensure profile directory exists with secure permissions (0700)
        if !profile_dir.exists() {
            let old_umask = umask(Mode::from_bits_truncate(0o077));
            fs::create_dir_all(&profile_dir)
                .expect("Failed to create daemon profile directory");
            umask(old_umask);
        }

        Self {
            server_name: server_name.to_string(),
            pid_file: profile_dir.join("daemon.pid"),
        }
    }

    fn get_socket_path(&self) -> Result<PathBuf> {
        // Read daemon PID from file
        let pid_str = fs::read_to_string(&self.pid_file)
            .context("Failed to read PID file")?;
        let pid = pid_str.trim();

        // Socket path includes PID to avoid conflicts
        Ok(PathBuf::from("/tmp/.mcp").join(format!("{}-{}.sock", self.server_name, pid)))
    }

    fn is_running(&self) -> Result<bool> {
        if !self.pid_file.exists() {
            return Ok(false);
        }

        let pid_str = fs::read_to_string(&self.pid_file)
            .context("Failed to read PID file")?;
        let pid = pid_str.trim().parse::<i32>()
            .with_context(|| format!("Invalid PID in file: '{}'", pid_str.trim()))?;

        // Check if process exists using kill with signal 0
        // This doesn't send any signal but checks if process exists and we have permission
        match kill(Pid::from_raw(pid), None) {
            Ok(_) => Ok(true),  // Process exists
            Err(nix::errno::Errno::ESRCH) => Ok(false),  // No such process
            Err(nix::errno::Errno::EPERM) => Ok(true),   // Process exists but no permission
            Err(_) => Ok(false),  // Other errors, assume not running
        }
    }

    fn start(
        &self,
        profile: &ServerProfile,
        extra_args: Option<Vec<String>>,
    ) -> Result<()> {
        if !profile.supports_daemon {
            return Err(anyhow!(
                "Server '{}' does not support daemon mode (supports_daemon: false)",
                self.server_name
            ));
        }

        if self.is_running()? {
            return Err(anyhow!("Daemon already running for '{}'", self.server_name));
        }

        let project = get_project_path();
        eprintln!("Project: {}", project);
        eprintln!("Profile: {}", self.pid_file.parent().unwrap().display());
        eprintln!("Starting MCP daemon for '{}'...", self.server_name);

        // Build daemon command
        let mut cmd = Command::new(std::env::current_exe()?);
        cmd.arg("__internal_daemon");
        cmd.arg("--server");
        cmd.arg(&self.server_name);

        if let Some(ref args) = extra_args {
            cmd.arg("--server-args");
            cmd.arg(serde_json::to_string(args)?);
        }

        // Create log file for daemon stderr
        let profile_dir = self.pid_file.parent().unwrap();
        let log_file = std::fs::File::create(profile_dir.join("daemon.log"))
            .context("Failed to create daemon log file")?;

        // Fork daemon process with proper daemonization
        let child = unsafe {
            cmd.pre_exec(|| {
                // Create new session to detach from controlling terminal
                setsid().map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
                Ok(())
            })
        }
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::from(log_file))
        .spawn()
        .context("Failed to spawn daemon process")?;

        let child_pid = child.id();

        // Write PID file
        fs::write(&self.pid_file, child_pid.to_string())
            .context("Failed to write PID file")?;

        // Construct expected socket path based on child PID
        let expected_socket = PathBuf::from("/tmp/.mcp")
            .join(format!("{}-{}.sock", self.server_name, child_pid));

        // Wait for socket file to appear
        for i in 0..50 {
            if expected_socket.exists() {
                eprintln!("Daemon started (PID: {})", child_pid);
                eprintln!("Socket: {}", expected_socket.display());
                return Ok(());
            }
            std::thread::sleep(Duration::from_millis(100));

            // After 2 seconds, check if process is still alive
            if i == 20 {
                // Use kill with signal 0 to check if process exists
                if kill(Pid::from_raw(child_pid as i32), None).is_err() {
                    fs::remove_file(&self.pid_file).ok();
                    return Err(anyhow!(
                        "Daemon process exited unexpectedly. Check {}/daemon.log",
                        profile_dir.display()
                    ));
                }
            }
        }

        // Timeout
        fs::remove_file(&self.pid_file).ok();
        Err(anyhow!(
            "Daemon failed to start - socket file not created within 5 seconds. Check {}/daemon.log",
            profile_dir.display()
        ))
    }

    fn stop(&self) -> Result<()> {
        if !self.is_running()? {
            return Err(daemon_not_running_error(&self.server_name));
        }

        let project = get_project_path();
        let pid_str = fs::read_to_string(&self.pid_file)?;
        let pid: i32 = pid_str.trim().parse()
            .context("Invalid PID in file")?;

        let socket_path = self.get_socket_path().ok();

        eprintln!("Project: {}", project);
        eprintln!("Stopping daemon (PID: {})...", pid);

        // Send SIGTERM
        kill(Pid::from_raw(pid), Signal::SIGTERM)
            .context("Failed to send SIGTERM")?;

        // Wait for graceful shutdown
        for _ in 0..10 {
            if !self.is_running()? {
                fs::remove_file(&self.pid_file).ok();
                if let Some(ref sp) = socket_path {
                    if sp.exists() {
                        fs::remove_file(sp).ok();
                    }
                }
                eprintln!("Daemon stopped");
                return Ok(());
            }
            std::thread::sleep(Duration::from_millis(500));
        }

        // Force kill
        kill(Pid::from_raw(pid), Signal::SIGKILL)
            .context("Failed to send SIGKILL")?;

        fs::remove_file(&self.pid_file).ok();
        if let Some(ref sp) = socket_path {
            if sp.exists() {
                fs::remove_file(sp).ok();
            }
        }

        eprintln!("Daemon stopped (forced)");
        Ok(())
    }

    fn status(&self) -> Result<()> {
        let project = get_project_path();
        let profile_dir = self.pid_file.parent().unwrap();
        println!("Project: {}", project);
        println!("Server: {}", self.server_name);
        println!("Profile: {}", profile_dir.display());

        if self.is_running()? {
            let pid_str = fs::read_to_string(&self.pid_file)?;
            let socket_path = self.get_socket_path()?;
            println!("Daemon is running");
            println!("  PID: {}", pid_str.trim());
            println!("  Socket: {}", socket_path.display());
        } else {
            println!("Daemon is not running");
            if self.pid_file.exists() {
                eprintln!("Warning: Stale PID file found, cleaning up...");
                let socket_path = self.get_socket_path().ok();
                fs::remove_file(&self.pid_file).ok();
                if let Some(sp) = socket_path {
                    if sp.exists() {
                        fs::remove_file(&sp).ok();
                    }
                }
            }
        }
        Ok(())
    }
}

// ============================================================================
// Unix Socket Communication
// ============================================================================

fn run_daemon(server_name: &str, profile: &ServerProfile, extra_args: Option<Vec<String>>) -> Result<()> {
    // Use /tmp for socket with daemon's own PID
    let socket_dir = PathBuf::from("/tmp/.mcp");

    // Ensure socket directory exists with secure permissions
    if !socket_dir.exists() {
        let old_umask = umask(Mode::from_bits_truncate(0o077));
        fs::create_dir_all(&socket_dir)
            .context("Failed to create socket directory")?;
        umask(old_umask);
    }

    let socket_path = socket_dir.join(format!("{}-{}.sock", server_name, std::process::id()));

    // Clean up old socket
    if socket_path.exists() {
        fs::remove_file(&socket_path)?;
    }

    let listener = UnixListener::bind(&socket_path)
        .context("Failed to bind Unix socket")?;

    // Restrict socket permissions to owner only (0600)
    fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600))
        .context("Failed to set socket permissions")?;

    eprintln!("Daemon listening on {:?}", socket_path);

    // Start MCP server instance
    let mut mcp = McpClient::start(profile, extra_args, server_name)?;

    // Handle connections
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                if let Err(e) = handle_client(&mut mcp, stream) {
                    eprintln!("Client error: {}", e);
                }
            }
            Err(e) => {
                eprintln!("Connection error: {}", e);
            }
        }
    }

    Ok(())
}

fn handle_client(mcp: &mut McpClient, mut stream: UnixStream) -> Result<()> {
    const MAX_REQUEST_SIZE: usize = 1024 * 1024; // 1MB limit

    let mut reader = BufReader::new(stream.try_clone()?);
    let mut line = String::with_capacity(8192);
    reader.read_line(&mut line)?;

    if line.len() > MAX_REQUEST_SIZE {
        return Err(anyhow!("Request too large: {} bytes", line.len()));
    }

    let request: Value = serde_json::from_str(line.trim())
        .context("Invalid JSON-RPC request")?;

    let method = request["method"].as_str()
        .ok_or_else(|| anyhow!("Missing method"))?;

    let response = match method {
        "tools/call" => {
            let params = &request["params"];
            let tool_name = params["name"].as_str()
                .ok_or_else(|| anyhow!("Missing tool name"))?;
            let args = params["arguments"].clone();

            match mcp.call_tool(tool_name, args) {
                Ok(result) => json!({
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "result": result
                }),
                Err(e) => json!({
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "error": {"message": e.to_string()}
                }),
            }
        }
        "tools/list" => {
            match mcp.list_tools() {
                Ok(result) => json!({
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "result": result
                }),
                Err(e) => json!({
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "error": {"message": e.to_string()}
                }),
            }
        }
        _ => json!({
            "jsonrpc": "2.0",
            "id": request["id"],
            "error": {"message": format!("Unknown method: {}", method)}
        }),
    };

    let response_str = serde_json::to_string(&response)?;
    writeln!(stream, "{}", response_str)?;

    Ok(())
}

fn connect_to_daemon(server_name: &str) -> Result<UnixStream> {
    let daemon_mgr = DaemonManager::new(server_name);
    let socket_path = daemon_mgr.get_socket_path()
        .context("Failed to get socket path (daemon not started?)")?;

    let stream = UnixStream::connect(&socket_path)
        .context("Failed to connect to daemon (is it running?)")?;

    // Set timeouts
    stream.set_read_timeout(Some(Duration::from_secs(30)))
        .context("Failed to set read timeout")?;
    stream.set_write_timeout(Some(Duration::from_secs(30)))
        .context("Failed to set write timeout")?;

    Ok(stream)
}

fn send_daemon_request(mut stream: UnixStream, request: Value) -> Result<Value> {
    let request_str = serde_json::to_string(&request)?;
    writeln!(stream, "{}", request_str)?;

    let mut reader = BufReader::new(stream);
    let mut line = String::new();
    reader.read_line(&mut line)?;

    let response: Value = serde_json::from_str(line.trim())
        .context("Invalid JSON-RPC response")?;

    if let Some(error) = response.get("error") {
        return Err(anyhow!("Daemon error: {}", error));
    }

    Ok(response["result"].clone())
}

fn call_via_daemon(server_name: &str, tool: &str, args: Value) -> Result<Value> {
    let stream = connect_to_daemon(server_name)?;

    let request = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": tool,
            "arguments": args
        }
    });

    send_daemon_request(stream, request)
}

fn list_tools_via_daemon(server_name: &str) -> Result<Value> {
    let stream = connect_to_daemon(server_name)?;

    let request = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/list",
        "params": {}
    });

    send_daemon_request(stream, request)
}

// ============================================================================
// Main
// ============================================================================

fn main() -> Result<()> {
    // Handle internal daemon command BEFORE clap parsing
    let args: Vec<String> = std::env::args().collect();
    if args.len() > 1 && args[1] == "__internal_daemon" {
        // Find --server, --server-args, and --config by manual parsing
        let server_name = args.iter()
            .position(|a| a == "--server")
            .and_then(|i| args.get(i + 1))
            .ok_or_else(|| anyhow!("__internal_daemon requires --server"))?
            .clone();

        let extra_args = args.iter()
            .position(|a| a == "--server-args")
            .and_then(|i| args.get(i + 1))
            .and_then(|s| serde_json::from_str::<Vec<String>>(s).ok());

        let cli_config = args.iter()
            .position(|a| a == "--config" || a == "-c")
            .and_then(|i| args.get(i + 1))
            .map(PathBuf::from);

        let config = load_server_config(cli_config)?;
        let profile = config.servers.get(&server_name)
            .ok_or_else(|| anyhow!("Server '{}' not found", server_name))?;

        return run_daemon(&server_name, profile, extra_args);
    }

    // Filter out empty arguments
    let filtered_args: Vec<String> = std::env::args()
        .filter(|arg| !arg.is_empty())
        .collect();

    let cli = Cli::parse_from(filtered_args);

    match cli.command {
        Commands::ListServers => {
            let config = load_server_config(cli.config.clone())?;
            println!("Configured MCP servers:\n");
            for (name, profile) in config.servers {
                let desc = if profile.description.is_empty() {
                    "No description"
                } else {
                    &profile.description
                };
                println!("  {}: {}", name, desc);
                println!("    Command: {:?}", profile.command);
                if !profile.default_args.is_empty() {
                    println!("    Default args: {:?}", profile.default_args);
                }
                if profile.supports_daemon {
                    println!("    Daemon support: yes");
                }
                println!();
            }
            Ok(())
        }

        Commands::Call { tool, args } => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required. Use 'list-servers' to see available servers.")
            })?;

            let config = load_server_config(cli.config.clone())?;
            let _profile = config
                .servers
                .get(&server_name)
                .ok_or_else(|| anyhow!("Server '{}' not found in config", server_name))?;

            // Require daemon to be running
            let daemon_mgr = DaemonManager::new(&server_name);
            if !daemon_mgr.is_running().unwrap_or(false) {
                return Err(daemon_not_running_error(&server_name));
            }

            // Parse tool arguments
            let json_str = if args == "-" {
                let mut buffer = String::new();
                std::io::stdin()
                    .read_to_string(&mut buffer)
                    .context("Failed to read JSON from stdin")?;
                buffer
            } else {
                args
            };

            let args_json: Value =
                serde_json::from_str(&json_str).context("Invalid JSON arguments")?;

            let result = call_via_daemon(&server_name, &tool, args_json)?;

            println!("{}", serde_json::to_string_pretty(&result)?);
            Ok(())
        }

        Commands::ListTools => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required. Use 'list-servers' to see available servers.")
            })?;

            let config = load_server_config(cli.config.clone())?;
            let _profile = config
                .servers
                .get(&server_name)
                .ok_or_else(|| anyhow!("Server '{}' not found in config", server_name))?;

            // Require daemon to be running
            let daemon_mgr = DaemonManager::new(&server_name);
            if !daemon_mgr.is_running().unwrap_or(false) {
                return Err(daemon_not_running_error(&server_name));
            }

            let result = list_tools_via_daemon(&server_name)?;
            println!("{}", serde_json::to_string_pretty(&result)?);
            Ok(())
        }

        Commands::Shell => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required. Use 'list-servers' to see available servers.")
            })?;

            let config = load_server_config(cli.config.clone())?;
            let _profile = config
                .servers
                .get(&server_name)
                .ok_or_else(|| anyhow!("Server '{}' not found in config", server_name))?;

            // Require daemon to be running
            let daemon_mgr = DaemonManager::new(&server_name);
            if !daemon_mgr.is_running().unwrap_or(false) {
                return Err(daemon_not_running_error(&server_name));
            }

            let project = get_project_path();
            println!("MCP Shell ({}) - Project: {}", server_name, project);
            println!("Commands: call <tool> [json], list-tools, exit");
            println!();

            loop {
                print!("mcp> ");
                std::io::stdout().flush()?;

                let mut input = String::new();
                std::io::stdin().read_line(&mut input)?;
                let input = input.trim();

                if input.is_empty() {
                    continue;
                }

                if input == "exit" || input == "quit" {
                    break;
                }

                if input == "list-tools" {
                    match list_tools_via_daemon(&server_name) {
                        Ok(result) => println!("{}", serde_json::to_string_pretty(&result)?),
                        Err(e) => eprintln!("Error: {}", e),
                    }
                    continue;
                }

                // Parse "call tool_name args" format
                if let Some(rest) = input.strip_prefix("call ") {
                    let parts: Vec<&str> = rest.splitn(2, ' ').collect();
                    if !parts.is_empty() {
                        let tool = parts[0];
                        let args = parts.get(1).unwrap_or(&"{}");

                        match serde_json::from_str(args) {
                            Ok(args_json) => match call_via_daemon(&server_name, tool, args_json) {
                                Ok(result) => {
                                    println!("{}", serde_json::to_string_pretty(&result)?)
                                }
                                Err(e) => eprintln!("Error: {}", e),
                            },
                            Err(e) => eprintln!("Invalid JSON args: {}", e),
                        }
                    } else {
                        eprintln!("Usage: call <tool_name> [json_args]");
                    }
                } else {
                    eprintln!("Usage: call <tool_name> [json_args] | list-tools | exit");
                }
            }

            println!("Goodbye!");
            Ok(())
        }

        Commands::StartDaemon => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required")
            })?;

            let config = load_server_config(cli.config.clone())?;
            let profile = config
                .servers
                .get(&server_name)
                .ok_or_else(|| anyhow!("Server '{}' not found in config", server_name))?;

            let extra_args = if let Some(args_str) = &cli.server_args {
                Some(serde_json::from_str::<Vec<String>>(args_str)
                    .context("Invalid JSON in --server-args")?)
            } else {
                None
            };

            let daemon_mgr = DaemonManager::new(&server_name);
            daemon_mgr.start(profile, extra_args)?;
            Ok(())
        }

        Commands::StopDaemon => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required")
            })?;

            let daemon_mgr = DaemonManager::new(&server_name);
            daemon_mgr.stop()?;
            Ok(())
        }

        Commands::DaemonStatus => {
            let server_name = cli.server.ok_or_else(|| {
                anyhow!("--server required")
            })?;

            let daemon_mgr = DaemonManager::new(&server_name);
            daemon_mgr.status()?;
            Ok(())
        }
    }
}