SQL Server MCP server

Provides a bridge to Microsoft SQL Server databases for executing queries, exploring schemas, and managing data through natural language interaction with Windows authentication security
Back to servers
Provider
Dennis Martis
Release date
Mar 25, 2025
Language
Python

The MCP SQL Server provides SQL database interaction capabilities through a conversational AI interface. It allows users to query and manipulate MS SQL Server databases using natural language, leveraging the FastMCP framework to bridge the gap between natural language instructions and database operations.

Requirements

  • Python 3.7+
  • pyodbc
  • asyncio
  • FastMCP framework
  • Microsoft SQL Server
  • ODBC Driver 17 for SQL Server

Installation

  1. Install Python dependencies:
pip install pyodbc asyncio fastmcp
  1. Ensure you have Microsoft SQL Server installed and the ODBC Driver 17 for SQL Server.

  2. Configure the connection settings in the script:

# Connection parameters
SERVER = "server\\instance"  # Change to your SQL Server instance
DATABASE = "db_name"         # Change to your database name

Usage

To start the server, run:

python mcp_sql_server.py

The server will initialize and establish a connection to the specified SQL Server database.

Available Tools

The MCP SQL Server offers several tools for interacting with your database:

Query SQL

Execute a SQL query and return the results.

query_sql(query: str = None) -> str
  • If no query is provided, it defaults to SELECT * FROM [dbo].[Table_1]
  • Returns query results as a formatted string

Example:

query_sql("SELECT TOP 5 * FROM Customers ORDER BY CustomerID")

List Tables

List all tables available in the database.

list_tables() -> str
  • Returns a list of table names as a string

Describe Table

Get the structure of a specific table.

describe_table(table_name: str) -> str
  • table_name: Name of the table to describe
  • Returns column information including names and data types

Example:

describe_table("Customers")

Execute Non-Query

Execute INSERT, UPDATE, DELETE or other non-query SQL statements.

execute_nonquery(sql: str) -> str
  • sql: The SQL statement to execute
  • Returns operation results, including number of affected rows
  • Automatically handles transactions (commit/rollback)

Example:

execute_nonquery("UPDATE Customers SET ContactName = 'John Doe' WHERE CustomerID = 'ALFKI'")

List ODBC Drivers

List all available ODBC drivers on the system.

list_odbc_drivers() -> str
  • Returns a comma-separated list of installed ODBC drivers

Database Info

Get general information about the connected database.

database_info() -> str
  • Returns server name, database name, SQL Server version, current server time, and table count

Troubleshooting

Common issues you might encounter:

  • Connection errors: Verify the SQL Server instance name and ensure it's running
  • ODBC driver errors: Confirm ODBC Driver 17 for SQL Server is installed
  • Permission errors: Check that the Windows user running the application has appropriate SQL Server permissions

Security Considerations

  • The server uses Windows Authentication ("Trusted_Connection=yes")
  • Consider implementing input validation for SQL queries to prevent SQL injection
  • Restrict database user permissions based on the principle of least privilege

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later