text-to-sql
一个基于 FastMCP 框架的安全数据库查询 MCP 服务,用于数据库查询和分析。
claude mcp add --transport stdio tmstack-text-to-sql-mcp-server python mcp_server.py \ --env DB_HOST="localhost" \ --env DB_NAME="your_database" \ --env DB_PORT="3306" \ --env DB_USER="your_username" \ --env MCP_HOST="127.0.0.1" \ --env MCP_PORT="8000" \ --env DB_PASSWORD="your_password"
How to use
This MCP server provides a secure text-to-SQL interface for querying a MySQL database. Built on the FastMCP framework, it exposes a set of API tools that allow you to check service health, inspect user permissions, list and inspect database tables, and execute safe SQL queries. Authentication is handled via RSA-based JWT Bearer tokens, and each tool enforces granular permissions to protect sensitive data. Available tools include: health_check (service and database status), get_user_permissions (current user permissions), get_database_tables (list all tables), get_table_structure (structure, row count, and sample data for a table), execute_sql_query (safely run SQL with automatic LIMIT enforcement), generate_sql_from_question (natural language to SQL generator), and analyze_query_result (inspect results). To use these tools, obtain a valid Bearer token, include it in the Authorization header, and supply required parameters (for example, table_name for table structure or sql_query for execute_sql_query).
How to install
Prerequisites:
- Python 3.10+ installed on the host
- MySQL server reachable from the host
- Network access to bind MCP (default 127.0.0.1:8000 can be changed in .env)
- Clone or download the repository containing the MCP server.
- Create and activate a Python virtual environment (optional but recommended): python -m venv venv source venv/bin/activate # on Unix/macOS venv\Scripts\activate.bat # on Windows
- Install dependencies from requirements.txt: pip install -r requirements.txt
- Copy the example environment file and customize connection details:
cp .env.example .env
Edit .env to set DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, MCP_HOST, MCP_PORT as needed
- Initialize the MySQL database if needed using dataset.sql (optional example data): mysql -u your_username -p your_database < dataset.sql
- Start the MCP server: python mcp_server.py
- Verify the server is running by visiting the configured address, e.g. http://127.0.0.1:8000, and using the provided tools.
Additional notes
Tips and considerations:
- Ensure the database user has the minimum required privileges for read operations and any specific queries you plan to run.
- Use the environment variables in .env to customize host/port and security settings; consider enabling HTTPS and firewall rules for production.
- Tokens should be rotated regularly; keep a robust key management process for RSA keys used to sign JWTs.
- The server automatically applies LIMIT to SQL queries to prevent large data dumps; construct queries accordingly and request specific columns when possible.
- If you encounter connection failures, verify network reachability between the MCP server and MySQL and confirm that DB_HOST/DB_PORT are correct.
- Review and adjust permissions in your application to enforce data access controls at the tool level (data:read_tables, data:read_table_data, etc.).
Related MCP Servers
mcp-vegalite
MCP server from isaacwasserman/mcp-vegalite-server
github-chat
A Model Context Protocol (MCP) for analyzing and querying GitHub repositories using the GitHub Chat API.
nautex
MCP server for guiding Coding Agents via end-to-end requirements to implementation plan pipeline
pagerduty
PagerDuty's official local MCP (Model Context Protocol) server which provides tools to interact with your PagerDuty account directly from your MCP-enabled client.
futu-stock
mcp server for futuniuniu stock
mcp -boilerplate
Boilerplate using one of the 'better' ways to build MCP Servers. Written using FastMCP