Skip to main content
Glama

SQL MCP Server

by polarisxb

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/*:日志、错误、通用安全工具
-
security - not tested
A
license - permissive license
-
quality - not tested

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

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.

  1. 安装
    1. 快速开始
      1. 配置
        1. MCP Inspector
          1. 安全
            1. 日志
              1. 部署
                1. npm 发布
                2. Docker 镜像(GHCR)
                3. Compose
              2. 开发
                1. 目录

                  Related MCP Servers

                  • -
                    security
                    A
                    license
                    -
                    quality
                    An 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 -
                    751
                    14
                    JavaScript
                    MIT License
                  • A
                    security
                    A
                    license
                    A
                    quality
                    An MCP server that provides read-only access to MySQL databases.
                    Last updated -
                    4
                    913
                    41
                    JavaScript
                    MIT License
                    • Linux
                    • Apple
                  • -
                    security
                    F
                    license
                    -
                    quality
                    An 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 -
                    600
                    2
                    JavaScript
                  • -
                    security
                    A
                    license
                    -
                    quality
                    A lightweight MCP server that provides read-only access to SQLite databases, allowing users to execute SELECT queries, list tables, and describe table schemas.
                    Last updated -
                    Python
                    MIT License

                  View all related MCP servers

                  MCP directory API

                  We provide all the information about MCP servers via our MCP API.

                  curl -X GET 'https://glama.ai/api/mcp/v1/servers/polarisxb/sql-mcp'

                  If you have feedback or need assistance with the MCP directory API, please join our Discord server