@albertsnowden/sql-mcp-server v1.0.1
@albertsnowden/sql-mcp-server
A comprehensive Model Context Protocol (MCP) server that enables Large Language Models to safely interact with SQL databases. This server provides secure database access, schema introspection, and intelligent query execution with built-in safety measures.
Perfect for integration with VS Code MCP extension, Claude Desktop, and other MCP-compatible clients.
🚀 Quick Install
npm install -g @albertsnowden/sql-mcp-server✨ Features
- 🔒 Security First: Role-based access control, query validation, and SQL injection prevention
- 🗃️ Multi-Database Support: PostgreSQL, MySQL, SQLite, and SQL Server
- 🤖 LLM Optimized: Rich context provision and natural language query support
- ⚡ High Performance: Connection pooling, query caching, and optimization
- 📊 Monitoring: Comprehensive logging, metrics, and error tracking
- 🛡️ Production Ready: Rate limiting, authentication, and audit trails
- 🔌 VS Code Integration: Seamless integration with VS Code MCP extension
🔧 VS Code Setup
1. Install the MCP Extension
Install the MCP extension for VS Code from the marketplace.
2. Install the SQL Server
npm install -g @albertsnowden/sql-mcp-server3. Configure VS Code Settings
Add this to your VS Code settings.json:
{
"mcp.servers": {
"sql-server": {
"command": "sql-mcp-server",
"env": {
"DB_TYPE": "postgresql",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_NAME": "your_database",
"DB_USER": "your_user",
"DB_PASSWORD": "your_password"
}
}
}
}4. Alternative: Use Configuration File
Create a .env file in your workspace:
DB_TYPE=postgresql
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database
DB_USER=your_user
DB_PASSWORD=your_password📦 Installation Options
Global Installation (Recommended for VS Code)
npm install -g @albertsnowden/sql-mcp-serverLocal Project Installation
npm install @albertsnowden/sql-mcp-serverUsing npx (No Installation Required)
npx @albertsnowden/sql-mcp-server🗃️ Database Setup
Create a dedicated user for the MCP server with limited permissions:
PostgreSQL
CREATE USER mcp_user WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE your_database TO mcp_user;
GRANT USAGE ON SCHEMA public TO mcp_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_user;MySQL
CREATE USER 'mcp_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'mcp_user'@'localhost';
FLUSH PRIVILEGES;🛠️ MCP Tools Available
The server exposes these MCP tools for LLM interaction:
| Tool | Description | Parameters |
|---|---|---|
9f1_list_tables | Get all available tables | schema? (optional) |
9f1_describe_table | Get table schema and relationships | table_name |
9f1_execute_query | Run SELECT queries safely | query, max_rows?, timeout? |
9f1_get_sample_data | Fetch representative data samples | table_name, limit? |
9f1_validate_query | Check query syntax and permissions | query |
9f1_search_schema | Find tables/columns by name | search_term |
🔐 Environment Variables
| Variable | Description | Default | Required |
|---|---|---|---|
DB_TYPE | Database type (postgresql, mysql, sqlite) | postgresql | ✅ |
DB_HOST | Database host | localhost | ✅ |
DB_PORT | Database port | 5432 | ✅ |
DB_NAME | Database name | - | ✅ |
DB_USER | Database user | - | ✅ |
DB_PASSWORD | Database password | - | ✅ |
LOG_LEVEL | Logging level (error, warn, info, debug) | info | ❌ |
MAX_QUERY_RESULTS | Maximum rows returned | 10000 | ❌ |
QUERY_TIMEOUT | Query timeout in seconds | 30 | ❌ |
💡 Usage Examples
Ask Natural Language Questions
Once configured in VS Code, you can ask your AI assistant questions like:
- "Show me all tables in the database"
- "What's the structure of the users table?"
- "How many orders were placed last month?"
- "Find customers who haven't placed orders"
Direct MCP Protocol Usage
{
"method": "tools/call",
"params": {
"name": "9f1_execute_query",
"arguments": {
"query": "SELECT COUNT(*) FROM users WHERE created_at > NOW() - INTERVAL '30 days'"
}
}
}🔒 Security Features
- Query Validation: Prevents dangerous SQL operations (DROP, ALTER, etc.)
- Parameter Binding: All queries use parameterized statements
- Rate Limiting: Configurable request limits per client
- Audit Logging: All queries and access attempts are logged
- Connection Security: SSL/TLS database connections supported
- Read-Only by Default: Only SELECT operations allowed unless explicitly configured
🚀 Production Deployment
Docker
FROM node:18-alpine
RUN npm install -g @albertsnowden/sql-mcp-server
CMD ["sql-mcp-server"]Systemd Service
[Unit]
Description=MCP SQL Server
After=network.target
[Service]
Type=simple
User=mcp
ExecStart=/usr/bin/sql-mcp-server
Environment=DB_TYPE=postgresql
Environment=DB_HOST=localhost
Restart=always
[Install]
WantedBy=multi-user.target🤝 Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
npm test - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Submit a pull request
📋 Changelog
v1.0.0
- Initial release
- Support for PostgreSQL, MySQL, SQLite
- VS Code MCP integration
- Comprehensive security features
- Production-ready deployment options
📄 License
MIT License - see LICENSE file for details.
🆘 Support
🙏 Acknowledgments
Built with the Model Context Protocol SDK and inspired by the need for secure, intelligent database access for LLMs.