Skip to content

Build an end-to-end agent loop

Combine a collection-scoped webhook with MCP to process atomic notes safely and idempotently.

Last updated

An agent loop turns Quire changes into bounded, repeatable work. A common pattern is an Inbox collection containing atomic notes, with an agent that updates a derived document after those notes settle.

1. Define the boundary

Create an Inbox collection for the source notes and a separate collection for generated documents. Write the operating instructions in a clearly named Quire document that the agent can read at the beginning of every run.

2. Create the subscription

Create a collection-scoped subscription on Inbox. Select document.created, document.updated, document.moved, and any comment events that should trigger review. Leave Include agent events off unless agent changes must intentionally cascade.

3. Accept and verify

Your public HTTPS endpoint should:

  • Capture the raw request bytes.
  • Verify the timestamp and HMAC signature.
  • Persist the delivery ID, event ID, headers, and body.
  • Return 2xx after the event is durably queued.

Reject duplicates before starting a second agent run.

4. Run the agent

Start an agent with a Quire MCP authorization belonging to an account that can access both the source and destination. Give it the event metadata, operating-instructions document ID, and an explicit outcome.

The agent should read the current source document through MCP, not assume the webhook represents a content snapshot. It can then find related atomic notes, update the derived document, leave a comment, or create a change-order note.

5. Prevent loops and races

Keep agent events excluded when the agent writes back into the subscribed collection. If they must be included, tag or record handled event IDs and add a workflow rule that ignores the agent's own expected mutations.

Because events can arrive more than once or out of order, make every run reconcile current state. Use stable IDs, history sequence bounds, and explicit workflow status rather than relying only on arrival order.

Separate “event accepted” from “agent completed.” The webhook endpoint should be fast and reliable even when an agent run takes minutes or needs human review.