# n8n

<!-- description: Connect Herald to n8n: start a workflow with the full briefing as JSON each time Herald publishes, or give an n8n AI Agent Herald's MCP tools. -->

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

Official docs used:
[Webhook node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/),
[Webhook common issues](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/common-issues),
[MCP Client Tool node](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolmcp/),
[MCP Client node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-langchain.mcpclient),
[Schedule Trigger](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.scheduletrigger/).

The push recipe works the same way for any generic webhook consumer (Make, Zapier, your own
service): an HTTPS endpoint, a bearer or header token, and Herald's JSON.

## What you get

- **Push:** a workflow that starts every time Herald publishes, with the full briefing as
  structured JSON (sections, stories, sources), to route anywhere n8n reaches.
- **Pull:** an n8n AI Agent that can call Herald's MCP tools.

## Push: webhook

**1. Create the trigger.** Add a **Webhook** node:

- HTTP Method: `POST`
- Path: `herald`
- Authentication: **Header Auth**, with a credential whose name is `Authorization` and whose
  value is `Bearer <token>` (generate the token with `openssl rand -hex 32`)
- Respond: **Immediately**, so Herald gets a 2xx right away

The node shows a test URL (listens for 120 seconds after you click to listen) and a production
URL (`https://your-n8n.example/webhook/herald`, live once the workflow is published).

**2. Point Herald at it.** In Herald's `.env`: `N8N_WEBHOOK_URL` (the production URL) and
`N8N_WEBHOOK_TOKEN` (the token without `Bearer`).

<!-- herald-config -->
```json
{
  "type": "webhook",
  "enabled": true,
  "name": "n8n",
  "languages": ["en"],
  "url": "${N8N_WEBHOOK_URL}",
  "auth": { "type": "bearer", "token": "${N8N_WEBHOOK_TOKEN}" },
  "events": ["briefing.published", "run.failed"],
  "format": "full"
}
```

**3. Use the data.** The Webhook node outputs `body`, `headers`, `params` and `query`. Herald's
envelope is under `body`, so in later nodes:

- `{{ $json.body.type }}`: `briefing.published` or `run.failed` (branch on it with an **If** or
  **Switch** node)
- `{{ $json.body.data.title }}`, `{{ $json.body.data.markdown }}`
- `{{ $json.body.data.sections }}`: split it with **Split Out** to handle each story
- `{{ $json.body.id }}`: the event id; Herald may deliver the same id twice after a retry, so
  drop ids you have already handled if a duplicate would hurt

If you prefer HMAC signatures (`"auth": {"type": "hmac", ...}`), the Webhook node's **Raw Body**
option and the **Crypto** node (Hmac, SHA256, HEX, secret in the Crypto credential) can recompute
`"<t>." + body`; the bearer header is simpler and enough over HTTPS.

## Pull: MCP

n8n is usually remote, so use streamable HTTP: run `uv run herald-mcp --http` behind a TLS
reverse proxy (see the [index](README.md#common-ground)).

In an **AI Agent** workflow, add the **MCP Client Tool** as a tool:

- Endpoint: `https://herald.example.com/mcp`
- Authentication: **Bearer**, with `HERALD_MCP_TOKEN` as the token
- Tools to Include: **Selected**: `herald_briefing`, `herald_categories`,
  `herald_history_search` (add `herald_follow` / `herald_unfollow` only if the agent may change
  what you follow)

Herald serves streamable HTTP only, not SSE. The MCP Client Tool docs list an SSE endpoint field;
newer n8n versions add a transport choice. Check your version's docs and pick HTTP Streamable.

To post the briefing on a schedule without a webhook, start the workflow with a **Schedule
Trigger** set after Herald's run and have the agent call `herald_briefing`.

## Troubleshooting

| Symptom | Cause |
|---|---|
| Run report: `webhook:n8n HTTP 404` | the workflow is not published (production URL), or you used the test URL while nobody was listening |
| `HTTP 403` / `401` | the Header Auth credential does not match: name must be `Authorization`, value `Bearer <token>` |
| Run report: `HTTP 3xx` | n8n's URL redirects (often `http` to `https`); Herald does not follow redirects, so use the final URL |
| MCP Client Tool cannot connect | SSE transport selected (Herald only speaks streamable HTTP), URL not ending in `/mcp`, or a wrong bearer |
| Expressions are empty | read under `body`: `$json.body.data...`, not `$json.data...` |

## Testing without n8n

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

The sink checks the token, validates the envelope against the schema and saves it as
`herald-sink/evt_<id>.json`. Then click "Listen for test event" in the Webhook node and replay:

```bash
curl -i -X POST "$N8N_TEST_URL" -H "Content-Type: application/json" \
  -H "Authorization: Bearer $N8N_WEBHOOK_TOKEN" --data-binary @herald-sink/evt_<id>.json
```
