writgate

command module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

writgate

No agent action without a writ.

writgate turns an OpenAPI spec into safe MCP tools for AI agents. Every call is checked against a permission policy and written to a tamper-evident, hash-chained audit log before it happens.

It's built for teams that own an API, such as a fintech exposing a payments API, and want agents like Claude to use it without handing over the keys to everything.

How it works

01 · Point at your OpenAPI spec Each operation becomes one MCP tool, with a typed input schema. Writes are marked destructive.
02 · Set a policy Allow or deny tools by name. Deny by default, and deny always wins. Denied tools are hidden from the agent.
03 · Every call is written down An intent record before the request, an outcome record after. No record, no action. Secrets are masked.
04 · Verify & recover writgate audit verify checks the hash chain and checkpoints. writgate audit recover settles interrupted calls by Idempotency-Key.

A call goes through in this order. Nothing reaches the API before its record is on disk.

sequenceDiagram
    participant A as Agent
    participant W as writgate
    participant L as Audit log
    participant P as Your API
    A->>W: tools/call create_refund
    W->>W: check the policy
    W->>L: intent (fsync)
    W->>P: POST /charges/ch_1/refunds
    P-->>W: 201 Created
    W->>L: outcome
    W-->>A: result
    Note over W,L: If denied, one denied record is written and nothing is sent
    Note over W,P: If no reply comes, the result is unknown until recovery asks the API

Demo

Three scenes against the mock payments API in examples/payments-api:

  1. The agent sees four of the API's six tools, refunds the duplicate charge, and is refused when it tries to create one.
  2. writgate is killed after the API made a refund but before the reply arrived. audit verify lists the call as unknown, and audit recover finds it by its Idempotency-Key.
  3. One number in the log is edited by hand, and audit verify names the records.

The agent in the recording is a scripted stand-in (examples/demo/agent), so it plays the same way every time; sh examples/demo/record.sh records it again. docs/DEMO.md is the same story with Claude Code as the agent.

Quickstart

Sixty seconds, with Docker and Claude Code. It starts a mock payments API, charges a customer twice by mistake, and gives Claude a writgate that may look things up and refund, nothing else.

go install github.com/mattchris3/writgate@latest

git clone https://github.com/mattchris3/writgate && cd writgate/examples/payments-api
docker compose up -d --build --wait
sh scripts/seed_demo.sh

claude mcp add payments \
  -e WRITGATE_UPSTREAM_API_KEY=sk_test_local \
  -- writgate serve --spec "$PWD/openapi.json" --upstream http://localhost:8000 \
     --policy "$PWD/writgate.yaml" --audit-log "$PWD/demo.audit.jsonl"

Start claude and ask: "Customer cus_42 says they were charged twice for order 1002. Find the duplicate and refund it." Then check what it did:

writgate audit verify demo.audit.jsonl

docs/DEMO.md carries on from here: the agent is refused a charge, a hand-edited log is caught, and a refund interrupted by a crash is recovered.

Use it with your API

export WRITGATE_UPSTREAM_API_KEY=sk_live_...
writgate serve --spec ./openapi.json --upstream https://api.example.com --policy writgate.yaml
# writgate.yaml
allow: [readonly, create_refund]
deny:  [cancel_*]

writgate needs an OpenAPI 3.0 or 3.1 spec and nothing else from the API. Two optional marks in the spec improve the log: x-writgate-sensitive: true keeps a field's value out of it, and x-writgate-lookup names the read-only operation recovery uses to find a write by its Idempotency-Key.

See what a spec turns into
$ writgate tools ./openapi.json
create_charge            POST    /charges                           destructive
list_charges             GET     /charges                           readonly
get_charge               GET     /charges/{charge_id}               readonly
create_refund            POST    /charges/{charge_id}/refunds       destructive
list_refunds             GET     /charges/{charge_id}/refunds       readonly
cancel_refund            DELETE  /refunds/{refund_id}               destructive

6 tools

The last column shows the policy groups each tool falls in. Add --json for the full MCP tools/list result, with input and output schemas.

Serve it to Claude Code

Over stdio, Claude Code starts writgate itself:

claude mcp add payments \
  -e WRITGATE_UPSTREAM_API_KEY=sk_test_local \
  -- writgate serve --spec ./examples/payments-api/openapi.json --upstream http://localhost:8000 \
     --policy ./examples/payments-api/writgate.yaml

