LogAnalyze-MCP
Integrates with a local Ollama instance to provide semantic log analysis and root cause diagnosis, including checking Ollama connection and model status, with automatic fallback to a built-in rule engine when Ollama is unavailable.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@LogAnalyze-MCP 分析一下 D:/logs/app.log 最近的错误,告诉我根因和修复方案"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
LogAnalyze-MCP
基于 MCP(Model Context Protocol) 的本地日志智能分析服务,让 Cursor / Claude Code / Trae 等 AI Agent 直接读取、筛选、统计并自动诊断你的日志文件。
为什么做这个项目
排查线上问题时,开发者通常要反复执行「打开日志 → 找 ERROR → 搜关键词 → 看堆栈 → 百度报错」。 LogAnalyze-MCP 把这套流程封装成标准 MCP 工具,AI Agent 用一句自然语言就能自主完成排查,并给出根因与修复建议。
🔒 纯本地运行:日志文件不离开本机,基于 stdio 通信
🧠 双引擎分析:本地 Ollama 大模型语义分析 + 内置规则引擎兜底(未装 Ollama 也能用)
📦 零配置上手:9 个工具覆盖日志排查 80% 日常场景
🪶 轻量无依赖:仅需 Python,流式读取 GB 级大日志不爆内存
🗜️ 压缩日志直读:透明支持
.gz(如app.log.gz)与.zip(支持多成员),无需先手动解压
Related MCP server: Local Lens
功能一览(9 个 MCP 工具)
工具 | 作用 |
| 列出目录下日志文件(含 |
| 读取日志末尾 N 行(恒定内存,适合超大文件) |
| 读取日志开头 N 行(查看启动信息) |
| 关键词/正则搜索,带回溯上下文 |
| 按 FATAL/ERROR/WARN 等级别筛选并计数 |
| 统计报告:行数、时间范围、级别分布、高频错误 Top5 |
| 智能诊断:抽取错误 → 根因分析 → 修复建议 |
| 直接分析粘贴的报错/堆栈片段 |
| 检查 Ollama 连接与模型状态 |
快速开始
1. 环境要求
Python 3.10+
(可选)Ollama:启用大模型语义分析;不安装则使用规则引擎
2. 获取代码并安装依赖
git clone https://github.com/<your-name>/log-analyze-mcp.git
cd log-analyze-mcp
# 建议使用虚拟环境
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
pip install -r requirements.txt3.(可选)启用本地大模型
# 安装 Ollama 后拉取模型(CPU 可选 3b/1.5b,追求效果选 7b)
ollama pull qwen2.5:7b如需自定义,复制 .env.example 为 .env,修改 OLLAMA_BASE_URL、OLLAMA_MODEL。
4. 验证安装
# 单元测试(不依赖 MCP/Ollama)
python tests/test_tools.py
# 端到端验证:启动 MCP server → 握手 → 真实调用工具
python examples/test_client.py接入 AI Agent
Cursor
Settings → Features → MCP → Add new MCP server,填入(路径替换为本机实际路径):
{
"mcpServers": {
"log-analyze": {
"command": "python",
"args": ["-u", "src/server.py"],
"cwd": "C:/path/to/log-analyze-mcp",
"env": {
"OLLAMA_MODEL": "qwen2.5:7b"
}
}
}
}完整模板见 examples/cursor-config.json。
Claude Code / Claude Desktop
复制 examples/claude-code-mcp.json 中的 mcpServers 段到项目 .mcp.json 或 claude_desktop_config.json。
使用示例
接入后,直接在 Agent 对话框用自然语言:
「帮我看看
D:/logs目录下有哪些日志文件」「分析一下
app.log最近的错误,告诉我根因和修复方案」「搜索日志里所有
timeout相关记录,带 3 行上下文」「统计这个日志里 ERROR 和 WARN 各有多少条」
Agent 会自动编排工具调用链:
list_log_files → get_log_stats → filter_log_by_level → analyze_log,最终输出结构化诊断报告。
工作原理
┌──────────────┐ stdio / JSON-RPC ┌──────────────────────┐ HTTP ┌──────────┐
│ AI Agent │ ◀──────────────────▶ │ LogAnalyze-MCP │ ◀─────────────▶ │ Ollama │
│ Cursor / CC │ tools/call │ (FastMCP Server) │ (本地,可选) │ 本地大模型 │
└──────────────┘ │ 9 个日志分析工具 │ └──────────┘
│ 规则引擎(兜底) │
└──────────────────────┘
│ 只读
▼
本地日志文件(不上传)AI Agent 作为 MCP Host,通过 stdio 启动本服务并完成能力协商;
Agent 自主规划排查步骤,按需调用工具;
工具流式读取本地日志,返回结构化文本;
诊断类工具调用本地 Ollama 生成根因分析;Ollama 不可用时自动降级为规则引擎。
项目结构
log-analyze-mcp/
├── src/
│ ├── server.py # MCP 服务入口(FastMCP,9 个工具)
│ ├── config.py # 配置加载(环境变量 > config.json > 默认值)
│ ├── tools/
│ │ ├── log_reader.py # 日志读取/搜索/筛选/统计
│ │ └── log_analyzer.py # 智能诊断编排
│ └── llm/
│ └── ollama_client.py # Ollama 客户端 + 规则引擎
├── examples/
│ ├── sample.log # 演示用样例日志
│ ├── test_client.py # 端到端验证脚本
│ ├── cursor-config.json
│ └── claude-code-mcp.json
├── tests/test_tools.py
├── requirements.txt
└── config.json安全与合规说明
本工具仅做本地只读日志分析,不会修改、删除、上传任何文件;
请仅在你有权限访问的机器与日志上使用;
规则引擎与大模型的分析结论仅供参考,生产环境请结合监控与代码复核。
路线图
支持 gzip/zip 压缩日志直接读取(v1.1.1)
支持日志多格式自动解析(JSON Lines、自定义正则、logback 模板)
错误时间线视图与跨文件关联分析
支持 OpenAI 兼容接口(如内网 vLLM 部署)
可选 Web 看板(只读)
License
Available Tools
9 toolsanalyze_error_textA
对用户直接粘贴的一段报错/堆栈文本进行智能根因分析(无需日志文件)。
Args: error_text: 报错信息或异常堆栈原文。
Inputs: error_text (required). Returns: result
analyze_logA
智能分析日志:自动抽取错误,给出根因、证据与修复建议。
优先使用本地 Ollama 大模型;未安装或未启动时自动降级为内置规则引擎。
Args: file_path: 日志文件绝对路径。 max_lines: 送入分析的最大日志行数,0 表示使用配置默认值(400)。
Inputs: file_path (required), max_lines. Returns: result
check_llm_statusA
检查本地 Ollama 服务与模型是否可用;不可用时会提示安装命令与降级状态。
Inputs: no named parameters. Returns: result
filter_log_by_levelA
按日志级别筛选记录并统计条数;筛选某级别时会同时包含更严重级别。
Args: file_path: 日志文件绝对路径。 level: 目标级别,可选 FATAL/ERROR/WARN/INFO/DEBUG/TRACE,默认 ERROR。
Inputs: file_path (required), level. Returns: result
get_log_statsB
生成日志统计报告:文件大小、总行数、时间范围、级别分布、高频错误 Top5。
Args: file_path: 日志文件绝对路径。
Inputs: file_path (required). Returns: result
list_log_filesA
列出指定目录下的所有日志文件(.log/.txt/.out/.err/.trace),含大小与修改时间。
Args: dir_path: 日志目录的绝对路径。 recursive: 是否递归扫描子目录,默认 True。
Inputs: dir_path (required), recursive. Returns: result
read_log_headA
读取日志文件开头指定行数(适合查看启动参数与启动阶段日志)。
Args: file_path: 日志文件绝对路径。 lines: 要读取的行数,默认 100,最大 5000。
Inputs: file_path (required), lines. Returns: result
read_log_tailA
读取日志文件末尾指定行数(适合查看最新报错,对超大文件同样高效)。
Args: file_path: 日志文件绝对路径。 lines: 要读取的行数,默认 100,最大 5000。
Inputs: file_path (required), lines. Returns: result
search_log_keywordB
在日志中按关键词或正则表达式搜索,返回命中行及其上下文。
Args: file_path: 日志文件绝对路径。 keyword: 关键词;regex 为 True 时视为正则表达式。 regex: 是否按正则匹配,默认 False(普通子串匹配)。 context: 每个命中前后展示的上下文行数,默认 2。 max_results: 最多返回的命中组数,默认 50。
Inputs: keyword (required), file_path (required), regex, context, +1 more. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 9 tools
Each tool targets a distinct operation: listing, head/tail reading, keyword search, level filtering, stats, and AI analysis. The main potential overlap is between search_log_keyword and filter_log_by_level, but their descriptions clearly distinguish regex/keyword matching from level-based severity filtering. Boundaries are clear enough that misselection is unlikely.
All tools use snake_case with a consistent verb_noun pattern (list_log_files, read_log_tail, search_log_keyword, filter_log_by_level, get_log_stats, analyze_log, check_llm_status). The convention is predictable and readable throughout.
Nine tools is well-scoped for a log analysis server, with each tool earning its place across the read/search/filter/analyze lifecycle. No redundancy or bloat.
Coverage is strong: discovery, head/tail reads, keyword and level filtering, stats, AI-driven root-cause analysis for both files and pasted text, plus an LLM status check. Minor gaps exist (e.g., no cross-file aggregation or export/report-writing operation), but core log analysis workflows are fully supported.
Maintenance
Related MCP Connectors
Query application logs, traces, and metrics from your AI coding assistant via Foam's MCP server.
MCP server for your apps' tools and custom tools, plus hosted AI agents and approval-gated workflows
Remote MCP access to user-authorized Linux, Windows and Intel Mac devices for AI assistants.
Persistent memory, knowledge graph, task management and more for AI agents via MCP.
Related MCP Servers
- FlicenseBqualityCmaintenanceEnables AI-assisted analysis of log files through advanced searching, filtering, and test execution capabilities. Supports time-based queries, pattern matching, test summarization, and code coverage reporting directly within compatible MCP clients.12-
- AlicenseNot gradedqualityDmaintenanceA 100% local development monitoring tool that captures browser console logs, network requests, and backend server output for analysis by AI assistants via MCP. It enables LLMs to debug applications by providing structured, real-time access to full-stack log data and persistent local storage.7MIT
- AlicenseNot gradedqualityBmaintenanceAn MCP server that connects Claude (or any MCP compatible client) to your existing log infrastructure. Query, summarize, and trace logs in plain English across GCP Cloud Logging, AWS CloudWatch, Azure Log Analytics, Grafana Loki, and Elasticsearch without writing filter expressions or leaving your editor.5MIT
- AlicenseNot gradedqualityDmaintenanceAggregates logs from multiple processes and provides an MCP server for AI agents to access, search, and manage logs in real-time.27 npm91BSD 3-Clause