Repository avatar
Browser Automation
v0.1.1
active

formfill-broker

io.github.gamai-io/formfill-broker

Isolated browser for form filling: open, snapshot, fill, verify and read OTP, with evidence.

Documentation

FormFill Broker

license: MIT

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:

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

MethodPathPurpose
GET/healthBroker and CDP health
POST/openOpen a URL ({"url": "...", "slot"?: 0})
GET/snapshotList visible interactive elements with refs
POST/clickClick a snapshot ref ({"ref": "e3"})
POST/fillFill by ref/label/selector, optional submit ref
POST/verifyCheck text/url_contains, save screenshot evidence
POST/otpRead a verification code from an IMAP inbox (read-only)
POST/uploadDecode base64 to a 0600 file under FF_UPLOADS, return sha256
POST/press, /typeSend a key or type text into the page
POST/egressReport browser public IP and geolocation
POST/closeClose 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

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

Run and verify

# 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.

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:

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

Browser slots

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

VariableDefaultMeaning
FF_CDPhttp://127.0.0.1:9330Chrome DevTools endpoint
FF_EVIDENCEevidenceScreenshot output directory
FF_UPLOADS/tmp/ff_uploadsUpload directory (0700, files 0600)
FF_MAIL_ACCOUNTSmail_accounts.jsonIMAP accounts file
FF_MAIL_ACCOUNTdefaultDefault account id for /otp
FF_SLOTS0,1,2Slots slot_up.sh may start
FF_PROXYsocks5://172.17.0.1:1080Proxy 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/.

UnitTypeRole
ffbroker.servicesimple, Restart=alwaysuvicorn app.broker:app on 127.0.0.1:8030
ff-iptables.serviceoneshot, RemainAfterExit=yes, Before=docker.servicere-adds the rule that lets containers reach the SOCKS5 egress on 172.17.0.1:1080 (idempotent iptables -C ... || iptables -I ...)
ff-slot0.serviceoneshot, RemainAfterExit=yes, After=docker.service ff-iptables.servicescripts/slot_up.sh 0, ExecStop=docker rm -f ff-slot0

Install / enable (once):

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):

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

Verify:

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):

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.