Windows CLI MCP Server

repository·main·Indexed 42 days ago

https://github.com/simon-ami/win-cli-mcp-server

A Model Context Protocol (MCP) server providing secure, controlled command-line access to Windows systems (PowerShell, CMD, Git Bash) and remote systems via SSH. It allows AI clients like Claude Desktop to interact with the system while enforcing strict security boundaries through blocked commands, argument filtering, and path restrictions. Features include remote execution via ssh_execute, connection management, and command history tracking.

Tokens
5.3K
Snippets
15
Records
22
Agent score
50%

What's inside Windows CLI MCP Server

  1. Understand built-in security protections

    main

    The Windows CLI MCP Server includes several hard-coded security measures that cannot be disabled to ensure safe operation:

    • Case-insensitive command blocking: Prevents bypasses like DEL.EXE if del is blocked.
    • Smart path parsing: Blocks full paths (e.g., C:\Windows\System32\rm.exe) if the base command (rm) is in the blocked list.
    • Command parsing intelligence: Uses logic to avoid false positives (e.g., warm_dir will not be blocked by an rm rule).
    • Input validation: All user inputs are validated before execution.
    • Shell process management: Ensures processes are terminated after execution or upon timeout.
    • Sensitive data masking: Automatically replaces passwords with ******** in all resource outputs.
  2. Initialize or create a default configuration file

    main

    The server uses a JSON configuration file to manage security, shells, and SSH connections. You can create a new configuration file in two ways:

    1. Manual: Copy the config.json.example file to config.json in your project or desired directory.
    2. CLI Command: Run the following command to generate a new config file at the specified path:
    npx @simonb97/server-win-cli --init-config ./config.json
  3. Install the Windows CLI MCP Server for Claude Desktop

    main

    To use the Windows CLI MCP Server with Claude Desktop, add the server configuration to your claude_desktop_config.json file. You can run the server using npx without a manual installation.

    Standard Configuration: Use this to run the server with default settings.

    Custom Configuration: If you have a specific configuration file, use the --config flag in the args array to point to its path.

    {
      "mcpServers": {
        "windows-cli": {
          "command": "npx",
          "args": ["-y", "@simonb97/server-win-cli"]
        }
      }
    }
    {
      "mcpServers": {
        "windows-cli": {
          "command": "npx",
          "args": [
            "-y",
            "@simonb97/server-win-cli",
            "--config",
            "path/to/your/config.json"
          ]
        }
      }
    }
  4. Configure the Windows CLI MCP Server

    main

    The server searches for configuration files in this order:

    1. The path provided via the --config flag.
    2. ./config.json in the current working directory.
    3. ~/.win-cli-mcp/config.json in the user's home directory.

    If no configuration is found, the server defaults to a highly restrictive mode. The configuration is divided into three main sections: security, shells, and ssh.

  5. Configure Security Settings

    main

    The security block controls the safety boundaries of command execution. Key settings include:

    • maxCommandLength: Maximum allowed length for any command.
    • blockedCommands: A list of commands to block (e.g., rm, del, shutdown). This is case-insensitive and blocks both direct names and full paths.
    • blockedArguments: Arguments that will be blocked when used with any command (e.g., --exec, -e, -enc).
    • allowedPaths: A list of directories where commands are permitted to run.
    • restrictWorkingDirectory: If true, commands can only run within the allowedPaths.
    • logCommands: If true, saves command history.
    • maxHistorySize: Maximum number of commands to keep in history.
    • commandTimeout: Timeout for command execution in seconds.
    • enableInjectionProtection: Enables protection against command injection (e.g., blocking ;, &, |, `).
    {
      "security": {
        "maxCommandLength": 1000,
        "blockedCommands": [
          "rm",
          "del",
          "rmdir",
          "format",
          "shutdown",
          "restart",
          "reg",
          "regedit",
          "net",
          "netsh",
          "takeown",
          "icacls"
        ],
        "blockedArguments": [
          "--exec",
          "-e",
          "/c",
          "-enc",
          "-encodedcommand",
          "-command",
          "--interactive",
          "-i",
          "--login",
          "--system"
        ],
        "allowedPaths": ["C:\\Users\\YourUsername", "C:\\Projects"],
        "restrictWorkingDirectory": true,
        "logCommands": true,
        "maxHistorySize": 1000,
        "commandTimeout": 30,
        "enableInjectionProtection": true
      }
    }
  6. Configure SSH Connections

    main

    The ssh block enables remote command execution. To use it, set enabled: true. You can define multiple connection profiles in the connections object.

    Global SSH Settings:

    • defaultTimeout: Timeout for SSH commands in seconds.
    • maxConcurrentSessions: Maximum number of simultaneous SSH sessions.
    • keepaliveInterval: Interval for keepalive packets in milliseconds.
    • keepaliveCountMax: Max failed keepalive attempts before disconnecting.
    • readyTimeout: Timeout for establishing connections in milliseconds.

    Connection Profiles: Each profile (e.g., raspberry-pi) can specify:

    • host: Hostname or IP.
    • port: SSH port.
    • username: SSH username.
    • password: Password authentication.
    • privateKeyPath: Path to a private key for key-based authentication.
    • Overrides for keepaliveInterval, keepaliveCountMax, and readyTimeout can be applied per connection.
    {
      "ssh": {
        "enabled": false,
        "defaultTimeout": 30,
        "maxConcurrentSessions": 5,
        "keepaliveInterval": 10000,
        "keepaliveCountMax": 3,
        "readyTimeout": 20000,
        "connections": {
          "raspberry-pi": {
            "host": "raspberrypi.local",
            "port": 22,
            "username": "pi",
            "password": "raspberry",
            "keepaliveInterval": 10000,
            "keepaliveCountMax": 3,
            "readyTimeout": 20000
          },
          "dev-server": {
            "host": "dev.example.com",
            "port": 22,
            "username": "admin",
            "privateKeyPath": "C:\\Users\\YourUsername\\.ssh\\id_rsa",
            "keepaliveInterval": 10000,
            "keepaliveCountMax": 3,
            "readyTimeout": 20000
          }
        }
      }
    }
  7. Configure Shells (PowerShell, CMD, Git Bash)

    main

    The shells block allows you to enable or disable specific command-line interfaces and define how they are invoked. Each shell configuration includes:

    • enabled: Boolean to turn the shell on or off.
    • command: The path to the shell executable.
    • args: Default arguments passed to the shell.
    • blockedOperators: An optional list of command operators to block (e.g., &, |, ;, `) to prevent command chaining.
    {
      "shells": {
        "powershell": {
          "enabled": true,
          "command": "powershell.exe",
          "args": ["-NoProfile", "-NonInteractive", "-Command"],
          "blockedOperators": ["&", "|", ";", "`"]
        },
        "cmd": {
          "enabled": true,
          "command": "cmd.exe",
          "args": ["/c"],
          "blockedOperators": ["&", "|", ";", "`"]
        },
        "gitbash": {
          "enabled": true,
          "command": "C:\\Program Files\\Git\\bin\\bash.exe",
          "args": ["-c"],
          "blockedOperators": ["&", "|", ";", "`"]
        }
      }
    }
  8. Configure security settings in config.json

    main

    Security features are configurable via the config.json file. Note that some features are hard-coded and always active.

    Configurable Settings

    • blockedCommands: An array of command names to block (e.g., ["rm", "del", "format"]). Blocking is case-insensitive and uses smart path parsing.
    • blockedArguments: An array of arguments/flags to block.
    • enableInjectionProtection: Boolean (default: true) to prevent command chaining.
    • restrictWorkingDirectory: Boolean (default: true) to limit execution to specified directories.
    • allowedPaths: A list of directories where command execution is permitted.
    • commandLengthLimit: Maximum allowed command length (default: 2000 characters).
    • commandTimeout: Maximum execution time in seconds (default: 30 seconds).
    • logCommands: Boolean (default: true) to enable recording command history.
  9. Retrieve command history and directory info

    main

    Use these tools to inspect the state and history of the CLI server:

    • get_command_history: Returns a timestamped history of executed commands and their outputs. You can provide an optional limit (number) to restrict the results.
    • get_current_directory: Returns the current working directory path of the server.
  10. Execute commands via execute_command

    main

    Use the execute_command tool to run commands in a local shell. You can specify the shell type, the command string, and an optional working directory.

    Inputs:

    • shell (string): The shell to use. Supported values: "powershell", "cmd", or "gitbash".
    • command (string): The command to execute.
    • workingDir (optional string): The directory in which to execute the command.

    Returns: The command output as text, or an error message if execution fails.

    {
      "name": "execute_command",
      "arguments": {
        "shell": "powershell",
        "command": "dir",
        "workingDir": "C:\\Users\\Public"
      }
    }
  11. Manage SSH connections and remote execution

    main

    The server provides a suite of tools to manage SSH connections and execute commands on remote systems.

    Remote Execution

    • ssh_execute: Executes a command on a remote system using a specific connectionId and a command string.

    Connection Management

    • create_ssh_connection: Establishes a new connection. Requires a connectionId and a connectionConfig object containing host, port, username, and either password or privateKeyPath.
    • update_ssh_connection: Updates an existing connection's configuration.
    • delete_ssh_connection: Removes a connection by its connectionId.
    • read_ssh_connections: Retrieves a list of all configured SSH connections.
    • ssh_disconnect: Terminates an active SSH connection using its connectionId.
    // Example: Creating an SSH connection
    {
      "name": "create_ssh_connection",
      "arguments": {
        "connectionId": "raspberry-pi",
        "connectionConfig": {
          "host": "192.168.1.50",
          "port": 22,
          "username": "pi",
          "privateKeyPath": "C:\\Users\\me\\.ssh\\id_rsa"
        }
      }
    }
  12. Configure Security settings

    main

    The security configuration object controls the safety boundaries of the MCP server, including command length limits, command/argument blacklists, and path restrictions. Use these settings to prevent unauthorized access or command injection.

    {
      "security": {
        "maxCommandLength": 1000,
        "blockedCommands": ["format", "del"],
        "blockedArguments": ["/s", "/q"],
        "allowedPaths": ["C:\\Users\\Public", "C:\\Projects"],
        "restrictWorkingDirectory": true,
        "logCommands": true,
        "maxHistorySize": 100,
        "commandTimeout": 30,
        "enableInjectionProtection": true
      }
    }