Skip to main content
Glama
README.md
# nanobot-host-broker

A host-side MCP server for [nanobot](https://pypi.org/project/nanobot-ai/) agents whose shell runs in a sandbox.

With `tools.exec.sandbox: "bwrap"` and `restrictToWorkspace: true`, nanobot's shell sees only `/usr` and its workspace. It holds no API key, can't read your mail and can't touch the rest of the machine. That is the point, but some host actions are still useful: reading mail, drafting a reply, adding a calendar event, starting a coding task, or running one command as root.

This server runs outside the sandbox. The nanobot gateway starts it as a stdio MCP server. It exposes a fixed set of tools, each with a narrow contract, and it asks you on Telegram before any action that leaves the machine or changes something others see.

It is an independent project, not part of nanobot. Linux only. Version 0.1: the plugin interface may still change.

## How it keeps the agent in its lane

- **Fixed tools, argv only.** Every subprocess gets an argument list, never a shell string built from tool input.
- **Read freely, write narrowly.** Reads are free. Writes are append-only or create-only, to files you list in the config folder, never to a path the agent picks.
- **Outward actions need your code.** A calendar event, a coding task, or a plugin's own gated action sends you a one-time 6-digit code on Telegram, together with the exact reply to type. The agent can only act once you paste that reply into a chat with it. Codes are stored hashed, used once, valid for 2 hours, and three wrong tries cancel the request.
- **Root goes through your phone.** `root_request` uses [agent-sudo](https://github.com/citizenu03bb/agent-sudo)'s phone approval: a tap plus a TOTP code, checked by a root service the agent can't reach. The agent never sees the approval.
- **No code from the agent's reach.** The loader refuses a config, plugin or plugin folder inside `untrusted_paths` (the agent's workspace), or writable by other users.
- **Untrusted text is labelled.** Mail, calendar entries and GitHub issues come back with a note that they are data, not instructions.

## Install

Into the Python environment nanobot runs in:

```bash
~/.nanobot/venv/bin/pip install git+https://github.com/citizenu03bb/nanobot-host-broker
```

Copy `examples/config.toml` (and `examples/plugins/` if you want local plugins) somewhere outside the agent's workspace, and edit it. Check what nanobot will see:

```bash
~/.nanobot/venv/bin/python -m hostbroker --config ~/hostbroker/config.toml --list-tools
```

Then add the server to nanobot's config (`~/.nanobot/config.json`, under `tools.mcpServers`) and restart the gateway:

```json
"host": {"command": "/home/you/.nanobot/venv/bin/python",
         "args": ["-m", "hostbroker", "--config", "/home/you/hostbroker/config.toml"],
         "toolTimeout": 660}
```

`toolTimeout` must exceed 630 seconds if you enable `root_request`, which waits up to 5 minutes for your answer and then for the command.

## Configuration

One TOML file. `[broker]`:

| Key | Meaning |
|---|---|
| `name` | MCP server name, the key in `tools.mcpServers` (default `host`) |
| `owner` | your name as tool descriptions use it (default `the user`) |
| `agent` | the agent's name in approval messages, branches and commits (default `nanobot`) |
| `language` | language of messages sent to you: `en` or `fr` |
| `data_name` | folder name under `~/.config` and `~/.local/state` (default `nanobot-host-broker`); `config_dir` and `state_dir` set full paths |
| `untrusted_paths` | folders the agent can write (default `["~/.nanobot/workspace"]`) |
| `plugins` | built-in plugins to load, in order |
| `plugin_paths`, `local_plugins` | folders of your own plugins (relative to the config file) and the `<name>.py` files to load from them |

`[telegram]`: `env_file` (a `KEY=VALUE` file read without a shell), `token_var` (default `TELEGRAM_BOT_TOKEN`), `chat_id` (default: the first approved Telegram chat in nanobot's `pairing.json`). Codes come from the same bot you talk to nanobot with.

Each plugin reads its options from `[plugins.<name>]`.

## Built-in plugins

| Plugin | Tools | Needs |
|---|---|---|
| `root` | `root_request` | agent-sudo with its phone approval service; option `agent_sudo` |
| `codetask` | `code_task_repos`, `code_task_request`, `code_task_run`, `code_task_status` | the Codex CLI; allowed repos in `<config_dir>/code-repos.json` (`{"name": "~/path"}`). Codex runs in a git worktree on a new branch; nothing is pushed or merged |
| `gmail` | `gmail_search`, `gmail_read` | a Google token with `gmail.readonly` only |
| `gmail_drafts` | `gmail_draft_create` | a second Google token (`gmail.compose` + `calendar.events`); only the drafts endpoint is ever called |
| `calendar` | `calendar_list`, `calendar_event_request`, `calendar_event_create` | the second token; option `timezone`. Events get no attendees and `sendUpdates=none` |
| `github` | `github_digest`, `github_read` | `gh` logged in; read-only commands only; option `owner` |
| `capture` | `capture_inboxes`, `capture` | inboxes in `<config_dir>/capture.json` (`{"name": "~/notes/inbox.md"}`) |

When a loaded plugin declares an approval kind, the core adds `approval_pending`, which lists open requests (never the codes) and tells the agent which tool takes each kind of code.

### Google tokens

Create an OAuth client of type "Desktop app" in a Google Cloud project with the Gmail API (and the Calendar API for `calendar`) enabled. Save its JSON as `gmail-client.json` in the config folder, or enter its ID and secret with `--setup-client`. Then, from a desktop session:

```bash
python -m hostbroker.google.auth --config config.toml          # read-only Gmail token
python -m hostbroker.google.auth --config config.toml --write  # drafts + calendar token
python -m hostbroker.google.auth --config config.toml --all    # both
```

Each token is refused if Google grants any scope beyond its own. While the OAuth app stays in Google's "Testing" mode, refresh tokens expire after 7 days, so rerun `--all` weekly.

## Writing a plugin

A plugin is a module with `register(broker, options)`:

```python
from hostbroker import approvals
from hostbroker.core import fill


def register(broker, options):
    @broker.tool()
    def hello(name: str) -> str:
        """Greet someone on behalf of {owner}."""     # the description the agent reads
        return f"{options.get('greeting', 'Hello')}, {name}!"
```

- `@broker.tool()` registers the function; its signature becomes the tool's schema. `{owner}` and `{agent}` in the docstring are filled in from the config.
- `hostbroker.core` has the helpers: `run` (argv, timeout, bounded), `cap`, `read_json`/`write_json` (0600), `write_lock`, `md_cell`, `fill`, `t(en=..., fr=...)` and `settings`.
- For an action that needs your code, call `approvals.request(kind, payload, summary, what)` in one tool and `approvals.check(kind, request_id, code)` in another, and declare the pair with `broker.approval_kind(kind, tool_name)`. See `examples/plugins/hello.py`.

Local plugins are imported from their file as `hostbroker_local.<name>`, so they can't shadow other modules. Keep the folder outside the agent's workspace: the loader checks it and every `.py` file in it.

## Tests

```bash
python -m unittest discover -s tests
```

Hermetic: no network, no Telegram, no Google, no real agent-sudo or Codex.

## License

MIT