# SQLx MCP Server
A Model Context Protocol (MCP) server implementation in Rust that provides comprehensive database management tools using SQLx.
[](https://www.rust-lang.org)
[](https://opensource.org/licenses/MIT)
## 🚀 Features
This server provides 6 powerful MCP tools for database management:
1. **🔍 get_database_info** - Get basic database information
2. **📋 list_tables** - List all tables with metadata (comments, row counts, etc.)
3. **🏗️ get_table_structure** - Get detailed table structure information
4. **📜 get_table_ddl** - Export table DDL (CREATE TABLE statements)
5. **👁️ execute_readonly_query** - Execute read-only SQL queries safely
6. **✏️ execute_write_query** - Execute write SQL operations
## 🗄️ Supported Databases
- **PostgreSQL** - Full support with native features
- **MySQL** - Complete functionality including engine-specific features
- **SQLite** - Comprehensive support for embedded database operations
## 🔧 Installation
### Prerequisites
- Rust 1.70 or later
- Git
### Build from Source
```bash
git clone https://github.com/yourusername/sqlx-mcp.git
cd sqlx-mcp
cargo build --release
```
## 🚀 Quick Start
### 1. Run as Standalone Server
```bash
# With environment variable
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
./target/release/sqlx-mcp
# With command line argument
./target/release/sqlx-mcp --database-url "postgresql://user:pass@localhost/mydb"
```
### 2. Integrate with Claude Desktop
Add to your Claude Desktop configuration:
**Windows (`%APPDATA%\Claude\claude_desktop_config.json`):**
```json
{
"mcpServers": {
"sqlx-mcp": {
"command": "D:\\path\\to\\sqlx-mcp\\target\\release\\sqlx-mcp.exe",
"args": ["--database-url", "postgresql://user:pass@localhost/mydb"]
}
}
}
```
**macOS/Linux:**
```json
{
"mcpServers": {
"sqlx-mcp": {
"command": "/path/to/sqlx-mcp/target/release/sqlx-mcp",
"args": ["--database-url", "postgresql://user:pass@localhost/mydb"]
}
}
}
```
## 🔗 Database URL Priority
The server resolves database connections with the following priority:
1. **User Input** (highest) - `database_url` parameter in tool calls
2. **Command Line** - `--database-url` argument
3. **Environment Variable** (lowest) - `DATABASE_URL`
This allows flexible connection management for multiple databases.
## 🛠️ Tool Usage Examples
### Get Database Information
```json
{
"name": "get_database_info",
"arguments": {
"database_url": "postgresql://user:pass@localhost/mydb"
}
}
```
### List Tables with Metadata
```json
{
"name": "list_tables",
"arguments": {
"database_url": "postgresql://user:pass@localhost/mydb"
}
}
```
### Export Table DDL
```json
{
"name": "get_table_ddl",
"arguments": {
"database_url": "postgresql://user:pass@localhost/mydb",
"table_name": "users"
}
}
```
### Execute Safe Read-Only Queries
Supports SELECT, WITH (CTE), SHOW, DESCRIBE, EXPLAIN:
```json
{
"name": "execute_readonly_query",
"arguments": {
"database_url": "postgresql://user:pass@localhost/mydb",
"query": "WITH recent_users AS (SELECT * FROM users WHERE created_at > '2024-01-01') SELECT count(*) FROM recent_users"
}
}
```
## 🔒 Security Features
- **Query Validation**: Strict read-only query enforcement
- **SQL Injection Protection**: Parameterized queries where possible
- **Connection Security**: Supports SSL/TLS encrypted connections
- **Access Control**: Respects database user permissions
## 🏗️ Architecture
Built with modern Rust ecosystem:
- **rmcp** - Rust MCP SDK for protocol compliance
- **SQLx** - Async SQL toolkit with compile-time checked queries
- **Tokio** - Async runtime for high performance
- **Serde** - Serialization framework for JSON handling
- **Tracing** - Structured logging and debugging
## 📚 Documentation
- [Detailed Usage Guide](README.md)
- [DDL Export Feature](DDL_EXPORT_FEATURE.md)
- [List Tables Feature](LIST_TABLES_FEATURE.md)
- [Improvements Summary](IMPROVEMENTS_SUMMARY.md)
- [Deployment Guide](DEPLOYMENT_SUMMARY.md)
## 🤝 Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## 📄 License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## 🙏 Acknowledgments
- [Model Context Protocol](https://modelcontextprotocol.io/) for the protocol specification
- [SQLx](https://github.com/launchbadge/sqlx) for the excellent SQL toolkit
- [rmcp](https://github.com/modelcontextprotocol/rust-mcp-sdk) for the Rust MCP SDK
## 📞 Support
- Create an [Issue](https://github.com/yourusername/sqlx-mcp/issues) for bug reports
- Start a [Discussion](https://github.com/yourusername/sqlx-mcp/discussions) for questions
- Check the [Documentation](README.md) for detailed guides
---
Made with ❤️ and 🦀 (Rust)