动态数据库连接服务器

MCP数据库服务器是一个支持PostgreSQL和Redis的动态数据库连接服务,提供安全的运行时凭证管理和多样化的数据库操作功能。

  • by Yadhindrasrivarshan
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 Yadhindrasrivarshan/mcp-db-server

使用说明

MCP数据库服务器

用于支持PostgreSQL和Redis的动态数据库连接的模型上下文协议(MCP)服务器。

特性

  • 🔌 动态连接:使用运行时凭据连接到数据库
  • 🗄️ PostgreSQL支持:执行查询、检查模式、列出表
  • ⚡ Redis支持:执行缓存操作(GET、SET、DEL、KEYS等)
  • 🔒 安全:在运行时传递的凭据,不是硬编码的
  • 🛠️ 轻松设置:在本地工作,可以远程部署

安装和设置

先决条件

  1. Node.js (v18或更高版本)
  2. PostgreSQL (用于Postgres功能)
  3. 瑞迪斯 (针对Redis功能)

安装PostgreSQL

macOS(使用Homebrew):

brew install postgresql@16
brew services start postgresql@16

# Create a test database
createdb testdb

Linux(Ubuntu/Debian):

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Create a test database
sudo -u postgres createdb testdb

窗户:

  • 下载自 postgresql.org
  • 运行安装程序并按照安装向导进行操作
  • 将PostgreSQL bin目录添加到PATH

安装Redis

macOS(使用Homebrew):

brew install redis
brew services start redis

Linux(Ubuntu/Debian):

sudo apt update
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-server

窗户:

设置MCP服务器

  1. 克隆
https://github.com/Yadhindrasrivarshan/mcp-db-server
  1. 安装依赖项:
npm install
  1. 构建项目:
npm run build
  1. 创建配置文件:
cp config/example.config.json config/local.config.json

编辑 config/local.config.json 使用您的数据库凭据:

{
  "postgres": {
    "host": "localhost",
    "port": 5432,
    "database": "testdb",
    "user": "your_username",
    "password": "your_password"
  },
  "redis": {
    "host": "localhost",
    "port": 6379,
    "password": ""
  }
}

用法

运行服务器

开发模式(自动重新加载):

npm run dev

生产方式:

npm start

在Claude Desktop中配置

添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 窗户: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "mcp-db-server": {
      "command": "node",
      "args": [
        "/Users/Z00GJBZ/Documents/MCP AI/mcp-db-help/build/index.js"
      ],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_DB": "testdb",
        "POSTGRES_USER": "your_username",
        "POSTGRES_PASSWORD": "your_password",
        "REDIS_HOST": "localhost",
        "REDIS_PORT": "6379",
        "REDIS_PASSWORD": ""
      }
    }
  }
}

在github copilot中配置mcp.json

mac -> cmd + shift + P -> Opens vscode configuration option
search -> mcp , click MCP: Open user configuration
json -> Update the json with below JSON
{ 
    "mcp-db-server": {
            "command": "/Users/{username}/.nvm/versions/node/v24.13.1/bin/node",
            "args": [
                "/Users/{username}/folders/MCP_AI/mcp-db-help/build/index.js"
            ],
            "type": "stdio",
            "env": {
                "POSTGRES_HOST": "localhost",
                "POSTGRES_PORT": "5432",
                "POSTGRES_DB": "testdb",
                "POSTGRES_USER": "postgres",
                "POSTGRES_PASSWORD": "",
                "REDIS_HOST": "localhost",
                "REDIS_PORT": "6379"
            }
        }
}        

可用工具

PostgreSQL工具

  • postgres_query -执行SQL查询
  • postgres_list_tables -列出数据库中的所有表
  • postgres_describe_table -获取表的架构信息
  • postgres_list_databases -列出所有数据库(仅限管理员)

Redis工具

  • redis_get -按键获取值
  • redis_set -设置键值对
  • redis_del -删除密钥
  • redis_keys -列出匹配模式的密钥
  • redis_exists -检查密钥是否存在
  • redis_ttl -有时间为钥匙而活

构建问题

# Clean and rebuild
rm -rf build node_modules
npm install
npm run build

贡献

这是一个学习项目!您可以随意尝试并添加新功能。

许可证

麻省理工学院