Engineering · AI · Infrastructure
Giving Our AI Agent Read-Only Access to Production, Behind Four Locks
One server that lets an agent read tickets, errors, logs and analytics, and ask production a question safely.
While writing the other half of this pair, we wanted one number: of all the critiques our marketing agent has written of its own videos, how many held a video back from posting?
Nobody opened a dashboard. The question went to an agent in plain words. It wrote one query, sent it to production, and had the answer 758 milliseconds later: 11 of 196. The question and the count are real; the query ran while this post was being drafted.
Before the end of October that meant opening a database console and writing the query by hand. This post is about the admin MCP server that replaced them. MCP, the Model Context Protocol, is a standard way to hand an AI agent a set of tools: each tool has a name, a description and a list of arguments, and the agent decides when to call one. Ours gives a coding agent the founder's own job as tools. The companion post, about the videos that agent makes and grades, is How Our AI Agent Grades Its Own Lesson Videos Before They Post.
One caution before the engineering. Cereby is run by one founder, and this server has one user. Nothing here is about scale. It is about what it takes to let an agent read production without being able to hurt it.
The courier
Before the server, a bug report went like this. A student files a ticket with a screenshot. The founder opens the ticket page, then the error dashboard, then the logs, then the user's account, and pastes pieces of each into a chat with a coding agent. The agent, which can read every line of the codebase, could see none of production. The founder was the cable between them:
Analytics had the same shape. The numbers lived in a dashboard built for eyes, and every question that the dashboard did not already chart meant writing SQL in a console.
The ticket page is the first of those stops:
A ticket in the admin portal, on a synthetic report.
The shape we landed on
The server is one endpoint. It was built in a day, in four commits, and the first decision is the one that made that possible: it has no logic of its own.
The admin dashboard has a function behind every page: list tickets, load an error group, run a report. Each tool on the server calls the same function the dashboard page calls, and gets the same answer. There is nothing to keep in sync, because there is only one implementation:
A few rules sit on top of that. Every tool is declared as a read or a write, and a write that cannot be taken back (a reply to a user, a post going out, a plan being comped) is marked destructive, so the agent's client can ask before running it. Every call writes a log line with the tool, whether it worked and how long it took. Every successful write also logs what it touched, as ids and status values only, never the text of a reply. That is an audit trail the dashboard's own buttons never had.
Two of the early fixes were about something remembered for too long. We added tools, deployed, reconnected, and one client kept showing the old list of 122. It had saved the list under the server's version, and the version had not changed. The version is now built from the deploy's commit, so every deploy announces itself as a new server. The chat client also signs in through our sign-in provider, and the server passes along that provider's directions for doing so. We cached them at first, and they went stale. The next commit stopped caching them.
And the server never changes code. A bug is investigated through it and fixed in the repository, by the same agent, under the same review rules as any other change.
Read the case before the code
The first tool we wanted was the one that replaced the five tabs. Given a ticket, it builds a case file: one document with what the user said, a snapshot of their app at that moment, the errors that user hit on the server and in the browser within ten minutes of the report, what they did in the thirty minutes either side of it, and the log lines of the requests that failed.
Two details matter more than the list. The windows are centered on the report because the bug usually happened just before someone filed it. And the file says what it could not read. A query that failed is a line in the document, never a silent gap, because an agent that does not know a section is missing will reason as if nothing was there.
It ends with where to start.
Four locks on one query
The purpose-built tools answer the questions we thought of. For the rest there is one tool that runs a single SQL query against production. Handing an agent a database connection is the part of this that deserves suspicion, so the query has to get through four checks, and any one of them alone stops a write:
The colors run from the lightest check to the hardest wall. The first is a courtesy: the server looks at the statement's first keyword and refuses anything that is not a read before it connects, so the agent is told plainly instead of reading a database error. The second is the wire protocol, which accepts exactly one statement, so a read with a write tacked on after a semicolon is refused by the database itself. The third wraps the query in a transaction declared read-only and rolls it back whatever happens. The fourth is the one that would hold if the other three were deleted: the connection logs in as a database role that was granted the right to select and nothing else.
Then two limits that are about cost, not safety. A query gets 15 seconds, and an answer gets 500 rows. A third limit came from a failure: a big answer cut off at a size limit is worse than no answer, because it does not parse. An answer over one megabyte is now refused with its size and a suggestion to narrow the call.
No new secret. That role needs a password, and we did not want a second credential to store and rotate. So the password is derived from the server's own key with a keyed hash. The key already lets its holder run queries, so the derived password grants nothing new, and it cannot be turned back into the key. Rotate the key and the password rotates with it.
The first query after a deploy has to give the read-only role its derived password, and it knows to do that when the login is refused. The login that failed looked like a missing user: the database's connection pooler did not say "wrong password", it said the user was not found. That is now one more answer the server reads as "set the login and try again". When the pooler has not caught up with the new password yet, the tool says to ask again shortly.
What it actually gets used for
This is the section the title did not plan for. The log buffer holds about two days of tool calls: 1,621 of them, a handful being the queries behind this post.
We built this server for bugs and numbers. In those two days, 93% of what went through it was the marketing agent making, grading and scheduling videos. Investigation was 85 calls, and analytics was 17.
The video tools had already left their mark on the server, in a failure that grew into its own rule. Every tool's description is read by the agent before it does anything, and the video tools had swollen that reading to 348 kilobytes. A test now caps it, and catalogs are looked up on demand.
Inside the 85 is the more useful finding. Eighty-three were the raw SQL tool, two were log searches, and the case file was not called once. That last number has an innocent reading: only 5 tickets have been filed since the server shipped, all 24 we have ever received are resolved, and the buffer reaches back only about two days. The first number does not. Fifteen analytics tools were called 17 times between them while the one general-purpose query ran 83 times. An agent that can write SQL will write SQL.
It was also fast enough not to matter. The median call took 777 milliseconds, and the median query 752.
Before and after
| Before | Now | |
|---|---|---|
| A question the dashboard does not chart | SQL in a console, by hand | One guarded query, in under a second at the median |
| A ticket | Five tabs, pasted into a chat | One case file that lists what it could not read |
| Who changed what | Not recorded for dashboard edits | A log line for every call and every write |
| A write the agent might make by accident | Possible in a console | Stopped four times over on the query tool, and flagged destructive elsewhere |
What this taught us
The cheapest server is one with no logic in it. Because every tool calls the function a dashboard page already called, the first version took a day, and a test that compares the two lists is all that keeps them honest.
Safety that depends on one check depends on that check being right. The keyword filter is the lock we trust least and it is the one the agent meets first. The role is the lock we trust most and it is the one a write would reach last.
Tell the agent what is missing. The case file's list of what it could not read, and the size refusal that replaced truncated answers, are the same idea: a gap the agent knows about is a question, and a gap it does not know about is a wrong conclusion.
Count what gets used. We would have written this post about ticket triage. The log says the agent mostly asks its own questions in SQL.
What's next
Read the 83 queries. Each one is a question no purpose-built tool answered, or one the agent did not think to ask a tool, and the two cases want different fixes. And exercise the case file on the next real ticket, because a tool with zero calls in the log is a tool we have tested and not yet used.