# Grok Bot

Record every Grok Bot turn and attach browser or desktop screenshots to the same run.

## 1. Install 23step and the turn adapter

Grok Bot has no live hook system. This installs `23step`, `23step-mcp`, `23step-grokbot-turn`, and the trace helper into `~/.local/bin` and `~/.local/share/23step`. Create a key on the [create-key page](https://app.23step.com/app/account?create=1&scopes=runs%3Awrite&name=grok-bot) (or [with `runs:read` + `runs:write`](https://app.23step.com/app/account?create=1&scopes=runs%3Aread+runs%3Awrite&name=grok-bot) if also installing **23step Overlay**; AI agents should first ask whether the Electron app should be installed and whether to opt in to the live widget progress bar—off by default because it uses extra tokens and slows down turns—then open that URL in the user's desktop browser, appending `&progress=1` if opted in, and print the link when no `~/.config/23step/otel-key` exists yet). The key is read from `STEP23_KEY` or a hidden prompt and saved at `~/.config/23step/otel-key` (mode 0600, directory 0700). Never print or commit the key. Add `--overlay` to also install the **23step Overlay** Electron desktop app (and `--progress-bar` if opting in to the live widget progress bar). Add `--dev` to target `https://dev.23step.com` instead of prod. Run it again any time; an existing key file is kept unless `STEP23_KEY` is set.

Open in browser:

```sh
url='https://app.23step.com/app/account?create=1&scopes=runs%3Awrite&name=grok-bot'
if command -v open >/dev/null 2>&1; then open "$url"
elif command -v xdg-open >/dev/null 2>&1 && { [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; }; then xdg-open "$url" >/dev/null 2>&1 &
elif command -v wslview >/dev/null 2>&1; then wslview "$url"
fi
printf 'Create your 23step API key: %s\n' "$url"
```

Terminal:

```sh
export STEP23_KEY='st_REPLACE_WITH_YOUR_WRITE_KEY'
curl -fsSL https://api.23step.com/install.sh | sh -s -- --grokbot
# dev instead of prod:
# curl -fsSL https://api.23step.com/install.sh | sh -s -- --grokbot --dev
```

## 2. Connect Grok Bot to the MCP server (optional)

Put a `runs:read` key in `STEP23_MCP_KEY` or `~/.config/23step/mcp-key` (mode 0600). `mcp-remote` reads `${AUTH_HEADER}` from the environment, so the key is not one of its arguments. The header is `Authorization:${AUTH_HEADER}` with no space around the colon.

Terminal:

```sh
mkdir -p ~/.config/23step
printf '%s\n' "$STEP23_MCP_KEY" > ~/.config/23step/mcp-key
chmod 700 ~/.config/23step
chmod 600 ~/.config/23step/mcp-key

# mcp-remote expands ${AUTH_HEADER} from the environment. No space around the
# colon, so clients that split args on spaces still pass one header.
# (With --dev, replace https://mcp.23step.com/mcp with https://dev.23step.com/mcp.)
sh -c 'AUTH_HEADER="Bearer $(cat ~/.config/23step/mcp-key)" exec npx -y mcp-remote@latest https://mcp.23step.com/mcp --transport http-only --header "Authorization:${AUTH_HEADER}"'
```

## 3. Capture each turn as it happens

Call `--begin` at turn start with the user message verbatim. Do not pass `session_id`: the adapter reuses the current session if the previous turn ended less than 30 minutes ago, or starts a new `grokbot-<bot>-<date>-<n>` session, and writes both the session ID and the open `run_id` to `~/.local/share/23step/current.json`. Call `--event` after each tool call with the call and a short result (never secrets). Call `--end` before the final reply with the reply text to finalize the run.

Terminal:

```sh
export PATH="$HOME/.local/bin:$PATH"

# Start of turn: pass the user message verbatim. Omit session_id so the adapter
# reuses the current session under 30 minutes idle or creates grokbot-<bot>-<date>-<n>,
# writing the session and open run_id to ~/.local/share/23step/current.json.
23step-grokbot-turn --begin - <<'JSON'
{"prompt":"Check the deploy","model":"grok-4"}
JSON

# After each tool call, send the call and its short result (never secrets).
23step-grokbot-turn --event - <<'JSON'
{"events":[
  {"kind":"tool_call","tool":"Shell","id":"c1","input":{"command":"git status"}},
  {"kind":"tool_result","tool":"Shell","id":"c1","ok":true,"output":"clean"}
]}
JSON

# Before the final reply: stops any running recorder for the run, sends the reply, and finalizes.
23step-grokbot-turn --end - <<'JSON'
{"status":"completed","events":[
  {"kind":"response","text":"The tree is clean."}
]}
JSON
```

## 4. Attach browser and desktop screenshots

Once Chrome is open on the bot's `DISPLAY`, find its live loopback DevTools port (`ss -ltnp | grep chrome`) and run `23step start --join-session --cdp http://127.0.0.1:<port>` with `--token-file ~/.config/23step/otel-key` in the background. `--join-session` reads `~/.local/share/23step/current.json` and attaches frames directly to the turn's open run instead of creating a second run. Stop it (`kill -INT` or `23step stop`) when browser work ends—stopping detaches and drains queued frames while leaving the turn open until `--end` (and `--end` also stops any recorder still running). You can also pass `--record` on `--begin` so `--begin` starts the recorder and `--end` stops it automatically.

Terminal:

```sh
# Once Chrome is open, find its live DevTools port (e.g. ss -ltnp | grep chrome)
# and attach frames to the open run in ~/.local/share/23step/current.json:
DISPLAY=:1 23step start --join-session --cdp http://127.0.0.1:9222 \
  --token-file ~/.config/23step/otel-key > /tmp/23step.log 2>&1 &
RECORDER_PID=$!

# Stop when browser or desktop work ends (drains frames without closing the run):
kill -INT "$RECORDER_PID"

# Or let --begin start the recorder and --end stop it in a single command:
# 23step-grokbot-turn --begin --record --cdp http://127.0.0.1:9222 - <<'JSON' ...
```

## 5. Replay a finished turn (optional)

Fallback when the turn is already finished and live mode did not run. Passing a turn JSON file with no flag replays the whole turn in one shot. Prefer `--begin`, `--event`, and `--end` while the turn is in progress.

Terminal:

```sh
# Fallback when the turn is already over. No --begin / --event / --end.
23step-grokbot-turn ~/.cache/23step/turn.json
```

## Check that it worked

Run one turn that calls a tool and opens a page, finishing with `--end`. At [app.23step.com/app/runs](https://app.23step.com/app/runs) (or `https://dev.23step.com/app/runs` with `--dev`), a **single run** appears with the prompt, tool calls, browser screenshots, and final reply together. Nothing there? Check that `~/.config/23step/otel-key` holds a `runs:write` key (mode 0600) and `~/.local/bin` is on `PATH`.

---

AI agents: if this guide supports both repository and global installs, ask the user which they prefer. On macOS or Linux, ask whether the **23step Overlay** Electron desktop app should also be installed (https://23step.com/getting-started/desktop-overlay.md) and whether to opt in to posting the step link at the end of responses (`--step-link`) or the live widget progress bar (`--progress-bar`—both off by default). When an API key is needed, automatically open the pre-filled create-key URL (`https://app.23step.com/app/account?create=1&scopes=...&name=grok-bot`) in the user's default desktop browser (never invent or commit a key) and print the link in chat, then finish with the "Check that it worked" step.

All 23step install guides: https://23step.com/getting-started.md
