SQL Server MCP Server

Abdelrahmanghoniem/SQL-MCP
0 starsCommunity

Install to Claude Code

This server doesn't publish a one-line install command. Follow the setup in the source repository.

Summary

Enables Cursor AI to interact with SQL Server databases, including querying, schema exploration, report generation, and chart creation.

README.md

SQL Server MCP Server for Cursor

A local Model Context Protocol (MCP) server that enables Cursor AI to interact with SQL Server databases. This MCP server provides comprehensive database capabilities including querying, reporting, and chart generation directly within Cursor.

What is This?

This is a local MCP server that you install and configure in Cursor to give the AI assistant the ability to:

  • Connect to your SQL Server databases
  • Execute SQL queries
  • Explore database schemas and tables
  • Generate reports and visualizations
  • Export data in various formats

All database operations happen through Cursor's AI interface - you can ask the AI to query your databases, generate reports, or create charts.

Quick Start Guide

Step 1: Install and Build

  1. Install dependencies:
   npm install
  1. Generate Prisma client:
   npm run prisma:generate
  1. Build the project:
   npm run build

Step 2: Configure in Cursor

  1. Open Cursor Settings:
  • Press Ctrl+, (or Cmd+, on Mac) to open settings
  • Click on the {} icon to open JSON settings
  1. Add the MCP server configuration:

Find or create the mcpServers section and add: ``json { "mcpServers": { "sql-server": { "command": "node", "args": ["D:\\Work\\Code\\Projects\\local_MCPs\\SQLMCP\\dist\\index.js"] } } } ``

Important: Replace the path with your actual project path. On Windows, use double backslashes \\ or forward slashes /.

  1. Restart Cursor to load the MCP server

Step 3: Configure Database Connections

You have two options for connecting to databases:

Option A: Automatic Connection (Recommended)

Create a .env file in the project root directory:

# Format: DATABASE_URL_<connectionId>
# Use any name you want for <connectionId> - this will be how you reference the database

DATABASE_URL_production=sqlserver://server:1433;database=ProductionDB;user=admin;password=YourPassword;encrypt=true
DATABASE_URL_staging=sqlserver://server:1433;database=StagingDB;user=admin;password=YourPassword;encrypt=true
DATABASE_URL_local=sqlserver://localhost:1433;database=LocalDB;user=sa;password=YourPassword;encrypt=true

Connection String Format: `` sqlserver://[server]:[port];database=[database];user=[username];password=[password];encrypt=true ``

After creating .env:

  • Restart Cursor (or the MCP server)
  • Databases will automatically connect
  • Use the connectionId (e.g., production, staging, local) when asking the AI to query databases

Option B: Manual Connection

You can connect to databases on-demand by asking the AI to use the connect_database tool with your connection string.

Step 4: Using the MCP Server

Once configured, you can use the MCP server by simply asking Cursor's AI to interact with your databases. Here are some examples:

Example 1: List Tables

"List all tables in the production database"

The AI will use the list_tables tool with connectionId: "production"

Example 2: Query Data

"Show me the top 10 customers from the production database"

The AI will execute a SQL query like: ``sql SELECT TOP 10 * FROM Customers ORDER BY ... ``

Example 3: Get Table Structure

"What columns are in the Orders table in production?"

The AI will use get_table_structure to show you the table schema

Example 4: Generate a Report

"Generate a report of all orders from 2024 in the production database"

Example 5: Create a Chart

"Create a bar chart showing monthly sales from the Sales table in production, using Month as labels and Revenue as data"

Example 6: Export Data

"Export the Customers table from production as CSV"

Available Tools

The MCP server provides these tools (you can ask the AI to use them):

  1. list_connections - See all connected databases
  2. list_tables - List all tables in a database
  3. get_table_structure - Get detailed table schema
  4. list_schemas - List all database schemas
  5. execute_query - Run SQL queries (SELECT, INSERT, UPDATE, DELETE, EXEC)
  6. generate_report - Generate data reports from tables
  7. generate_chart_report - Create charts (bar, line, pie, scatter, doughnut, area)
  8. export_report - Export data as JSON or CSV
  9. connect_database - Manually connect to a database
  10. disconnect_database - Disconnect from a database

Features

  • Multiple Database Connections: Connect to multiple SQL Server databases simultaneously
  • Automatic Connection: Auto-connect databases from .env file on startup
  • SQL Query Execution: Run any SQL query (SELECT, INSERT, UPDATE, DELETE, EXEC)
  • Schema Exploration: Browse tables, columns, and database structure
  • Report Generation: Create data reports with filtering and limits
  • Chart Visualization: Generate various chart types (bar, line, pie, etc.)
  • Data Export: Export data in JSON or CSV formats
  • Security: SQL injection protection and input validation

Troubleshooting

MCP Server Not Loading

  • Verify the path in Cursor settings is correct
  • Make sure you've run npm run build
  • Check Cursor's MCP server logs (usually in the output panel)

Connection Errors

  • Verify your connection string format is correct
  • Check that your SQL Server is accessible
  • Ensure credentials are correct
  • Try connecting manually first using connect_database tool

Database Not Found

  • If using .env file, make sure variable names start with DATABASE_URL_
  • Restart Cursor after adding databases to .env
  • Check that the connectionId matches (e.g., DATABASE_URL_prod → use prod as connectionId)

Query Errors

  • Verify your SQL syntax is correct
  • Check that you have proper database permissions
  • Ensure table and column names are spelled correctly

Development

Project Structure

SQLMCP/
├── src/
│   ├── server/          # MCP server implementation
│   ├── database/        # Database connection management
│   ├── reporting/       # Report generation
│   └── charts/          # Chart generation
├── dist/                # Compiled JavaScript
├── prisma/              # Prisma schema
└── package.json

Building

npm run build

Development Mode

npm run dev

Security Notes

  • Connection strings in .env files should be kept secure
  • Never commit .env files to version control
  • All SQL inputs are validated to prevent injection attacks
  • Only SELECT, INSERT, UPDATE, DELETE, and EXEC statements are allowed
  • Table and column names are sanitized

License

MIT

"# SQL-MCP"

Related MCP servers

Browse all →