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.
pip install pyodbc asyncio fastmcp
Ensure you have Microsoft SQL Server installed and the ODBC Driver 17 for SQL Server.
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
To start the server, run:
python mcp_sql_server.py
The server will initialize and establish a connection to the specified SQL Server database.
The MCP SQL Server offers several tools for interacting with your database:
Execute a SQL query and return the results.
query_sql(query: str = None) -> str
SELECT * FROM [dbo].[Table_1]
Example:
query_sql("SELECT TOP 5 * FROM Customers ORDER BY CustomerID")
List all tables available in the database.
list_tables() -> str
Get the structure of a specific table.
describe_table(table_name: str) -> str
table_name
: Name of the table to describeExample:
describe_table("Customers")
Execute INSERT, UPDATE, DELETE or other non-query SQL statements.
execute_nonquery(sql: str) -> str
sql
: The SQL statement to executeExample:
execute_nonquery("UPDATE Customers SET ContactName = 'John Doe' WHERE CustomerID = 'ALFKI'")
List all available ODBC drivers on the system.
list_odbc_drivers() -> str
Get general information about the connected database.
database_info() -> str
Common issues you might encounter:
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.
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"
]
}
}
}
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.
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.