MCP Server to Access SQL Server

Published
Length
3 min read

Luis Felipe Dias

All posts

An mcp server to access sql server connects AI assistants to your relational databases. It translates agent requests into queries and returns structured results to the model. Teams use the Model Context Protocol to give AI tools live data context. The protocol sets a clear standard between an AI app and local data. Instead of copying schema text by hand, engineers let the model inspect tables. This setup speeds up debugging, report writing, and schema checks across the team. Still, direct datab

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.

An mcp server to access sql server connects AI assistants to your relational databases. It translates agent requests into queries and returns structured results to the model.

Teams use the Model Context Protocol to give AI tools live data context. The protocol sets a clear standard between an AI app and local data. Instead of copying schema text by hand, engineers let the model inspect tables. This setup speeds up debugging, report writing, and schema checks across the team. Still, direct database connections bring real risks for security and compliance teams.

How an mcp server to access sql server works in practice

In a standard setup, the MCP host runs inside an engineer's coding app. When the user asks a data question, the host calls the MCP server. The server holds the database connection string and runs the requested query. It then sends the rows back to the AI model as plain text.

To make this work, an admin must set up a database identity first. In this database engine, a login in the master database maps to a user. That database user is the identity of the login inside a specific database. When someone needs many databases on one instance, a login in master is common. For one or a few databases, a contained database user is another option. A contained user has a password and does not tie to a master login. An admin must enable contained databases on the server and database first. Across the platform, there are thirteen types of database users available for setup.

After creating the user, the admin assigns fixed database roles to control actions. The permissions assigned to fixed database roles cannot be changed by the admin. Every role, including the public role, has basic permission to connect. A member of the data reader role can read all user tables and views. A member of the data writer role can add, delete, or change table data. An owner role can run all maintenance tasks and even drop the database.

Security gaps with static keys and fixed database roles

Many teams start by putting a read-only login into a local config file. They add the account to the data reader role and think it is safe. While this stops data changes, it leaves big gaps during an active session.

First, static passwords sitting on laptops are easy to steal or leak. Every developer or AI agent uses the same shared key with no clear log. When the database logs a query, it only sees the shared service account. You cannot tell which engineer or prompt ran an expensive or risky query.

Kubernetes API Server Access + Customer Support Access to Production: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Second, the fixed data reader role reads all data from every user table. You cannot edit a fixed role to hide specific columns with private data. If a table holds emails, phone numbers, card numbers, or national IDs, queries return them. Those sensitive fields go straight into the AI model context window and third-party logs. Adding a deny data reader role blocks all table reads, which breaks the tool.

Adding session controls and data masking for AI agents

Safe AI database access needs controls that sit between the server and the database. Instead of handing out raw database keys, teams should tie sessions to user identity. Each query then links back to the real person who triggered the AI agent.

Query approval rules add another layer of safety for production databases. If an agent needs write permissions to fix data, a peer can check it first. Real-time data masking also solves the all-or-nothing limits of fixed reader roles. As query results return, masking rewrites matched fields like emails, phone numbers, and card numbers. The AI agent gets the schema and row counts it needs to finish work. At the same time, sensitive customer values get replaced before reaching the model.

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 sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.

Frequently asked questions

Can I restrict an AI agent to read-only queries in SQL Server?

Yes, you can assign the database user to the fixed data reader role. You can also add the deny data writer role to block all data changes. Keep in mind that fixed reader roles still expose all columns in user tables by default.

Should I use a master login or a contained user for MCP connections?

Use a contained database user when the agent only needs one or a few databases. This keeps the identity inside the target database instead of the master database. Use a standard login in master if the user needs many databases on an instance.

How does data masking protect fields during AI queries?

Data masking inspects query results in flight before they reach the MCP client. It rewrites matched fields such as emails, phone numbers, card numbers, and national IDs. The AI tool receives valid structure for its task while private strings stay masked.

The query runs. The emails stay hidden.