CLI MCP Server
A secure Model Context Protocol (MCP) server implementation for executing controlled command-line operations with comprehensive security features.
Table of Contents
- Overview
- Features
- Configuration
- Available Tools
- Usage with Claude Desktop
- Security Features
- Error Handling
- Development
- License
Overview
This MCP server enables secure command-line execution with robust security measures including command whitelisting, path validation, and execution controls. Perfect for providing controlled CLI access to LLM applications while maintaining security.
Features
- π Secure command execution with strict validation
- βοΈ Configurable command and flag whitelisting with βallβ option
- π‘οΈ Path traversal prevention and validation
- π« Shell operator injection protection
- β±οΈ Execution timeouts and length limits
- π Detailed error reporting
- π Async operation support
- π― Working directory restriction and validation
Configuration
Configure the server using environment variables:
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 |
Note: Setting ALLOWED_COMMANDS
or ALLOWED_FLAGS
to βallβ will allow any command or flag respectively.
Installation
To install CLI MCP Server for Claude Desktop automatically via Smitheryβ:
npx @smithery/cli install cli-mcp-server --client claude
Available Tools
run_command
Executes whitelisted CLI commands within allowed directories.
Input Schema:
{
"command": {
"type": "string",
"description": "Single command to execute (e.g., 'ls -l' or 'cat file.txt')"
}
}
Security Notes:
- Shell operators (&&, |, >, >>) are not supported
- Commands must be whitelisted unless ALLOWED_COMMANDS=βallβ
- Flags must be whitelisted unless ALLOWED_FLAGS=βallβ
- All paths are validated to be within ALLOWED_DIR
show_security_rules
Displays current security configuration and restrictions, including:
- Working directory
- Allowed commands
- Allowed flags
- Security limits (max command length and timeout)
Usage with Claude Desktop
Add to your ~/Library/Application\ Support/Claude/claude_desktop_config.json
:
Development/Unpublished Servers Configuration
{
"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"
}
}
}
}
Published Servers Configuration
{
"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"
}
}
}
}
In case itβs not working or showing in the UI, clear your cache via
uv clean
.
Security Features
- β Command whitelist enforcement with βallβ option
- β Flag validation with βallβ option
- β Path traversal prevention and normalization
- β Shell operator blocking
- β Command length limits
- β Execution timeouts
- β Working directory restrictions
- β Symlink resolution and validation
Error Handling
The server provides detailed error messages for:
- Security violations (CommandSecurityError)
- Command timeouts (CommandTimeoutError)
- Invalid command formats
- Path security violations
- Execution failures (CommandExecutionError)
- General command errors (CommandError)
Development
Prerequisites
- Python 3.10+
- MCP protocol library
Building and Publishing
To prepare the package for distribution:
-
Sync dependencies and update lockfile:
uv sync
-
Build package distributions:
uv build
This will create source and wheel distributions in the
dist/
directory. -
Publish to PyPI:
uv publish --token {{YOUR_PYPI_API_TOKEN}}
Debugging
Since MCP servers run over stdio, debugging can be challenging. For the best debugging experience, we strongly recommend using the MCP Inspectorβ.
You can launch the MCP Inspector via npm
with
this command:
npx @modelcontextprotocol/inspector uv --directory {{your source code local directory}}/cli-mcp-server run cli-mcp-server
Upon launching, the Inspector will display a URL that you can access in your browser to begin debugging.
License
This project is licensed under the MIT License - see the LICENSE file for details.
For more information or support, please open an issue on the project repository.