Claude Code MCP Ms SQL Server

Published
Length
3 min read

Luis Felipe Dias

All posts

A claude code mcp ms sql server setup lets an AI agent query relational data. You link the agent to your database using a Model Context Protocol server. Teams use this setup to inspect schemas, debug issues, and check table records. Because AI agents run queries fast, database teams must control how these sessions work. How a claude code mcp ms sql server link works in practice Claude Code reaches external systems and databases through the Model Context Protocol. In official docs, MCP server

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.

A claude code mcp ms sql server setup lets an AI agent query relational data. You link the agent to your database using a Model Context Protocol server.

Teams use this setup to inspect schemas, debug issues, and check table records. Because AI agents run queries fast, database teams must control how these sessions work.

Claude Code reaches external systems and databases through the Model Context Protocol. In official docs, MCP servers give the agent a path to tools, databases, and APIs. Querying databases is a common use case for engineering teams.

When an engineer asks a question, the agent sends a request to the MCP server. The MCP server holds the database connection string and runs the SQL query. It then sends the query results back to the AI agent.

Claude Code can also run shell commands through its built-in Bash tool. In Manual mode, the Bash tool asks for user approval before running most commands. Only a small set of safe read-only commands run without a prompt.

Vendor docs advises teams to use a read-only database user in the connection string. This step prevents the queries that the agent runs from changing or deleting data. It also warns users to verify that they trust each MCP server before use.

How SQL Server logins and roles control agent queries

Inside Microsoft SQL Server, a database user represents the identity of a login. When a team needs to reach many databases on one instance, they use a login. This SQL user login lives in the master database.

For one or a few databases, admins can set up a contained database user. This user has a password and does not tie to a login in master. An admin must first enable contained databases on the server and on the database. Microsoft SQL Server supports thirteen types of database users.

To enforce read-only rules, admins assign the user to fixed database roles. Every role, including the public role, has basic permission to connect to the database. You cannot change the permissions that belong to fixed database roles.

Claude Code Security + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

The data reader role lets a user read all data from all user tables. This role also lets the user read all views in that database. In contrast, the data writer role can add, delete, or change table data.

Admins can also apply explicit deny roles to lock down an account further. The deny data reader role blocks reading data or metadata from user tables and views. The deny data writer role stops the user from adding, changing, or deleting data. Meanwhile, the database owner role can run all maintenance tasks and drop the database.

Why static database roles leave gaps during live agent sessions

Setting up a read-only user with the data reader role is a great start. It stops accidental writes and protects your core tables from bad updates. Still, static database credentials and roles leave several security gaps open.

First, the MCP server needs a database connection string stored on the local machine. If an engineer copies credentials into a local file, those keys are hard to rotate. Shared static passwords also make it hard to track which person ran a query.

Second, the data reader role can read every user table and view in the database. An AI agent querying a customer table will pull raw sensitive records into its context. This data often includes emails, phone numbers, card numbers, and national IDs.

Third, database roles apply at the account level, not at the individual query level. If an agent needs to run one approved update, a static role cannot adapt. You must either block all writes or grant broad write permissions for the whole session.

Check SQL Server batches and results on the wire

SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server, inline in the TDS wire protocol. On every batch an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a batch hits the server, hoop.dev guardrails refuse disallowed actions with a native TDS error or hold risky statements for approval. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns in memory, and every decision is logged. Explore the MIT-licensed project on GitHub.

Frequently asked questions

How does Claude Code run queries against a SQL Server database?

Claude Code uses an MCP server that holds the database connection string. The agent sends a request to the MCP server, which runs the query and returns results.

Which SQL Server role should an AI coding agent use?

Vendor docs advise using a read-only database user in your MCP connection string. In SQL Server, the data reader role lets a user read user tables without modifying data.

Can a read-only SQL Server role stop an agent from reading sensitive fields?

No, the fixed data reader role allows reading all data from all user tables and views. To hide emails, phone numbers, or card numbers, you need data masking in front of queries.

The query runs. The emails stay hidden.