Context
Client work at Computech for a national electric utility. Back-office reporting questions used to go into a queue for people who write SQL. The agent covers four subject areas: sales, finance, operations and customer. That means questions about revenue, debt aging, outage MTTR and complaint SLA compliance.
The agent runs against Oracle 23ai. The model had to stay swappable, so Azure OpenAI, Huawei MaaS DeepSeek and Anthropic are all a configuration change away.
The problem
A manager asks “how much debt is overdue more than 90 days, by region?” The answer is one query away, but the query needs someone who knows the schema. The agent has to write correct read-only Oracle SQL against five schemas, ask when a question is ambiguous, and return a report a manager can act on. It must not hand customer personal data back by default.
Design
The agent is a LangGraph ReAct loop built with create_agent, in about 130 lines of Python. It does three things at startup: build the model, connect to MCP, and load the prompt.
- Database access through MCP. I did not write a custom tool layer. The agent talks to Oracle’s own SQLcl MCP server, which exposes
schema-information,run-sqlandrun-sqlcl. Swapping Oracle for PostgreSQL or Supabase means pointingMCP_URLat a different MCP server. The agent code does not change. - Model through
init_chat_model. Changing provider or model is an.envchange. Temperature is 0.1. - Prompt in LangSmith Hub. The system prompt is pulled at startup, with a local fallback if the Hub is unreachable. A prompt edit goes live on the next process start without a redeploy, and a deployment can pin a prompt commit.
- Self-hosted runtime. The same graph factory runs under
langgraph dev,langgraph upor Aegra. I chose Aegra, a self-hosted Agent Protocol server on Postgres and Redis, over LangGraph Platform, so the runtime does not depend on LangChain-hosted infrastructure.
The prompt carries the domain conventions the model would otherwise guess at: customer types, tariff codes, how a bill total is composed, how debt aging is computed, and the regional hierarchy.
Guardrails
- Read-only. The system prompt forbids INSERT, UPDATE, DELETE, MERGE and DDL. In the development schema, the agent’s database user is granted
SELECTonly on the business tables and views. - The model never touches the database. The LLM provider only sees prompts and tool results. Only the agent process talks to the MCP gateway.
- Connection tools are hidden. The agent connects once at startup.
connect,disconnectandlist-connectionsare filtered out of the tool list, so the model cannot drop the session or reconnect to the wrong name. - Personal data stays out of answers by default. Names, phone numbers, emails, ID numbers and GPS coordinates are withheld unless the user asks for individual customer detail. Answers default to aggregates and account numbers.
- Bounded retries. If a query errors, the agent refines it and retries at most three times. For ambiguous questions it asks one clarifying question before running any SQL.
Evals and results
Every model call, tool call and prompt render is traced in LangSmith, with cost, latency and token counts. Review is manual: the trace shows the SQL the agent wrote and the rows that came back. Development ran against a dummy schema that mirrors the production structure, loaded with about 1,500 synthetic rows.
What is not measured: the repository has no automated eval suite or golden question set, so I have no accuracy figure for SQL correctness. What I would add first is a LangSmith dataset of real questions with reviewed SQL, run on every prompt change.
A failure I handled
The first Oracle runs lost their database session between tool calls. SQLcl’s MCP server is stateful: you connect once and later run-sql calls use that session. The default langchain-mcp-adapters behaviour started a fresh subprocess for every tool call, so each query arrived at a SQLcl process that had never connected.
The first fix held the MCP session open for the life of the process and called connect once at startup. The model also tended to confuse schema names with connection names, so I removed the connection tools from its tool list. That version logged a cancel-scope RuntimeError at shutdown, because the session was entered and never exited. It was cosmetic, and I recorded it as a known issue in the commit.
The lasting fix moved the transport to Streamable HTTP. supergateway --stateful holds one SQLcl child process and multiplexes HTTP calls onto it, and the agent caches its tool discovery so server warm-up does not pay for it on every request. As a side benefit, the MCP server can now live anywhere the agent can reach over HTTP.
Stack
Python, LangGraph, LangChain create_agent, langchain-mcp-adapters, SQLcl MCP, supergateway, Oracle Database 23ai, LangSmith (tracing and prompt hub), Aegra. Models: Azure OpenAI, Huawei MaaS DeepSeek, Anthropic.
Links
Private repository. This is client work, so the code is not public.