The fastest way to connect a legacy system to an AI assistant is a tool that runs SQL.
It is also the way I don't take.
The question comes up sooner or later in every existing project: can't the assistant just look it up in the system?
It can. The Model Context Protocol exists for exactly that. A server offers tools, an assistant calls them. The legacy system doesn't have to be rebuilt for it, the server sits in front.
But how those tools are cut decides whether you end up with something useful or a second back door into the database.
I have put a server like this in front of a grown system. What I take from it is written down here.
In front, not inside
A legacy system rarely has an interface meant for machines. It has screens, reports and a database.
The obvious idea is to build an AI feature into the system itself. That means new code in a codebase that is hard to test, with dependencies nobody there wants.
The MCP server is a small service of its own. Its own language, its own deployment, its own permissions. If it goes down, the legacy system doesn't notice. If it is switched off, nothing is left behind.
It is the same pattern as with an API in front of the monolith: the cut is made on the outside.
Where the server connects
There is an order of preference.
If there is an API, the server uses it. The system's own checks and permissions then apply by themselves.
If there isn't, it reads from the database. But not with the application's credentials and not on the primary. Instead, with a user of its own that can only read, and only certain views, on a replica.
The separate user means a bug in the server can't change anything. The views decide which columns are visible at all. And the replica absorbs the query that runs longer than expected.
On top of that come a time limit per query and a cap on the number of rows. An assistant will occasionally ask for "all orders".
Cut tools by question, not by table
This is where the real work is.
The convenient version is one tool per table, or a single one that accepts any SQL. The model then writes the query itself.
That doesn't fail on syntax. It fails on meaning.
In a grown schema, the status column holds a 7. That 7 means "blocked" is written in a constant in the code. That a blocked order still counts as open as long as a certain date is empty is written in a method nobody has touched in years.
The model knows none of this. It answers anyway, fluently and wrongly.
That is why I cut tools by the questions people ask. Not "read the orders table". But "what is the state of this order".
from fastmcp import FastMCP
mcp = FastMCP("order-lookup")
STATUS = {1: "open", 4: "under review", 7: "blocked"}
@mcp.tool
def order_status(order_number: str) -> dict:
"""Status of an order with reason and last change."""
row = read_order(order_number) # one fixed query against a view
if row is None:
return {"found": False}
return {
"found": True,
"status": STATUS.get(row["status"], "unknown"),
"reason": row["block_reason"],
"changed_at": row["changed_at"].isoformat(),
}The tool does three things the model can't. It runs a fixed, reviewed query. It translates the legacy system's codes into words. And it returns only what belongs to the question.
That last one is a question of cost, too. Everything a tool returns lands in the context and is paid for again at every further step. Returning forty columns because they happen to exist costs you on every call.
In the project I draw this from, we grouped the tools by what the user is trying to achieve, not by how the database is built. Someone who needs an answer should find a tool named like their question.
The objection is obvious: that's a lot of tools, and somebody has to write each one.
True. But each of them is a place where the knowledge about the legacy system is written down once, instead of being guessed anew with every request.
With whose permissions
A legacy system has a permission model. Mostly it lives in the code, not in the database: which screen someone sees is decided by the application.
A server that reads straight from the database goes around that. Everyone who may use the assistant then sees everything the database user sees.
So the server has to know who is asking. The user's identity arrives with the request, and the tool checks against the same source as the legacy system whether this user may get this answer.
And it keeps a record: who, when, which tool, with which arguments. In a log of its own, not in the legacy system's.
How that is built with OAuth and a token broker is described on the page MCP server development. The principle is enough here: without a user identity, the assistant is the way around the permissions.
What comes back is input
Legacy systems are full of free text. Note fields, remarks, forwarded emails.
To the model it is all text, and text can contain instructions. If a note field says "ignore all previous rules", the assistant reads that together with the answer.
You can't prevent that entirely. You can limit it.
A tool returns free text only where the question needs it, and then shortened. And the server gets no tools that trigger anything.
A server that only reads can't change anything because of a planted sentence. Whatever other tools the assistant has still belongs in the same consideration.
That is the practical reason for the order in the title.
Writing: later, and never around the application
In the project, none of the tools writes. None of them blocks, approves or deletes anything. That was a decision, not a gap.
A large part of the work on a system like this is looking things up: opening three screens to answer one question. Reading is enough for that.
When writing is added, it comes with three conditions.
The write goes through the application, never straight into the database. The checks, the history, the email to the customer: all of it hangs on the legacy system's code. An UPDATE that bypasses it produces a state the system itself would never have produced.
A person confirms before anything is executed. The assistant proposes, with all the details, and someone says yes.
And a call executed twice must not create two bookings. Models repeat calls when an answer doesn't arrive. So every writing call gets a key the system can use to recognize the repeat.
When it isn't worth it
Not every question needs an assistant.
If ten people need the same number every Monday, that is a report. It is cheaper, faster and answers the same way every time.
And if the data itself is unreliable, the server doesn't make it better. It makes it more believable. A wrong value on a screen looks like a data error. The same value in a fully formed sentence looks like an answer.
An MCP server pays off where the questions change and the answer today is spread over several screens.
What remains
The server itself is quickly built. That is true, and it is not the hard part.
The hard part is putting the knowledge about the legacy system into the tools: what a code means, who may see what, what is returned and what is not.
Reading is the start you can stand behind. Writing comes once you trust the reading.

