SQL数据库查询服务器

一个基于TypeScript的Model Context Protocol (MCP)服务器,支持通过SSH隧道安全查询PostgreSQL数据库,适用于本地、容器化或AI驱动的用例。

  • by t3ta
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 t3ta/sql-mcp-server

使用说明

SQL MCP服务器(TypeScript)

该项目提供了 TypeScript实现 模型上下文协议(MCP)服务器,使语言模型和其他MCP兼容客户端能够查询PostgreSQL数据库——在需要时通过SSH堡垒隧道。

它专为灵活性和安全的数据库访问而构建,支持AWS RDS的只读事务,并使用基于stdin/stdout的通信,使其适用于本地、容器化或AI驱动的用例。

特性

  • 🔒 SSH堡垒支持通过SSH隧道安全访问私有RDS实例
  • 🐘 PostgreSQL只读查询引擎使用 pg 图书馆
  • 📡 基于STDIO的MCP协议传输
  • 🧠 兼容 存储体mcp服务器
  • ⚙️ 通过以下方式轻松配置 .env 或环境变量
  • 🧪 完全可通过Jest和mocks进行测试

安装

克隆存储库并安装依赖项:

git clone https://github.com/your-org/sql-mcp-server.git
cd sql-mcp-server
npm install
npm run build

Configuration (Optional .env file)

Create a .env file in the project root:

DB_USER=postgres
DB_PASS=yourpassword
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydatabase
USE_SSH_TUNNEL=true
SSH_BASTION_HOST=bastion.example.com
SSH_BASTION_USER=ec2-user
SSH_PRIVATE_KEY_PATH=~/.ssh/id_rsa

Usage

Run the server using npx:

npx -y @modelcontextprotocol/server-postgres postgresql://<user>:<pass>@localhost:5433/<dbname>

For direct connection without SSH tunneling, set the appropriate environment variables:

DB_HOST=rds-host.amazonaws.com DB_PORT=5432 npx -y @modelcontextprotocol/server-postgres postgresql://<user>:<pass>@rds-host.amazonaws.com/<dbname>

Sample Input

{
  "type": "call_tool",
  "params": {
    "name": "query",
    "arguments": {
      "sql": "SELECT * FROM users LIMIT 10"
    }
  }
}

Sample Output

{
  "content": [
    {
      "type": "text",
      "text": "[{\"id\":1,\"name\":\"Alice\"}]"
    }
  ],
  "isError": false
}



⸻

Related Docs
	•	Architecture
	•	Domain Models
	•	Glossary
	•	Coding Standards
	•	Tech Stack
	•	User Guide

⸻

License

MIT

⸻

Contribution Guide

We welcome community contributions! Please see CONTRIBUTING.md for details.

⸻

Compatibility

This implementation follows the Model Context Protocol (MCP) and has been tested for compatibility with:
	•	memory-bank-mcp-server
	•	Claude Desktop (via STDIO)
	•	Cursor IDE
	•	Supabase + MCP integration