MariaDB MCP Server

MariaDB MCP Server

Enables AI assistants to interact with MariaDB databases through schema exploration, query execution, and database statistics. Includes security features like read-only mode, parameterized queries, and connection pooling with support for both JSON and Markdown output formats.

Category
访问服务器

README

MariaDB MCP Server

A powerful Model Context Protocol (MCP) server that enables AI assistants like Claude to interact with your MariaDB databases. This server provides comprehensive database access with schema exploration, query execution, and advanced features.

Features

  • 🔍 Schema Exploration: Browse databases, tables, and detailed schema information
  • 📊 Query Execution: Execute both read-only and write queries (configurable)
  • 📄 Multiple Output Formats: JSON for programmatic use, Markdown for human readability
  • 📑 Pagination Support: Handle large result sets efficiently
  • 🔒 Security Features: Parameterized queries, SQL injection prevention, read-only mode
  • 🏊 Connection Pooling: Efficient database connection management
  • Performance: Query timeouts, result size limits, and optimized response formatting

Installation

Prerequisites

  1. Python 3.8+ installed on your system
  2. MariaDB Connector/C (required for the Python MariaDB connector)

Installing MariaDB Connector/C

On macOS:

brew install mariadb-connector-c
export MARIADB_CONFIG=$(brew --prefix mariadb-connector-c)/bin/mariadb_config

On Ubuntu/Debian:

sudo apt-get update
sudo apt-get install libmariadb-dev

On RHEL/CentOS/Fedora:

sudo yum install mariadb-connector-c-devel

On Windows: Download and install from MariaDB Connector/C downloads

Install Dependencies

pip install -r requirements.txt

Or install packages individually:

pip install mcp mariadb

Configuration

Environment Variables

Configure the server using these environment variables:

# Required - Database Connection
export MARIADB_HOST="localhost"           # Database server hostname
export MARIADB_PORT="3306"               # Database server port
export MARIADB_USER="your_username"      # Database username
export MARIADB_PASSWORD="your_password"  # Database password
export MARIADB_DATABASE=""               # Default database (optional)

# Optional - Security & Performance
export MARIADB_READ_ONLY="true"         # Set to "false" to allow write queries
export MARIADB_POOL_SIZE="5"            # Connection pool size (default: 5)

Claude Desktop Configuration

Add this to your Claude Desktop configuration file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "mariadb": {
      "command": "python",
      "args": ["/path/to/mariadb_mcp_server.py"],
      "env": {
        "MARIADB_HOST": "localhost",
        "MARIADB_PORT": "3306",
        "MARIADB_USER": "your_username",
        "MARIADB_PASSWORD": "your_password",
        "MARIADB_DATABASE": "your_database",
        "MARIADB_READ_ONLY": "true"
      }
    }
  }
}

Alternative: Using uvx (Recommended for easy updates)

If you package and publish the server to PyPI:

{
  "mcpServers": {
    "mariadb": {
      "command": "uvx",
      "args": ["mariadb-mcp-server"],
      "env": {
        "MARIADB_HOST": "localhost",
        "MARIADB_PORT": "3306",
        "MARIADB_USER": "your_username",
        "MARIADB_PASSWORD": "your_password",
        "MARIADB_DATABASE": "your_database"
      }
    }
  }
}

Available Tools

The server provides the following tools:

1. list_databases

List all accessible databases on the MariaDB server.

Example usage in Claude:

  • "What databases are available?"
  • "Show me all databases"

2. list_tables

List all tables in a specific database with metadata.

Parameters:

  • database_name: Name of the database
  • response_format: "json" or "markdown" (default: "markdown")

Example usage in Claude:

  • "What tables are in the sales database?"
  • "List all tables in my_app database"

3. get_table_schema

Get detailed schema information for a specific table.

Parameters:

  • database_name: Name of the database
  • table_name: Name of the table
  • include_indexes: Include index information (default: true)
  • include_foreign_keys: Include foreign key relationships (default: true)
  • response_format: "json" or "markdown"

Example usage in Claude:

  • "Show me the schema for the users table"
  • "What columns does the orders table have?"

4. execute_query

Execute SQL queries against the database.

