P25-12-10">
Azure SQL SQL Server AI Agents
12 min read Database Security

How to Securely Unleash AI Agents on Azure SQL and SQL Server

AI agents can revolutionize how you interact with your database - but they also introduce new security risks. Learn how role-level security in SQL Server lets you safely allow agents to generate dynamic queries while maintaining strict data access controls, ensuring your sensitive information stays protected.

The AI Agent Security Challenge

Database administrators face a new reality in : AI agents are now generating and executing SQL queries dynamically, without human oversight. While this enables powerful natural language interactions with your data, it also means you no longer control the exact SQL being executed against your database.

Traditional application security relied on developers writing queries with proper WHERE clauses to filter data by user permissions. But when AI agents write the queries, you can't guarantee they'll include these critical security filters. A malicious prompt or even an overly-enthusiastic agent could expose sensitive data unintentionally.

Key insight: Role-level security in SQL Server acts as a safety net, enforcing data access rules regardless of what queries your AI agents generate. It's the difference between hoping agents behave properly and knowing your data stays protected.

Role-Level Security Explained

Row-level security (RLS) has been a SQL Server feature for over 20 years, but it's never been more critical than in the age of AI agents. RLS works by automatically filtering which rows a user can access, based on security policies you define.

The magic happens through three components:

  1. Security table: Defines which users can access which data (e.g., "User X can only see Pacific Northwest territories")
  2. Filter function: Contains the logic that determines if a row should be visible to the current user
  3. Security policy: Applies the filter function to specific tables

What makes RLS perfect for AI agents is that it operates at the database engine level. Even if an agent generates a query without any WHERE clauses, or with attempts to bypass security (like WHERE 1=1), the policy still filters the results.

The Two-Agent Architecture

Beyond database-level security, the architecture of your AI agent system plays a crucial role in maintaining security. The demonstrated solution uses two specialized agents working together:

1. Orchestrator Agent

Knows about all tables at a high level (names and purposes) but no schema details. This agent breaks user requests into logical steps and determines which tables are needed.

2. Specialist Agents

Each knows detailed schema information for specific tables but nothing about other tables. They generate and execute the actual SQL queries based on the orchestrator's instructions.

Security benefit: This separation of knowledge means no single agent has complete understanding of your database schema, reducing the risk of accidental or intentional schema exposure through prompt engineering.

Implementing Security Policies

Setting up role-level security involves creating a security table, filter function, and security policy. Here's how it works in practice:

Step 1: Create the Security Table

Define which users can access which data territories. For example:

 CREATE TABLE Security.TerritoryAccess (     UserName NVARCHAR(128) NOT NULL,     TerritoryID INT NOT NULL,     PRIMARY KEY (UserName, TerritoryID) ); 

Step 2: Create the Filter Function

This function determines if a row should be visible:

 CREATE FUNCTION Security.fn_securitypredicate(@TerritoryID INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN SELECT 1 AS fn_securitypredicate_result FROM Security.TerritoryAccess WHERE UserName = USER_NAME() AND TerritoryID = @TerritoryID; 

Step 3: Create the Security Policy

Apply the filter to your tables:

 CREATE SECURITY POLICY Security.TerritoryFilter ADD FILTER PREDICATE Security.fn_securitypredicate(TerritoryID) ON Sales.SalesTerritory WITH (STATE = ON); 

The policy can be toggled on/off as needed while maintaining security definitions.

SQL MCP Server Tool

For additional security, Microsoft's new SQL MCP Server Tool (currently in preview) provides another layer of protection. This self-hosted container:

  • Exposes selected database objects (tables, views, stored procedures) as tools for AI agents
  • Builds on Data API Builder technology with added MCP support
  • Can be hosted in Azure Container Apps for horizontal scalability
  • Includes semantic descriptions of objects to help agents understand their purpose

The MCP approach is particularly valuable when you want to restrict agents to using predefined stored procedures rather than generating arbitrary SQL. It provides a more controlled interaction model while still enabling natural language queries.

Real-World Demo

The video demonstration shows these security measures in action. At 8:45, we see:

  1. An AI agent successfully querying France sales data with security disabled
  2. The same query returning "no recorded sales" when security is enabled, even though France data exists
  3. Attempts to bypass security with SQL tricks failing because RLS operates below the query level

This proves that role-level security effectively protects your data regardless of what queries AI agents generate. The demo also shows how the two-agent architecture works in practice, with the orchestrator breaking down complex natural language questions into steps executed by specialist agents.

Best Practices

When implementing AI agents with SQL Server or Azure SQL, follow these security best practices:

1. Always Implement Role-Level Security

Make this your baseline security layer before enabling any agent access to production data.

2. Use the Two-Agent Architecture

Separate broad knowledge (orchestrator) from detailed access (specialists) to limit exposure.

3. Consider SQL MCP for High-Security Scenarios

When you need to restrict agents to specific stored procedures rather than arbitrary SQL.

4. Use Azure SQL Hyperscale for Production

Its cloud-native architecture provides the best performance and scalability for agent workloads.

5. Monitor and Log All Agent Activity

Maintain complete visibility into what data agents are accessing and what queries they're generating.

Remember: These security measures work together. Role-level security protects your data, the two-agent architecture limits schema exposure, and SQL MCP provides controlled access points. Used together, they let you safely harness the power of AI agents while maintaining strict data governance.

Watch the Full Tutorial

See these security measures in action with a complete walkthrough of the implementation. The video demonstrates how to configure role-level security, set up the two-agent architecture, and use the SQL MCP server tool to securely expose database functionality to AI agents.

Video tutorial: Securing AI Agents on Azure SQL

Frequently Asked Questions

Common questions about securing AI agents with SQL Server

The primary challenge is that AI agents dynamically generate and execute SQL queries at runtime, which means developers lose control over the exact SQL being executed. Unlike traditional applications where developers write all queries with proper security filters, agent-generated queries might lack critical WHERE clauses or other security measures.

This creates potential security risks if the agent generates queries that access unauthorized data, either through malicious prompts, accidental oversights, or being tricked by data injection attacks. Role-level security addresses this by enforcing data access rules at the database level, regardless of the query content.

  • Agents generate SQL dynamically without developer review
  • Traditional query-based security becomes unreliable
  • Database-level security policies provide the safety net

Role-level security (RLS) acts as a filter that automatically restricts which rows a user (or agent acting on their behalf) can access, regardless of the SQL query's contents. It works by evaluating each row against security policies you define before including it in results.

Even if a query lacks proper WHERE clauses or contains attempts to bypass security (like WHERE 1=1), the RLS policy still filters the results. This means agents can generate whatever queries they need to answer questions, but the database ensures only authorized data is returned.

  • Filters rows before they're returned to the user/agent
  • Operates below the query level - can't be bypassed by SQL tricks
  • Applies consistently to all queries, whether human or agent-generated

Implementing role-level security requires three main components working together. First, you need a security table that defines which users can access which data - for example, mapping users to the sales territories they're authorized to view. This table serves as the source of truth for access permissions.

Second, you create a filter function that contains the logic determining whether a row should be visible to the current user. This function queries your security table and returns a value indicating whether access should be granted. Finally, you create a security policy that applies this filter function to specific tables, activating the row filtering.

  • Security table: Defines user-data access relationships
  • Filter function: Implements the row-filtering logic
  • Security policy: Applies the filter to specific tables

No, role-level security cannot be bypassed through SQL tricks because it operates at a lower level than the query optimizer. The security filtering happens as part of the query execution plan generation, before the query itself is fully processed. This means even queries with WHERE 1=1 or other attempts to circumvent filtering will still only return rows permitted by the security policy.

In the demo video at 12:30, we see this in action - even when logged in as an administrator and using queries designed to bypass restrictions, the security policy still filters results as configured. This makes RLS particularly valuable for AI agent scenarios where you can't control the exact queries being generated.

  • Operates at the execution plan level, below query processing
  • Even administrators are subject to the policies
  • Demonstrated effective against SQL injection-style bypass attempts

The SQL MCP server tool is a new preview feature that allows you to securely expose database objects (tables, views, stored procedures) as tools that AI agents can safely interact with. It builds on Data API Builder technology and provides an additional security layer for agent-based database access.

This tool lets you select exactly which database entities should be exposed to agents, and provides semantic descriptions of each object's purpose to help agents use them correctly. It's particularly useful when you want to restrict agents to using predefined stored procedures rather than generating arbitrary SQL queries, providing a more controlled interaction model while still enabling natural language queries.

  • Self-hosted container that exposes database objects as tools
  • Built on Data API Builder with added MCP support
  • Provides semantic descriptions to help agents use tools properly

The two-agent architecture improves security by limiting each agent's knowledge of the database schema. The orchestrator agent knows about all tables at a high level (names and general purposes) but no detailed schema information. This allows it to break down user requests into logical steps and determine which tables are needed.

Specialist agents, in contrast, know detailed schema information for specific tables but nothing about other tables. They generate and execute the actual SQL queries based on the orchestrator's instructions. This separation means no single agent has complete understanding of your database schema, reducing the risk of accidental or intentional schema exposure through prompt engineering.

  • Orchestrator knows table names/purposes but no details
  • Specialists know details but only about specific tables
  • Limits potential damage from any single compromised agent

Azure SQL Hyperscale is recommended for AI agent implementations because it's designed for cloud-scale applications with AI capabilities. Hyperscale provides the performance and scalability needed for agent-based workloads while maintaining all the security features discussed in this article.

The Hyperscale service tier offers several advantages for agent scenarios: nearly instantaneous scaling to accommodate unpredictable query patterns, high availability across zones, and the ability to handle both transactional and analytical workloads efficiently. This makes it ideal for the varied demands of AI agents interacting with your data.

  • Designed for cloud-scale with AI capabilities
  • Instant scaling for unpredictable agent query patterns
  • Maintains all SQL Server security features

GrowwStacks specializes in implementing secure AI agent solutions on Azure SQL and SQL Server. We can design and deploy role-level security policies tailored to your specific data access requirements, configure SQL MCP server tools to safely expose database functionality, and build custom agent architectures that balance flexibility with security.

Our team ensures your AI implementations maintain strict data governance while delivering powerful natural language query capabilities. We handle the complex security configurations so you can focus on deriving value from your data. Book a free consultation to discuss how we can help secure your AI agent implementation.

  • Custom role-level security policy implementation
  • SQL MCP server tool configuration and deployment
  • Tailored agent architectures for your specific needs

Ready to Secure Your AI Agent Implementation?

Every day without proper security measures exposes your sensitive data to potential AI agent risks. GrowwStacks can implement these protections in as little as 2 weeks, giving you the confidence to safely leverage AI agents with your SQL data.