Enables environment variable management for configuring database connections and server settings through .env files.
Provides secure database query services for MySQL databases, enabling table structure inspection, SQL query execution, and data retrieval with built-in security features to prevent SQL injection.
Mentioned as the author's work - a workflow automation template site that collects ready-to-use automation workflows.
Uses Python as the implementation language, requiring Python 3.10+ for running the MCP server.
Text-to-SQL MCP 服务器
一个基于 FastMCP 框架的安全数据库查询服务,用于数据库查询和分析。
🚀 项目简介
本项目是一个 MCP (Model Context Protocol) 服务器,专门用于提供安全的数据库查询服务。它基于 FastMCP 框架构建,支持 MySQL 数据库连接,并提供了完整的权限管理和安全控制机制。
✨ 主要功能
- 数据库连接管理: 安全的 MySQL 数据库连接和查询
- 权限认证: 基于 RSA 密钥对的 Bearer Token 认证
- 安全查询: 防止 SQL 注入和危险操作的安全检查
- 表结构查询: 获取数据库表列表和表结构信息
- SQL 执行: 安全的 SQL 查询执行,支持结果限制
- 健康检查: 服务状态监控
- 权限管理: 细粒度的权限控制
🛠️ 技术栈
- Python 3.x: 主要编程语言
- FastMCP: MCP 服务器框架
- MySQL: 数据库系统
- uvicorn: ASGI 服务器
- python-dotenv: 环境变量管理
📦 安装依赖
依赖包说明
python >=3.10
fastmcp==2.10.6
: MCP 服务器框架python-dotenv==1.1.0
: 环境变量加载mysql-connector-python==8.2.0
: MySQL 数据库连接器uvicorn==0.24.0
: ASGI 服务器
⚙️ 配置
1. 环境变量配置
复制 .env.example
文件为 .env
并配置数据库连接信息:
编辑 .env
文件:
2. 数据库初始化
使用提供的 dataset.sql
文件初始化示例数据:
该文件包含了一个 contracts
表的示例数据,包含合同信息。
🚀 启动服务
服务启动后会显示:
MCP客户端配置
🔧 API 工具
1. 健康检查
- 工具名:
health_check
- 描述: 检查服务和数据库连接状态
- 权限: 无需特殊权限
2. 获取用户权限
- 工具名:
get_user_permissions
- 描述: 获取当前用户的权限信息
- 权限: 需要有效的访问令牌
3. 获取数据库表列表
- 工具名:
get_database_tables
- 描述: 获取数据库中所有表的列表
- 权限: 需要
data:read_tables
权限
4. 获取表结构
- 工具名:
get_table_structure
- 参数:
table_name
(string): 表名
- 描述: 获取指定表的结构信息、行数和样本数据
- 权限: 需要
data:read_tables
权限
5. 执行 SQL 查询
- 工具名:
execute_sql_query
- 参数:
sql_query
(string): SQL 查询语句limit
(int, 可选): 返回结果的最大行数,默认 100
- 描述: 安全执行 SQL 查询
- 权限: 需要
data:read_table_data
权限 - 安全限制: 禁止执行 DROP, DELETE, UPDATE, INSERT 等修改操作
🔐 安全特性
权限系统
- 基于 RSA 密钥对的 JWT Token 认证
- 细粒度权限控制:
data:read_tables
: 读取表结构权限data:read_table_data
: 读取表数据权限
安全检查
- SQL 注入防护: 禁止危险的 SQL 操作
- 查询限制: 自动添加 LIMIT 限制,防止大量数据查询
- 敏感数据保护: 对包含敏感关键词的查询进行额外权限检查
📁 项目结构
🔍 核心模块说明
mcp_server.py
主服务器文件,包含:
- MCP 服务器初始化
- 所有 API 工具的定义
- 权限验证逻辑
- 安全检查机制
database.py
数据库管理模块,提供:
- MySQL 数据库连接管理
- SQL 查询执行
- 表结构信息获取
- 数据类型转换
auth_token.py
认证模块,负责:
- RSA 密钥对生成
- JWT 访问令牌创建
- 认证提供者配置
🚨 注意事项
- 数据库安全: 确保数据库用户权限最小化,只授予必要的查询权限
- 网络安全: 生产环境建议使用 HTTPS 和防火墙保护
- 令牌管理: 定期更新访问令牌,避免长期使用同一令牌
- 查询监控: 监控查询性能,避免复杂查询影响数据库性能
🤝 贡献
欢迎提交 Issue 和 Pull Request 来改进这个项目。
我的作品
n8n 工作流自动化模版站,收集了大量可直接使用的自动化流程:https://n8ntemplates.dev
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
A secure database query service built on FastMCP framework that allows users to query MySQL databases using natural language, with comprehensive permission management and security controls.
Related MCP Servers
- -securityAlicense-qualityAn MCP server that integrates with MySQL databases, enabling secure read and write operations through LLM-driven interfaces with support for transaction handling and performance monitoring.Last updated -75114JavaScriptMIT License
- -securityFlicense-qualityA FastMCP server that provides natural language interaction with MS SQL databases, enabling users to query data, list tables, describe structures, and execute database operations through a conversational AI interface.Last updated -Python
- -securityFlicense-qualityAn MCP server that allows working with MySQL databases by providing tools for executing read-only SQL queries, getting table schemas, and listing database tables.Last updated -6852JavaScript
- -securityFlicense-qualityA FastMCP server that enables natural language querying of PostgreSQL databases through LLM integration, allowing users to generate SQL queries from plain English and visualize the results.Last updated -Python