home / mcp / python mssql mcp server
Provides access to MSSQL databases via MCP with async operations, endpoints to list/read data and execute SQL.
Configuration
View docs{
"mcpServers": {
"amornpan-py-mcp-mssql": {
"command": "python",
"args": [
"server.py"
],
"env": {
"MSSQL_USER": "your_username",
"MSSQL_DRIVER": "{ODBC Driver 17 for SQL Server}",
"MSSQL_SERVER": "your_server",
"MSSQL_DATABASE": "your_database",
"MSSQL_PASSWORD": "your_password"
}
}
}
}You can run a Python MCP Server that connects to Microsoft SQL Server databases, exposes endpoints to inspect table schemas, list and read data, and execute SQL queries. This server is designed to work asynchronously, with environment-based configuration and robust error handling, making it easy to integrate SQL data access into your MCP workflows.
You connect to the MSSQL MCP server from your MCP client to access MSSQL databases. Use the provided endpoints to list available tables, read data from a table in CSV format, and execute SQL queries. The server handles connection pooling, validation, and error reporting, so you can focus on building features that rely on MSSQL data.
Follow these steps to install and run the MSSQL MCP server locally.
# Clone the project repository
git clone https://github.com/amornpan/py-mcp-mssql.git
cd py-mcp-mssql
# Install dependencies
pip install -r requirements.txtConfigure the server using environment variables. The following variables are required to connect to your MSSQL database and select the driver to use.
MSSQL_SERVER=your_server
MSSQL_DATABASE=your_database
MSSQL_USER=your_username
MSSQL_PASSWORD=your_password
MSSQL_DRIVER={ODBC Driver 17 for SQL Server}The MCP client can start this server locally with a stdio configuration. The example shows how to run the Python script that provides the MCP endpoints.
{
"mcpServers": {
"mssql": {
"command": "python",
"args": [
"server.py"
],
"env": {
"MSSQL_SERVER": "your_server",
"MSSQL_DATABASE": "your_database",
"MSSQL_USER": "your_username",
"MSSQL_PASSWORD": "your_password",
"MSSQL_DRIVER": "{ODBC Driver 17 for SQL Server}"
}
}
}
}The server uses asynchronous operations, reads configuration from the environment, and relies on the pyodbc driver for MSSQL connectivity. It integrates with FastAPI for API endpoints and validates input with Pydantic models. Ensure the ODBC Driver 17 for SQL Server is installed on your system.
Lists all available MSSQL tables in the connected database and returns table names with URIs in the format mssql://<table_name>/data, including table descriptions and MIME types.
Reads data from a specified table URI (mssql://<table_name>/data) and returns the first 100 rows in CSV format with column headers.
Executes SQL queries against the connected database. Returns CSV results for SELECT queries or the affected row count for modification queries.