SQL MCP Server

SQL MCP Server

Enables read-only interaction with SQL databases through MCP, providing database metadata exploration, sample data retrieval, and secure query execution. Supports MySQL with multiple transport options and built-in security features including SQL injection protection and data sanitization.

Category
Visit Server

README

SQL-MCP

数据库上下文协议(MCP)服务器,提供数据库元数据、样本数据与只读查询能力,支持 stdio 与 Streamable HTTP 传输。

安装

npm install
npm run build

快速开始

  • 使用 stdio 启动:
node dist/cli.js --host 127.0.0.1 --port 3306 --user root --password ****** --database mydb --type mysql --transport stdio
  • 使用 HTTP 启动:
node dist/cli.js --host 127.0.0.1 --port 3306 --user root --password ****** --database mydb --type mysql --transport http --httpPort 3000
  • 带日志控制:
node dist/cli.js --verbose
node dist/cli.js --log-dest file --log-file ./logs/sql-mcp.log

配置

应用从如下位置加载配置(后者覆盖前者):

  • 内置默认值(见 src/core/config/defaults.ts
  • 环境变量(前缀 SQL_MCP_
  • 配置文件(.env/.json/.js),通过 CONFIG_FILE 指定

可复制 ENV.example 作为模板并填入生产环境变量。

环境变量映射(节选):

  • 数据库:
    • SQL_MCP_DB_TYPEdatabase.typemysql
    • SQL_MCP_DB_HOSTdatabase.host
    • SQL_MCP_DB_PORTdatabase.port
    • SQL_MCP_DB_USERdatabase.user
    • SQL_MCP_DB_PASSWORDdatabase.password
    • SQL_MCP_DB_NAMEdatabase.database
    • SQL_MCP_DB_TIMEOUTdatabase.connectionTimeout
  • 日志:
    • SQL_MCP_LOG_LEVELlogging.leveldebug|info|warn|error
    • SQL_MCP_LOG_DESTINATIONlogging.destinationconsole|file
    • SQL_MCP_LOG_FILE_PATHlogging.filePath
  • MCP:
    • SQL_MCP_MCP_TRANSPORTmcp.transportstdio|http
    • SQL_MCP_MCP_HTTP_PORTmcp.httpPort

更多映射详见 src/core/config/loader.ts

MCP Inspector

建议使用 MCP Inspector 进行调试与测试。

  • stdio:选择本地命令,指向 node dist/cli.js ... --transport stdio
  • http:配置 POST /mcp 地址,首次初始化后会返回 Mcp-Session-Id 并复用

安全

  • 所有查询为只读,服务层与处理器对 SQL/WHERE 做注入校验。
  • 输出统一脱敏(敏感字段如 password/token 等会被 *** 处理)。
  • 可选凭证保护:见 src/utils/security.tsCredentialManager

日志

  • 使用 src/utils/logging.ts,支持级别、彩色控制台、文件输出、子作用域。
  • 通过 CLI --verbose/--log-dest/--log-file 或环境变量控制。

部署

npm 发布

  • 打 tag(例如 v1.0.0)推送后,Release (npm) 工作流会构建、测试并发布到 npm。
  • 需要在仓库设置 NPM_TOKEN(Actions secrets)。

Docker 镜像(GHCR)

  • Push 到 main 或推 tag,会由 Docker (GHCR) 工作流构建并推送镜像到 ghcr.io/polarisxb/sql-mcp
  • 运行示例:
docker run --rm -p 3000:3000 \
  -e SQL_MCP_DB_TYPE=mysql \
  -e SQL_MCP_DB_HOST=host \
  -e SQL_MCP_DB_PORT=3306 \
  -e SQL_MCP_DB_USER=user \
  -e SQL_MCP_DB_PASSWORD=pass \
  -e SQL_MCP_DB_NAME=mydb \
  ghcr.io/polarisxb/sql-mcp:<tag> --transport http --httpPort 3000

Compose

  • docker-compose.yml 已提供(MySQL + 服务),直接 docker-compose up -d

开发

  • 测试:
node ./node_modules/vitest/vitest.mjs run
  • 构建:
node ./node_modules/typescript/bin/tsc

目录

  • src/connectors/mysql:MySQL 连接器与元数据映射/SQL 构建
  • src/services/*:元数据/采样/安全服务
  • src/mcp/*:MCP 定义、处理器、服务器工厂、传输
  • src/utils/*:日志、错误、通用安全工具

Recommended Servers

playwright-mcp

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.

Official
Featured
TypeScript
Magic Component Platform (MCP)

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.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

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.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

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.

Official
Featured
TypeScript
Kagi MCP Server

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.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

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.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured