mcp-server-commands

repository·master·Indexed 42 days ago

https://github.com/g0t4/mcp-server-commands

An MCP (Model Context Protocol) server version 0.8.2 that enables LLMs to execute arbitrary system commands on the host machine. It provides the `run_process` tool and prompt, supporting both shell mode via `command_line` and direct executable invocation via `argv`, with optional support for `stdin_text`, custom working directories (`cwd`), and execution timeouts.

Tokens
2.6K
Snippets
11
Records
14
Agent score
52%

What's inside mcp-server-commands

  1. Choosing between child_process methods

    master

    When working with the Node.js child_process module, all higher-level methods are convenience wrappers around spawn and spawnSync. When deciding which method to use, consider the following:

    • spawn and spawnSync: These are the core primitives. Use them directly if you need fine-grained control.
    • Async vs Sync: Choosing between asynchronous methods (like spawn) and synchronous methods (like spawnSync) depends on whether you can afford to block the event loop. Synchronous methods are more convenient for simple scripts but will block the event loop, whereas asynchronous methods allow the process to continue handling other tasks while waiting for the child process.
  2. Develop the mcp-server-commands repository

    master

    To contribute or modify the server, follow these commands to set up your local environment:

    1. Install dependencies: npm install
    2. Build the server: npm run build
    3. Development mode (auto-rebuild): npm run watch
    npm install
    npm run build
    npm run watch
  3. Expose the server via HTTP/OpenAPI using mcpo

    master

    The server natively uses STDIO transport. To use it with clients that require HTTP (like Open-WebUI), use mcpo to bridge STDIO to HTTP.

    This command uses uvx to run mcpo, which then invokes npx to start the server.

    uvx mcpo --port 3010 --api-key "supersecret" -- npx mcp-server-commands
  4. Install mcp-server-commands from a local build

    master

    If you have cloned the repository, you must first build the project using npm run build. You can then point your MCP client directly to the compiled index.js file.

    {
      "mcpServers": {
        "mcp-server-commands": {
          "command": "/path/to/mcp-server-commands/build/index.js"
        }
      }
    }
  5. Install mcp-server-commands via npm

    master

    To use the published version of the server with Claude Desktop, add the following configuration to your claude_desktop_config.json file.

    Config Locations:

    • MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%/Claude/claude_desktop_config.json
    • Groq Desktop (macOS): ~/Library/Application Support/groq-desktop-app/settings.json
    {
      "mcpServers": {
        "mcp-server-commands": {
          "command": "npx",
          "args": ["mcp-server-commands"]
        }
      }
    }
  6. Configure verbose logging

    master

    By default, the server only logs important messages (errors). To see more detailed logs in Claude Desktop, add the --verbose flag to the args array in your configuration.

    Log Location (MacOS): ~/Library/Logs/Claude/mcp-server-mcp-server-commands.log

    {
      "mcpServers": {
        "mcp-server-commands": {
          "command": "npx",
          "args": ["mcp-server-commands", "--verbose"]
        }
      }
    }
  7. Run the mcp-server-commands server via stdio

    master

    The mcp-server-commands server is an MCP (Model Context Protocol) server designed to run commands on the host machine. It is implemented as a Node.js application that communicates via the StdioServerTransport. When running, it exposes both tools and prompts to the MCP client. The server automatically detects the host platform (e.g., linux, darwin, win32) to describe its capabilities.

    # The server is intended to be run as a node process, typically via an MCP client configuration
    # that points to the compiled entrypoint.
    node dist/index.js
  8. Use the `run_process` tool

    master

    The run_process tool allows an LLM to execute commands on the host machine. It provides two mutually exclusive invocation methods:

    1. command_line (string): Executes the command via the system's default shell (e.g., bash, fish, pwsh). This supports shell features like pipes (|), redirects (>), and variable expansion.
    2. argv (string array): Direct executable invocation. argv[0] is the executable, and the remaining elements are arguments. This bypasses shell interpretation.

    Output: The tool returns STDOUT and STDERR as text.

    Stdin Support: An optional stdin parameter allows the LLM to pass text to the command. This is useful for:

    • Passing scripts to shells like bash, zsh, or python.
    • Creating files using commands like cat >> foo/bar.txt.
    WARNING

    Permissions are dictated by the user running the server. DO NOT run with sudo. In Claude Desktop, use Approve Once to review each command before execution.

    // Example conceptual tool call structure
    {
      "name": "run_process",
      "arguments": {
        "command_line": "ls -al"
      }
    }
  9. Use the run_process prompt

    master

    The run_process prompt allows you to execute a command and append its output to the conversation context.

    Arguments:

    • command_line: The command string to execute.
    • argv: Arguments for the command.

    Behavior: When invoked, the server executes the command via exec. It constructs a series of messages for the LLM:

    1. A user message stating the command was run: I ran the following command, if there is any output it will be shown below: <command_line>
    2. If stdout is present, a message containing STDOUT:\n<output>.
    3. If stderr is present, a message containing STDERR:\n<output>.

    Note: Implementation details suggest that error handling for failed executions is currently a work in progress and may result in chat panel failures in some clients (like Zed) rather than a structured prompt message.

  10. Available prompts in mcp-server-commands

    master

    The MCP server provides a prompt designed to help LLMs incorporate command output into their context. This is useful for scenarios where a user wants to treat a command execution as a tool call and have the resulting output (STDOUT/STDERR) automatically formatted for the model.

    Currently, the server implements the run_process prompt.

    {
      "name": "run_process",
      "description": "Include command output in the prompt. This is effectively a user tool call.",
      "arguments": [
        { "name": "command_line" },
        { "name": "argv" }
      ]
    }
  11. Use the runProcess tool

    master

    The runProcess tool allows you to execute system commands in two modes: Shell Mode (using a single command string) or Executable Mode (using an array of arguments).

    Execution Modes

    1. Shell Mode: Provide a command_line string. This is useful for complex commands involving pipes or shell built-ins. You cannot use argv in this mode.
    2. Executable Mode: Provide an argv array where the first element is the executable and subsequent elements are arguments. You cannot use command_line in this mode.

    Arguments

    When calling runProcess, you can pass the following keys in the arguments object:

    • command_line (string): The full shell command to execute.
    • argv (string[]): An array of strings representing the command and its arguments.
    • cwd (string): The working directory where the command should run.
    • stdin_text (string): Text to write to the process's standard input (STDIN).
    • timeout_ms (number): Maximum execution time in milliseconds. Defaults to 30000 (30 seconds).

    Constraints

    • You must provide either command_line or argv. Providing both or neither will result in an error.
    • If stdin_text is provided, the tool pipes it to the process and then closes the STDIN stream.
    // Example: Shell Mode
    await runProcess({
      command_line: "ls -la /tmp",
      cwd: "/home/user"
    });
    
    // Example: Executable Mode
    await runProcess({
      argv: ["grep", "search_term", "file.txt"],
      timeout_ms: 5000
    });
    
    // Example: Providing STDIN
    await runProcess({
      argv: ["cat"],
      stdin_text: "Hello from STDIN"
    });