The upstream API key is read only from WRITGATE_UPSTREAM_API_KEY. It is never a flag and never comes from the agent. Logs go to stderr, because stdout is the MCP channel. Add -v to log each tool's policy decision.

Serve it over HTTP

For an agent that isn't on the same machine, serve over Streamable HTTP:

export WRITGATE_UPSTREAM_API_KEY=sk_test_local
export WRITGATE_HTTP_KEY=$(openssl rand -hex 24)

writgate serve --transport http --listen 127.0.0.1:8080 \
  --spec ./examples/payments-api/openapi.json --upstream http://localhost:8000

claude mcp add --transport http payments http://127.0.0.1:8080/mcp \
  --header "Authorization: Bearer $WRITGATE_HTTP_KEY"
  • The endpoint is /mcp. Every request must carry the key in WRITGATE_HTTP_KEY as a bearer token, or it gets a 401. The key is at least 16 characters and, like the upstream key, is read only from the environment.
  • A request a browser sends from another site is refused with a 403, so a web page can't reach a writgate on your machine.
  • It is stateless: each request stands alone and gets a JSON response, so several instances can sit behind a load balancer. Each instance needs its own audit log file.
  • /healthz answers 200 without a key.
  • writgate speaks plain HTTP. To listen beyond localhost, put TLS in front of it.
  • On Ctrl-C or SIGTERM, calls in flight get 30 seconds to finish, so their outcomes are recorded.
Audit log

Every call is written to writgate.audit.jsonl, one JSON object per line. Use --audit-log to put it elsewhere.

{"v":1,"seq":1,"time":"2026-12-02T18:09:37.623Z","type":"intent","call_id":"01a237f2-…","client":"claude-code/2.1.0","tool":"create_refund","arguments":{"Idempotency-Key":"refund-1","amount":500,"charge_id":"ch_1"},"rule":"allow create_refund","policy":"sha256:b545…","request":{"method":"POST","url":"http://localhost:8000/charges/ch_1/refunds","idempotency_key":"refund-1"},"prev":""}
{"v":1,"seq":2,"time":"2026-12-02T18:09:37.671Z","type":"outcome","call_id":"01a237f2-…","intent_seq":1,"tool":"create_refund","result":"completed","duration_ms":45,"response":{"status":201,"bytes":260,"truncated":false,"body":{"id":"re_1","amount":500,"status":"pending"}},"prev":"sha256:083f…"}
{"v":1,"seq":3,"time":"2026-12-02T18:09:38.626Z","type":"denied","call_id":"01a237f2-…","client":"claude-code/2.1.0","tool":"cancel_refund","arguments":{"refund_id":"re_1"},"rule":"default deny","policy":"sha256:b545…","prev":"sha256:4a3a…"}
  • Intent, then outcome. The intent is on disk before the request is sent. If it can't be written, the call is refused. An intent with no outcome means the result is unknown.
  • result is completed (a response came back; see response.status), not_sent or unknown.
  • prev is the SHA-256 of the line before, which chains the records together.
  • Secrets are masked. Mark a field format: password or x-writgate-sensitive: true in the spec and its value is written as "[redacted]" in arguments, URLs and responses. The upstream API key is never written.
Verify the log
$ writgate audit verify
writgate.audit.jsonl: 5 records
chain:       ok, every record follows the one before it
checkpoints: ok, 3 read from writgate.audit.checkpoints.jsonl, the newest matching one covers record 5

OK

Change one character in the log and it says where:

$ writgate audit verify
writgate.audit.jsonl: 5 records
chain:       BROKEN
...
TAMPERED OR DAMAGED, 1 problem(s):
  records 4-5: record 5 does not carry the hash of the line before it: record 4 was changed, or records were swapped here
  • The chain catches a record that was edited, removed, added or moved.
  • Checkpoints ("record 5 had this hash at this time") are written to a second file and to stderr every 100 records, after 60 seconds, and at shutdown. They catch a log rewritten with all its hashes recomputed, or cut short. Send the stderr copy somewhere the machine running writgate can't rewrite.
  • It also lists calls whose result is unknown: an intent with no outcome.
  • The exit code is 1 when the log isn't intact, so it can run in a scheduled job.
Recover interrupted calls

If writgate is killed mid-refund, the log has an intent and no outcome: the refund may or may not have happened. On its next start, or with writgate audit recover, writgate asks the API by Idempotency-Key and writes down the answer:

