Connect Claude Code to SQL Server Database

Published
Length
3 min read

Luis Felipe Dias

All posts

You can connect claude code to sql server database systems through Model Context Protocol servers. You can also let the agent run local query commands through its Bash tool. Both approaches let the coding agent inspect schemas and read table rows during a session. Good security requires strict database roles and query controls before the agent runs any command. How to connect claude code to sql server database in practice Claude Code reaches external tools and data sources through the Model C

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 can connect claude code to sql server database systems through Model Context Protocol servers. You can also let the agent run local query commands through its Bash tool. Both approaches let the coding agent inspect schemas and read table rows during a session. Good security requires strict database roles and query controls before the agent runs any command.

How to connect claude code to sql server database in practice

Claude Code reaches external tools and data sources through the Model Context Protocol, or MCP. MCP servers give Claude Code access to your tools, databases, and APIs. Vendor docs list database queries as a primary use case for MCP servers. You register an MCP server and pass a connection string for your SQL Server instance. The MCP server then runs queries on behalf of the agent and returns the output.

Claude Code can also reach a database by running shell commands through its Bash tool. In Manual mode, the Bash tool asks for your approval before it runs most commands. It only skips approval for a small built-in list of safe local read commands. When you approve a database command, the tool sends the query straight to SQL Server. The official MCP docs warn you to verify that you trust each server first.

How SQL Server logins and fixed database roles work

Before you connect the agent, you must set up a database user in SQL Server. A database user is the identity of a login when it connects to a database. Microsoft SQL Server supports thirteen types of database users for different access needs. When a user needs access to many databases on an instance, admins create a login. That login lives in the master database and maps to users in target databases.

For one or a few databases, you can use a contained database user instead. A contained database user has its own password and does not need a master login. An admin must first turn on contained databases on the server and target database. After you create the user, you assign a fixed database role to limit its power. You cannot change the built-in permissions that belong to fixed database roles.

Claude Code Security + Database Connection Strings Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Security tradeoffs of static roles and active sessions

Claude Code docs advise you to put a read-only database user in your connection string. That step helps stop the queries Claude runs from changing or deleting your data. In SQL Server, the db_datareader role lets members read all data from all user tables. It also lets members read data from all views inside that database. If you add the db_denydatawriter role, the user cannot add, change, or delete table data. By contrast, the db_owner role can run all maintenance tasks and drop the whole database.

Static roles still leave major gaps during a live coding agent session. A user with db_datareader can read all column in every user table and view by default. Query results can return raw emails, phone numbers, card numbers, and national IDs to prompts. A connection string stored on a laptop also stays valid until someone rotates the password. Teams often add a proxy layer that checks each statement and masks sensitive result fields. Dynamic masking rewrites matched fields like emails or card numbers in query results before delivery.

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

How does Claude Code send queries to SQL Server?

Claude Code sends queries through an MCP server configured with a database connection string. It can also run local database client tools through its built-in Bash tool after approval.

Which SQL Server role should I give to a coding agent?

Give the agent a dedicated user with the db_datareader and db_denydatawriter fixed roles. Never assign db_owner or db_datawriter to an automated coding agent unless you review every write.

Does a read-only SQL Server role protect sensitive user data?

No, the db_datareader role allows the user to read all user tables and views. To hide emails, phone numbers, or card numbers in results, you need dynamic data masking.

The query runs. The emails stay hidden.