SQLx MCP Server

SQLx MCP Server

Provides comprehensive database management tools for PostgreSQL, MySQL, and SQLite databases. Enables querying table structures, executing read-only and write queries, exporting DDL statements, and managing database metadata through natural language.

Category
访问服务器

README

SQLx MCP Server

A Model Context Protocol (MCP) server implementation in Rust that provides comprehensive database management tools using SQLx.

Rust License: 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

🎯 Smart Configuration Management

  • Auto-Configuration Detection: Server automatically detects and reports database configuration at startup
  • Client Notification: Clients receive configuration status via MCP protocol initialization
  • Flexible URL Management: Optional database URL parameters when server is pre-configured
  • Secure Display: Database credentials are automatically masked for security

🗄️ Supported Databases

  • PostgreSQL - Full support with native features
  • MySQL - Complete functionality including engine-specific features
  • SQLite - Comprehensive support for embedded database operations

🔧 Installation

Option 1: NPM (Recommended)

Install globally via npm:

npm install -g @sqlx-mcp/sqlx-mcp

Or use directly with npx (no installation needed):

npx @sqlx-mcp/sqlx-mcp --database-url "postgresql://user:pass@localhost/mydb"

Option 2: Build from Source

Prerequisites

  • Rust 1.70 or later
  • Git

Build Steps

git clone https://github.com/lihongjie0209/sqlx-mcp.git
cd sqlx-mcp
cargo build --release

🚀 Quick Start

1. Run as Standalone Server

Using NPM/npx (Recommended):

# Install globally and run
npm install -g @sqlx-mcp/sqlx-mcp
sqlx-mcp --database-url "postgresql://user:pass@localhost/mydb"

# Or run directly with npx (no installation needed)
npx @sqlx-mcp/sqlx-mcp --database-url "postgresql://user:pass@localhost/mydb"

# With environment variable
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
npx @sqlx-mcp/sqlx-mcp

Using Built Binary:

# 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. The server will automatically notify Claude about the current database configuration status.

Using NPM Package (Recommended):

With Environment Variable (Recommended):

{
  "mcpServers": {
    "sqlx-mcp": {
      "command": "npx",
      "args": ["@sqlx-mcp/sqlx-mcp"],
      "env": {
        "DATABASE_URL": "postgresql://user:pass@localhost/mydb"
      }
    }
  }
}

With Command Line Argument:

{
  "mcpServers": {
    "sqlx-mcp": {
      "command": "npx",
      "args": ["@sqlx-mcp/sqlx-mcp", "--database-url", "postgresql://user:pass@localhost/mydb"]
    }
  }
}

Without Pre-configuration (require database_url in each tool call):

{
  "mcpServers": {
    "sqlx-mcp": {
      "command": "npx",
      "args": ["@sqlx-mcp/sqlx-mcp"]
    }
  }
}

Using Built Binary:

