Sponsored by Deepsite.site

MySQL MCP Server

Created By
OTA-EITA9 months ago
Content

MySQL MCP Server

このプロジェクトは、MCP (Model Context Protocol) を使用してMySQLデータベースとやり取りするためのサーバーを提供します。WSL環境でMySQLデータベースにアクセスするためのインターフェースを提供します。

機能

  • MySQLデータベース内のテーブル一覧の取得
  • テーブルからのデータ読み取り(最大100行)
  • SQLクエリの実行(SELECT、SHOW TABLES、INSERT、UPDATE、DELETEなど)

プロジェクト構造

src/mysql_mcp_server/
├── __init__.py        # パッケージ初期化
├── config/           # 設定関連モジュール
│   ├── __init__.py  
│   └── settings.py   # データベース設定
├── database/         # データベース操作モジュール
│   ├── __init__.py  
│   ├── connection.py # DB接続処理
│   └── errors.py     # エラークラス
├── tools/            # ツールモジュール
│   ├── __init__.py  
│   └── sql_tools.py  # SQL実行ツール
└── server.py         # MCPサーバーメイン

インストールと実行

前提条件

  • Python 3.11以上
  • MySQLサーバー(ローカルまたはリモート)
  • 仮想環境(virtualenv)
  • Docker(Dockerを使用する場合)

セットアップ

  1. リポジトリをクローン:

    git clone <repository-url>
    cd mysql_mcp_server
    
  2. 仮想環境を作成し、アクティベート:

    python -m venv .venv
    source .venv/bin/activate  # Linuxの場合
    
  3. 依存関係をインストール:

    pip install -r requirements.txt .
    
  4. セットアップスクリプトを実行して、実行スクリプトに実行権限を付与:

    chmod +x setup.sh
    ./setup.sh
    

実行方法

以下のいずれかの方法でサーバーを実行できます:

1. 直接実行

./run_direct.sh

2. モジュールとして実行

./run_module.sh

3. Dockerコンテナで実行

# Dockerイメージをビルド
docker build -t mysql-mcp-server:latest .

# コンテナを実行
./run_docker.sh

環境変数

サーバーは以下の環境変数を使用してMySQLに接続します:

  • MYSQL_HOST: MySQLサーバーのホスト(デフォルト: host.docker.internal
  • MYSQL_PORT: MySQLサーバーのポート(デフォルト: 13306
  • MYSQL_USER: MySQLユーザー名
  • MYSQL_PASSWORD: MySQLパスワード
  • MYSQL_DATABASE: 使用するデータベース名

MCPプロトコル

このサーバーはMCP(Model Control Protocol)を実装しており、以下のエンドポイントを提供します:

  • list_resources: データベース内のテーブル一覧を取得
  • read_resource: 特定のテーブルからデータを読み取り
  • list_tools: 利用可能なツール(SQLクエリの実行)を一覧表示
  • call_tool: SQLクエリを実行

WSL連携

WSL(Windows Subsystem for Linux)環境内のMySQLサーバーに接続する場合:

  1. WSL内のMySQLサーバーが起動していることを確認
  2. WSL内のMySQLがリモート接続を許可するように設定されていることを確認
    • /etc/mysql/mysql.conf.d/mysqld.cnfファイルでbind-addressを0.0.0.0に変更
    • 使用するMySQLユーザーにリモートからのアクセス権限を付与
  3. Windows側のファイアウォールがMySQLポート(デフォルト13306)への接続を許可していることを確認

開発

Noxセッション

このプロジェクトは、テスト、フォーマット、リントのためのNoxセッションを提供しています。

  1. Noxをインストール:

    pip install nox
    
  2. 全てのテストを実行:

    nox -s test
    
  3. コードをフォーマット:

    nox -s format
    
  4. リントチェックを実行:

    nox -s lint
    

直接テストを実行

pytest tests/

コードカバレッジを確認

pytest --cov=src.mysql_mcp_server tests/

ライセンス

このプロジェクトはMITライセンスの下で公開されています。

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Tavily Mcp
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
DeepChatYour AI Partner on Desktop
WindsurfThe new purpose-built IDE to harness magic
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
ChatWiseThe second fastest AI chatbot™
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
CursorThe AI Code Editor
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Amap Maps高德地图官方 MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Playwright McpPlaywright MCP server
Serper MCP ServerA Serper MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.