Labsco
brunorozendo logo

MCP Command Server

from brunorozendo

A server for securely executing commands on the host system, requiring Java 21 or higher.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeAdvanced setup

MCP Command Server

A Model Context Protocol (MCP) server that enables secure command execution on the host system. This server allows AI assistants like Claude to run shell commands with optional working directory and stdin support.

The project is now modularized to support multiple transport methods: stdio, HTTP, and Server-Sent Events (SSE).

Features

  • Command Execution: Run any shell command on the host system
  • Working Directory Support: Execute commands in specific directories
  • STDIN Support: Pipe input data to commands
  • Cross-Platform: Works on macOS, Linux, and Windows
  • Timeout Protection: Commands timeout after 60 seconds to prevent hanging
  • Special Fish Shell Support: Enhanced handling for the Fish shell
  • Advanced Logging: Powered by Logback with configurable log levels (ERROR, WARN, INFO, DEBUG, TRACE), optional file logging, and rolling file support

API Reference

Tool: run_command

Executes a command on the host system.

Parameters:

  • command (string, required): The command to execute with arguments
  • workdir (string, optional): The working directory for command execution
  • stdin (string, optional): Text to pipe into the command's STDIN

Returns:

  • stdout: Standard output from the command
  • stderr: Standard error output from the command
  • message: Error message if command fails
  • isError: Boolean indicating if the command failed

Example Request:

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "id": 1,
  "params": {
    "name": "run_command",
    "arguments": {
      "command": "echo Hello World",
      "workdir": "/tmp",
      "stdin": "Input data"
    }
  }
}

Development

Project Structure

mcp-server-command/
โ”œโ”€โ”€ stdio/                      # Standard I/O transport module
โ”‚   โ”œโ”€โ”€ src/main/java/com/brunorozendo/mcp/
โ”‚   โ”‚   โ””โ”€โ”€ McpServer.java      # Main server entry point
โ”‚   โ””โ”€โ”€ build.gradle
โ”œโ”€โ”€ http/                       # HTTP transport module  
โ”‚   โ”œโ”€โ”€ src/main/java/com/brunorozendo/mcp/
โ”‚   โ”‚   โ””โ”€โ”€ McpServer.java      # HTTP server implementation
โ”‚   โ””โ”€โ”€ build.gradle
โ”œโ”€โ”€ sse/                        # Server-Sent Events transport module
โ”‚   โ”œโ”€โ”€ src/main/java/com/brunorozendo/mcp/
โ”‚   โ”‚   โ””โ”€โ”€ McpServer.java      # SSE server implementation  
โ”‚   โ””โ”€โ”€ build.gradle
โ”œโ”€โ”€ tools/                      # Shared command execution logic
โ”‚   โ”œโ”€โ”€ src/main/java/com/brunorozendo/mcp/
โ”‚   โ”‚   โ”œโ”€โ”€ CommandExecutor.java     # Command execution logic
โ”‚   โ”‚   โ”œโ”€โ”€ CommandResult.java       # Result data structure
โ”‚   โ”‚   โ”œโ”€โ”€ ExecCommandTool.java     # Tool implementation
โ”‚   โ”‚   โ”œโ”€โ”€ Transport.java           # Transport configuration
โ”‚   โ”‚   โ””โ”€โ”€ ToolSchemas.java         # Tool schema definitions
โ”‚   โ””โ”€โ”€ build.gradle
โ”œโ”€โ”€ settings.gradle             # Multi-module configuration
โ””โ”€โ”€ docs/                       # Documentation

Running Tests

Execute the test script to verify functionality:

./test_server.sh

Building a Distribution

Create distribution archives (ZIP and TAR):

./gradlew distZip distTar

Security Considerations

โš ๏ธ WARNING: This server executes commands with the same privileges as the user running the Java process.

Security Best Practices:

  1. Only install this server if you trust the AI assistant
  2. Run the server with minimal necessary privileges
  3. Consider using a restricted user account for the server
  4. Be cautious when sharing your screen or command outputs
  5. Regularly review which commands are being executed
  6. Consider implementing command whitelisting for production use

Potential Risks:

  • File system access and modification
  • Network operations
  • Process execution
  • Access to environment variables and system information

Version History

  • 1.0.0 - Current version with modular architecture
    • Modular design with stdio, HTTP, and SSE transport support
    • Command execution with timeout protection
    • Working directory support
    • STDIN support
    • Special Fish shell handling
    • Advanced Logback-based logging with file rotation support
    • Updated to MCP SDK 0.17.0
    • Java 25 support
    • GraalVM native compilation support

Custom Logback Configuration

For advanced users, you can provide your own logback.xml configuration file:

  1. Create your custom logback.xml
  2. Place it in the classpath or specify its location with -Dlogback.configurationFile=/path/to/logback.xml

Support

For issues and questions:

  • Check the troubleshooting section
  • Set appropriate log level for debugging
  • [Add support contact/repository issues link]