SQL数据分析服务器

将Claude或ChatGPT转变为具有SQL功能的数据分析工具,支持导入CSV或Google表格,通过自然语言执行复杂查询并获取即时洞察。

  • by rico007
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 rico007/MCP-Analyst

使用说明

MCP数据分析服务器

将Claude(或ChatGPT)转变为具有SQL功能的强大数据分析师。导入CSV或Google表格,运行复杂的查询,并通过自然语言获得即时见解。

🎯 这有什么作用

此MCP服务器允许AI助手通过以下方式分析您的数据:

  • 导入 CSV文件和谷歌表格
  • 查询 具有完整SQL的数据(JOIN、聚合、窗口函数)
  • 分析 从100行到数十亿行的数据集
  • 出口 将结果转换为CSV

简单示例:

You: "Load sales_2024.csv as sales and show me the top 10 products by revenue"

Claude/ChatGPT:
1. Imports your CSV into a DuckDB database
2. Writes SQL: SELECT product, SUM(revenue) FROM sales GROUP BY product ORDER BY revenue DESC LIMIT 10
3. Executes the query
4. Shows results with insights: "Your top product is Widget A with $50K revenue..."

🚀 快速开始

先决条件

  • Python 3.8+(或Docker桌面版)
  • Claude桌面或ChatGPT桌面
  • 2GB+内存

安装

选项1:Docker(推荐)

# Clone or download this repo
cd mcp-data-analyst

# Start the container
./docker-start.sh

# Falls das File nicht ausführbar ist. führe im Terminal folgenden Befehl aus:

chmod +x docker-start.sh

# Danach starte es erneut:

./docker-start.sh 


# Follow the instructions to configure Claude/ChatGPT

选项2:直接安装Python

# Clone or download this repo
cd mcp-data-analyst

# Install dependencies
pip install -r requirements.txt

# Run the server
python server.py

📋 配置

步骤1:配置您的AI助手

适用于克劳德桌面

编辑配置文件:

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

如果使用Docker:

{
  "mcpServers": {
    "mcp-data-analyst": {
      "command": "docker",
      "args": ["exec", "-i", "data-analyst-mcp", "python3", "server.py"]
    }
  }
}

如果使用直接安装:

{
  "mcpServers": {
    "mcp-data-analyst": {
      "command": "python3",
      "args": ["/absolute/path/to/mcp-data-analyst/server.py"]
    }
  }
}

适用于ChatGPT桌面

编辑配置文件:

  • macOS: ~/Library/Application Support/ChatGPT/config.json
  • 视窗: %APPDATA%\ChatGPT\config.json

使用与上述Claude Desktop相同的JSON格式。

步骤2:配置环境(可选)

对于使用MotherDuck的云存储(处理数十亿行):

# Copy the example
cp .env.example .env

# Edit .env and add your token
nano .env

添加您的MotherDuck令牌:

MOTHERDUCK_TOKEN=your_token_here
MEMORY_LIMIT=4G
CPU_LIMIT=2.0

获取免费代币 motherduck.com (10GB免费层)。

没有母鸭: 数据存储在内存中(快速,但仅限于会话)。

步骤3:重新启动AI助手

完全退出并重新启动Claude Desktop或ChatGPT Desktop。

第四步:测试一下!

You: "Load example_data.csv as customers and show me the data"

AI将导入文件并向您显示结果!

📊 特性

6强大的工具

  1. import_csv -加载CSV文件、Google表格或URL
  2. 查询数据 -在完全支持DuckDB的情况下执行SQL查询
  3. list_tables -显示所有可用表格
  4. describe_table -获取模式和示例数据
  5. export_query_results -将查询结果保存到CSV
  6. get_table_stats -获取统计摘要

支持的数据源

  • 本地CSV文件 -您计算机上的任何CSV
  • CSV URL -直接HTTP/HTTPS链接
  • 谷歌表格 -自动将共享链接转换为CSV
  • 多个文件 -将多个CSV作为单独的表加载

SQL功能

完整的SQL支持,包括:

  • 选择、地点、分组方式、排序方式、限制
  • 接头(内、左、右、全外)
  • 聚合函数(SUM、AVG、COUNT、MIN、MAX)
  • 窗口功能(ROW_NUMBER、RANK、LAG、LEAD)
  • CTE(含条款)
  • 子查询
  • 日期/时间函数

数据大小限制

行 CSV大小 模式 性能
<1M ~100MB 内存中 ⚡ 即时
1-10M ~1GB 内存中 ✅ 快速(秒)
10M+ 1GB+ 母鸭 ☁️ 优化(云)

推荐:在内存中用于<1000万行,MotherDuck用于更大的数据集。

💡 用法示例

示例1:基本分析

You: "Load my sales data from https://example.com/sales.csv as sales"
AI: ✓ Imported 50,000 rows into 'sales' table

You: "What are the top 5 products by revenue?"
AI: [Writes and executes SQL, shows results with insights]

You: "Show me monthly revenue trends"
AI: [Creates time-series analysis with DATE_TRUNC]