Parameters:

  • database_name: Database to query
  • query: SQL query to execute
  • parameters: Query parameters for parameterized queries (optional)
  • limit: Maximum rows to return (default: 100, max: 1000)
  • offset: Number of rows to skip for pagination (default: 0)
  • response_format: "json" or "markdown"

Example usage in Claude:

  • "Find all users who registered in the last month"
  • "Show me the top 10 selling products"
  • "Update the status of order 12345 to 'shipped'"

5. get_database_stats

Get statistics and metadata about a database.

Parameters:

  • database_name: Name of the database
  • response_format: "json" or "markdown"

Example usage in Claude:

  • "How large is the analytics database?"
  • "Show me statistics for the production database"

Security Features

Read-Only Mode

By default, the server runs in read-only mode, allowing only SELECT, SHOW, DESCRIBE, and EXPLAIN queries. To enable write operations:

export MARIADB_READ_ONLY="false"

SQL Injection Prevention

  • The server uses parameterized queries
  • SQL comments are automatically stripped
  • Query validation is performed before execution

Connection Security

  • Connection pooling with automatic reconnection
  • Configurable timeouts
  • Secure credential handling through environment variables

Usage Examples

Once configured, you can interact with your MariaDB database through Claude:

Example 1: Explore Database Structure

You: "What databases do I have access to?"
Claude: [Uses list_databases tool]

You: "Show me the tables in the ecommerce database"
Claude: [Uses list_tables tool]

You: "What's the structure of the customers table?"
Claude: [Uses get_table_schema tool]

Example 2: Query Data

You: "Find the top 5 customers by total purchase amount"
Claude: [Uses execute_query to run an aggregation query]

You: "Show me all orders from last month"
Claude: [Uses execute_query with date filtering]

Example 3: Data Analysis

You: "Analyze the sales trends for Q4"
Claude: [Uses multiple tools to explore schema and run analytical queries]

You: "What's the average order value by product category?"
Claude: [Uses execute_query with GROUP BY and aggregation]

Troubleshooting

Common Issues

  1. Connection Error: Verify your database credentials and that MariaDB is running
  2. MariaDB Connector/C Not Found: Ensure you've installed the connector and set MARIADB_CONFIG
  3. Permission Denied: Check that your database user has appropriate permissions
  4. Query Timeout: Complex queries may timeout; consider optimizing or increasing timeout

Debug Mode

To enable detailed logging:

# In the server file, change:
logging.basicConfig(level=logging.DEBUG)

Performance Optimization

Best Practices

  1. Use Indexes: Ensure your tables have appropriate indexes for common queries
  2. Limit Results: Always use LIMIT clauses for large tables
  3. Parameterized Queries: Use parameters instead of string concatenation
  4. Connection Pooling: Adjust MARIADB_POOL_SIZE based on your workload

Response Size Management

  • Results are automatically truncated if they exceed 25,000 characters
  • Use pagination (limit/offset) for large result sets
  • Choose JSON format for programmatic processing, Markdown for readability

Development

Testing the Server

# Test connection
python mariadb_mcp_server.py

# The server will wait for MCP commands on stdin
# Press Ctrl+C to exit

Extending the Server

To add new tools, follow this pattern:

@mcp.tool(
    name="your_tool_name",
    annotations={
        "title": "Human Readable Title",
        "readOnlyHint": True,
        "destructiveHint": False,
        "idempotentHint": True,
        "openWorldHint": True
    }
)
async def your_tool_function(params: YourInputModel) -> str:
    """Tool documentation"""
    # Implementation
    pass

License

MIT License - See LICENSE file for details

Support

For issues, questions, or contributions:

  1. Check the troubleshooting section
  2. Review environment variable configuration
  3. Ensure MariaDB Connector/C is properly installed
  4. Verify database permissions

Changelog

Version 1.0.0 (Current)

  • Initial release with core functionality
  • Schema exploration tools
  • Query execution with pagination
  • Multiple output formats
  • Security features and connection pooling

Roadmap

  • [ ] Transaction support for complex operations
  • [ ] Query history and caching
  • [ ] Advanced query builder assistance
  • [ ] Performance profiling tools
  • [ ] Backup and restore utilities
  • [ ] Vector search capabilities (MariaDB 11.7+)

Credits

Built with the MCP Python SDK (FastMCP) and MariaDB Connector/Python.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选