CLI MCP Server

repository·main·Indexed 41 days ago

https://github.com/mladensu/cli-mcp-server

A secure Model Context Protocol (MCP) server that allows LLMs to execute command-line operations within a sandboxed environment. Version 0.2.5 features customizable security policies via environment variables, including ALLOWED_DIR, ALLOWED_COMMANDS, and ALLOWED_FLAGS. It provides tools like run_command for executing whitelisted operations and show_security_rules to inspect the active configuration.

Tokens
2K
Snippets
8
Records
11
Agent score
52%

What's inside cli-mcp-server

  1. Build and Publish CLI MCP Server

    main

    To prepare the package for distribution using uv:

    1. Sync dependencies and update lockfile:
      uv sync
    2. Build package distributions:
       ```bash
    uv build

    (This creates source and wheel distributions in the dist/ directory) 3. Publish to PyPI:

    uv publish --token {{YOUR_PYPI_API_TOKEN}}
    uv sync
    uv build
    uv publish --token {{YOUR_PYPI_API_TOKEN}}
  2. Configure CLI MCP Server for Claude Desktop (Development)

    main

    If you are running the server from a local repository that is not yet published, add the following configuration to your ~/Library/Application\ Support/Claude/claude_desktop_config.json file. Replace placeholders like <path/to/the/repo> and </your/desired/dir> with your actual local paths.

    {
      "mcpServers": {
        "cli-mcp-server": {
          "command": "uv",
          "args": [
            "--directory",
            "<path/to/the/repo>/cli-mcp-server",
            "run",
            "cli-mcp-server"
          ],
          "env": {
            "ALLOWED_DIR": "</your/desired/dir>",
            "ALLOWED_COMMANDS": "ls,cat,pwd,echo",
            "ALLOWED_FLAGS": "-l,-a,--help,--version",
            "MAX_COMMAND_LENGTH": "1024",
            "COMMAND_TIMEOUT": "30",
            "ALLOW_SHELL_OPERATORS": "false"
          }
        }
      }
    }
  3. Configure CLI MCP Server for Claude Desktop (Published)

    main

    To use the published version of the server in Claude Desktop, add this configuration to your ~/Library/Application\ Support/Claude/claude_desktop_config.json. If the server does not appear in the UI, try running uv clean to clear your cache.

    {
      "mcpServers": {
        "cli-mcp-server": {
          "command": "uvx",
          "args": [
            "cli-mcp-server"
          ],
          "env": {
            "ALLOWED_DIR": "</your/desired/dir>",
            "ALLOWED_COMMANDS": "ls,cat,pwd,echo",
            "ALLOWED_FLAGS": "-l,-a,--help,--version",
            "MAX_COMMAND_LENGTH": "1024",
            "COMMAND_TIMEOUT": "30",
            "ALLOW_SHELL_OPERATORS": "false"
          }
        }
      }
    }
  4. Configure CLI MCP Server environment variables

    main

    The server is configured using environment variables. Note that ALLOWED_DIR is required. You can use 'all' for ALLOWED_COMMANDS or ALLOWED_FLAGS to bypass whitelisting.

    | Variable             | Description                                          | Default            |
    |---------------------|------------------------------------------------------|-------------------|
    | `ALLOWED_DIR`       | Base directory for command execution (Required)      | None (Required)   |
    | `ALLOWED_COMMANDS`  | Comma-separated list of allowed commands or 'all'    | `ls,cat,pwd`      |
    | `ALLOWED_FLAGS`     | Comma-separated list of allowed flags or 'all'       | `-l,-a,--help`    |
    | `MAX_COMMAND_LENGTH`| Maximum command string length                        | `1024`            |
    | `COMMAND_TIMEOUT`   | Command execution timeout (seconds)                  | `30`              |
    | `ALLOW_SHELL_OPERATORS` | Allow shell operators (&&, ||, |, >, etc.)    | `false`           |
  5. Debug CLI MCP Server with MCP Inspector

    main

    Since MCP servers communicate over stdio, use the MCP Inspector to debug. Replace {{your source code local directory}} with the absolute path to your local repository.

    npx @modelcontextprotocol/inspector uv --directory {{your source code local directory}}/cli-mcp-server run cli-mcp-server
  6. Use the run_command tool

    main

    The run_command tool executes whitelisted CLI commands within the directories specified by ALLOWED_DIR.

    Input Schema:

    {
      "command": {
        "type": "string",
        "description": "Single command to execute (e.g., 'ls -l' or 'cat file.txt')"
      }
    }

    Security Constraints:

    • Commands must be in ALLOWED_COMMANDS (unless set to 'all').
    • Flags must be in ALLOWED_FLAGS (unless set to 'all').
    • All paths must reside within ALLOWED_DIR.
    • Shell operators (&&, |, >, >>) are disabled by default; enable them via ALLOW_SHELL_OPERATORS=true.
  7. Configure the CLI MCP Server via Environment Variables

    main

    The server's security constraints and execution environment are configured using environment variables. If these are not set, the server uses default values.

    Environment VariableDescriptionDefault
    ALLOWED_DIRThe absolute path to the directory where commands are allowed to run.(Required)
    ALLOWED_COMMANDSComma-separated list of permitted command names or all.ls,cat,pwd
    ALLOWED_FLAGSComma-separated list of permitted flags/options or all.-l,-a,--help
    MAX_COMMAND_LENGTHMaximum character length for a command string.1024
    COMMAND_TIMEOUTMaximum execution time in seconds.30
    ALLOW_SHELL_OPERATORSSet to true or 1 to enable shell operators (e.g., &&, |, ;).false
  8. Handle Command Execution Errors

    main

    When using the MCP server, command failures or security violations will return error messages in the tool response. Common error types include:

    • Security violation: Occurs if a command, flag, or path is not permitted, or if shell operators are used without being enabled.
    • Command timed out: Occurs if the command exceeds the COMMAND_TIMEOUT limit.
    • Error: General execution failures (e.g., command not found or internal subprocess errors).

    Errors are returned as TextContent objects with error=True.

  9. Run the CLI MCP Server via main()

    main

    The main() function serves as the primary entry point for the cli_mcp_server package. It initializes and runs the MCP server's main event loop using asyncio. This is the function you should call when executing the server as a standalone application.

    from cli_mcp_server import main
    
    if __name__ == "__main__":
        main()