How to Configure LlamaIndex SQL Server MCP Server

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up a llamaindex sql server mcp server, connect your agent to a database tool. Then, give that database tool a read-only SQL Server user role. Teams often wire AI agents right to their main databases with shared keys. They want fast answers from their data. Still, broad permissions let a model change or delete rows by mistake. If an agent uses an owner role, one bad query can drop a whole database. How a llamaindex sql server mcp server works LlamaIndex agents can talk to outside tools

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To set up a llamaindex sql server mcp server, connect your agent to a database tool. Then, give that database tool a read-only SQL Server user role. Teams often wire AI agents right to their main databases with shared keys. They want fast answers from their data. Still, broad permissions let a model change or delete rows by mistake. If an agent uses an owner role, one bad query can drop a whole database.

How a llamaindex sql server mcp server works

LlamaIndex agents can talk to outside tools through the Model Context Protocol. When you ask a question, the agent picks the right database tool. In native setups, LlamaIndex uses a light wrapper around SQLAlchemy to reach tables. Its natural language query engine turns user questions into raw SQL queries. You must give the engine a specific list of tables to check. Without that table list, the engine pulls all the schema context at once.

The MCP server sits between the LlamaIndex agent and your SQL Server instance. It takes the tool call from the agent and runs the query. To log in, the MCP server needs a SQL Server identity. SQL Server supports thirteen different types of database users for this job. When a login connects to a database, it takes on a database user identity. For access to many databases on an instance, teams create a login in master. For one or a few databases, a contained database user works well. This user has a password and does not tie to a master login. An admin must first turn on contained databases on the server and database.

Native SQL Server roles for AI agents

LlamaIndex docs warns that running arbitrary SQL queries is a real security risk. The authors suggest using restricted roles, read-only databases, and sandboxing for safety. SQL Server gives you fixed database roles to limit what the agent can do. You cannot change the permissions assigned to these fixed database roles. Every role, including the public role, comes with basic connect rights.

To keep data safe, add your MCP server user to the data reader role. Members of this role can read all data from all user tables and views. You should also add the user to the deny data writer role. This role stops the user from adding, changing, or deleting any table data. If you want to hide all tables, the deny data reader role blocks all reads. It also blocks the user from reading metadata about those tables and views. Never put an AI agent in the database owner role. That role can run all maintenance tasks and even drop the database.

Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native roles solve and what they miss

Fixed database roles solve the first half of the agent security problem. They control who can log in and stop write actions at the database level. A read-only user cannot drop tables or wipe out customer records. This setup protects your core data from destructive text-to-SQL mistakes.

Yet native roles leave a big operational gap open during live sessions. Database roles decide who logs in, not what individual statements do in flight. A read-only user can still read every single column in every user table. If a table holds emails, phone numbers, card numbers, or national IDs, the query reads them. The database sends those raw fields right back to the MCP server and model. Native roles also cannot pause a risky query to ask a human for approval. Plus, static database passwords still sit in config files on the MCP host.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev is an open-source proxy that sits next to SQL Server as a sidecar for your SQL Server. It inspects TDS batches and parameterized queries in real time.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused with your message. Matched sensitive fields in supported result columns are masked in memory, and each decision is logged per listener. Check out the code on GitHub.

Frequently asked questions

How do I make the SQL Server user read-only for my agent?

Add the database user to the fixed data reader role and the deny data writer role. This lets the agent read user tables and views while blocking all data changes.

Should I use a standard login or a contained user for the MCP server?

Use a contained database user if the agent only needs one or a few databases. Use a login in master if the agent must reach many databases on the instance.

Why does LlamaIndex pull the whole database schema by default?

If you do not name specific tables, the query engine pulls all schema context. Naming only the needed tables keeps prompts smaller and limits what the agent sees.

The query runs. The emails stay hidden.