SQL MCP 服务器(TypeScript 实现)

SQL MCP 服务器(TypeScript 实现)

一个基于 TypeScript 的 Model Context Protocol (MCP) 服务端,支持安全访问 PostgreSQL 数据库。

快速入门

  1. 克隆仓库并安装依赖:

    git clone https://github.com/your-org/sql-mcp-server.git
    cd sql-mcp-server
    npm install
    npm run build
    
  2. 配置环境变量: 在项目根目录创建 .env 文件,填写数据库和 SSH 隧道相关配置。 示例:

    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
    
  3. 启动服务: 使用 npx 运行服务端:

    npx -y @modelcontextprotocol/server-postgres postgresql://<user>:<pass>@localhost:5433/<dbname>
    
  4. 测试输入输出: 输入示例:

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

    输出示例:

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