host-protection-mcp
by khaosdoctor
README.md
# host-protection-mcp
An MCP server for agents that run their tools inside a sandbox but still need to change a few files on the host, like their own config, or run a restart.
The agent can't touch those files from the sandbox. It asks this server instead, you approve the call in your MCP client, and the server does the write or runs the command on the host. Anything that isn't in your config file is refused.
I built this for [Hermes Agent](https://github.com/NousResearch/hermes-agent) on my homelab, but nothing in the code is Hermes specific.
## How it works
```mermaid
sequenceDiagram
participant A as Agent (sandbox)
participant C as MCP client
participant Y as You
participant S as host-protection-mcp (host)
A->>C: edit_file(config.yaml, edits, reason)
C->>Y: Approve?
Y->>C: Yes
C->>S: edit_file
S->>S: check allow list, validate, back up, write
S-->>A: done, run "restart" next
```
The agent never sends a whole file. Each edit is an exact snippet and its replacement, like a small diff, so the approval prompt shows you only what changes. The snippet has to match exactly once.
Edits are refused when the path isn't in the allow list, when the result doesn't parse, or when a symlink is anywhere on the way. Every changed file gets a backup first.
Actions run without a shell, and every `{placeholder}` has to match the regex you declare for it.
## Tools
| Tool | Does |
|---|---|
| `list_protected` | Lists the allowed files and actions |
| `read_file` | Reads an allowed file |
| `edit_file` | Applies snippet edits to an allowed file, returns the actions to run after it |
| `run_action` | Runs a configured action |
## Install
You need Node 22.18 or newer. It's written in TypeScript and Node runs the `.ts` files directly, so there's no build step.
```sh
git clone https://github.com/khaosdoctor/host-protection-mcp.git
cd host-protection-mcp
npm ci --omit=dev
mkdir -p ~/.config/host-protection-mcp
cp examples/hermes.yaml ~/.config/host-protection-mcp/config.yaml
```
Install it somewhere the agent can't write to. If the agent can edit this server or its config, the protection is gone.
## Running
```sh
node src/server.ts [config-file]
```
The server talks MCP over stdin and stdout, so you won't run it by hand. Your MCP client starts it with this command (see [Connecting a client](#connecting-a-client)).
There are no flags. The only argument is the path to the config file, and you can leave it out. The server takes the first config it finds in this order.
1. The argument, `node src/server.ts /etc/my-agent/protection.yaml`
2. The `HOST_PROTECTION_CONFIG` environment variable
3. `~/.config/host-protection-mcp/config.yaml`
If the config is missing or has no files in it, the server exits with an error.
To check that it starts, send it an MCP handshake and a tool listing.
```sh
printf '%s\n' \
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}' \
'{"jsonrpc":"2.0","method":"notifications/initialized"}' \
'{"jsonrpc":"2.0","id":2,"method":"tools/list"}' \
| node src/server.ts
```
## Configuration
The config is a YAML file with a list of files the agent can change and a list of actions it can run. Anything that isn't listed is refused.
```yaml
files:
- path: ~/.hermes/config.yaml
validate: yaml
after: [restart]
- path: ~/.hermes/profiles/*/SOUL.md
actions:
restart:
description: Restart the gateway
command: [systemctl, --user, restart, hermes-gateway]
install_plugin:
description: Install a plugin from owner/repo
command: [hermes, plugins, install, "{source}"]
args:
source: '[\w.-]+/[\w.-]+'
timeoutSeconds: 300
backupDir: ~/.local/state/host-protection-mcp/backups
maxBytes: 1000000
```
[`examples/hermes.yaml`](examples/hermes.yaml) has a full Hermes setup.
### files
Each entry allows one file, or a group of files with a glob. The agent can read these with `read_file` and change them with `edit_file`.
| Key | Required | Default | What it does |
|---|---|---|---|
| `path` | yes | | The file. `~` is your home folder. `*` matches inside one folder, `**` matches across folders |
| `validate` | no | `none` | Checks the file after the edit and refuses it if it doesn't parse. `yaml`, `json` or `none` |
| `after` | no | `[]` | Names of actions the agent is told to run after a successful edit, like a restart. The server doesn't run them, the agent asks for them with `run_action` and you approve that too |
Paths are compared after `..` is resolved, so `~/.hermes/profiles/../.env` doesn't match `~/.hermes/profiles/*/SOUL.md`.
### actions
Each key under `actions` is a name the agent passes to `run_action`.
| Key | Required | Default | What it does |
|---|---|---|---|
| `command` | yes | | The program and its arguments as a list. It runs without a shell, so pipes, `&&` and `$VARS` don't work. A leading `~` in the program path works |
| `description` | no | | Shown to the agent in `list_protected`, so it knows when to use the action |
| `args` | no | `{}` | Placeholders the agent can fill. The key is the placeholder name used as `{name}` in `command`, the value is a regex the whole value has to match. Undeclared arguments are refused |
| `timeoutSeconds` | no | `120` | The command is killed after this long |
The agent gets the exit code and the last 8000 characters of output back.
Commands that restart the agent's own process will also kill this server before it can answer. For those, schedule the restart a few seconds later, like `systemd-run --user --on-active=3 systemctl --user restart my-agent` in the Hermes example.
### Top-level keys
| Key | Default | What it does |
|---|---|---|
| `backupDir` | `~/.local/state/host-protection-mcp/backups` | Where the old version of a file goes before every edit. The name is the full path with `/` swapped for `_`, plus a timestamp. Nothing cleans this folder up, so it grows with every change |
| `maxBytes` | `1000000` | Edits that would make a file bigger than this are refused |
To undo a change, copy the backup over the file.
```sh
ls ~/.local/state/host-protection-mcp/backups
cp ~/.local/state/host-protection-mcp/backups/_home_me_.hermes_config.yaml.1764430000000 ~/.hermes/config.yaml
```
## Connecting a client
The approval comes from your client, so set it to always ask for this server.
In Hermes, mark it as untrusted and you'll get Approve and Deny buttons for every call.
```yaml
mcp_servers:
host_protection:
command: node
args: [/path/to/host-protection-mcp/src/server.ts]
trust: untrusted
```
In Claude Code, add it and leave its tools out of your allow list so every call prompts.
```sh
claude mcp add host-protection -- node /path/to/host-protection-mcp/src/server.ts
```
## Running with Docker
The container only sees what you mount, so mount every file from your config. Inside the container `~` is `/root`.
```sh
docker build -t host-protection-mcp .
docker run -i --rm \
-v ~/.config/host-protection-mcp/config.yaml:/config/config.yaml:ro \
-v ~/.hermes/config.yaml:/root/.hermes/config.yaml \
host-protection-mcp
```
Actions run inside the container too, so host commands like `systemctl --user` won't work from there. If you need actions, run the server on the host.
## Development
```sh
npm install
npm run typecheck
npm test
```
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues