# MCP

`hermitbox mcp` gives an agent sandboxes of its own: it can create them, run commands, read and
write files, and snapshot, fork, sleep and wake them. It is an MCP server for one project, over
stdio or loopback HTTP. On the machine where you ran the installer it reads the saved credentials
by itself, and [Connect your agent](https://docs.hermitbox.dev/v0.3.2/get-started/agents.md) sets it up there.

## Remote stdio configuration

When Hermitbox runs on another machine, set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in the
agent's environment, never in a file you share. Standard output carries only MCP messages.

For a `.mcp.json` configuration, which names the variables and never their values:

```json
{
  "mcpServers": {
    "hermitbox": {
      "command": "hermitbox",
      "args": ["mcp"],
      "env": {
        "HERMITBOX_API_URL": "${HERMITBOX_API_URL}",
        "HERMITBOX_API_KEY": "${HERMITBOX_API_KEY}"
      }
    }
  }
}
```

For a `config.toml` configuration, whose `env_vars` passes them through without storing the key:

```toml
[mcp_servers.hermitbox]
command = "hermitbox"
args = ["mcp"]
env_vars = ["HERMITBOX_API_URL", "HERMITBOX_API_KEY"]
```

## Loopback HTTP transport

For a client that speaks HTTP, `hermitbox mcp --http 8090` serves stateless Streamable HTTP on
`http://127.0.0.1:8090/mcp` instead. Every request needs `Authorization: Bearer <project-key>`.
The listener binds loopback only and checks Host and Origin.

The client's configuration carries the same project key:

```json
{
  "mcpServers": {
    "hermitbox": {
      "type": "http",
      "url": "http://127.0.0.1:8090/mcp",
      "headers": { "Authorization": "Bearer <project-key>" }
    }
  }
}
```

## Tools and resources

The tools create, list, get and delete sandboxes; run commands or start them in the background;
read, write and list files; snapshot, fork, sleep and wake; expose ports; and report the project's
usage. Each sandbox's state and logs are resources, at
`hermitbox://sandboxes/<sandbox-id>/state` and `hermitbox://sandboxes/<sandbox-id>/logs`.

Every call goes through the public SDK, so the project's admission limits and each sandbox's
isolation still apply. A tool that fails says so, and a command with an uncertain outcome is never
sent again. File reads stop at 1 MiB; for larger files, stream them with an SDK.

## Per-sandbox MCP endpoint

To give an agent one sandbox and nothing else, use that sandbox's own MCP endpoint. Get its
expiring URL with `hermitbox sandbox mcp-url <sandbox-id>`, `await box.mcpUrl()` in TypeScript, or
`box.mcp_url()` in Python (`await box.mcp_url()` for the async handle).

Put the URL in an HTTP client's URL field. It carries a capability scoped to that sandbox alone and
no project key. It expires on the same schedule as the sandbox agent's tokens; ask for a new one
when it does.

Its tools run commands and background processes, send stdin and signals, and work with files and
exposed ports. Its resources are `hermitbox://sandbox/state` and `hermitbox://sandbox/logs`.
Requests with a browser Origin are refused. To build the sandbox agent without this optional
endpoint, run `cargo build -p hermitbox-agent --no-default-features --features server`.

Both surfaces speak MCP 2025-11-25.
