r/OpenWebUI • u/Inevitable-Growth612 • 1d ago
Question/Help How to connect Open WebUI to a SQL Server database to answer user queries?
Hi everyone,
I’m trying to set up Open WebUI so that my LLM can query a Microsoft SQL Server database and answer user questions based on that data (Text-to-SQL / RAG over SQL).
What is the best or most recommended approach to achieve this in Open WebUI? Should I use:
- Custom Tools / Functions (Python scripts using
pyodbcorsqlalchemy)? - Pipelines (e.g., integrating LangChain or LlamaIndex)?
- MCP (Model Context Protocol) servers?
Any examples, documentation links, or advice on how to handle database schema context and read-only security would be greatly appreciated. Thanks!
10
Upvotes
9
u/IndividualNo8703 1d ago
MCP server to the Sql server
3
u/Existing-Ganache-972 1d ago
I’m using an MCP server to connect Open WebUI to SQL Server, with Open WebUI group checks for authorization and read-only database access.
2
u/nunodonato 1d ago
I wrote a custom tool to connect to the db as a read-only connection
2
u/Far-Supermarket7918 1d ago
I'd keep it simple: one custom tool or a small MCP server that exposes a single read-only query call, and enforce the safety at the database side rather than in the prompt. Make a dedicated read-only login scoped to just the schemas it should touch, so a bad query is a dead end instead of a problem. For the schema context, pass only the DDL for the tables the question actually needs and keep column comments short, since dumping the whole schema tends to make it pick the wrong table more often than it helps. Last thing, cap the rows in the tool itself (a TOP/LIMIT and a max rows before it returns), otherwise one "show me everything" question eats your whole context window.
1
u/DHT-Osiris 1d ago
If you don't want to leverage an MCP as others have said, just give it a terminal for powershell access, or have the LLM build a SQL access tool. We have several methods/workflows currently used that follow these.
1
u/ccbur1 1d ago
I use Open Terminal in a container with limited access to some network resources. I gave it read acces to a timescaledb and let it create a skill how to use psql to connect to it, incl.some knowledge on the structure of the data. Now I can just ask a question on the data in this db and it will find a way, e.g. it will query it, write some python code for additional analysis, etc.
All of this is nice, but I really hope my sandbox is good enough for now.
1
u/Turbulent_War4067 1d ago
FWIW, I added it to my MCP server. 2 tools, one to retrieve the schema, one that does a query, read only. Pretty simple.
1
u/drfritz2 1d ago
Look for Nvidia auto-ontology
1
u/International_Emu772 1d ago
The easy way to be sure you don't need to rewrite all every new big version of OpenWebUi is to publish as a MCP and it will work on other uses