Command line interface with secure execution and customizable security policies
This document details a robust and secure implementation of a Model Context Protocol (MCP) server, designed to facilitate controlled command-line operations within Large Language Model (LLM) applications. It emphasizes security best practices and provides a comprehensive guide for configuration, usage, and development.
The integration of Large Language Models (LLMs) with external tools and data sources is crucial for building sophisticated AI applications. The Model Context Protocol (MCP) provides a standardized framework for this integration. This document focuses on a specific MCP server implementation that enables LLMs to securely interact with the command-line interface (CLI). This server is designed with security as a paramount concern, implementing multiple layers of protection to prevent malicious command execution and unauthorized access.
This MCP server offers a range of features designed to ensure secure command-line execution:
ALLOWED_DIR
, preventing access to sensitive areas of the file system. Path normalization is performed to further mitigate traversal attempts.&&
, |
, >
, >>
) to prevent command chaining and redirection, which could be exploited to execute arbitrary code.The server's behavior is controlled through environment variables, allowing for flexible and secure configuration. Understanding these variables is critical for maintaining a secure environment.
| Variable | Description of the most important aspects of this server.
Recommendation: Prioritize security audits and penetration testing to identify and address potential vulnerabilities. Regularly update dependencies and review the configuration to ensure it aligns with the latest security best practices.
The server exposes two primary tools to LLMs: run_command
and show_security_rules
.
This tool allows LLMs to execute pre-approved CLI commands within the confines of the ALLOWED_DIR
.
Input Schema:
{ "command": { "type": "string", "description": "Single command to execute (e.g., 'ls -l' or 'cat file.txt')" } }
Security Considerations:
This tool provides LLMs (and administrators) with the ability to inspect the server's current security configuration.
Output:
The tool displays the following information:
ALLOWED_DIR
)ALLOWED_COMMANDS
)ALLOWED_FLAGS
)This information is invaluable for auditing the server's security posture and ensuring that it aligns with the organization's security policies.
The following examples demonstrate how to integrate this MCP server with Claude Desktop.
This configuration is suitable for local development and testing.
{ "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" } } } }
Note: Replace <path/to/the/repo>/cli-mcp-server
with the actual path to the server's source code.
This configuration is intended for production deployments.
{ "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" } } } }
Important: Ensure that the server is properly packaged and deployed before using this configuration. Clearing the cache via uv clean
may be necessary if the server is not functioning as expected.
The server's security architecture is designed to mitigate a wide range of potential attacks. Key security features include:
The server provides detailed error messages to aid in debugging and troubleshooting. These error messages can also provide valuable insights into potential security violations.
Error Types:
CommandSecurityError
: Indicates a security violation, such as an attempt to execute a non-whitelisted command or use a non-whitelisted flag.CommandTimeoutError
: Indicates that a command exceeded the maximum execution time.InvalidCommandFormat
: Indicates that the command format is invalid.PathSecurityViolation
: Indicates an attempt to access a file or directory outside of the allowed directory.CommandExecutionError
: Indicates a general error during command execution.CommandError
: Indicates a generic command error.The following sections outline the development workflow for this MCP server.
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 MCP servers can be challenging due to their reliance on standard input and output. The MCP Inspector provides a valuable tool for debugging.
Launching the MCP Inspector:
npx @modelcontextprotocol/inspector uv --directory {{your source code local directory}}/cli-mcp-server run cli-mcp-server
The Inspector will provide a URL that you can access in your browser to begin debugging.
This MCP server provides a secure and reliable way to integrate LLMs with the command-line interface. By implementing robust security measures and providing detailed error reporting, this server enables developers to build sophisticated AI applications while minimizing the risk of security vulnerabilities. Continuous monitoring, regular security audits, and adherence to the principle of least privilege are essential for maintaining a secure environment.
๐ ๐ Autonomous shell execution, computer control and coding agent. (Mac)
๐ MCP server that can execute commands such as keyboard input and mouse movement
๐ ๐ ๐ฆ Analyzes your codebase identifying important files based on dependency relationships. Generates diagrams and importance scores, helping AI assistants understand the codebase.
๐ ๐ An MCP Server and VS Code Extension which enables (language agnostic) automatic debugging via breakpoints and expression evaluation.