# OpenClaw

<!-- description: Connect Herald to OpenClaw: your agent reads the briefing over MCP from any channel, and Herald calls its /hooks/agent when a new briefing is out. -->

Status: **documented** (follows the official docs; not yet run against a real install).

Official docs used:
[MCP registry](https://docs.openclaw.ai/cli/mcp/registry),
[MCP transports](https://docs.openclaw.ai/cli/mcp/transports),
[`openclaw mcp`](https://docs.openclaw.ai/cli/mcp),
[configuration](https://docs.openclaw.ai/gateway/configuration),
[hooks config](https://docs.openclaw.ai/gateway/config-hooks),
[webhooks](https://docs.openclaw.ai/automation/cron-jobs/webhooks),
[`openclaw cron`](https://docs.openclaw.ai/cli/cron).

## What you get

- **Pull:** your OpenClaw agent reads today's briefing, one category of it, or past stories, and
  can change what you follow, from any channel OpenClaw is connected to.
- **Push:** when Herald publishes a briefing it calls OpenClaw's `/hooks/agent`; the agent
  summarizes or forwards it to the channel you choose.

## Pull: MCP

MCP servers live under `mcp.servers.<name>` in `~/.openclaw/openclaw.json` (`openclaw config file`
prints the active path). `openclaw mcp set` writes that entry for you.

**Same machine (stdio).** OpenClaw starts `herald-mcp` itself:

```bash
openclaw mcp set herald '{"command":"/path/to/herald/.venv/bin/herald-mcp",
  "env":{"HERALD_CONFIG":"/path/to/herald/config.json"}}'
```

**Another machine (streamable HTTP).** Run `uv run herald-mcp --http` next to Herald (see the
[index](README.md#common-ground)), put `HERALD_MCP_TOKEN` in OpenClaw's environment, then:

```bash
openclaw mcp set herald '{"url":"https://herald.example.com/mcp","transport":"streamable-http",
  "headers":{"Authorization":"Bearer ${HERALD_MCP_TOKEN}"}}'
openclaw mcp doctor herald --probe
```

Set `"transport": "streamable-http"` explicitly: without it OpenClaw assumes `sse`.
`openclaw mcp doctor` also warns when a header holds a literal secret instead of `${VAR}`.

**Tools to allow.** Start read-only with the per-server filter:

```bash
openclaw mcp tools herald --include 'herald_briefing,herald_categories,herald_history_search'
```

Add `herald_follow,herald_unfollow` to the list when the agent may change what you follow. The
JSON shape of the filter (`toolFilter.include` / `toolFilter.exclude`) is not spelled out in the
docs; prefer the CLI, or check your version's docs before editing it by hand.

Then ask the agent: "Use herald_briefing for today's Brazilian markets section."

## Push: webhook

**1. Enable hooks in OpenClaw** (`openclaw.json`, JSON5):

```json5
{
  hooks: {
    enabled: true,
    token: "${OPENCLAW_HOOKS_TOKEN}",
    path: "/hooks",
    allowedAgentIds: ["main"],
  },
}
```

The token is sent as `Authorization: Bearer <token>` (or `x-openclaw-token`). A `?token=` query
parameter is rejected.

**2. Point Herald at `/hooks/agent`.** Herald's `template` builds the body OpenClaw expects:
`message` is the prompt, `channel` and `to` (always together) pick where the answer goes, and
`idempotencyKey` stops a retried delivery from starting a second run. In Herald's `.env`:
`OPENCLAW_HOOK_URL=http://127.0.0.1:18789/hooks/agent` and `OPENCLAW_HOOKS_TOKEN` (the same
value as in OpenClaw).

<!-- herald-config -->
```json
{
  "type": "webhook",
  "enabled": true,
  "name": "openclaw",
  "languages": ["en"],
  "url": "${OPENCLAW_HOOK_URL}",
  "auth": { "type": "bearer", "token": "${OPENCLAW_HOOKS_TOKEN}" },
  "events": ["briefing.published"],
  "template": "{\"name\": \"Herald\", \"agentId\": \"main\", \"idempotencyKey\": \"herald-$date-$language\", \"message\": \"Here is today's Herald briefing ($count stories). Send me the five items that matter most, one line each, with links.\\n\\n$markdown\", \"deliver\": true, \"channel\": \"telegram\", \"to\": \"${OPENCLAW_DELIVER_TO}\"}"
}
```

Replace `telegram` with your channel and set `OPENCLAW_DELIVER_TO` to the chat id OpenClaw uses
for it. OpenClaw answers `200 {"ok": true, "runId": ...}` when it admits the run; that does not
mean the answer was delivered.

**Alternative: a mapped hook.** To keep Herald's full envelope (`"format": "full"`) and shape the
prompt on the OpenClaw side, add an entry to `hooks.mappings` with `match.path` (for example
`herald`) and a `messageTemplate` such as `{{payload.data.title}}: {{payload.data.markdown}}`,
then point Herald at `/hooks/herald`. See the mapping fields in the
[hooks config](https://docs.openclaw.ai/gateway/config-hooks) docs.

**Failures too.** For an alert when a run fails, add a second notifier with
`"events": ["run.failed"]` and a template whose `message` is `$text`.

## Troubleshooting

| Symptom | Cause |
|---|---|
| `openclaw mcp doctor --probe` cannot connect over HTTP | `transport` missing (defaults to `sse`), wrong URL (must end in `/mcp`), or `herald-mcp --http` not running |
| Probe gets 401 | `HERALD_MCP_TOKEN` not set in OpenClaw's environment, or different from Herald's |
| Run report: `webhook:openclaw HTTP 401` | `OPENCLAW_HOOKS_TOKEN` differs between the two sides |
| `HTTP 400` | `channel` without `to` (or the reverse), `channel: "last"` (not allowed on `/hooks/agent`), or the `agentId` is not in `allowedAgentIds` |
| `HTTP 404` | `hooks.enabled` is false, or the path is not `/hooks/agent` |
| `HTTP 413` | body over 1 MiB; use `$text` or a shorter prompt instead of `$markdown` |
| `HTTP 429` / `503` | auth throttling or a busy gateway; Herald retries (honoring `Retry-After`) and `idempotencyKey` keeps it to one run |
| Run report: `webhook url not resolved: ${OPENCLAW_HOOK_URL}` | the variable is missing from Herald's `.env`; nothing was sent |

## Testing without OpenClaw

1. Run the sink with the same token, and point `OPENCLAW_HOOK_URL` at it:

   ```bash
   uv run herald dev sink --port 8787 --token "$OPENCLAW_HOOKS_TOKEN" --out herald-sink
   # OPENCLAW_HOOK_URL=http://127.0.0.1:8787/ in .env
   uv run herald --data-dir /tmp/herald-try run --hours 6
   ```

   The sink prints `briefing.published evt_...: non-envelope payload` and saves the body that
   OpenClaw would receive in `herald-sink/evt_<id>.json`.
2. Replay it to OpenClaw with `"deliver": false` edited in, so nothing is sent to a chat:

   ```bash
   curl -i http://127.0.0.1:18789/hooks/agent \
     -H "Authorization: Bearer $OPENCLAW_HOOKS_TOKEN" -H "Content-Type: application/json" \
     --data-binary @herald-sink/evt_<id>.json
   ```
