How to Configure Claude Code SQL Server Read Only Access
You set up claude code sql server read only access with a dedicated database user. Then you connect that user to Claude Code through a Model Context Protocol server. Many teams start by reusing an existing app login or an admin account. That choice gives an AI coding agent far too much control over your data. A single bad query from an agent with broad rights can change or drop tables. Setting up a scoped SQL Server user limits what the agent can do. How claude code sql server read only access
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.
You set up claude code sql server read only access with a dedicated database user. Then you connect that user to Claude Code through a Model Context Protocol server. Many teams start by reusing an existing app login or an admin account. That choice gives an AI coding agent far too much control over your data. A single bad query from an agent with broad rights can change or drop tables. Setting up a scoped SQL Server user limits what the agent can do.
How claude code sql server read only access works
SQL Server separates server logins from database users to control who can enter. A database user is the identity of a login when it connects to a database. If a person or agent needs many databases on an instance, teams create a login. That login sits in the master database and maps to a user in each target database. If the agent needs just one or a few databases, you have another option. You can create a contained database user with a password inside the target database. That user does not tie back to a login in the master database. An admin must first turn on contained databases on the server and the database.
Once the user exists, you assign a fixed database role to limit its rights. All roles, including the public role, have the basic permission to connect. The fixed datareader role lets members read all data from all user tables and views. You can also pair that with the denydatawriter role for extra safety. Members of the denydatawriter role cannot add, change, or delete any table data. In contrast, the datawriter role lets a user add, delete, or change table data. Even worse, the owner role can run all maintenance tasks and drop the database. You cannot change the permissions that belong to these fixed database roles.
Connecting Claude Code to the database user
Claude Code reaches external tools and databases through the Model Context Protocol, or MCP. MCP servers give Claude Code access to your tools, databases, and APIs. Querying databases is a common use case for this protocol. When you set up an MCP server for database queries, you pass a connection string. Official guidance states you should use a read-only database user in that string. That way, the queries Claude runs cannot change your stored data. You should also verify that you trust each MCP server before connecting it.
Claude Code can also reach external systems by running shell commands through its Bash tool. In Manual mode, the Bash tool asks for human approval before running most commands. Only a built-in set of safe read-only commands can run without approval. Still, if a developer approves a database command in the shell, the agent runs it. Using a read-only SQL Server user stops write queries from both MCP and Bash.
Auditor Read-Only Access + Claude Code Security: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native roles solve and what they miss
Native SQL Server roles decide which user can log in and read tables. A user in the datareader role stops the agent from deleting rows or dropping tables. That solves the risk of accidental data loss when Claude Code writes a bad query.
Still, the datareader role lets the user read all data from all user tables. It does not check what individual queries do while they run in flight. If a table holds emails, phone numbers, card numbers, or national IDs, the agent reads them. You can add the denydatareader role to block all user tables and views. Yet blocking all tables stops the agent from doing useful work on that database. Also, connection strings stored on developer laptops for MCP servers carry long-lived passwords. Anyone with the local file can reuse that password outside of Claude Code. Native roles alone cannot mask fields in query results or remove static passwords from laptops.
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
Should I use a standard login or a contained user for Claude Code?
Use a contained database user if Claude Code only needs one or a few databases. Use a standard SQL login in master if it needs many databases on the instance. Remember that an admin must enable contained databases on the server and database first.
Does the SQL Server datareader role hide sensitive columns from AI agents?
No, members of the fixed datareader role can read all data from all user tables. You cannot change the permissions assigned to fixed database roles in SQL Server. To rewrite matched fields like emails or card numbers in results, you need data masking.
How does Claude Code run queries against a SQL Server database?
Claude Code connects to databases through Model Context Protocol servers using a connection string. It can also run database tools through its Bash tool after user approval in Manual mode. In both cases, you should give it a read-only database user so queries cannot modify data.
Related guides
In this post