Cover image
Try Now
2025-04-14

3 years

Works with Finder

0

Github Watches

0

Github Forks

0

Github Stars

SQL MCP Server

This project provides a Python-based Model Context Protocol (MCP) server that allows a Large Language Model (LLM) to securely interact with SQL Server or PostgreSQL databases in a read-only capacity.

Features

  • Connects to PostgreSQL or SQL Server databases.
  • Provides MCP tools to:
    • get_schema: Fetch database schema (all tables or a specific table).
    • query_data: Execute read-only SELECT queries.
  • Enforces read-only access through query validation.

Setup

  1. Environment: This project uses uv for environment and dependency management. Ensure you have uv installed (pip install uv).

  2. Dependencies: The necessary dependencies have already been installed into the .venv directory using uv add. If you need to reinstall:

    uv sync
    
  3. Configuration:

    • Copy the example environment file:
      cp .env.example .env
      
    • Edit the .env file and set the DATABASE_URL variable to your database connection string. Examples are provided in the file for PostgreSQL and SQL Server (using pyodbc).
    • Choose the appropriate SQLAlchemy driver prefix (postgresql+asyncpg, mssql+pyodbc, etc.) based on your database and installed driver.
    • Ensure you have the necessary database drivers installed (e.g., psycopg2-binary is included, but for SQL Server, you might need pyodbc and system-level ODBC drivers).
    • Configure the TRANSPORT (e.g., stdio or sse), HOST, and PORT as needed.

Usage

Development & Testing

You can run the server locally for testing using the MCP development tools:

# Activate the virtual environment (optional, uv run handles it)
# source .venv/bin/activate  # Linux/macOS
# .\.venv\Scripts\activate  # Windows

# Run with uv (recommended)
uv run mcp dev src/main.py

# Or run directly if environment is activated
# mcp dev src/main.py

This will start the MCP Inspector, allowing you to interact with the get_schema and query_data tools.

Running Standalone

You can also run the server directly using the configured transport (stdio or sse):

# Ensure .env is configured (especially TRANSPORT)
uv run python src/main.py

Integration with MCP Clients (e.g., Claude Desktop)

Refer to the mcp install command and the MCP client's documentation for integrating the server. You'll typically provide the command to run the server (using uv run python src/main.py or similar) and necessary environment variables.

Example Stdio Configuration (Conceptual):

{
  "mcpServers": {
    "sql-explorer": {
      "command": "uv",
      "args": ["run", "python", "c:/path/to/sql-mcp/src/main.py"],
      "envFiles": ["c:/path/to/sql-mcp/.env"],
      "env": {
          "TRANSPORT": "stdio"
          // DATABASE_URL will be picked from .env file
      }
    }
  }
}

Example SSE Configuration (Conceptual):

Ensure TRANSPORT=sse, HOST, and PORT are set in .env.

{
  "mcpServers": {
    "sql-explorer": {
      "transport": "sse",
      "url": "http://localhost:8051/sse" // Or configured HOST/PORT
    }
  }
}

相关推荐

  • av
  • Führen Sie mühelos LLM -Backends, APIs, Frontends und Dienste mit einem Befehl aus.

  • 1Panel-dev
  • 🔥 1Panel bietet eine intuitive Weboberfläche und einen MCP -Server, um Websites, Dateien, Container, Datenbanken und LLMs auf einem Linux -Server zu verwalten.

  • WangRongsheng
  • 🧑‍🚀 全世界最好的 llm 资料总结(数据处理、模型训练、模型部署、 O1 模型、 MCP 、小语言模型、视觉语言模型) | Zusammenfassung der weltbesten LLM -Ressourcen.

  • rulego
  • ⛓️Rugele ist ein leichter, leistungsstarker, leistungsstarker, eingebetteter Komponenten-Orchestrierungsregel-Motor-Rahmen für GO.

  • sigoden
  • Erstellen Sie einfach LLM -Tools und -Argarten mit einfachen Bash/JavaScript/Python -Funktionen.

  • hkr04
  • Leichtes C ++ MCP (Modellkontextprotokoll) SDK

  • RockChinQ
  • 😎简单易用、🧩丰富生态 - 大模型原生即时通信机器人平台 | 适配 qq / 微信(企业微信、个人微信) / 飞书 / 钉钉 / diskord / telegram / slack 等平台 | 支持 Chatgpt 、 Deepseek 、 Diffy 、 Claude 、 Gemini 、 xai 、 ppio 、 、 ulama 、 lm Studio 、阿里云百炼、火山方舟、 siliconflow 、 qwen 、 mondshot 、 chatglm 、 sillytraven 、 mcp 等 llm 的机器人 / agent | LLM-basierte Instant Messaging Bots-Plattform, unterstützt Zwietracht, Telegramm, Wechat, Lark, Dingtalk, QQ, Slack

  • dmayboroda
  • On-Premise-Konversationslappen mit konfigurierbaren Behältern

  • modelscope
  • Bauen Sie LLM-Multi-Agent-Anwendungen auf einfachere Weise auf.

  • paulwing
  • Ein Test -Repository, das mit MCP -Dienst erstellt wurde

    Reviews

    3.5 (2)
    Avatar
    user_hjB7bZ2r
    2025-04-23

    As a dedicated user of sql-mcp, I am thoroughly impressed by its performance and ease of use. This tool, developed by hmn53, offers seamless SQL management functionalities that streamline database interactions. Its user-friendly interface and comprehensive features have significantly improved my workflow. Highly recommended for anyone looking to enhance their SQL management experience!

    Avatar
    user_UV10gkXG
    2025-04-23

    As a dedicated user of the sql-mcp application, I find it incredibly reliable and efficient for managing SQL queries and databases. Developed by hmn53, this tool streamlines database operations and enhances productivity. Highly recommend it to anyone in need of a robust SQL management tool!