SQL Server monitoring and diagnostics for AI agents using Extended Events. No ODBC drivers required.
io.github.tkmawarire/sql-sentinel MCP Server
The io.github.tkmawarire/sql-sentinel (io.github.tkmawarire/sql-sentinel) MCP server provides SQL Server monitoring, diagnostics, and database operations for AI agents. It is built using Extended Events and native SQL Server connectivity via .NET 9 and Microsoft.Data.SqlClient. No ODBC drivers are required.
๐ ๏ธ Key Features
SQL Server monitoring and diagnostics using Extended Events
Database operations for AI agents
Native SQL Server connection via Microsoft.Data.SqlClient
Built with .NET 9
No ODBC drivers required
๐ Use Cases
Monitoring SQL Server activity for AI agents
Running diagnostics on SQL Server using Extended Events
Performing SQL-related database operations within agent workflows
โก Developer Benefits
Production-ready MCP server
Uses native SQL Server connectivity (Microsoft.Data.SqlClient)
Deployable via NuGet or Docker (ghcr.io)
โ ๏ธ Limitations
Coverage details beyond monitoring/diagnostics/database operations are not provided in the available source text.
A production-ready MCP (Model Context Protocol) server for SQL Server monitoring, diagnostics, and database operations. Built with .NET 9 and Microsoft.Data.SqlClient for native SQL Server connectivity โ no ODBC drivers required.
Features
Session Management โ Create, start, stop, drop, and list Extended Events sessions
Smart Filtering โ Filter by application, database, user, duration, host, and text patterns
Query Fingerprinting โ Normalize and group similar queries differing only in literal values
Sequence Analysis โ Trace execution order with timing gaps and cumulative duration
Deadlock Detection โ Capture and analyze XML deadlock reports with victim/process details
Blocking Analysis โ Monitor blocked process events with wait resource and SQL text
Wait Stats โ Query sys.dm_os_wait_stats directly, categorized by type (CPU, I/O, Lock, Memory, etc.)
Health Check โ Comprehensive server diagnostic: slow queries, deadlocks, blocking, wait stats, and insights
Real-Time Streaming โ Stream captured events for a specified duration
Production-Safe โ Auto-excludes noise (sp_reset_connection, SET statements, trace queries)
Database Operations โ List tables, describe schemas, query data, insert, update, and drop tables
AI-Optimized โ Structured JSON output with optional Markdown formatting
Requirements
SQL Server 2012+ with Extended Events enabled (default)
Required permissions:
sql
GRANTALTERANY EVENT SESSION TO [your_login];
GRANTVIEW SERVER STATE TO [your_login];
Network access: The -i flag is required for stdio transport. Use --network host so the container can reach SQL Server on your host machine. For remote SQL Server, omit --network host and use the accessible hostname in your connection string.
Connection string: Set SQL_SENTINEL_CONNECTION_STRING via -e. All tools read the connection string from this environment variable.
Note: Only use TrustServerCertificate=true in development environments with self-signed certificates.
For production, always use TrustServerCertificate=false with a valid SSL certificate.
-- These become one fingerprint:SELECT*FROM Users WHERE id =123SELECT*FROM Users WHERE id =456-- Fingerprint: abc123:SELECT * FROM Users WHERE id = ?-- Execution count: 2
SQL Server 2012+ instance (local, Docker, or remote)
Docker (optional, for container builds)
Clone & Build
bash
git clone https://github.com/tkmawarire/sql-sentinel.git
cd sql-sentinel
dotnet restore
dotnet build
Running the MCP Server Locally
bash
dotnet run --project SqlServer.Profiler.Mcp/
The server communicates over stdio using the MCP protocol. Connect it to an MCP client (Claude Desktop, Claude Code, etc.) for interactive use.
Using the Debug API
The API project provides a REST wrapper around all MCP tools with Swagger UI for manual testing.
bash
dotnet run --project SqlServer.Profiler.Mcp.Api/
Swagger UI: http://localhost:5100/
Configure the connection string via environment variable SQL_SENTINEL_CONNECTION_STRING
Using the Debug CLI
The CLI project provides an interactive REPL and script mode for testing tools directly.
bash
# Interactive REPL mode
dotnet run --project SqlServer.Profiler.Mcp.Cli/
# List all available tools
dotnet run --project SqlServer.Profiler.Mcp.Cli/ list
# Get help for a specific tool
dotnet run --project SqlServer.Profiler.Mcp.Cli/ help sqlsentinel_quick_capture
# Execute a single tool
dotnet run --project SqlServer.Profiler.Mcp.Cli/ call sqlsentinel_list_sessions
Set the SQL_SENTINEL_CONNECTION_STRING environment variable before running.
Dependency injection via Microsoft.Extensions.Hosting
stdio transport โ stdout is reserved for MCP protocol; all logging goes to stderr
Tool auto-discovery โ MCP tools are discovered from the assembly via WithToolsFromAssembly()
XE session prefix โ All created sessions are prefixed with mcp_sentinel_
Two event shapes โ Standard events (query, login, recompile) with typed fields, and XML-payload events (deadlock, blocking) parsed from Extended Events XML
Adding a New MCP Tool
Create a public static method in the appropriate file under Tools/ (or create a new file)
Decorate with [McpServerTool(Name = "sqlsentinel_your_tool")] and [Description("...")]
Add parameters with [Description("...")] attributes โ they become the tool's input schema
Inject services via method parameters (e.g., IProfilerService, IWaitStatsService)
Return a string (JSON or Markdown) โ the framework handles MCP response wrapping
csharp
[McpServerTool(Name = "sqlsentinel_example")]
[Description("Description shown to AI agents")]
publicstaticasync Task<string> Example(
IProfilerService profilerService,
[Description("Optional filter")] string? filter = null)
{
var connectionString = ConnectionStringResolver.Resolve();
// Implementationreturn JsonSerializer.Serialize(result);
}
Troubleshooting
"Permission denied" creating session
sql
GRANTALTERANY EVENT SESSION TO [your_login];
GRANTVIEW SERVER STATE TO [your_login];
"Login failed"
Check connection string credentials
For Windows auth, ensure process runs under correct user
For Azure SQL, ensure firewall allows your IP
No events captured
Verify session is RUNNING (sqlsentinel_list_sessions)
Check filters aren't too restrictive
Verify target database/app is generating queries
Check minDurationMs isn't filtering everything
No deadlock events
Ensure session was created with eventTypes: "Deadlock"
Deadlocks must actually occur while the session is running
No blocking events
Ensure blocked process threshold is configured: sp_configure 'blocked process threshold', 5
Ensure session was created with eventTypes: "BlockedProcess"
Blocking must exceed the configured threshold (seconds)
Timeout reading events
Large ring buffers with many events can be slow to parse. Use:
Time filters to narrow the window
Increase command timeout in code if needed
Security Notes
The SQL_SENTINEL_CONNECTION_STRING environment variable contains credentials โ secure appropriately
Don't leave sessions running indefinitely on production
Query text may contain sensitive data
Grant minimum required permissions
Contributing
See CONTRIBUTING.md for guidelines on submitting issues and pull requests.