An easy-to-use bridge that lets AI assistants like Claude directly query and explore Microsoft SQL Server databases. No coding experience required!
This tool allows AI assistants to:
# Clone this repository
git clone https://github.com/dperussina/mssql-mcp-server.git
# Navigate to the project directory
cd mssql-mcp-server
# Install dependencies
npm install
# Copy the example environment file
cp .env.example .env
Edit the .env
file with your database credentials:
DB_USER=your_username
DB_PASSWORD=your_password
DB_SERVER=your_server_name_or_ip
DB_DATABASE=your_database_name
PORT=3333
TRANSPORT=stdio
SERVER_URL=http://localhost:3333
DEBUG=false # Set to 'true' for detailed logging (helpful for troubleshooting)
QUERY_RESULTS_PATH=/path/to/query_results # Directory where query results will be saved as JSON files
# Start with default stdio transport
npm start
# OR start with HTTP/SSE transport for network access
npm run start:sse
# Run the interactive client
npm run client
Explore your database structure without writing SQL
mcp_SQL_mcp_discover_database()
Get detailed information about a specific table
mcp_SQL_mcp_table_details({ tableName: "Customers" })
Run a safe query
mcp_SQL_mcp_execute_query({ sql: "SELECT TOP 10 * FROM Customers", returnResults: true })
Find tables by name pattern
mcp_SQL_mcp_discover_tables({ namePattern: "%user%" })
Use pagination to navigate large result sets
// First page
mcp_SQL_mcp_execute_query({
sql: "SELECT * FROM Users ORDER BY Username OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY",
returnResults: true
})
// Next page
mcp_SQL_mcp_execute_query({
sql: "SELECT * FROM Users ORDER BY Username OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY",
returnResults: true
})
Cursor-based pagination for optimal performance
// First page
mcp_SQL_mcp_execute_query({
sql: "SELECT TOP 10 * FROM Users ORDER BY Username",
returnResults: true
})
// Next page using the last value as cursor
mcp_SQL_mcp_execute_query({
sql: "SELECT TOP 10 * FROM Users WHERE Username > 'last_username' ORDER BY Username",
returnResults: true
})
Ask natural language questions
"Show me the top 5 customers with the most orders in the last month"
The bundled client provides an easy menu-driven interface:
When working with Claude or other AI assistants through this MCP server, the way you phrase your requests significantly impacts the results. Here's how to help the AI use the database tools effectively:
When prompting an AI to use this tool, follow this structure:
Can you use the SQL MCP tools to [your goal]?
For example:
- Check what tables exist in my database
- Query the Customers table and show me the first 10 records
- Find all orders from the past month
Here are the main tools and their correct syntax:
// Discover the database structure
mcp_SQL_mcp_discover_database()
// Get detailed information about a specific table
mcp_SQL_mcp_table_details({ tableName: "YourTableName" })
// Execute a query and return results
mcp_SQL_mcp_execute_query({
sql: "SELECT * FROM YourTable WHERE Condition",
returnResults: true
})
// Find tables by name pattern
mcp_SQL_mcp_discover_tables({ namePattern: "%pattern%" })
// Access saved query results (for large result sets)
mcp_SQL_mcp_get_query_results({ uuid: "provided-uuid-here" })
When to use each tool:
For complex tasks, guide the AI through a series of steps:
I'd like to analyze our sales data. Please:
1. First use mcp_SQL_mcp_discover_tables to find tables related to sales
2. Use mcp_SQL_mcp_table_details to examine the structure of relevant tables
3. Create a query with mcp_SQL_mcp_execute_query that shows monthly sales by product category
First, discover what tables exist in my database. Then, look at the structure
of the Customers table. Finally, show me the top 10 customers by total purchase amount.
Query the top 5 underperforming products based on sales vs. forecasts,
and explain your approach to writing this query.
Remind the AI about SQL Server's specific syntax:
Please use SQL Server syntax for pagination:
- For offset/fetch: "OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY"
- For cursor-based: "WHERE ID > last_id ORDER BY ID"
If the AI uses incorrect syntax, you can help it with:
That's not quite right. Please use this format for the tool call:
mcp_SQL_mcp_execute_query({
sql: "SELECT * FROM Customers WHERE Region = 'West'",
returnResults: true
})
If the AI is struggling with a database task, try these approaches:
Be more specific about tables: "Before writing that query, please check if the CustomerOrders table exists and what columns it has."
Break complex tasks into steps: "Let's approach this step by step. First, look at the Products table structure. Then, check the Orders table..."
Ask for intermediate results: "Run a simple query on that table first so we can verify the data format before trying more complex analysis."
Request query explanations: "After writing this query, explain what each part does so I can verify it's doing what I need."
The MCP Server provides powerful tools for exploring your database structure:
Pattern-based table discovery: Find tables matching specific patterns
mcp_SQL_mcp_discover_tables({ namePattern: "%order%" })
Schema overview: Get a high-level view of tables by schema
mcp_SQL_mcp_execute_query({
sql: "SELECT TABLE_SCHEMA, COUNT(*) AS TableCount FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_SCHEMA"
})
Column exploration: Examine column metadata for any table
mcp_SQL_mcp_table_details({ tableName: "dbo.Users" })
The server supports multiple pagination methods for handling large datasets:
Offset/Fetch Pagination: Standard SQL pagination using OFFSET and FETCH
mcp_SQL_mcp_execute_query({
sql: "SELECT * FROM Users ORDER BY Username OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY"
})
Cursor-Based Pagination: More efficient for large datasets
// Get first page
mcp_SQL_mcp_execute_query({
sql: "SELECT TOP 10 * FROM Users ORDER BY Username"
})
// Get next page using last value as cursor
mcp_SQL_mcp_execute_query({
sql: "SELECT TOP 10 * FROM Users WHERE Username > 'last_username' ORDER BY Username"
})
Count with Data: Retrieve total count alongside paginated data
mcp_SQL_mcp_execute_query({
sql: "WITH TotalCount AS (SELECT COUNT(*) AS Total FROM Users) SELECT TOP 10 u.*, t.Total FROM Users u CROSS JOIN TotalCount t ORDER BY Username"
})
Explore relationships between tables with join operations:
mcp_SQL_mcp_execute_query({
sql: "SELECT u.Username, u.Email, r.RoleName FROM Users u JOIN UserRoles ur ON u.Username = ur.Username JOIN Roles r ON ur.RoleId = r.RoleId ORDER BY u.Username"
})
Run aggregations and analytical queries to gain insights:
mcp_SQL_mcp_execute_query({
sql: "SELECT UserType, COUNT(*) AS UserCount, SUM(CASE WHEN IsActive = 1 THEN 1 ELSE 0 END) AS ActiveUsers FROM Users GROUP BY UserType"
})
The MCP server supports SQL Server-specific features:
Connect this tool directly to Claude Desktop in a few easy steps:
~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"mssql": {
"command": "node",
"args": [
"/FULL/PATH/TO/mssql-mcp-server/server.mjs"
]
}
}
}
/FULL/PATH/TO/
with the actual path to where you cloned this repositoryCursor is an AI-powered code editor that can leverage this tool for advanced database interactions. Here's how to set it up:
npm run start:sse
Once connected, you can use MCP commands directly in Cursor's AI chat:
Ask Claude in Cursor to explore your database:
Can you show me the tables in my database?
Execute specific queries:
Query the top 10 records from the Customers table
Generate and run complex queries:
Find all orders from the last month with a value over $1000
Best for: Using directly with Claude Desktop or the bundled client
npm start
Best for: Network access or when used with web applications
npm run start:sse
.env
file.env
file for correct database credentialsnpm install
again to ensure all dependencies are installedclaude_desktop_config.json
If you're new to SQL Server, here are some key concepts:
This tool helps you explore all of these without needing to be a SQL expert!
The MS SQL MCP Server is built with a modular architecture that separates concerns for maintainability and extensibility:
database.mjs
- Database Connectivitytools.mjs
- Tool Registrationresources.mjs
- Database Resourcespagination.mjs
- Results Navigationerrors.mjs
- Error Handlinglogger.mjs
- Logging Systemtools.mjs
database.mjs
, with possible pagination from pagination.mjs
errors.mjs
logger.mjs
This architecture ensures:
The .env
file controls how the MS SQL MCP Server connects to your database and operates. Here's a detailed explanation of each setting:
# Database Connection Settings
DB_USER=your_username # SQL Server username
DB_PASSWORD=your_password # SQL Server password
DB_SERVER=your_server_name # Server hostname or IP address (example: localhost, 10.0.0.1, myserver.database.windows.net)
DB_DATABASE=your_database_name # Name of the database to connect to
# Server Configuration
PORT=3333 # Port for the HTTP/SSE server to listen on
TRANSPORT=stdio # Connection method: 'stdio' (for Claude Desktop) or 'sse' (for network connections)
SERVER_URL=http://localhost:3333 # Base URL when using SSE transport (must match your PORT setting)
# Advanced Settings
DEBUG=false # Set to 'true' for detailed logging (helpful for troubleshooting)
QUERY_RESULTS_PATH=/path/to/query_results # Directory where query results will be saved as JSON files
TRANSPORT=stdio
in your .env filenpm start
TRANSPORT=sse
in your .env fileSERVER_URL
to match your server addressnpm run start:sse
DB_USER=sa
DB_PASSWORD=YourStrongPassword
DB_SERVER=localhost
DB_DATABASE=AdventureWorks
DB_USER=azure_admin@myserver
DB_PASSWORD=YourStrongPassword
DB_SERVER=myserver.database.windows.net
DB_DATABASE=AdventureWorks
Query results are saved as JSON files in the directory specified by QUERY_RESULTS_PATH
. This prevents large result sets from overwhelming the conversation. You can:
query-results
directory in the project/Users/username/Documents/query-results
ISC
{
"mcpServers": {
"mssql": {
"env": {},
"args": [
"/FULL/PATH/TO/mssql-mcp-server/server.mjs"
],
"command": "node"
}
}
}
Seamless access to top MCP servers powering the future of AI integration.