A Model Context Protocol (MCP) server for SQL Server built with .NET 10. This server provides tools to interact with SQL Server databases through the MCP protocol.
β Compatible with SQL Server 2005 and later versions
This MCP server exposes the following tools for SQL Server operations:
- query - Execute SQL queries and return results
- list_tables - List all tables in a database
- describe_table - Get detailed table structure information
- list_databases - List all databases on the server
- list_stored_procedures - List all stored procedures in a database
- list_functions - List all user-defined functions in a database
- get_routine_definition - Get the source code of a stored procedure or function
- execute_non_query - Execute INSERT, UPDATE, DELETE, CREATE commands
- .NET 10 SDK or later
- SQL Server instance (SQL Server 2005 or later)
- Valid SQL Server connection string
- Clone or download this repository
- Build the project:
dotnet build
This MCP server is designed to work with Visual Studio 2026's MCP integration.
The server uses stdio transport for communication:
dotnet runIn Visual Studio 2026, configure the MCP server through:
Tools > Options > AI > Model Context Protocol
Add a new MCP server with the following settings:
- Name: SQL Server
- Type: stdio
- Command:
dotnet - Arguments:
run --project c:\Dev\sqlserver-mcp\SqlServerMcp.csproj - Working Directory:
c:\Dev\sqlserver-mcp
Alternatively, you can publish the project and use the executable:
dotnet publish -c Release -o publishThen configure:
- Command:
c:\Dev\sqlserver-mcp\publish\SqlServerMcp.exe - Arguments: (leave empty)
For SQL Server 2005, use these connection string formats:
Windows Authentication (Recommended for SQL Server 2005):
Data Source=VM-SQLTEST;Database=master;Integrated Security=True;Encrypt=False
Or using the standard format:
Server=localhost;Database=MyDatabase;Integrated Security=true;Encrypt=false;
SQL Server Authentication:
Data Source=VM-SQLTEST;Database=MyDatabase;User Id=sa;Password=YourPassword;Encrypt=False
Remote Server with specific port:
Server=192.168.1.100,1433;Database=MyDatabase;User Id=myuser;Password=mypass;Encrypt=false;
Named Instance:
Server=localhost\SQLEXPRESS;Database=MyDatabase;Integrated Security=true;Encrypt=false;
For modern SQL Server versions, use:
Windows Authentication:
Server=localhost;Database=MyDatabase;Integrated Security=true;TrustServerCertificate=true;
SQL Server Authentication:
Server=localhost;Database=MyDatabase;User Id=sa;Password=YourPassword;TrustServerCertificate=true;
- SQL Server 2005: This server enables TLS 1.0 support for compatibility. Use
Encrypt=falsein connection strings. - Production environments: Consider upgrading to a modern SQL Server version with TLS 1.2+ support.
- Certificates: The server accepts self-signed certificates for development purposes.
Execute a SQL query and return results as JSON.
Parameters:
connectionString(string, required): SQL Server connection stringquery(string, required): The SQL query to executemaxRows(int, optional): Maximum rows to return (default: 100, max: 1000)timeoutSeconds(int, optional): Query timeout in seconds (default: 30)
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;Encrypt=false;",
"query": "SELECT TOP 10 * FROM Sales.Customer",
"timeoutSeconds": 60
}List all tables in a database.
Parameters:
connectionString(string, required): SQL Server connection stringschema(string, optional): Filter by schema name
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;Encrypt=false;",
"schema": "Sales"
}Get detailed information about a table's structure.
Parameters:
connectionString(string, required): SQL Server connection stringschema(string, required): Schema nametableName(string, required): Table name
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;Encrypt=false;",
"schema": "Sales",
"tableName": "Customer"
}List all user databases on the SQL Server instance (auto-detects SQL Server 2005).
Parameters:
connectionString(string, required): SQL Server connection string (can connect to any database)
Example:
{
"connectionString": "Server=localhost;Integrated Security=true;Encrypt=false;"
}List all stored procedures in a database.
Parameters:
connectionString(string, required): SQL Server connection stringschema(string, optional): Filter by schema name
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;TrustServerCertificate=true;",
"schema": "dbo"
}List all user-defined functions in a database.
Parameters:
connectionString(string, required): SQL Server connection stringschema(string, optional): Filter by schema name
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;TrustServerCertificate=true;",
"schema": "dbo"
}Get the source code/definition of a stored procedure or function.
Parameters:
connectionString(string, required): SQL Server connection stringschema(string, required): Schema nameroutineName(string, required): Name of the stored procedure or function
Example:
{
"connectionString": "Server=localhost;Database=AdventureWorks;Integrated Security=true;TrustServerCertificate=true;",
"schema": "dbo",
"routineName": "uspGetEmployeeManagers"
}Response includes:
- Routine type (PROCEDURE or FUNCTION)
- Return type (for functions)
- Creation and last altered dates
- Complete source code definition
Execute a non-query SQL command (INSERT, UPDATE, DELETE, CREATE, etc.).
Parameters:
connectionString(string, required): SQL Server connection stringcommand(string, required): The SQL command to executetimeoutSeconds(int, optional): Command timeout in seconds (default: 30)
Example:
{
"connectionString": "Server=localhost;Database=MyDatabase;Integrated Security=true;TrustServerCertificate=true;",
"command": "INSERT INTO Users (Name, Email) VALUES ('John Doe', 'john@example.com')"
}All tools return JSON responses with a consistent format:
Success Response:
{
"success": true,
"data": [...],
"rowCount": 5
}Error Response:
{
"success": false,
"error": "Error message",
"type": "SqlException"
}- Always use secure connection strings
- Consider using Windows Authentication when possible
- Never hardcode passwords in your code
- Use environment variables or secure configuration for connection strings
- Be cautious with execute_non_query as it can modify data
- Limit permissions on SQL Server accounts used by the MCP server
- SQL Server 2005: Be aware that TLS 1.0 is enabled for compatibility but is considered insecure for production use
| Version | Status | Notes |
|---|---|---|
| SQL Server 2005 | β Fully Supported | Use Encrypt=false in connection strings |
| SQL Server 2008/2008 R2 | β Fully Supported | Use TrustServerCertificate=true |
| SQL Server 2012+ | β Fully Supported | Use TrustServerCertificate=true |
| SQL Server 2016+ | β Fully Supported | Use TrustServerCertificate=true |
| SQL Server 2019+ | β Fully Supported | Use TrustServerCertificate=true |
| SQL Server 2022 | β Fully Supported | Use TrustServerCertificate=true |
The server automatically detects SQL Server 2005 and adapts its queries for compatibility.
sqlserver-mcp/
βββ Program.cs # Main entry point
βββ Tools/
β βββ SqlServerTools.cs # MCP tool implementations
βββ SqlServerMcp.csproj # Project file
βββ .github/
β βββ copilot-instructions.md
βββ README.md # This file
To add a new tool:
- Add a new method to
SqlServerTools.cswith the[McpServerTool]attribute - Add a
[Description]attribute for documentation - Use descriptive parameter names with
[Description]attributes - Return JSON formatted results
Example:
[McpServerTool(Name = "my_tool"), Description("Description of what the tool does")]
public static async Task<string> MyTool(
[Description("Parameter description")] string parameter,
CancellationToken cancellationToken = default)
{
// Implementation
}You can debug this MCP server directly in Visual Studio 2026:
- Open the solution in Visual Studio 2026
- Set breakpoints in your code
- Press F5 to start debugging
- Visual Studio will automatically launch the MCP server
- Use GitHub Copilot or other AI features in VS 2026 that will connect to your server
- Breakpoints will be hit when the AI invokes your SQL Server tools
This project is provided as-is for educational and development purposes.