# custodian narrow mcp gateway

- Status: `experiment`
- Track: [delegated authority & keyless execution](https://aifoc.us/labs/delegated-authority/)
- Layer: 3. Identity, Permissions & Delegated Authority
- Updated: 2 December 2026
- Live demo: https://custodian-narrow-mcp-gateway.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/custodian-narrow-mcp-gateway

## Inspired by

- [calling apis from the browser shouldn't mean exposing keys](https://aifoc.us/dangerous/)
- [on-device ai needs a trust model](https://aifoc.us/on-device/)
- [building a claw in the browser](https://aifoc.us/building-a-claw-in-the-browser/)

## Why this matters

Connecting an automated tool directly to a wide REST catalog or handing it a static provider API key floods its context with dozens of schemas and lets a single prompt injection call arbitrary URLs or leak credentials in tool outputs. Putting a narrow 4-tool MCP gateway in front of fixed allowlisted operations keeps upstream secrets inside edge custody.

## What this opens up

Browser-hosted agents can discover and invoke typed backend operations through just 4 fixed [Model Context Protocol](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports) tools (`search_capabilities`, `describe_operation`, `connection_status`, `invoke_operation`)—with human-approved call budgets, byte-bound per-call agent signatures, instant epoch revocation, and zero raw credentials in the client.

## How the tests work

1. Click **1. Create Dual Keys & Enroll Grant** to generate two separate non-extractable ECDSA P-256 keypairs (`HumanApprovalKey` and `AgentSessionKey`), confirm the browser Worker's locked CSP self-check, and record the human-signed delegation in SQLite.
2. Click **2. Call repo.release.list@v1 via MCP** to sign the exact request arguments inside `/workers/secret-custodian.js` and send `invoke_operation` to `POST /mcp`, returning schema-projected output while `UPSTREAM_CANARY_TOKEN` stays server-side.
3. Click **3. Run All 6 Exploits & Leak Scan** to fire URL/header injection (`403`), nonce replay (`409`), 1-byte argument tampering (`401`), stale-epoch post-revocation calls (`401`), single-key collapse (`403`), and a permissive Worker CSP probe (`unfit`) while scanning every response and audit row for canary leaks.
4. Use the **Narrow 4-Tool MCP Gateway Console** and **DO SQLite Ledger Inspector** to call each of the 4 MCP tools individually and inspect the recorded audit table and raw P-256 proof envelopes.


## Hypothesis

- **Claim:** Placing a narrow 4-tool Streamable HTTP MCP gateway in front of fixed allowlisted operations—gated by distinct `HumanApprovalKey` and `AgentSessionKey` ECDSA P-256 proofs, a SQLite Durable Object nonce/epoch ledger, and a browser Worker CSP self-check—blocks arbitrary target injection, key collapse, nonce replay, tampering, and revoked-epoch calls with zero upstream credential leakage.
- **Confirms it:** Authorized invocations return `200 OK` while `UPSTREAM_CANARY_TOKEN` never appears in any response, header, or audit row; all 6 live red-team attacks fail closed in `CustodianLedger`; and `/workers/secret-custodian.js` verifies `securitypolicyviolation.originalPolicy` before holding `AgentSessionKey`.
- **Bounds or disproves it:** If a browser fails to enforce Worker script CSP headers (letting the blocked probe succeed), the custodian Worker refuses key admission rather than running with silent egress exposure, and narrowing operations behind 4 generic MCP tools adds discovery round-trips compared to static tool lists.


## Under the hood

[Custodian Narrow MCP Gateway](https://custodian-narrow-mcp-gateway.kinlan-test.workers.dev) ([source on GitHub](https://github.com/PaulKinlan/custodian-narrow-mcp-gateway)) combines a narrow 4-tool Streamable HTTP MCP endpoint (`/mcp`) with edge credential custody and browser-side Worker verification:

- **Dual ECDSA P-256 WebCrypto proofs:** `HumanApprovalKey` signs `{ agentJwkThumbprint, allowedOps, maxCalls, epoch, exp }` while `AgentSessionKey` signs the SHA-256 digest of `{ opId, argsJson, nonce, epoch, ts }`. Submitting the same key for both roles fails closed with `403 KEY_COLLAPSE_FORBIDDEN`, and altering even 1 byte of `argsJson` fails with `401 PROOF_DIGEST_MISMATCH`.
- **SQLite Durable Object `CustodianLedger`:** enforces a monotonic `revocationEpoch` (`401 EPOCH_REVOKED`), atomic single-use `consumed_nonces` (`409 NONCE_REPLAYED`), fixed operation allowlists (`403 ARBITRARY_TARGET_FORBIDDEN`), and server-only injection of `UPSTREAM_CANARY_TOKEN` (exposed only as a non-reversible SHA-256 fingerprint).
- **Dedicated Worker CSP self-check:** `/workers/secret-custodian.js` fetches a blocked cross-origin URL and inspects `securitypolicyviolation.originalPolicy` (`default-src 'none'; script-src 'self'; connect-src 'self'`) before accepting `AgentSessionKey`. A second permissive Worker (`/workers/secret-custodian-permissive.js`) demonstrates how an unverified runtime marks itself `unfit` and refuses key custody.


## Key terms & further reading

- [Model Context Protocol (Streamable HTTP)](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports) — A JSON-RPC 2.0 protocol over HTTP for exposing structured tools; narrowing it to 4 fixed tools keeps schemas compact and blocks arbitrary URL routing.
- [SubtleCrypto ECDSA P-256](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) — The browser's built-in asymmetric cryptography API, used to create separate non-extractable keys for human delegation and per-call agent possession.
- [RFC 7638 JWK Thumbprints](https://datatracker.ietf.org/doc/html/rfc7638) — A canonical SHA-256 hash of a public JSON Web Key used here to prove `HumanApprovalKey` and `AgentSessionKey` are distinct keys.
- [securitypolicyviolation event](https://developer.mozilla.org/en-US/docs/Web/API/Element/securitypolicyviolation_event) — A browser event fired when Content Security Policy blocks a fetch, letting the Dedicated Worker verify its own `connect-src 'self'` policy before accepting keys.
- [Cloudflare Durable Objects (CustodianLedger)](https://developers.cloudflare.com/durable-objects/) — A single-threaded SQLite edge store enforcing monotonic revocation epochs, single-use nonces, and server-only injection of the upstream canary secret.

## Open questions to iterate on

- How many allowlisted operations can live behind `search_capabilities` + `describe_operation` before retrieval precision drops for smaller local models?
- How should epoch revocation propagate offline across cached Worker delegates without requiring a synchronous ledger round-trip on read-only operations?

## Iteration log

- **2026-12-02:** Deployed Streamable HTTP 4-tool MCP gateway (`/mcp`, `2025-03-26`), dual non-extractable WebCrypto ECDSA P-256 proofs (`HumanApprovalKey` + `AgentSessionKey` with RFC 7638 thumbprint separation), SQLite Durable Object `CustodianLedger`, 6 live red-team attacks, and `/workers/secret-custodian.js` runtime `securitypolicyviolation.originalPolicy` self-check.

## Primitives tested

- `Streamable HTTP MCP (4-Tool Surface)`
- `crypto.subtle ECDSA P-256 (Dual Proofs)`
- `RFC 7638 JWK Thumbprints`
- `Dedicated Worker CSP Self-Check`
- `Durable Object SQLite (CustodianLedger)`

## Connected labs in other tracks

- [passkey attenuated capabilities](https://aifoc.us/labs/delegated-authority/passkey-agent-caps/) — Tests hardware WebAuthn Level 3 PRF key wrapping and cumulative dollar spend caps.
- [keyless browser proxy](https://aifoc.us/labs/delegated-authority/keyless-browser-proxy/) — Holds upstream secrets at the Worker edge behind origin-bound ephemeral HMAC quotas.
- [csp & iframe sandbox studio](https://aifoc.us/labs/browser-containment/csp-iframe-sandbox-studio/) — Benchmarks Dedicated Worker CSP enforcement across 4 real browser topologies.
- [mcp oauth 2.1 handshake lab](https://aifoc.us/labs/agent-bridges-and-discovery/mcp-oauth-handshake-lab/) — Exercises RFC 9728/8414/8707 OAuth 2.1 discovery and PKCE S256 token issuance for MCP servers.

---
Canonical: https://aifoc.us/labs/delegated-authority/custodian-narrow-mcp-gateway/
