# SQLx MCP Server
A Model Context Protocol (MCP) server implementation in Rust that provides database tools using SQLx.
## Features
This server provides the following MCP tools:
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 table structure information
4. **get_table_ddl** - Get table DDL (CREATE TABLE statement)
5. **execute_readonly_query** - Execute read-only SQL queries
6. **execute_write_query** - Execute write SQL queries
## Supported Databases
- PostgreSQL
- MySQL
- SQLite
## Installation
1. Clone this repository
2. Build the project:
```bash
cargo build --release
```
## Usage
### As a standalone MCP server
Run the server using standard I/O transport:
```bash
cargo run --release
```
### With Claude Desktop
1. Build the server:
```bash
cargo build --release
```
2. Add to your Claude Desktop configuration (`claude_desktop_config.json`):
**Windows:**
```json
{
"mcpServers": {
"sqlx-mcp": {
"command": "path/to/sqlx-mcp/target/release/sqlx-mcp.exe",
"args": []
}
}
}
```
**macOS/Linux:**
```json
{
"mcpServers": {
"sqlx-mcp": {
"command": "path/to/sqlx-mcp/target/release/sqlx-mcp",
"args": []
}
}
}
```
3. Restart Claude Desktop
### Tool Usage Examples
Once connected, you can use the tools in Claude:
#### Get Database Information
```
get_database_info {"database_url": "postgresql://user:password@localhost/dbname"}
```
#### List Tables with Metadata
获取所有表的详细metadata,包括注释、行数、创建时间等:
```
list_tables {"database_url": "postgresql://user:password@localhost/dbname"}
```
#### Get Table Structure
```
get_table_structure {
"database_url": "postgresql://user:password@localhost/dbname",
"table_name": "users"
}
```
#### Get Table DDL
获取表的完整DDL定义(CREATE TABLE语句):
```
get_table_ddl {
"database_url": "postgresql://user:password@localhost/dbname",
"table_name": "users"
}
```
#### Execute Read-only Query
支持 SELECT、WITH (CTE)、SHOW、DESCRIBE、EXPLAIN 等安全查询:
```
execute_readonly_query {
"database_url": "postgresql://user:password@localhost/dbname",
"query": "WITH recent_users AS (SELECT * FROM users WHERE created_at > '2024-01-01') SELECT count(*) FROM recent_users"
}
```
#### Execute Write Query
```
execute_write_query {
"database_url": "postgresql://user:password@localhost/dbname",
"query": "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')"
}
```
## 🔧 数据库URL优先级
系统按以下优先级解析数据库连接:
1. **用户输入** - 工具调用时提供的 `database_url` 参数 (最高优先级)
2. **命令行参数** - 启动时的 `--database-url` 参数
3. **环境变量** - `DATABASE_URL` 环境变量 (最低优先级)
这种设计让您可以:
- 设置默认数据库连接(环境变量或命令行)
- 在需要时连接不同的数据库(工具参数)
- 在同一服务器实例中操作多个数据库
**启动示例:**
```bash
# 使用环境变量
export DATABASE_URL="postgresql://user:pass@localhost/db"
./target/release/sqlx-mcp
# 使用命令行参数
./target/release/sqlx-mcp --database-url "postgresql://user:pass@localhost/db"
# 工具调用时可以覆盖默认连接
get_database_info {"database_url": "mysql://user:pass@remote/other_db"}
```
## Database URL Format
### PostgreSQL
```
postgresql://username:password@host:port/database
```
### MySQL
```
mysql://username:password@host:port/database
```
### SQLite
```
sqlite:///path/to/database.db
```
## Development
### Running in Development Mode
```bash
cargo run
```
### Testing with MCP Inspector
You can test the server using the MCP Inspector:
```bash
npx @modelcontextprotocol/inspector
```
Then connect to your running server.
## Dependencies
- `rmcp` - Rust MCP SDK
- `sqlx` - Async SQL toolkit
- `tokio` - Async runtime
- `serde` - Serialization framework
- `tracing` - Logging
- `anyhow` - Error handling
## License
This project is licensed under the MIT License.
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request
## Troubleshooting
### Connection Issues
- Ensure your database is running and accessible
- Verify the database URL format is correct
- Check that the database user has appropriate permissions
### Tool Errors
- Make sure the database URL is provided in the correct format
- For write operations, ensure the user has write permissions
- Check the SQL syntax for query operations