Openai Agents Sdk SQL Server MCP

Published
Length
3 min read

Luis Felipe Dias

All posts

An openai agents sdk sql server mcp setup links AI models to your database. The agent sends tool calls to a protocol server, which runs SQL queries and returns results. Teams build this bridge so agents can read business tables and answer user questions. Still, giving an automated model direct database credentials creates real security and data risks. How an openai agents sdk sql server mcp workflow runs in practice The workflow starts when a user asks the AI agent a data question. The agent

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 openai agents sdk sql server mcp setup links AI models to your database. The agent sends tool calls to a protocol server, which runs SQL queries and returns results.

Teams build this bridge so agents can read business tables and answer user questions. Still, giving an automated model direct database credentials creates real security and data risks.

How an openai agents sdk sql server mcp workflow runs in practice

The workflow starts when a user asks the AI agent a data question. The agent decides which tool to call based on the Model Context Protocol definition. It sends a structured request to the protocol server over a local or network link. That protocol server holds the database credentials and opens a session with SQL Server.

Inside SQL Server, a database user is the identity of a login for that database. Teams that need access to many databases on an instance often use a login in master. For one or a few databases, a contained database user with a password is an option. An admin must first enable contained databases on the server and the database itself. In total, SQL Server offers 13 types of database users for different access needs.

Once the protocol server logs in, it runs the query generated by the agent. The database sends the rows back to the protocol server, which formats the output. The agent reads that response and writes a plain answer for the end user.

Security tradeoffs of static roles and active agent sessions

Running arbitrary SQL generated by a model carries serious security risks for any team. Database admins usually start by limiting what the agent account can do inside the database. SQL Server includes fixed database roles whose assigned permissions cannot be changed by admins. All roles, including the public role, have basic permission to connect to the database.

To restrict an agent, admins often add its user to the read-only role. Members of that read-only role can read all data from all user tables and views. Meanwhile, members of the write role can add, delete, or change data in user tables. Admins can also use deny roles to block reading or writing across user tables. By contrast, the owner role can run all maintenance tasks and even drop the database.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Fixed roles stop an agent from dropping tables, but they leave big gaps open. A read-only user can still read all column across every user table and view by default. If a table holds customer emails, phone numbers, card numbers, or national IDs, the query returns them. Static credentials in a protocol server config file also stay active around the clock. They do not give you query-level checks or field masking during an active session.

Adding policy checks and data masking to agent queries

Safe agent access requires checking every query and every result set at runtime. Instead of putting raw database passwords in the agent app, teams can route traffic through a proxy. The proxy checks the identity of the caller before any query reaches SQL Server. It can block risky commands or require a human to approve a query first.

Data masking also protects sensitive fields before the agent processes the response. When a query returns emails, phone numbers, or card numbers, masking rewrites matched fields in results. The agent gets the structure it needs to finish the task without exposing raw personal records. Every query and result is also logged for the security team to review later.

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 do you make a SQL Server user read-only for an AI agent?

You add the database user to the built-in read-only fixed database role. You can also add the deny write role so the user cannot change any table data.

Should an MCP server use a login in master or a contained user?

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

Does a read-only SQL Server role hide sensitive customer columns?

No, the fixed read-only role lets the user read all data in all user tables and views. You need column permissions or dynamic data masking to rewrite emails, phone numbers, or card numbers.

The query runs. The emails stay hidden.