Metadata-Version: 2.4
Name: formfill-broker
Version: 0.1.1
Summary: Isolated browser for form filling, with a stdio MCP server over open/fill/verify/OTP
Author-email: Renato Šuster <renato@gamatrade.ba>
License-Expression: MIT
Project-URL: Repository, https://github.com/gamai-io/formfill-broker
Project-URL: Issues, https://github.com/gamai-io/formfill-broker/issues
Keywords: mcp,browser,forms,automation,cdp
Classifier: Development Status :: 3 - Alpha
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: broker
Requires-Dist: fastapi==0.115.6; extra == "broker"
Requires-Dist: uvicorn[standard]==0.34.0; extra == "broker"
Requires-Dist: playwright==1.63.0; extra == "broker"
Dynamic: license-file

# FormFill Broker

<!-- mcp-name: io.github.gamai-io/formfill-broker

An isolated browser API: Chromium runs in a locked-down container and a small
FastAPI service drives it over CDP, so an agent can fill web forms without ever
holding the browser session itself. It also ships an MCP (stdio) server, so any
MCP-capable client can call the same open / fill / verify / egress / OTP /
upload primitives as tools. Field values are passed per request and are never
stored by the broker:

```bash
docker build -t ff-browser . && bash scripts/slot_up.sh 0 && pip install -r requirements.txt && uvicorn app.broker:app --host 127.0.0.1 --port 8030
```

Broker listens on `http://127.0.0.1:8030`.

## HTTP API

| Method | Path | Purpose |
| --- | --- | --- |
| GET | `/health` | Broker and CDP health |
| POST | `/open` | Open a URL (`{"url": "...", "slot"?: 0}`) |
| GET | `/snapshot` | List visible interactive elements with refs |
| POST | `/click` | Click a snapshot ref (`{"ref": "e3"}`) |
| POST | `/fill` | Fill by ref/label/selector, optional `submit` ref |
| POST | `/verify` | Check `text`/`url_contains`, save screenshot evidence |
| POST | `/otp` | Read a verification code from an IMAP inbox (read-only) |
| POST | `/upload` | Decode base64 to a 0600 file under `FF_UPLOADS`, return sha256 |
| POST | `/press`, `/type` | Send a key or type text into the page |
| POST | `/egress` | Report browser public IP and geolocation |
| POST | `/close` | Close page and browser context |

## MCP server

`mcp_server.py` is a stdio Model Context Protocol server (JSON-RPC 2.0,
one JSON object per line) that wraps the HTTP API. It uses only the Python
standard library and writes logs to stderr only, so stdout stays clean.

Tools:

- `formfill_open` `{url}`
- `formfill_snapshot` `{}`
- `formfill_fill` `{fields: [{ref|label|selector, value}], submit?}`
- `formfill_click` `{ref}`
- `formfill_verify` `{text?, url_contains?}`
- `formfill_egress` `{}`
- `formfill_close` `{}`
- `formfill_health` `{}`

The broker URL defaults to `http://127.0.0.1:8030` and can be overridden with
`FF_BROKER_URL`. Requests time out after 60 seconds.

### opencode MCP config

```json
{"mcp": {"formfill": {"type": "local", "command": ["python3","/path/to/formfill-broker/mcp_server.py"], "enabled": true}}}
```

### Run and verify

```bash
# broker must already be running on 127.0.0.1:8030
python3 -m pytest tests -q
bash scripts/mcp_smoke.sh
```

`scripts/mcp_smoke.sh` sends `initialize`, `notifications/initialized`,
`tools/list`, and a `formfill_health` call, then prints every response.

### OTP retrieval

Codes are read from an IMAP inbox with `BODY.PEEK[]`, so messages stay unread.

```bash
FF_MAIL_ACCOUNTS=./mail_accounts.json python3 -m app.otp --account demo --timeout 60
```

`mail_accounts.json` is not part of this repo; supply your own:

```json
{"accounts": [{"id": "demo", "email": "demo@example.com",
               "imap": {"host": "imap.example.com", "port": 993, "tls": "SSL"},
               "app_password": "<app password>"}]}
```

### Browser slots

```bash
bash scripts/slot_up.sh 0      # start ff-slot0, CDP on 127.0.0.1:9330
bash scripts/slot_down.sh 0    # guarded: needs FF_ALLOW_SLOT0=1
```

## Configuration

| Variable | Default | Meaning |
| --- | --- | --- |
| `FF_CDP` | `http://127.0.0.1:9330` | Chrome DevTools endpoint |
| `FF_EVIDENCE` | `evidence` | Screenshot output directory |
| `FF_UPLOADS` | `/tmp/ff_uploads` | Upload directory (0700, files 0600) |
| `FF_MAIL_ACCOUNTS` | `mail_accounts.json` | IMAP accounts file |
| `FF_MAIL_ACCOUNT` | `default` | Default account id for `/otp` |
| `FF_SLOTS` | `0,1,2` | Slots `slot_up.sh` may start |
| `FF_PROXY` | `socks5://172.17.0.1:1080` | Proxy passed to the container |
| `FF_EGRESS_HOST` | — | `user@host` for `scripts/egress_up.sh` |

Screenshots in `evidence/` are runtime output and are git-ignored.

## Deployment (systemd)

Three units make the stack survive reboots. Unit files live in
`/etc/systemd/system/`.

| Unit | Type | Role |
| --- | --- | --- |
| `ffbroker.service` | simple, `Restart=always` | uvicorn `app.broker:app` on `127.0.0.1:8030` |
| `ff-iptables.service` | oneshot, `RemainAfterExit=yes`, `Before=docker.service` | re-adds the rule that lets containers reach the SOCKS5 egress on `172.17.0.1:1080` (idempotent `iptables -C ... \|\| iptables -I ...`) |
| `ff-slot0.service` | oneshot, `RemainAfterExit=yes`, `After=docker.service ff-iptables.service` | `scripts/slot_up.sh 0`, `ExecStop=docker rm -f ff-slot0` |

Install / enable (once):

```bash
systemctl daemon-reload
systemctl enable ff-iptables.service ffbroker.service ff-slot0.service
systemctl start ff-iptables.service
systemctl start ff-slot0.service
systemctl start ffbroker.service
```

Prerequisite for egress (not a unit; the SSH tunnel is external):

```bash
FF_EGRESS_HOST=user@egress-host.example bash scripts/egress_up.sh
```

Verify:

```bash
systemctl is-enabled ff-iptables ffbroker ff-slot0      # enabled enabled enabled
systemctl is-active  ff-iptables ffbroker ff-slot0      # active active active
iptables -C INPUT -i docker0 -s 172.17.0.0/16 -p tcp --dport 1080 -j ACCEPT && echo rule-ok
curl -s http://127.0.0.1:8030/health                    # {"ok":true,...}
curl -s http://127.0.0.1:9330/json/version | jq -r .Browser
```

Restart cycle (container is recreated by the unit, not by docker alone):

```bash
systemctl stop ff-slot0 && systemctl start ff-slot0
curl -s http://127.0.0.1:9330/json/version              # 200 after ~2 s
```

Notes:

- The container runs with `--restart unless-stopped` and **no** `--remove`
  flag, so a docker daemon restart brings it back without losing the name.
- `ff-slot0.service` is idempotent: if `ff-slot0` is already running the
  script exits 0 and leaves it alone; use `docker rm -f ff-slot0` (or
  `systemctl restart ff-slot0`) to apply new flags.
- `iptables` is persisted by `ff-iptables.service`. A snapshot is also kept at
  `/etc/iptables/rules.v4` (`iptables-save`), but `netfilter-persistent` is not
  installed, so that file is a reference/restore artifact only.
- Broker environment overrides: drop `KEY=value` lines in `/etc/ffbroker.env`
  (loaded via `EnvironmentFile=-/etc/ffbroker.env`).

## License

MIT — see [LICENSE](LICENSE).
