# Recipe: webhook to agent

> When a monitor fails, upfour tells your own AI agent. The agent fetches the incident over MCP, runs a fresh check and writes its finding back as an assessment. Built on webhooks; no extra service.

Source: https://upfour.io/docs/api/recipes/webhook-to-agent

When upfour confirms an outage, your own agent gets the incident id, reads the details over [MCP](https://upfour.io/docs/api/connect-an-agent.md), runs a fresh check and writes what it found into the incident as an assessment. upfour calls no model: the agent runs on your machine or server, under your account with the provider.

## What you need

- An agent you can start from a command line without a person, such as `claude -p`, with upfour connected as in [Connect an agent](https://upfour.io/docs/api/connect-an-agent#server).
- A key for the agent with `incident.read`, `monitor.read`, `monitor.write` (a fresh check counts as a normal check) and `incident.insight.write`. It cannot acknowledge or resolve an incident, delete a monitor or publish anything. Keep it apart from the key your webhook receiver uses.
- A webhook receiver the internet can reach, as in [Webhook consumer](https://upfour.io/docs/api/recipes/webhook-consumer.md).

## Subscribe to incidents

```bash
curl -s -X POST https://app.upfour.io/v1/webhook-subscriptions \
  -H "Authorization: Bearer $UP4_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://ops.example.com/hooks/up4",
    "events": ["incident.opened"]
  }'
```

Verify the signature and store the event before you start anything, exactly as in the [webhook consumer](https://upfour.io/docs/api/recipes/webhook-consumer.md), and answer 2xx first. An agent run takes much longer than upfour waits for an answer, so start it after the response, from your stored events.

## Start the agent

```javascript
import { execFile } from "node:child_process";

// Called by your verified webhook receiver after the event is stored (see "Webhook consumer").
// Only the incident id goes into the prompt: the title and other texts come from monitored
// systems, and the agent reads them from upfour itself, labelled as untrusted.
export function startAgent(event) {
  if (event.type !== "incident.opened") return;
  const id = event.data.incident_id;
  if (!/^[0-9a-f-]{36}$/.test(id)) return;
  const prompt = `upfour incident ${id} just opened. Use upfour: read it with get_incident, run check_now for its monitor, \
and add your finding with add_incident_insight. Say what you measured and what you assume.`;
  execFile("claude", ["-p", prompt, "--allowedTools", "mcp__up4__get_incident,mcp__up4__check_now,mcp__up4__add_incident_insight"],
    { timeout: 120_000, env: { ...process.env, UP4_API_KEY: process.env.UP4_AGENT_KEY } },
    (err) => { if (err) console.error("agent run failed", id, err.message); });
}
```

`incident.opened` arrives once upfour has confirmed the outage. The [payload](https://upfour.io/docs/api/webhooks#events) carries `data.incident_id`; everything else the agent needs it fetches over MCP: the incident with its first failed checks per region, then a fresh check from every region, then its assessment. A webhook is delivered at least once, so deduplicate on the event `id` before you start a run, and cap how many runs you allow per hour: each run uses your provider's quota.

## What arrives in the incident

The assessment appears in the incident with a headline, the evidence and its basis, marked as written by an API key, not by a person. It states what was measured, for example "check from three regions at 14:02 UTC: all fail with a timeout", separately from what the agent assumes. Your team reads it and decides; an assessment never changes the incident's state.

## Keep it safe

- Allow only the three tools the run needs, as in the command above, so a text planted on a monitored page cannot make the agent do more.
- Texts in the incident come from monitored systems. Tool results name them in `untrusted_fields`; see [Security](https://upfour.io/docs/api/connect-an-agent#security).
- The agent's provider receives what the tools return. Limit the key to the clients that may be shown to it.
