1.0.1 • Published 6 months ago

@albertsnowden/sql-mcp-server v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@albertsnowden/sql-mcp-server

npm version License: MIT

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-server

3. 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-server

Local Project Installation

npm install @albertsnowden/sql-mcp-server

Using 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:

ToolDescriptionParameters
9f1_list_tablesGet all available tablesschema? (optional)
9f1_describe_tableGet table schema and relationshipstable_name
9f1_execute_queryRun SELECT queries safelyquery, max_rows?, timeout?
9f1_get_sample_dataFetch representative data samplestable_name, limit?
9f1_validate_queryCheck query syntax and permissionsquery
9f1_search_schemaFind tables/columns by namesearch_term

🔐 Environment Variables

VariableDescriptionDefaultRequired
DB_TYPEDatabase type (postgresql, mysql, sqlite)postgresql
DB_HOSTDatabase hostlocalhost
DB_PORTDatabase port5432
DB_NAMEDatabase name-
DB_USERDatabase user-
DB_PASSWORDDatabase password-
LOG_LEVELLogging level (error, warn, info, debug)info
MAX_QUERY_RESULTSMaximum rows returned10000
QUERY_TIMEOUTQuery timeout in seconds30

💡 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

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests: npm test
  5. Commit: git commit -m 'Add amazing feature'
  6. Push: git push origin feature/amazing-feature
  7. 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.