示例2:多表分析

You: "Load sales.csv as sales and products.csv as products"
AI: ✓ Imported both tables

You: "Join these tables and show me which product categories generate the most revenue"
AI: [Automatically identifies the relationship (product_id), performs JOIN, provides analysis]

示例3:高级分析

You: "Analyze customer behavior and create segments based on purchase patterns"
AI: [Performs multi-step analysis with CTEs, window functions, and provides business insights]

示例4:谷歌表格

You: "Load this Google Sheet: https://docs.google.com/spreadsheets/d/abc123/edit"
AI: ✓ Converted to CSV and imported

You: "Calculate the correlation between marketing spend and sales"
AI: [Performs statistical analysis]

🐳 Docker命令

# Start server
./docker-start.sh

# Stop server
./docker-stop.sh

# View logs
docker-compose logs -f

# Restart
docker-compose restart

# Stop and remove
docker-compose down

# Rebuild
docker-compose down && docker-compose up -d --build

🔧 故障排除

MCP服务器未出现

  1. 验证配置文件路径是否正确
  2. 使用绝对路径(不 ~ 或相对)
  3. 确保容器正在运行: docker ps
  4. 检查日志: docker-compose logs
  5. 完全重启AI助手(退出,而不仅仅是关闭)

脚本权限被拒绝

chmod +x docker-start.sh docker-stop.sh setup.sh

导入错误

  • 本地文件:使用绝对路径
  • 谷歌表格:与“任何有链接的人”分享
  • 网址:验证URL是否可公开访问

内存不足

对于大型数据集(>1000万行):

选项1:增加Docker内存 docker-compose.yml:

memory: 8G  # or 16G
cpus: '4.0'

选项2:使用MotherDuck(建议用于大于10M行):

  1. 从获取令牌 motherduck.com
  2. 添加到 .env: MOTHERDUCK_TOKEN=your_token
  3. 重新启动: docker-compose restart

🏗️ 建筑

┌─────────────────────────────────────┐
│   Claude Desktop / ChatGPT Desktop  │
│   (Natural Language Interface)      │
└─────────────────┬───────────────────┘
                  │ MCP Protocol
                  ▼
┌─────────────────────────────────────┐
│   MCP Data Analyst Server (Python) │
│   - 6 Tools (FastMCP)               │
│   - CSV Import & SQL Query          │
└─────────────────┬───────────────────┘
                  │
                  ▼
┌─────────────────────────────────────┐
│   DuckDB Database                   │
│   - In-Memory (< 10M rows)          │
│   - MotherDuck Cloud (billions)     │
└─────────────────────────────────────┘

🔒 安全与隐私

  • 内存模式:所有数据都保留在您的计算机上,永远不会离开您的机器
  • 母鸭模式:存储在您的私有云帐户中的数据
  • 无外部共享:您的数据永远不会发送给第三方
  • 只读SQL:仅SELECT查询,无数据修改
  • 容器隔离:Docker提供了额外的安全层

📚 高级配置

自定义内存限制

编辑 docker-compose.yml:

deploy:
  resources:
    limits:
      memory: 8G      # Increase for large datasets
      cpus: '4.0'     # More CPU for faster processing

多个表

加载多个CSV:

You: "Load sales.csv, products.csv, and customers.csv"
AI: [Imports all three as separate tables]

You: "Show me how these tables relate to each other"
AI: [Analyzes schemas, identifies foreign keys, suggests JOINs]

导出结果

You: "Export the top 100 customers to a CSV file"
AI: [Executes query and saves to /data/top_customers.csv]

🛠️ 发展

添加自定义工具

FastMCP框架使添加新工具变得容易:

@mcp.tool()
def my_custom_tool(param: str, count: int = 10) -> dict:
    """Description that appears to the AI"""
    # Your logic here
    return {"result": "..."}

就是这样!FastMCP处理验证、错误和协议细节。

项目结构

mcp-data-analyst/
├── server.py              # Main MCP server (FastMCP)
├── requirements.txt       # Python dependencies
├── Dockerfile            # Container definition
├── docker-compose.yml    # Docker orchestration
├── docker-start.sh       # Easy start script
├── docker-stop.sh        # Easy stop script
├── setup.sh              # Direct install script
├── .env.example          # Environment template
├── .gitignore            # Git ignore rules
└── example_data.csv      # Sample data for testing

🤝 贡献

欢迎投稿!拜托:

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 提交拉取请求

📄 许可证

MIT许可证-免费使用、修改和分发。

🙏 鸣谢

内置:

  • FastMCP -现代MCP框架
  • DuckDB -快速分析数据库
  • 鸭妈妈 -云数据仓库
  • MCP协议 通过Anthropic

📞 支持

  • 问题:打开GitHub问题
  • 问题:开始讨论
  • 文档:查看此自述文件

🌟 Star This Repo

如果你觉得这很有用,请在repo上加星! ⭐


在5分钟内将您的AI助手转变为数据分析师! 📊✨