Kibana MCP Server
🔍 Kibana 日志查询 MCP Server - 提供自动认证、会话管理和多种日志查询工具
功能特性
✅ 自动认证管理
- 自动根据
KIBANA_USERNAME和KIBANA_PASSWORD环境变量登录 - 自动获取并管理
sidcookie - 会话过期自动续期
- 401 错误透明处理并重新登录
✅ 丰富的查询工具
- 日志搜索(关键词查询 + DSL 查询)
- 聚合统计(按服务、按时间)
- 错误日志快速搜索
- 原始 Elasticsearch DSL 查询
安装
# 1. 安装 Python 依赖
pip install -r requirements.txt
# 或使用 uv (推荐)
uv pip install -r requirements.txt
运行方式 (推荐使用 uvx)
你可以直接使用 uvx 从本地目录运行:
uvx --from /tmp/kibana-mcp-server kibana-mcp-server
配置 MCP
Claude Desktop / OpenCode 配置
{
"mcpServers": {
"kibana": {
"command": "uvx",
"args": [
"--from",
"/tmp/kibana-mcp-server",
"kibana-mcp-server"
],
"env": {
"KIBANA_URL": "https://logs.example.com",
"KIBANA_VERSION": "8.17.1",
"KIBANA_USERNAME": "your_username",
"KIBANA_PASSWORD": "your_password"
}
}
}
}
1. kibana_search_logs
搜索日志(支持关键词搜索和完整 DSL 查询)
参数:
query- 搜索关键词或完整 Elasticsearch DSL JSONtime_range- 时间范围,默认 "now-1h"size- 返回数量,默认 20index_pattern- 索引模式,默认 "logstash-*"fields- 返回字段列表
示例 1: 关键词搜索
{
"query": "user_id",
"time_range": "now-30m",
"size": 10
}
示例 2: DSL 查询
{
"query": "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"kubernetes.container_name\":\"api-server\"}}]}}}",
"time_range": "now-1h",
"size": 50
}
3. kibana_aggregate_logs
聚合统计日志
参数:
aggregation_type- 聚合类型: "by_service" | "by_time" | "custom"time_range- 时间范围,默认 "now-1h"filter- 可选过滤条件(DSL JSON)custom_aggregation- 自定义聚合(仅 custom 类型)index_pattern- 索引模式
示例 1: 按服务统计
{
"aggregation_type": "by_service",
"time_range": "now-24h"
}
示例 2: 按时间统计
{
"aggregation_type": "by_time",
"time_range": "now-6h"
}
4. kibana_get_latest_logs
快速获取最新日志
参数:
service- 服务名称(可选)size- 返回数量,默认 10index_pattern- 索引模式
示例:
{
"service": "proxy-gateway",
"size": 20
}
5. kibana_search_errors
搜索错误日志
参数:
service- 服务名称(可选)severity- 严重程度: "error" | "exception" | "critical" | "all"time_range- 时间范围,默认 "now-1h"size- 返回数量,默认 20index_pattern- 索引模式
示例:
{
"service": "api-server",
"severity": "exception",
"time_range": "now-2h",
"size": 30
}
6. kibana_raw_query
执行原始 Elasticsearch DSL 查询(高级)
参数:
path- Elasticsearch 路径,默认 "/logstash-*/_search"query- 完整的 Elasticsearch DSL JSON 字符串
示例:
{
"path": "/logstash-*/_search",
"query": "{\"query\":{\"match_all\":{}},\"size\":5}"
}
使用流程
1. 配置环境变量
在 MCP 配置文件中设置 KIBANA_USERNAME 和 KIBANA_PASSWORD。
2. 查询日志
直接调用工具即可,系统会自动处理登录:
使用工具: kibana_search_logs
参数:
query: "error"
time_range: "now-1h"
size: 20
3. 统计分析
使用工具: kibana_aggregate_logs
参数:
aggregation_type: "by_service"
time_range: "now-24h"
技术细节
认证机制
- 系统从环境变量加载
KIBANA_USERNAME和KIBANA_PASSWORD - 首次请求时自动登录 Kibana
- 获取并保存
sidcookie(Iron 加密格式) - 后续请求自动携带 cookie
- 遇到 401 错误时自动重新登录
会话管理
- 会话超时: 23 小时(避免 24 小时边界问题)
- 自动检测: 每次请求前检查会话有效性
- 透明续期: 用户无需关心会话状态
错误处理
- ✅ 401 Unauthorized → 自动重新登录
- ✅ 网络超时 → 30 秒超时配置
- ✅ JSON 解析错误 → 友好错误提示
- ✅ 参数验证 → Pydantic 模型验证
常见时间范围
| 表达式 | 含义 |
|---|---|
now-5m |
最近 5 分钟 |
now-30m |
最近 30 分钟 |
now-1h |
最近 1 小时 |
now-6h |
最近 6 小时 |
now-24h |
最近 24 小时 |
now-7d |
最近 7 天 |
now-30d |
最近 30 天 |
常见字段
@timestamp- 日志时间戳log- 日志内容kubernetes.container_name- Kubernetes 容器名(服务名)kubernetes.namespace_name- Kubernetes 命名空间kubernetes.pod_name- Pod 名称stream- 输出流(stdout/stderr)
故障排查
问题: "Kibana 凭证未配置"
解决: 在 MCP 配置文件(如 mcp_config.json)的 env 部分设置 KIBANA_USERNAME 和 KIBANA_PASSWORD。
问题: "登录失败: 401"
解决: 检查环境变量中的用户名和密码是否正确
问题: "请求超时"
解决:
- 检查网络连接
- 减小查询范围(
size参数) - 缩短时间范围
问题: 查询返回空结果
解决:
- 检查索引模式是否正确
- 扩大时间范围
- 检查查询语法
开发
运行测试
python server.py
调试模式
设置环境变量启用详细日志:
export DEBUG=1
python server.py
许可证
MIT License
Metadata
Release files for kibana-mcp-server 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kibana_mcp_server-0.0.2.tar.gz | 12.5 kB | Without provenance | Details for kibana_mcp_server-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kibana_mcp_server-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for kibana_mcp_server-0.0.2-py3-none-any.whl |
Total release size: 22.0 kB
Release files / kibana_mcp_server-0.0.2.tar.gz
| Download URL | kibana_mcp_server-0.0.2.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
98b87de50db05182ee655ae96b31d1733d3c7a4dd41e32eab82bbfc44924e86f
|
|
BLAKE2b-256 checksum How to use checksums |
3d030fc789a89f6de59eb69016bbc55f29ab0c4123c21b7d6b3d5cd829b91f1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / kibana_mcp_server-0.0.2-py3-none-any.whl
| Download URL | kibana_mcp_server-0.0.2-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a8a84f1e184a283205ed9e583e0940141ce75d8a412fdeea6efbfa1c556c1d0
|
|
BLAKE2b-256 checksum How to use checksums |
9eb9180cd3abb54965e5e21f5290fdf275e8769ee567cc9edc481403acddcbfa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for kibana-mcp-server 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.