Database Query MCP Server
Enables secure read-only querying of multiple database types (MySQL, PostgreSQL, MSSQL, Oracle) through natural language. Automatically reads database configuration from project files and blocks any data modification operations for safety.
README
MCP 数据库查询工具
这是一个用于Coding Agent MCP 服务的数据库查询工具,可以通过提示词,从当前项目配置文件中读取数据库连接信息并连接多种类型的数据库(MySQL、PostgreSQL、MSSQL、Oracle)并执行 SQL 查询。该工具运行在只读模式下,确保不会意外修改数据库中的数据。
功能特点
- 支持连接多种数据库(MySQL、PostgreSQL、MSSQL、Oracle)
- 执行自定义 SQL 查询(只读模式)
- 自动阻止修改数据的操作(INSERT、UPDATE、DELETE等)
- 返回结构化查询结果
- 包含错误处理机制
- 实现为 MCP stdio 服务
安全特性
该工具具有以下安全保护措施:
- 只读模式:自动检测并阻止任何修改数据的SQL操作
- SQL语句检查:只允许SELECT、SHOW、DESCRIBE等只读查询
- 错误处理:对非法操作返回明确的错误信息
安装说明
方式一:从 npm 仓库进行全局安装
您可以从 npm 仓库进行全局安装:
npm install -g coding-db-mcp
安装后,您可以通过以下命令启动服务:
db-query-mcp
方式二:使用 npx 运行(无需安装)
您也可以直接使用 npx 运行此工具,无需预先安装:
npx coding-db-mcp
这种方式会在每次运行时临时下载并执行最新版本的工具。
配置到 Agent 的 MCP 中
要将此服务配置到 Agent 的 MCP 中,请按照以下步骤操作:
1. 配置 Agent 的 MCP 设置
在 Agent 的 MCP 配置中添加此服务。通常需要在 Agent 的配置文件中添加类似以下的配置:
{
"mcpServers": {
"database-query": {
"command": "coding-db-mcp",
"args": [
]
}
}
}
如果使用 npx 方式,配置应为:
{
"mcpServers": {
"database-query": {
"command": "npx",
"args": [
"coding-db-mcp"
]
}
}
}
2. 配置数据库连接信息(可选)
数据库连接信息文件不是必需的,Agent 可以通过提示词从当前项目配置文件中自动读取数据库连接信息。
例如,在 Spring Boot 项目中,Agent 可以直接读取 application-dev.yaml 文件中的数据库连接配置。
如果需要手动配置数据库连接信息,可以创建 .env 文件:
cp .env.example .env
在 .env 文件中设置以下变量:
DB_HOST: 数据库主机地址DB_PORT: 数据库端口DB_USER: 数据库用户名DB_PASSWORD: 数据库密码DB_NAME: 数据库名称
3. 启动 Agent
启动 Agent 并确保 MCP 服务正确加载。Agent 现在应该能够使用以下四个数据库查询工具:
query_mysql: MySQL 数据库查询工具query_postgresql: PostgreSQL 数据库查询工具query_mssql: MSSQL 数据库查询工具query_oracle: Oracle 数据库查询工具
当 Agent 需要查询数据库时,它会自动调用相应的工具并传入必要的参数(主机、端口、用户名、密码、数据库名和SQL查询语句)。
MCP工具说明
当作为MCP服务运行时,该服务提供以下工具:
query_mysql
执行MySQL数据库查询(只读模式)
参数:
host(string): 数据库主机地址port(integer): 数据库端口user(string): 数据库用户名pwd(string): 数据库密码db(string): 数据库名称querySql(string): 要执行的SQL查询语句(仅支持SELECT等只读操作)
query_postgresql
执行PostgreSQL数据库查询(只读模式)
参数:
host(string): 数据库主机地址port(integer): 数据库端口user(string): 数据库用户名pwd(string): 数据库密码db(string): 数据库名称querySql(string): 要执行的SQL查询语句(仅支持SELECT等只读操作)
query_mssql
执行MSSQL数据库查询(只读模式)
参数:
host(string): 数据库主机地址port(integer): 数据库端口user(string): 数据库用户名pwd(string): 数据库密码db(string): 数据库名称querySql(string): 要执行的SQL查询语句(仅支持SELECT等只读操作)
query_oracle
执行Oracle数据库查询(只读模式)
参数:
host(string): 数据库主机地址port(integer): 数据库端口user(string): 数据库用户名pwd(string): 数据库密码db(string): 数据库名称querySql(string): 要执行的SQL查询语句(仅支持SELECT等只读操作)
安全说明
- 该工具运行在只读模式下,自动阻止任何修改数据的SQL操作
- 建议限制数据库用户权限,避免使用具有管理员权限的账户
- 不要在代码中硬编码数据库凭证
- 生产环境中应启用SSL连接
错误处理
工具会返回结构化的错误信息,包括:
success: 布尔值,表示查询是否成功error: 错误消息code: 错误代码
返回格式
成功的查询将返回以下结构:
success: truedata: 查询结果数组columns: 列信息数组rowCount: 返回行数
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.
E2B
Using MCP to run code via e2b.