$ writgate audit recover --spec ./openapi.json --upstream https://api.example.com
record 1  create_refund  applied      the API has re_Ty7VpulJYRYJ8ihUaODX for this Idempotency-Key

1 call(s) settled
  • The spec says where to look: x-writgate-lookup: list_refunds on the write operation, naming a read-only operation with an idempotency_key query parameter.
  • Recovery only reads. It never sends the write again.
  • Until a call is settled, the same action under a different key is refused, so an agent can't refund twice by retrying. A retry with the same key goes through.
  • An API with no lookup by key leaves the call unknown, for a person to settle.
Policy
# writgate.yaml
allow: [readonly, create_refund]
deny:  [cancel_*]
  • A rule is a tool name, with * as a wildcard, or one of the groups readonly and destructive. Groups come from what the operation does (its HTTP method), not from its name.
  • Deny by default. A tool no allow rule matches is denied. With no --policy, only readonly tools are allowed.
  • Deny wins over allow, whatever the order.
  • A denied tool is left out of tools/list. Calling it anyway returns "Denied by writgate policy: is not allowed" and sends nothing to the API.
  • A mistake in the file (an unknown key such as alow:, a missing allow list, a rule with a space) stops writgate at startup.

Threat model

writgate assumes the agent is not trusted: it can be wrong, be misled by what it reads, and retry when unsure. It limits which operations the agent can call, keeps the API key away from it, records every call before it happens, and makes later edits to that record detectable.

It does not judge a call's arguments (no refund cap yet), and the audit log is tamper-evident, not tamper-proof: someone who can rewrite the log and every copy of its checkpoints leaves no trace. docs/THREAT-MODEL.md lists each threat, its defence and the limits.

Development

Requires Go 1.26 or later.

make test   # go test -race ./...
make lint   # golangci-lint run
make build  # ./writgate
make e2e    # needs Docker: writgate against the real payments API

make e2e starts the payments API with Postgres and Redis, then runs the built binary through the demo story: an agent refunds a duplicate charge, a forced call is denied, audit verify passes and then catches a hand-edited line, and a refund interrupted by a crash is recovered on restart. CI runs it on every pull request.

The design decisions are in the v0.1 spec.

Licence

Apache-2.0

Documentation

Overview

Command writgate serves an OpenAPI-described API as safe MCP tools, enforcing a permission policy and writing a tamper-evident audit log.

Directories

Path Synopsis
examples
demo/agent command
Command agent is a scripted stand-in for an AI agent, used to record the demo GIF (see ../record.sh).
Command agent is a scripted stand-in for an AI agent, used to record the demo GIF (see ../record.sh).
internal
audit
Package audit writes writgate's audit log (docs/SPEC.md section 6): one JSON object per line, each carrying the hash of the line before it.
Package audit writes writgate's audit log (docs/SPEC.md section 6): one JSON object per line, each carrying the hash of the line before it.
openapi
Package openapi loads an OpenAPI document and checks that writgate can serve it: OpenAPI 3.0.x or 3.1.x, with remote $refs off unless asked for.
Package openapi loads an OpenAPI document and checks that writgate can serve it: OpenAPI 3.0.x or 3.1.x, with remote $refs off unless asked for.
policy
Package policy decides which tools an agent may call.
Package policy decides which tools an agent may call.
recovery
Package recovery settles calls whose result the audit log doesn't know (docs/SPEC.md section 6): an intent with no outcome because writgate stopped in between, or an outcome saying no response came back.
Package recovery settles calls whose result the audit log doesn't know (docs/SPEC.md section 6): an intent with no outcome because writgate stopped in between, or an outcome saying no response came back.
server
Package server serves a set of tools as an MCP server.
Package server serves a set of tools as an MCP server.
tools
Package tools turns the operations of an OpenAPI document into MCP tool definitions: one operation becomes one tool.
Package tools turns the operations of an OpenAPI document into MCP tool definitions: one operation becomes one tool.
upstream
Package upstream turns a tool call into an HTTP request to the API behind writgate, and the HTTP response back into an MCP tool result (docs/SPEC.md section 3).
Package upstream turns a tool call into an HTTP request to the API behind writgate, and the HTTP response back into an MCP tool result (docs/SPEC.md section 3).
version
Package version holds the writgate build version.
Package version holds the writgate build version.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL