GreptimeDB MCP Server
官方为AI助手提供安全且结构化的方式来探索和分析GreptimeDB中的数据。
文档
greptimedb-mcp-server
一个用于 GreptimeDB 的模型上下文协议 (MCP) 服务器 — GreptimeDB 是一个开源的可观测性数据库,能在单一引擎中处理指标、日志和追踪。
使 AI 助手能够使用 SQL、TQL(兼容 PromQL)和 RANGE 查询来查询和分析 GreptimeDB,并内置了只读强制执行和数据脱敏等安全功能。
快速开始
# Install
pip install greptimedb-mcp-server
# Run (connects to localhost:4002 by default)
greptimedb-mcp-server --host localhost --database public
对于 Claude Desktop,请将此配置添加到您的配置文件中(macOS 上为 ~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"greptimedb": {
"command": "greptimedb-mcp-server",
"args": ["--host", "localhost", "--database", "public"]
}
}
}
功能特性
工具
| 工具 | 描述 |
|---|---|
execute_sql | 执行 SQL 查询,支持格式(csv/json/markdown)和限制选项 |
execute_tql | 执行 TQL(兼容 PromQL)查询,用于时间序列分析 |
query_range | 使用 RANGE/ALIGN 语法执行时间窗口聚合查询 |
describe_table | 检查表概要:模式、语义元数据、最新样本行和查询指导 |
explain_query | 分析 SQL 或 TQL 查询执行计划(analyze=true 用于运行时统计;添加 verbose=true 与 analyze=true 一起使用,以获取每个分区的扫描指标和索引修剪计数器) |
health_check | 检查数据库连接状态和服务器版本 |
管道管理
| 工具 | 描述 |
|---|---|
list_pipelines | 列出所有管道或获取特定管道的详细信息 |
create_pipeline | 使用 YAML 配置创建新管道 |
dryrun_pipeline | 使用样本数据测试管道,而不写入数据库 |
delete_pipeline | 删除管道的特定版本 |
仪表板管理
| 工具 | 描述 |
|---|---|
list_dashboards | 列出所有 Perses 仪表板定义 |
create_dashboard | 创建或更新 Perses 仪表板定义 |
delete_dashboard | 删除仪表板定义 |
资源和提示
- 资源:通过
greptime://<table>/dataURI 浏览表 - 提示:用于常见任务的内置 Jinja 模板 —
pipeline_creator、log_pipeline、metrics_analysis、promql_analysis、trace_analysis、table_operation、schema_design_advisor、observability_correlation、ingestion_troubleshooting、query_performance_tuning
有关 LLM 集成和提示使用,请参阅 docs/llm-instructions.md。
配置
环境变量
GREPTIMEDB_HOST=localhost # Database host
GREPTIMEDB_PORT=4002 # MySQL protocol port (default: 4002)
GREPTIMEDB_USER=root # Database user
GREPTIMEDB_PASSWORD= # Database password
GREPTIMEDB_DATABASE=public # Database name
GREPTIMEDB_TIMEZONE=UTC # Session timezone
# Optional
GREPTIMEDB_HTTP_PORT=4000 # HTTP API port for pipeline/dashboard management
GREPTIMEDB_HTTP_PROTOCOL=http # HTTP protocol (http/https)
GREPTIMEDB_POOL_SIZE=5 # Connection pool size
GREPTIMEDB_MASK_ENABLED=true # Enable sensitive data masking
GREPTIMEDB_MASK_PATTERNS= # Additional patterns (comma-separated)
GREPTIMEDB_AUDIT_ENABLED=true # Enable audit logging
GREPTIMEDB_ALLOW_WRITE=false # Allow write/DDL via execute_sql (DANGEROUS, local/test only)
# Transport (for HTTP server mode)
GREPTIMEDB_TRANSPORT=stdio # stdio, sse, or streamable-http
GREPTIMEDB_LISTEN_HOST=0.0.0.0 # HTTP server bind host
GREPTIMEDB_LISTEN_PORT=8080 # HTTP server bind port
GREPTIMEDB_ALLOWED_HOSTS= # DNS rebinding protection (comma-separated)
GREPTIMEDB_ALLOWED_ORIGINS= # CORS allowed origins (comma-separated)
CLI 参数
greptimedb-mcp-server \
--host localhost \
--port 4002 \
--database public \
--user root \
--password "" \
--timezone UTC \
--pool-size 5 \
--mask-enabled true \
--allow-write false \
--transport stdio
HTTP 服务器模式
适用于容器化或 Kubernetes 部署。需要 mcp>=1.8.0:
# Streamable HTTP (recommended for production)
greptimedb-mcp-server --transport streamable-http --listen-port 8080
# SSE mode (legacy)
greptimedb-mcp-server --transport sse --listen-port 3000
DNS 重绑定保护
默认情况下,为了兼容代理、网关和 Kubernetes 服务,DNS 重绑定保护是禁用的。要启用它,请使用 --allowed-hosts:
# Enable DNS rebinding protection with allowed hosts
greptimedb-mcp-server --transport streamable-http \
--allowed-hosts "localhost:*,127.0.0.1:*,my-service.namespace:*"
# With custom allowed origins for CORS
greptimedb-mcp-server --transport streamable-http \
--allowed-hosts "my-service.namespace:*" \
--allowed-origins "http://localhost:*,https://my-app.example.com"
# Or via environment variables
GREPTIMEDB_ALLOWED_HOSTS="localhost:*,my-service.namespace:*" \
GREPTIMEDB_ALLOWED_ORIGINS="http://localhost:*" \
greptimedb-mcp-server --transport streamable-http
如果您遇到 421 Invalid Host Header 错误,请禁用保护(默认)或将您的主机添加到允许列表中。
安全
只读数据库用户(推荐)
使用 静态用户提供程序 在 GreptimeDB 中创建一个只读用户:
mcp_readonly:readonly=your_secure_password
应用级安全门
所有查询都经过一个安全门,该安全门会:
- 阻止:DROP、DELETE、TRUNCATE、UPDATE、INSERT、ALTER、CREATE、GRANT、REVOKE、EXEC、LOAD、COPY
- 阻止:编码绕过尝试(hex、UNHEX、CHAR)
- 允许:SELECT、SHOW、DESCRIBE、TQL、EXPLAIN、UNION
写入模式(默认禁用)
服务器默认是只读的。对于本地开发或测试,您可以通过启用写入模式,允许通过 execute_sql 工具执行写入/破坏性 SQL(DDL/DML,例如 CREATE、DROP、ALTER、INSERT、UPDATE、DELETE):
# Environment variable
GREPTIMEDB_ALLOW_WRITE=true greptimedb-mcp-server
# Or CLI argument
greptimedb-mcp-server --allow-write true
启用后,execute_sql 的安全门将被绕过,并且服务器在启动时会记录一条警告。
⚠️ 危险:这允许 AI 助手对您的数据库运行破坏性语句。切勿对生产数据启用此功能。如果您只需要读取访问权限,请结合使用只读数据库用户。
数据脱敏
敏感列会根据列名模式自动脱敏(******):
- 身份验证:
password、secret、token、api_key、credential - 财务:
credit_card、cvv、bank_account - 个人:
ssn、id_card、passport
使用 --mask-patterns phone,email 进行配置以添加自定义模式。
审计日志
所有工具调用都会被记录:
2025-12-10 10:30:45 - greptimedb_mcp_server.audit - INFO - [AUDIT] execute_sql | query="SELECT * FROM cpu LIMIT 10" | success=True | duration_ms=45.2
使用 --audit-enabled false 禁用。
开发
# Clone and setup
git clone https://github.com/GreptimeTeam/greptimedb-mcp-server.git
cd greptimedb-mcp-server
uv venv && source .venv/bin/activate
uv sync
# Run tests
pytest
# Format & lint
uv run black .
uv run flake8 src
# Debug with MCP Inspector
npx @modelcontextprotocol/inspector uv --directory . run -m greptimedb_mcp_server.server
许可证
MIT 许可证 - 请参阅 LICENSE.md。
致谢
灵感来源: