MCP MySQL Server
The MCP MySQL Server provides a secure interface for managing MySQL databases, enabling comprehensive data operations and schema manipulation.
Database Connection: Connect using URL or separate host, user, password, and database parameters
Data Operations: Execute SELECT, INSERT, UPDATE, and DELETE queries to retrieve and modify data
Schema Management: List tables, retrieve detailed table structure information, create new tables with defined fields and indexes, and add columns to existing tables
Security & Monitoring: Validate SQL statements, prevent dangerous operations (DROP, TRUNCATE, ALTER), protect against SQL injection, and monitor database connection status
Supports configuration through environment variables or .env files for setting database connection parameters and service options.
Uses Express as the web server foundation with added security features through helmet and cors middleware, including rate limiting.
Provides tools for executing SQL queries, retrieving database table structures, checking connection status, and managing data safely with validation against dangerous operations and SQL injection.
Built for Node.js runtime (version 18+) for cross-platform database connectivity and service management.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MCP MySQL Server show me the top 10 customers by total purchases"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MCP MySQL Server
项目简介
MCP MySQL Server 是一个基于 @modelcontextprotocol/sdk 的 MySQL 工具服务,支持 SQL 查询、表结构获取、连接检测等功能,适用于 AI 代理、自动化工具等场景。
Related MCP server: MySQL MCP Server
主要功能
执行 SQL 查询(SELECT、INSERT、UPDATE、DELETE)
获取数据库表结构信息
检测数据库连接状态
SQL 语句和参数安全校验
优雅的服务启动与关闭
架构说明
入口文件:
src/index.js启动和管理 MCP 服务器实例服务实现:
src/server.js实现 MCP Server,注册工具、处理请求数据库管理:
src/database.js管理 MySQL 连接池、执行 SQL、获取表结构配置管理:
src/config.js支持 .env 环境变量和默认配置安全校验:
src/validators.js校验 SQL 语句和参数,防止危险操作和注入
安装与使用
克隆项目
git clone https://github.com/yourname/mcp-mysql-server.git cd mcp-mysql-server安装依赖
npm install配置数据库连接(可选,支持 .env 文件)
DB_HOST=localhost DB_PORT=3306 DB_USER=root DB_PASSWORD=yourpassword DB_NAME=yourdatabase启动服务
node src/index.js
配置说明
支持通过环境变量或
.env文件配置数据库和服务参数主要配置项见
src/config.js
主要实现细节
使用
mysql2/promise实现高效的连接池和异步 SQL 执行所有 SQL 语句和参数均经过安全校验,防止危险操作和 SQL 注入
MCP Server 支持三大工具:
execute_sql:执行 SQL 查询get_tables_info:获取所有表及字段结构get_connection_status:检测数据库连接
优雅处理进程信号,支持平滑关闭
安全性说明
禁止 DROP/TRUNCATE/ALTER 等危险操作
检查常见 SQL 注入模式
限制参数数量,防止滥用
禁用多语句执行
在 Cursor 中 验证 开发的 MCP Server 是否能运行
Cursor 添加 MCP Server
通过 Cursor Settings -> MCP Add new global MCP server,可以将 MCP 服务添加为全局可用。这意味着你配置的 MCP 服务将在所有项目中生效。
也可以只针对 项目级别 添加
在项目的 .cursor目录下,新建一个 mcp.json文件进行配置,这样的设置只会对特定项目生效。

mcp server 的格式是如何呢

配置 server 基本格式规范
{
"mcpServers": {
"<server-name>": {
"command": "<启动命令>",
"args": ["<参数1>", "<参数2>", ...],
"env": {
"<环境变量名1>": "<值1>",
"<环境变量名2>": "<值2>",
...
},
"transport": "<传输协议>",
"port": <端口号>,
"host": "<主机地址>"
}
}
}查询数据库表和字段信息

查看当前数据库是否连接
查询表里数据

看到这里,是不是觉得很神奇?我们用自然语言描述需求,AI助手自动生成并执行SQL,然后分析结果给出结论。
依赖
贡献方式
欢迎提交 Issue 和 PR 参与贡献!
许可证
Available Tools
7 toolsadd_columnC
Add a new column to existing table
Inputs: table (required), field (required).
connect_dbC
Connect to MySQL database using URL or config
Inputs: url, workspace, host, port, +3 more.
create_tableC
Create a new table in the database
Inputs: table (required), fields (required), indexes.
describe_tableB
Get table structure
Inputs: table (required).
executeC
Execute an INSERT, UPDATE, or DELETE query
Inputs: sql (required), params.
list_tablesB
List all tables in the database
Inputs: no named parameters.
queryC
Execute a SELECT query
Inputs: sql (required), params.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 7 tools
Each tool has a clearly distinct purpose with no overlap: add_column modifies table structure, connect_db handles connections, create_table creates tables, describe_table inspects structure, execute handles write queries, list_tables enumerates tables, and query handles read queries. The separation between execute (for INSERT/UPDATE/DELETE) and query (for SELECT) is particularly well-defined.
All tools follow a consistent verb_noun pattern with clear, descriptive names in snake_case: add_column, connect_db, create_table, describe_table, execute, list_tables, query. The naming convention is uniform throughout the set, making it easy to predict tool functions.
With 7 tools, this server is well-scoped for MySQL database operations. Each tool serves a distinct, essential function in the database management workflow, from connection and table listing to query execution and schema modification. The count is neither too sparse nor bloated.
The tool set covers core MySQL operations effectively: connection management, table CRUD (create, list, describe), schema modification (add_column), and query execution (both read and write). A minor gap is the lack of tools for deleting tables or columns, which might require workarounds using execute, but the surface is otherwise complete for basic database interactions.
Related MCP Connectors
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Query and update Teable records, manage tables, and access the Teable API with AI agents.
Work with your database from AI tools: ask, export, approved changes, and scheduled jobs.
Related MCP Servers
- AlicenseBqualityDmaintenanceEnables AI models to perform MySQL database operations through a standardized interface, supporting secure connections, query execution, and comprehensive schema management.779 npm33MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to securely connect to and manage MySQL databases with support for multiple database connections, complete CRUD operations, schema inspection, and dynamic connection management through natural language.15 npm81MIT
- FlicenseNot gradedqualityDmaintenanceEnables AI agents to interact with a MySQL database using natural language, automating SQL tasks like querying, inserting, updating, and deleting data.1-
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to directly query MySQL databases, list tables, and view table structures through natural language.20 npmMIT
