Skip to content
← All work

Case study 02

Oracle NL2SQL agent

Executives at a national electric utility ask questions in plain English and get answers from the Oracle warehouse on demand, instead of queuing BI report requests.

Stack
LangGraphSQLcl MCPOracle 23aiLangSmithAegra
Code
Private repository, code on request
Sections
Context · The problem · Design · Guardrails · Evals and results · A failure I handled · Stack · Links

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

Oracle NL2SQL agent: a question goes to a LangGraph ReAct agent, which calls an LLM and reaches Oracle 23ai only through an MCP client, a stateful supergateway and one SQLcl process.Executive questionplain English, in Studio or a UILangGraph agentcreate_agent ReAct loopmodel: init_chat_modelprompt: LangSmith Hubasks first if unclearLLMAzure OpenAIHuawei MaaSAnthropicset in .envMCP clientStreamable HTTPLangSmithevery callsupergateway--stateful: one SQLclprocess, one sessionThe model nevertouches thedatabase.Oracle 23aifour subject areasread-only by prompt
One question, end to end. The agent reaches Oracle only through MCP tools.

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-sql and run-sqlcl. Swapping Oracle for PostgreSQL or Supabase means pointing MCP_URL at a different MCP server. The agent code does not change.
  • Model through init_chat_model. Changing provider or model is an .env change. 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 up or 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 SELECT only 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, disconnect and list-connections are 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.

Private repository. This is client work, so the code is not public.

Next case study

AnalyGeek

Communication analytics on Microsoft 365 metadata. Deterministic code computes every metric, and the model only explains them.

Read it →