数据库导出服务器

基于Model Context Protocol (MCP)的服务器,提供从数据库高效导出数据到多种文件格式(CSV, TSV, JSON, BSON, Parquet, XLSX, Binary)并支持云存储目标的功能。

  • by aetperf
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 aetperf/fastbcp-mcp

使用说明

FastBCP MCP服务器

A. 模型上下文协议(MCP) 暴露的服务器 FastBCP 将数据从数据库导出到文件(CSV、TSV、JSON、BSON、Parquet、XLSX、Binary)的功能,并具有可选的云存储目标。

概述

FastBCP是一个高性能的CLI工具,用于将数据从数据库导出到文件。此MCP服务器封装了FastBCP功能,并提供:

  • 安全第一方针:执行前预览命令,需要用户确认
  • 密码屏蔽:凭据和连接字符串从不显示在日志或输出中
  • 智能验证:通过数据库特定兼容性检查进行参数验证
  • 聪明的建议:自动并行方法建议
  • 版本检测:使用功能注册表自动检测二进制版本
  • 综合录井:带有时间戳和结果的完整执行日志

MCP工具

1. preview_export_command

无需执行即可构建和预览FastBCP导出命令。显示密码被屏蔽的确切命令。总是先用这个。

2. execute_export

执行之前预览的命令。需要 confirmation: true 作为一种安全机制。

3. validate_connection

验证源数据库连接参数(仅用于参数检查,不测试实际连接)。

4. list_supported_formats

列出所有支持的源数据库、输出格式和存储目标。

5. suggest_parallelism_method

推荐基于源数据库类型和表特征的最佳并行方法。

6. get_version

报告检测到的FastBCP二进制版本、支持的类型和功能标志。

安装

先决条件

  • Python 3.10或更高版本
  • FastBCP二进制v0.29+(从 Arpe.io)
  • Claude Code或其他MCP客户端

设置

  1. 克隆或下载此存储库:

    cd /path/to/fastbcp-mcp
    
  2. 安装Python依赖项:

    pip install -r requirements.txt
    
  3. 配置环境:

    cp .env.example .env
    # Edit .env with your FastBCP path
    
  4. 添加到Claude代码配置 (~/.claude.json):

    {
      "mcpServers": {
        "fastbcp": {
          "type": "stdio",
          "command": "python",
          "args": ["/absolute/path/to/fastbcp-mcp/src/server.py"],
          "env": {
            "FASTBCP_PATH": "/absolute/path/to/FastBCP"
          }
        }
      }
    }
    
  5. 重新启动Claude代码 加载MCP服务器。

  6. 验证安装:

    # In Claude Code, run:
    /mcp
    # You should see "fastbcp: connected"
    

配置

环境变量

编辑 .env 配置:

# Path to FastBCP binary (required)
FASTBCP_PATH=./fastbcp/FastBCP

# Execution timeout in seconds (default: 1800 = 30 minutes)
FASTBCP_TIMEOUT=1800

# Log directory (default: ./logs)
FASTBCP_LOG_DIR=./logs

# Log level (default: INFO)
LOG_LEVEL=INFO

连接选项

服务器支持多种身份验证和连接方式:

参数 说明
server 主机:端口或主机\实例(可选 connect_string 或 dsn)
user / password 标准证书
trusted_auth Windows可信身份验证
connect_string 完整连接字符串(不包括服务器/用户/密码/dsn)
dsn ODBC DSN名称(不包括服务器/提供程序)
provider OleDB提供程序名称
application_intent SQL Server应用程序意图(只读/读写)

输出选项

选项 CLI标志 说明
format --format 输出格式:csv、tsv、json、bson、拼花、xlsx、二进制
file_output --fileoutput 输出文件路径
directory --directory 输出目录路径
storage_target --storagetarget 存储:本地、s3、s3兼容、azure_blob、azure_datalake、fabric_onelake
delimiter --delimiter 字段分隔符(CSV/TSV)
quotes --quotes 引用字符
encoding --encoding 输出编码
no_header --noheader 省略标题行(CSV/TSV)
decimal_separator --decimalseparator 小数分隔符(.or,)
date_format --dateformat 日期格式字符串
bool_format --boolformat 布尔格式:TrueFalse、OneZero、YesNo
parquet_compression --parquetcompression 拼花压缩:无、Snappy、Gzip、Lz4、Lzo、Zstd
timestamped --timestamped 在输出文件名中添加时间戳
merge --merge 合并并行输出文件

导出选项

选项 CLI标志 说明
method --method 平行法
distribute_key_column --distributeKeyColumn 数据分发栏
degree --degree 平行度(默认值:1)
load_mode --loadmode 追加或截断
batch_size --batchsize 出口操作的批量大小
map_method --mapmethod 列映射:位置或名称
run_id --runid 日志记录的运行ID
data_driven_query --datadrivenquery 数据驱动方法的自定义SQL
settings_file --settingsfile 自定义设置JSON文件
log_level --loglevel 覆盖日志级别(信息/调试)
no_banner --nobanner 抑制横幅输出
license_path --license 许可证文件路径或URL
cloud_profile --cloudprofile 云存储配置文件名称

使用示例

PostgreSQL到CSV导出

User: "Export the 'orders' table from PostgreSQL (localhost:5432, database: sales_db,
       schema: public) to CSV file at /tmp/orders.csv. Use parallel export."

Claude Code will:
1. Call suggest_parallelism_method to recommend Ctid for PostgreSQL
2. Call preview_export_command with your parameters
3. Show the command with masked passwords
4. Explain what will happen
5. Ask for confirmation
6. Execute with execute_export when you approve

导出到压缩拼花地板

User: "Export the 'transactions' table from SQL Server to Parquet format
       with Snappy compression, saved to /data/exports/."

Claude Code will use parquet format with parquet_compression set to Snappy.

导出到S3

User: "Export the 'users' table from PostgreSQL to CSV on S3 bucket
       s3://my-bucket/exports/ using my AWS profile."

Claude Code will use storage_target=s3 with cloud_profile.

检查版本和功能

User: "What version of FastBCP is installed?"

Claude Code will call get_version and display the detected version,
supported source types, output formats, and available features.

两步安全流程

此服务器实现了一个强制性的两步过程:

  1. 预览 -始终使用 preview_export_command 第一
  2. 执行 -使用 execute_export 随着 confirmation: true

如果不先预览并确认,则无法执行。

安全

  • 密码和连接字符串在所有输出和日志中都被屏蔽
  • 隐藏的敏感标志: --sourcepassword, --sourceconnectstring, -x, -g
  • 使用环境变量进行敏感配置
  • 执行前仔细检查命令
  • 使用所需的最低数据库权限

测试

运行测试套件:

# Run all tests
python -m pytest tests/ -v

# Run with coverage
python -m pytest tests/ --cov=src --cov-report=html

项目结构

fastbcp-mcp/
  src/
    __init__.py
    server.py          # MCP server (tool definitions, handlers)
    fastbcp.py         # Command builder, executor, suggestions
    validators.py      # Pydantic models, enums, validation
    version.py         # Version detection and capabilities registry
  tests/
    __init__.py
    test_command_builder.py
    test_validators.py
    test_version.py
  .env.example
  requirements.txt
  CHANGELOG.md
  README.md

许可证

此MCP服务器包装器按原样提供。FastBCP本身是Arpe.io的独立产品。

相关链接