Windows (%APPDATA%\Claude\claude_desktop_config.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:

{
  "mcpServers": {
    "sqlx-mcp": {
      "command": "/path/to/sqlx-mcp/target/release/sqlx-mcp",
      "args": ["--database-url", "postgresql://user:pass@localhost/mydb"]
    }
  }
}

🔗 Database Configuration

The server provides intelligent database configuration management with automatic detection and client notification.

Configuration Priority

The server resolves database connections with the following priority:

  1. Tool Parameter (highest) - database_url parameter in individual tool calls
  2. Command Line - --database-url argument at server startup
  3. Environment Variable (lowest) - DATABASE_URL environment variable

Auto-Detection & Client Notification

When clients connect to the MCP server, they automatically receive configuration information:

  • Configuration Status: Whether a database is pre-configured
  • Connection Source: How the database was configured (command line, environment, or none)
  • Masked URL: Database connection string with credentials safely hidden
  • Usage Guidance: Whether database_url parameter is required in tool calls

Example Client Notifications:

No Configuration:

Current database configuration: No database configured. Please provide database_url parameter in tool calls or set DATABASE_URL environment variable.

Environment Variable Configuration:

Current database configuration: Database configured via environment variable: postgresql://user:***@localhost:5432/mydb

If a database is already configured, you can omit the database_url parameter in tool calls.

Command Line Configuration:

Current database configuration: Database configured via command line: mysql://root:***@localhost:3306/testdb

If a database is already configured, you can omit the database_url parameter in tool calls.

Flexible Usage Patterns

Pre-configured Server (Recommended)

# Set up database via environment variable
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
npx @sqlx-mcp/sqlx-mcp

# Or via command line
npx @sqlx-mcp/sqlx-mcp --database-url "postgresql://user:pass@localhost/mydb"

Then use tools without database_url parameter:

{
  "name": "list_tables",
  "arguments": {}
}

Per-Tool Database URLs

{
  "name": "list_tables", 
  "arguments": {
    "database_url": "postgresql://user:pass@different-host/other-db"
  }
}

This allows flexible connection management for multiple databases.

🛠️ Tool Usage Examples

Get Database Information

{
  "name": "get_database_info",
  "arguments": {
    "database_url": "postgresql://user:pass@localhost/mydb"  // Optional if server is pre-configured
  }
}

List Tables with Metadata

{
  "name": "list_tables",
  "arguments": {
    "database_url": "postgresql://user:pass@localhost/mydb"  // Optional if server is pre-configured
  }
}

Export Table DDL

{
  "name": "get_table_ddl",
  "arguments": {
    "database_url": "postgresql://user:pass@localhost/mydb",  // Optional if server is pre-configured
    "table_name": "users"
  }
}

Execute Safe Read-Only Queries

Supports SELECT, WITH (CTE), SHOW, DESCRIBE, EXPLAIN:

{
  "name": "execute_readonly_query",
  "arguments": {
    "database_url": "postgresql://user:pass@localhost/mydb",  // Optional if server is pre-configured
    "query": "WITH recent_users AS (SELECT * FROM users WHERE created_at > '2024-01-01') SELECT count(*) FROM recent_users"
  }
}

Note: When the server is pre-configured with a database (via command line or environment variable), the database_url parameter becomes optional in all tool calls. The server will notify clients about the current configuration status during MCP initialization.

🔒 Security Features

  • Query Validation: Strict read-only query enforcement for safe operations
  • SQL Injection Protection: Parameterized queries where possible
  • Connection Security: Supports SSL/TLS encrypted connections
  • Access Control: Respects database user permissions
  • Credential Protection: Database passwords automatically masked in logs and client notifications
  • Secure Configuration Display: Connection strings shown with sensitive information hidden

🏗️ 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

🔧 Development & Release

This project includes an advanced automated release system powered by AI:

Environment Setup

For Windows PowerShell users who want to use the automated release features:

# Quick setup using .env file (recommended)
Copy-Item ".env.template" ".env"
notepad .env  # Add your OPENROUTER_API_KEY

# Or set environment variable for current session
$env:OPENROUTER_API_KEY = "your-api-key-here"

For detailed PowerShell environment setup, see PowerShell Environment Setup Guide.

Release Management

The project includes a comprehensive release script with AI-powered commit message generation:

# Create automated release with AI-generated commit messages
npm run release

# The script will:
# - Bump version numbers
# - Generate intelligent commit messages using AI
# - Update package files
# - Create git commits and tags
# - Push to repository

Features:

  • 🤖 AI-powered commit message generation using OpenRouter
  • 📦 Automatic version management
  • 🔄 Multi-file synchronization (Cargo.toml ↔ package.json)
  • 🚀 Git automation (commit, tag, push)
  • 🖥️ Cross-platform support (Windows, macOS, Linux)

🤝 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 file for details.

🙏 Acknowledgments

📞 Support


Made with ❤️ and 🦀 (Rust)

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选