MCP Data Analyst

MCP Data Analyst

Enables natural language querying of SQL databases using AI, supporting multiple database types and automatic schema discovery.

Category
访问服务器

README

MCP Data Analyst

A Model Context Protocol (MCP) server that enables natural language querying of SQL databases using AI. Connect your database and ask questions in plain English - the server will generate and execute SQL queries for you.

Features

  • 🤖 Natural Language to SQL: Ask questions in plain English, get SQL results
  • 🔌 Multiple Database Support: MySQL, PostgreSQL, MSSQL, MongoDB, SQLite, SSAS (MDX), Elasticsearch (SQL), InfluxDB (InfluxQL)
  • 📊 Schema Auto-Discovery: Automatically scans and caches your database schema
  • 🛠️ MCP Integration: Works seamlessly with MCP-compatible clients
  • Efficient: Connection pooling and schema caching for performance
  • 🔒 Read-only by Design: Only SELECT-style queries are executed

Query Languages

  • SQL: MySQL, PostgreSQL, MSSQL, SQLite, Elasticsearch (SQL API)
  • MDX: SSAS
  • InfluxQL: InfluxDB

Installation

Prerequisites

  • Python 3.12 or higher
  • One of: MySQL, PostgreSQL, MSSQL, MongoDB, SQLite, SSAS, Elasticsearch, or InfluxDB
  • OpenAI API key (or compatible API endpoint)

Setup

  1. Clone the repository:

    cd /path/to/your/workspace
    
  2. Create a virtual environment (recommended):

    python3 -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Configure environment variables:

    Copy the example below and create a .env file:

    # LLM Configuration
    LLM_API_KEY=your-api-key-here
    LLM_MODEL=gpt-3.5-turbo
    LLM_API_URL=https://api.openai.com/v1
    
    

Database Configuration

DB_TYPE=mysql # mysql|postgresql|mssql|mongodb|sqlite|ssas|elasticsearch|influxdb DB_HOST=127.0.0.1 DB_PORT=3306 # 5432 (PostgreSQL), 1433 (MSSQL), 27017 (MongoDB), 2383 (SSAS), 9200 (Elasticsearch), 8086 (InfluxDB) DB_USER=root DB_PASSWORD=your-password DB_NAME=your-database-name # For InfluxDB: database name; for SSAS/Elasticsearch: catalog/index database name

SQLite only

DB_PATH=database.db


## Usage

### Running the MCP Server

Start the server using the standard MCP stdio transport:

```bash
python server.py

The server will:

  1. Validate configuration
  2. Connect to your database
  3. Build a schema cache
  4. Start listening for MCP requests

Available MCP Tools

The server exposes 3 tools that can be called by MCP clients:

1. query_database_with_prompt

Ask questions in natural language and get SQL results.

# Example: "Show me the top 5 customers by total purchases"
{
  "success": true,
  "query": "SELECT c.name, SUM(o.total) as total_purchases FROM customers c...",
  "data": [...]
}

2. get_database_schema

Retrieve the complete database schema.

{
  "success": true,
  "schema": {
    "users": {
      "name": "users",
      "columns": {...}
    }
  }
}

3. build_db_definition

Rebuild the schema cache from the database.

{
  "success": true,
  "message": "Successfully loaded schema for 8 tables",
  "tables": ["users", "orders", "products", ...]
}

Integration with MCP Clients

To use this server with an MCP client (like Claude Desktop), add it to your MCP configuration:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "data-analyst": {
      "command": "python",
      "args": ["/path/to/mcp-data-analyst/server.py"],
      "env": {
        "LLM_API_KEY": "your-key",
        "DB_TYPE": "mysql",
        "DB_HOST": "localhost",
        "DB_NAME": "your_db"
      }
    }
  }
}

Development

Adding a New Database Type

  1. Create a new file in DataAnalyst/database/Type/ (e.g., SQLite.py)
  2. Extend the BaseDatabase abstract class
  3. Implement all required methods: __init__, execute_query, build_definition, close
  4. Add the new type to DbTypes enum
  5. Update DataAnalyst/database/Type/__init__.py to export your class
  6. Update server.py to handle the new database type

Examples

Example 1: Customer Analysis

Query: "Show me the top 10 customers by total order value"

Generated SQL:
SELECT c.customer_name, SUM(o.total_amount) as total_value
FROM customers c
JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.customer_name
ORDER BY total_value DESC
LIMIT 10;

Example 2: Product Inventory

Query: "Which products are low in stock (less than 10 units)?"

Generated SQL:
SELECT product_name, quantity_in_stock
FROM products
WHERE quantity_in_stock < 10
ORDER BY quantity_in_stock ASC;

Contributing

Contributions are welcome! Please ensure:

  1. Code follows PEP 8 style guidelines
  2. All functions have type hints and docstrings
  3. New database types extend BaseDatabase
  4. Changes maintain backward compatibility

Support

For issues, questions, or contributions, please open an issue on the repository.


Built with:

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选