Used for managing configuration variables including database connection parameters and API keys through environment variables.
Supports version control configuration through .gitignore settings for managing project files.
Provides natural language to SQL conversion for MariaDB databases, supporting secure SELECT queries, schema analysis, and paginated results based on user descriptions.
Enables natural language querying of MySQL databases by automatically generating and executing SQL statements based on user prompts, with features for query security, pagination, and schema exploration.
Serves as the runtime environment for the MCP server, with specific version requirements (>= 20.9.0).
Supported as an alternative package manager for dependency installation and running server commands.
Recommended as the primary package manager for installing and managing project dependencies.
基于MCP与QWEN大模型的自然语言MySQL数据库查询系统
附件
项目概述
本项目是实现了一个基于Model Context Protocol (MCP) 和大语言模型的自然语言到SQL查询转换系统。用户可以通过自然语言描述查询需求,系统自动生成并执行对应的SQL语句,返回查询结果。
主要功能
- 🔍 自然语言转SQL: 支持通过自然语言描述生成精确的SQL查询
- 🛡️ 安全控制: 只允许SELECT查询,过滤敏感字段
- 📝 查询日志: 记录所有查询操作和时间戳
- 📄 分页支持: 长查询结果支持分页展示
- 🗄️ 表结构管理: 动态获取和过滤数据库schema
- 🤖 智能优化: 提供SQL优化建议和多轮对话支持
项目目录结构
环境要求
- Node.js: >= 20.9.0
- 数据库: MySQL 5.7+ 或 MariaDB 10.3+
- 包管理器: pnpm (推荐) 或 npm
配置说明
1. 环境变量配置
创建 .env
文件(如果不存在):
2. 数据库配置
确保你已经安装并配置了MySQL数据库,并创建了测试数据库(如college数据库)。
修改 src/mysql-mcp/config/database.js
中的数据库连接配置:
安装与运行
1. 安装依赖
2. 启动Mastra开发环境(终端2)
启动后,在浏览器中打开Web界面,地址为 http://localhost:4111
MCP服务器通过stdio输出,因此其并不会影响其它进程的MCP服务
如果你想要手动启动MCP:
服务器启动后会显示:
3. 使用系统
- 在Web界面中选择 "MySQL Agent"
- 输入自然语言查询,例如:
- "列出所有课程的名称,按标题和学分排序"
- "查找计算机科学系的所有课程"
- "显示选修人数最多的前5门课程"
- 系统会自动生成SQL、执行查询并返回结果
核心工具说明
MCP工具集
- mysqlQueryTool: 执行SELECT查询,支持安全过滤
- mysqlSchemaTool: 获取数据库表结构信息
- mysqlDdlTool: 获取表的DDL语句
- paginationTool: 分页查询支持
- queryLogTool: 查询日志记录和查看
安全特性
- 只允许SELECT查询语句
- 过滤包含敏感字段的查询(password、salary等)
- SQL注入防护机制
- 白名单关键字验证
This server cannot be installed
A Model Context Protocol (MCP) server that converts natural language queries into SQL statements, allowing users to query MySQL databases using conversational language instead of writing SQL code.
Related MCP Servers
- AsecurityAlicenseAqualityA Model Context Protocol (MCP) server implementation that provides database interaction and business intelligence capabilities through SQLite. This server enables running SQL queries, analyzing business data, and automatically generating business insight memos.Last updated -650,196PythonMIT License
- -securityAlicense-qualityA Model Context Protocol server that enables natural language queries to MySQL databases, powered by XiYanSQL text-to-SQL technology.Last updated -114PythonApache 2.0
- -securityAlicense-qualityA Model Context Protocol (MCP) server that enables AI assistants to interact with MySQL databases by executing SQL queries and checking database connectivity.Last updated -TypeScriptMIT License
- -security-license-qualityIntentionally vulnerable Model Context Protocol (MCP) server designed for security research that processes natural language queries through an LLM to execute SQL queries or shell commands without restrictions.Last updated -Python