数据库架构服务器

一个强大的MCP服务器,提供从各种数据库类型(PostgreSQL、MySQL、SQLite等)读取和公开数据库架构的工具。

  • by Adhikram
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 adhikram/mcp-server

使用说明

数据库架构MCP服务器

一个功能强大的MCP服务器,提供从各种数据库类型(PostgreSQL、MySQL、SQLite等)读取和公开数据库模式的工具。

🌟 特性

  • 连接到不同类型的数据库
  • 列出数据库中的所有表
  • 获取特定表的详细架构信息
  • 获取完整的数据库架构,包括表、视图和索引
  • 支持多种数据库类型(PostgreSQL、MySQL、SQLite)

🚀 安装

先决条件

  1. Python 3.x
  2. 虚拟环境(推荐)

基本安装

  1. 创建并激活虚拟环境:
# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Unix/macOS:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
  1. 安装依赖项:
pip install -r requirements.txt

Windows MCP安装

  1. 安装Node.js:

    • 从下载并安装 Node.js官方网站
    • 通过运行验证安装 node --version 在PowerShell中
  2. 更新系统路径:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
  1. 如果无法识别Node.js,请手动添加到Path:

    • 按 Windows + R
    • 类型 sysdm.cpl 然后按Enter键
    • 转到“高级”选项卡
    • 点击“环境变量”
    • 在“系统变量”下,找到并选择“路径”
    • 点击“编辑”
    • 添加 C:\Program Files\nodejs\ 如果不存在
    • 在所有窗口上单击“确定”
    • 重新启动计算机
  2. 安装MCP:

    • 下载 mcp-install.ps1 脚本
    • 解除文件锁定(右键单击→ 属性→ 解锁)
    • 以管理员身份打开PowerShell并运行:
      Set-ExecutionPolicy unrestricted
      
    • 导航到脚本位置并运行:
      .\mcp-install.ps1 @modelcontextprotocol/server-postgres
      
    • 完全退出克劳德桌面(检查系统托盘)
    • 在中配置服务器 C:\Users\YOUR_USERNAME\AppData\Roaming\Claude\claude_desktop_config.json
    • 将MCP服务器配置复制到 mcp.json 在游标中
    • 重新启动游标

MCP配置示例

创建一个 .cursor/mcp.json 项目根目录中的文件具有以下结构:

{
    "mcpServers": {
        "local-db": {
            "args": [
                "C:\\Users\\YOUR_USERNAME\\AppData\\Roaming\\npm\\node_modules\\@modelcontextprotocol\\server-postgres\\dist\\index.js",
                "database-url"
            ],
            "command": "node"
        },
        "staging-db": {
            "args": [
                "C:\\Users\\YOUR_USERNAME\\AppData\\Roaming\\npm\\node_modules\\@modelcontextprotocol\\server-postgres\\dist\\index.js",
                "database-url"
            ],
            "command": "node"
        },
        "production-db": {
            "args": [
                "C:\\Users\\YOUR_USERNAME\\AppData\\Roaming\\npm\\node_modules\\@modelcontextprotocol\\server-postgres\\dist\\index.js",
                "database-url"
            ],
            "command": "node"
        }
    }
}

替换 YOUR_USERNAME 使用您的Windows用户名,并确保路径与您的Node.js模块安装位置匹配。

💻 用法

启动服务器

python src/server.py

可用工具

1.连接到数据库

config = {
    "type": "postgresql",  # or "mysql", "sqlite"
    "host": "localhost",
    "port": 5432,
    "database": "mydb",
    "username": "user",
    "password": "password"
}
result = await connect_database(config)

2.列表

tables = await list_tables("postgresql://user:password@localhost:5432/mydb")

3.获取表架构

schema = await get_table_schema("postgresql://user:password@localhost:5432/mydb", "users")

4.获取数据库架构

schema = await get_database_schema("postgresql://user:password@localhost:5432/mydb")

📚 Claude使用示例

# Connect to a database
config = {
    "type": "postgresql",
    "host": "localhost",
    "port": 5432,
    "database": "mydb",
    "username": "user",
    "password": "password"
}
await connect_database(config)

# Get the complete schema
schema = await get_database_schema("postgresql://user:password@localhost:5432/mydb")

# Analyze the schema
print(f"Database has {len(schema['tables'])} tables")
for table_name, table_info in schema['tables'].items():
    print(f"\nTable: {table_name}")
    print(f"Columns: {len(table_info['columns'])}")
    print(f"Primary Keys: {table_info['primary_keys']}")
    print(f"Foreign Keys: {len(table_info['foreign_keys'])}")

🔒 安全最佳实践

  1. 切勿在客户端代码中暴露敏感的数据库凭据
  2. 使用环境变量或安全配置管理凭据
  3. 实施适当的访问控制和身份验证
  4. 尽可能使用SSL/TLS进行数据库连接