MCP数据侦察员
A. 数据发现工具 构建为MCP(模型上下文协议)服务器。 允许AI代理和人类在多个数据源(CSV/Sqlite)中搜索表、列和数据,并能够上传新的数据源。
📋 导航
- 组件分解
- list_sources() - index_source(source_id) - search(query, limit?, source_ids?, match_types?) - get_schema(source_id, path)
- 克劳德桌面/光标
______________________________________________________________________
快速开始
git clone https://github.com/ArgentumX/mcp-data-scout.gitcd mcp-data-scoutcp .env.example .envdocker compose up --build- 流线型UI: http://localhost:8501
- REST API/MCP服务器: http://localhost:8000
- API文件: http://localhost:8000/docs
首次启动时,如果env var SEED为“true”(SQLite+CSV),服务器会自动生成示例数据。 进入 MASTER_API_KEY 从 .env 到现场
然后单击 “索引所有来源” 在前端侧边栏中为它们建立索引。
alt text alt text alt text
MCP快速测试
启动mcp数据侦察(快速启动)
cd mcp-testdocker build -t mcp-data-scout-test .docker run --network host mcp-data-scout-test______________________________________________________________________
建筑
┌─────────────────────────────────────────────────────┐
│ Docker Compose │
│ │
│ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ data-scout-ui │ │ data-scout-mcp │ │
│ │ (Streamlit) │─────▶│ (FastAPI + MCP) │ │
│ │ port 8501 │ HTTP │ port 8000 │ │
│ └──────────────────┘ └──────────┬───────────┘ │
│ │ │
│ ┌─────────▼──────────┐ │
│ │ /data (volume) │ │
│ │ uploads/ │ │
│ │ sqlite_main.db │ │
│ │ *.csv │ │
│ │ index.db (FTS5) │ │
│ │ sources.json │ │
│ └────────────────────┘ │
└─────────────────────────────────────────────────────┘组件分解
| 层 | 模块 | 责任 |
|---|---|---|
| 连接器 | connectors/ | 从源读取模式和数据 |
| 索引规则 | connectors/abstraction/base.py | 为每个连接器自定义索引规则 |
| BaseConnector | connectors/abstraction/base.py | 基础抽象数据源连接器类 |
| CSV连接器 | connectors/csv_connector.py | 单文件CSV连接器 |
| SQLite连接器 | connectors/sqlite_connector.py | 单文件SQLite连接器 |
| 索引器 | index/indexer.py | 将元数据存储在SQLite FTS5索引中 |
| 搜索 | search/engine.py | 全文+元数据LIKE搜索 |
| MCP工具 | server/mcp_app.py | FastMCP工具定义(SSE端点) |
| REST API | server/api_app.py | Streamlit UI的FastAPI端点 |
| 用户界面 | frontend/app.py | 流线型网络界面 |
| 注册表 | server/source_registry.py | 管理连接器实例+清单持久性 |
______________________________________________________________________
种子数据描述
| ID | 类型 | 描述 |
|---|---|---|
seeded_sqlite_main | SQLite | 业务数据库:客户、订单、产品、员工、订单项 |
seeded_sales_regions | CSV | 区域销售数据 |
seeded_marketing_campaigns | CSV | 营销活动绩效数据 |
seeded_inventory_snapshot | CSV | 跨仓库的库存快照 |
______________________________________________________________________
添加您自己的数据
通过Web UI
打开 添加源 Streamlit UI中的选项卡:
- csv文件 --上传任意
.csv文件,给它一个唯一的源ID,并可选地提供JSON格式的索引规则。 - SQLite数据库 --上传a
.db/.sqlite文件,给它一个唯一的源ID,并可选地提供索引规则。
上传的文件存储在 /data/uploads/ 由于 来源清单 /data/sources.json.
上传后,点击 立即索引 在侧边栏中的新源代码旁边。
索引规则JSON
每个上传的源都可以有自己的索引规则来控制索引的内容 以及行值是否可搜索。所有字段都是可选的。
{
"include_tables": ["sales", "products"],
"exclude_tables": ["logs", "audit"],
"exclude_columns": {
"users": ["password_hash", "token"]
},
"row_value_tables": ["products", "orders"],
"row_value_columns": {
"products": ["name", "category"],
"orders": ["status", "shipping_city"]
}
}| 字段 | 类型 | 效果 |
|---|---|---|
include_tables | 字符串列表 | 仅索引这些表 |
exclude_tables | 字符串列表 | 跳过这些表 |
include_columns | {table: [cols]} | 索引仅列出每个表的列 |
exclude_columns | {table: [cols]} | 跳过每个表中列出的列 |
row_value_tables | 字符串列表 | 为这些表启用行值索引 |
row_value_columns | {table: [cols]} | 将这些列索引为可搜索的行值 |
留空或 {} 无限制地索引所有内容。
______________________________________________________________________
MCP工具
服务器在以下位置公开MCP工具 http://localhost:8000/mcp/sse:
list_sources()
返回所有具有索引状态的已注册数据源。
[
{
"source_id": "sqlite_main",
"source_type": "sqlite",
"description": "Main SQLite database with business data",
"location": "/data/uploads/sqlite_main.db",
"is_indexed": true
}
]index_source(source_id)
从源读取模式并将元数据存储在FTS5索引中。
{ "success": true, "source_id": "sqlite_main", "tables_indexed": 5 }search(query, limit?, source_ids?, match_types?)
在表名、列名和索引行值之间进行全文搜索。 返回按匹配类型分组的排名结果(表→ 列→ row).
[
{
"match_type": "table",
"source_id": "sqlite_main",
"table_name": "customers",
"row_count": 200,
"columns": [{"name": "email", "data_type": "TEXT", "sample_values": ["..."]}]
}
]get_schema(source_id, path)
返回特定表的完整列架构+5个示例行。
{
"success": true,
"table_name": "orders",
"row_count": 500,
"columns": [...],
"sample_rows": [...]
}______________________________________________________________________
REST API
| 方法 | 路径 | 描述 |
|---|---|---|
| 得到 | /health | 健康检查 |
| 得到 | /api/sources | 列出所有来源(包括 is_dynamic 旗帜) |
| 职位 | /api/index/{source_id} | 索引特定来源 |
| 职位 | /api/index-all | 索引所有来源(每个来源的返回结果+失败列表) |
| 得到 | /api/search?q=... | 搜索元数据 |
| 得到 | /api/schema/{source_id}/{path} | 获取表架构 |
| 得到 | /api/tables?source_id=... | 列出所有索引表 |
| 得到 | /api/index-stats | 按来源索引统计数据 |
| 职位 | /api/upload/csv | 上传CSV文件作为新源 |
| 职位 | /api/upload/sqlite | 上传SQLite数据库作为新源 |
| 删除 | /api/sources/{source_id} | 删除用户上传的源 |
| 得到 | /docs | 交互式Swagger用户界面 |
______________________________________________________________________
连接AI代理
克劳德桌面/光标
添加到MCP配置中:
{
"mcpServers": {
"type": "sse",
"data-scout": {
"url": "http://localhost:8000/mcp/sse"
},
"headers": {
"X-API-KEY": "DEV_MASTER_API_KEY"
}
}
}然后,代理可以呼叫:
list_sources()
index_source("sqlite_main")
search("customer email")
get_schema("sqlite_main", "customers")______________________________________________________________________
项目结构
mcp-data-scout/
├── backend/
│ ├── connectors/
│ │ ├── abstraction/base.py # BaseConnector, IndexingRules, data models
│ │ ├── csv_connector.py # Single-file CSV connector
│ │ └── sqlite_connector.py # SQLite connector
│ ├── index/
│ │ └── indexer.py # SQLite FTS5 metadata indexer
│ ├── search/
│ │ └── engine.py # Full-text search engine
│ ├── server/
│ │ ├── api_app.py # FastAPI REST endpoints (incl. upload)
│ │ ├── config.py # Environment config
│ │ ├── mcp_app.py # FastMCP tool definitions
│ │ ├── server.py # Combined server entry point
│ │ ├── services.py # Shared singletons (registry, indexer, engine)
│ │ └── source_registry.py # ManagedRegistry + manifest persistence
│ └── scripts/
│ ├── seed_data.py # Generates sample SQLite + CSV data
│ └── entrypoint.sh # Docker entrypoint
├── frontend/
│ └── app.py # Streamlit web UI
├── docker-compose.yml
├── .env.example
└── requirements.txt______________________________________________________________________
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
UPLOADS_DIR | /data/uploads | 存储用户上传文件的目录 |
SOURCES_MANIFEST | /data/sources.json | 保存动态添加源的JSON文件 |
INDEX_DB_PATH | /data/index.db | FTS5索引数据库的路径 |
MCP_HOST | 0.0.0.0 | 服务器绑定地址 |
MCP_PORT | 8000 | 服务器端口 |
API_BASE_URL | http://backend:8000 | Streamlit UI使用的后端URL |
MASTER_API_KEY | DEV_MASTER_API_KEY | 必需。用于大多数端点的API密钥 |
SEED | true | 数据源种子设定标志 |
______________________________________________________________________
搜索索引设计
元数据存储在 SQLite FTS5 虚拟表——一个内置的全文搜索引擎,没有外部依赖关系。
三个索引实体:
table_fts—source_id,source_type,table_name,pathcolumn_fts—source_id,table_name,column_name,data_typerow_fts—source_id,table_name,row_text(连接索引列值)
搜索策略(优先级顺序):
- FTS5前缀匹配——快速,按BM25相关性排名
LIKE %query%回退--捕获部分中间词匹配
