Contents
The loop with no MCP: ygg-agent
ygg-agent (Python, Apache-2.0, on
PyPI) is the loop between a language model and a
database that runs this gate. The model writes one SQL statement; ygg-agent hands it to
agent_gate.propose() over PostgreSQL’s own protocol; the gate decides; the verdict goes back
to the model; repeat. There is no MCP server, no tool schema in the model’s context, and no
JSON between the model and its data.
It is a messenger with no power, on purpose. It connects as the agent role, refuses to work
unless agent_gate.whoami() says the session is behind the gate, never executes the model’s
SQL itself, and stops after three refusals in a row. Replace it with anything and it still can
only call the seven verbs: the guarantees come from the database, not from the loop.
pip install "ygg-agent[binary]" # model served over /v1/chat/completions
pip install "ygg-agent[binary,local]" # 0.2.0: the model in the same process
With LlamaCpp (0.2.0) the model is a GGUF file loaded in the agent’s own process: no model
server and no HTTP, and the only connection the loop opens is PostgreSQL’s.
Measured with gate 0.4.0 and a model served by llama.cpp (not 0.2.0’s in-process model), on a CPU-only CI run anyone can re-run: with the same gate behind both, the agent without MCP used 5x fewer prompt tokens than through MCP (24k vs 126k for 13 tasks) and finished each task 13x faster: a median of 14 s against 185 s. Through a popular Postgres MCP server, 4 of 7 trap tasks broke out; with the database deciding, 0 of 7. Every figure is tied to its raw data in pg-evidence.
For a client that only speaks MCP: a shim with no power
The gate is in the database, so there is nothing to put in front of it – an
agent reaches it through psql, a driver, or direct SQL, and it is governed all
the same. You do not need an MCP server, and that is the point: the piece that
used to hold the connection and run what the model asked is gone.
For a client that only speaks MCP, gated-mcp/ fills the gap without bringing
that power back. It is a small Bun + Hono server exposing six of the verbs as six
MCP tools (propose_and_commit, added in 0.2.5, is not exposed there yet) over Streamable HTTP, and it connects as the agent role, so it is
not where the gate lives – replace it with anything and it still can only call
the verbs. It is a compatibility layer, not the product. Six tools, never one
per table: what the agent may touch comes from discover.
cd gated-mcp && bun install
AGENT_GATE_DATABASE_URL=postgres://billing_agent@localhost/app bun run src/index.ts
# POST http://127.0.0.1:7878/mcp (set AGENT_GATE_TOKEN to require a bearer token)
It speaks two protocol eras on the same endpoint, chosen by what each request
says: the 2026-07-28 revision – stateless; MCP-Protocol-Version,
Mcp-Method and Mcp-Name must match the body or the request gets 400 and
-32020 before it reaches the database; an unsupported version gets 400 and
-32022; server/discover and tools/list carry ttlMs and cacheScope –
and the 2025-11-25 initialize handshake most clients still run. GET and
DELETE get 405.
Bound to loopback, Host and Origin are checked on every request and a
foreign one gets 403 before the token and before the database: any web page can
make a browser post to localhost. Bound elsewhere, set
AGENT_GATE_ALLOWED_HOSTS and AGENT_GATE_ALLOWED_ORIGINS.
Besides the six tools it offers resources and prompts, and they add no
power. The resources – agent-gate://catalog, agent-gate://whoami,
agent-gate://acts and the template agent-gate://relation/{schema}/{name} –
are read by calling a verb as the agent role, never with a query of their own:
the catalog as a resource is identical to what discover returns, and a
relation the agent was not granted answers -32602, not its content. The
prompts, change_data and investigate, carry no data: they teach a model the
gate’s loop. What a resource contains depends on the agent’s grants, so it is
marked cacheScope: private.
Checked against clients nobody here wrote: test/clients.mjs drives the gate
end to end with the official @modelcontextprotocol/client 2.0.0 pinned to
2026-07-28 and with @modelcontextprotocol/sdk 1.30.0, and checks from a
superuser connection that only the committed change happened;
test/transport.mjs checks the transport MUSTs with plain fetch; and the
official @modelcontextprotocol/conformance suite runs against it.
One command runs all of it – a throwaway cluster, an agent registered behind the gate, gated-mcp started as that role, and every suite against it:
make mcp PG_CONFIG=/path/to/pg_config # needs node and bun on PATH
CI (.github/workflows/verify.yml, the end-to-end job) runs that same command
on every commit, so “a real MCP client can only operate the gate” is re-proved,
not just asserted.
The pipe: what JSON costs
MCP is JSON-RPC, and JSON has no 64-bit integer, no exact decimal and no binary.
An agent that reaches the database through it moves its data over that pipe; one
that reaches the gate over a native connection does not. The gate’s guarantee is
identical either way – the same verbs, the same verification – so what you
weigh is the pipe. make transfer measures it on a replica of a real workload
(a 64-bit id, exact money, binary, arrays, nested documents, at volume):
make transfer PG_CONFIG=/path/to/pg_config
| the same data | native, PostgreSQL’s own protocol | forced through JSON |
|---|---|---|
the id 9007199254740993 |
exact | 9007199254740992 – a JSON number is an IEEE double |
numeric(40,12) |
exact | lost, unless it travels as a string |
| 256 bytes of binary | 256 bytes, raw | 348 bytes of base64 text (+35%) |
| the whole set | its typed, binary self | larger, every number and key as text |
None of it is real data; it is there so you can run it. The fidelity loss is shown live, by parsing the value with the same JSON a client would use.