Linux Diagnostics MCP Server

Linux Diagnostics MCP Server

This MCP server provides read-only Linux system diagnostics tools for inspecting system information, processes, and log snapshots. It enables AI models to analyze Linux system health, troubleshoot issues, and review security through workflow prompts and HTTP transport with API key authentication.

Category
访问服务器

README

Linux Diagnostics MCP Server - Lecture Demo

A Python/Linux adaptation of the original MCPDemo teaching repository. This repo now reaches Milestone 7 parity for the public teaching flow: compact system inspection, Linux process drill-down, log snapshots as resources, workflow prompts, authenticated MCP over HTTP on /mcp, explicit elicitation before process termination, sampling-assisted Linux diagnostics, and allowed-root proc/sys snapshots.

What This Demo Shows

This lecture demo now includes:

  • Tools: Linux diagnostics tools for get_system_info, get_process_list, get_process_by_id, get_process_by_name, and elicitation-gated kill_process
  • Resources: paged syslog://snapshot/... log snapshot resources
  • Prompts: MCP workflow prompts for error analysis, CPU investigation, security review, and health diagnosis
  • HTTP transport: streamable MCP over http://127.0.0.1:5000/mcp
  • API key auth: X-API-Key header or ?apiKey=secure-mcp-key
  • AI Chat Client: a Python Azure OpenAI client that launches the local HTTP server, lets the model call MCP tools, prompts, and resources, and handles local form elicitation in the terminal
  • Python 3.12 implementation with the official MCP Python SDK
  • Multiple testing methods
  • Milestone 5 elicitation for kill_process
  • Milestone 6 sampling-assisted Linux diagnostics
  • Milestone 7 roots for read-only /proc and /sys snapshots

Quick Start

1. Install

Server-only install:

python3 -m pip install --user --break-system-packages -e .

Install the lecture chat client extras:

python3 -m pip install --user --break-system-packages -e '.[llm]'

2. Quick Smoke Test (No LLM)

python3 scripts/smoke_test.py

This script:

  1. Starts the local HTTP MCP server
  2. Verifies 401 Unauthorized without an API key
  3. Performs the MCP initialize handshake on /mcp
  4. Confirms mcp-session-id flow works across requests
  5. Discovers tools, prompts, and resource templates
  6. Exercises the system, process, log snapshot, proc snapshot, and sampling-assisted diagnostics flows
  7. Verifies kill_process fails safely when the client does not advertise elicitation support
  8. Verifies the lecture chat client fails safely when Azure OpenAI settings are missing

3. Run the Server Manually

python3 -m mcp_linux_diag_server

The server listens on:

  • endpoint: http://127.0.0.1:5000/mcp
  • demo API key: secure-mcp-key

4. Test with MCP Inspector or VS Code MCP config

Start the server in one terminal, then connect using the HTTP endpoint above.

This repo includes .vscode/mcp.json with the required header:

{
  "servers": {
    "linux-diag-demo": {
      "url": "http://127.0.0.1:5000/mcp",
      "headers": {
        "X-API-Key": "secure-mcp-key"
      }
    }
  }
}

If your inspector accepts a URL directly, this query-string form also works:

http://127.0.0.1:5000/mcp?apiKey=secure-mcp-key

5. Use the Lecture Chat Client

Copy the sample environment file and fill in your local Azure OpenAI settings:

cp .env.example .env.local
$EDITOR .env.local
python3 -m mcp_linux_diag_server.client --prompt "Summarize this machine."

To mirror the original .NET credential flow more closely, set:

MCP_DEMO_AZURE_OPENAI_USE_DEFAULT_CREDENTIAL=true

and omit the API key.

Run interactive chat:

python3 -m mcp_linux_diag_server.client

Or run a single prompt:

python3 -m mcp_linux_diag_server.client --prompt "What is the system information?"

The Tools

System Information

  • get_system_info - Returns a compact Linux or WSL system snapshot
    • Host name
    • Current user
    • Linux distribution description
    • Kernel release
    • Architecture
    • Logical CPU count
    • Python runtime
    • Current working directory
    • Uptime
    • Load averages
    • Memory summary
    • WSL detection flag

Process Inspection

  • get_process_list - Returns a lightweight list of running processes with names and PIDs
  • get_process_by_id - Returns detailed Linux process information for one PID
  • get_process_by_name - Returns paged detailed process information for a process name
    • Defaults to page_number=1
    • Defaults to page_size=5
    • Keeps the list-first, detail-second teaching flow from the original demo
  • kill_process - Terminates a Linux process only after explicit elicitation
    • If process_id is omitted, the server samples the top CPU consumers and asks the client to choose one
    • The server always requires the typed confirmation phrase CONFIRM PID {pid}
    • The lecture client handles these prompts locally in the terminal when stdin/stdout are interactive
  • troubleshoot_linux_diagnostics - Uses sampling to convert a natural-language Linux diagnostics question into a validated /proc or /sys read
    • The server validates the sampled path and field against an allowlist before reading anything
    • Exact Python adaptation: the sampled query is a single safe PATH or PATH | grep FIELD line instead of WQL
    • The server then samples again to summarize the observation back to the user
  • create_proc_snapshot - Creates an immutable read-only snapshot from an allowed /proc or /sys path and returns resource URIs
    • File snapshots page line-by-line content
    • Directory snapshots page deterministic child metadata without following symlinks
    • Enforces explicit allowed roots before reading anything
  • request_proc_access - Uses elicitation to request read-only access to an additional /proc or /sys root
    • Adds the approved root to the server's in-memory allowlist
    • Lets the model ask for access proactively before a blocked snapshot attempt

Log Snapshots

  • create_log_snapshot - Creates an immutable snapshot from a common Linux log file and returns resource URIs
    • Supports system, security, kernel, and package log groups
    • Optional filter_text narrows the snapshot to matching lines
    • Returns a base resource URI plus a paginated resource template

Resources

  • syslog://snapshot/{snapshot_id} - Reads a stored Linux log snapshot with default pagination
  • syslog://snapshot/{snapshot_id}?limit={limit}&offset={offset} - Reads a specific page from a stored snapshot
  • proc://snapshot/{snapshot_id} - Reads a stored proc/sys snapshot with default pagination
  • proc://snapshot/{snapshot_id}?limit={limit}&offset={offset} - Reads a specific page from a stored proc/sys snapshot

Every resource read returns:

  • snapshot metadata
  • captured entries
  • pagination metadata (total_count, returned_count, limit, offset, has_more, next_offset)

Prompts

  • AnalyzeRecentApplicationErrors - Error-focused log analysis workflow
  • ExplainHighCpu - Correlate CPU-heavy processes with Linux logs
  • DetectSecurityAnomalies - Review suspicious processes plus auth/security log evidence
  • DiagnoseSystemHealth - End-to-end system health workflow
  • TroubleshootLinuxComponent - Focused deep-dive workflow that steers the agent toward troubleshoot_linux_diagnostics

Projects

src/mcp_linux_diag_server/server.py

The authenticated HTTP MCP server exposing the Milestone 1-7 diagnostics tools, resources, and workflow prompts.

src/mcp_linux_diag_server/client.py

The lecture chat client that:

  • launches the local HTTP server
  • connects over streamable HTTP with the demo API key
  • exposes MCP prompt/resource APIs as helper tools for the model
  • fulfills MCP form elicitation in the local terminal when the model triggers kill_process
  • fulfills MCP sampling requests so the server can synthesize safe Linux diagnostics queries and summaries
  • teaches the model to request proc/sys access before snapshotting blocked paths
  • executes tool-calling turns

Testing Methods

Method Visual Interactive LLM Best For
python3 scripts/smoke_test.py ❌ No ❌ No ❌ No quick verification of M1-M7 server behavior
MCP Inspector / .vscode/mcp.json ✅ Yes ✅ Yes ❌ No development, debugging, teaching
python3 -m mcp_linux_diag_server.client ❌ No ✅ Yes ✅ Yes lecture demo flow

For the Milestone 1 validation checklist that still underpins the base lecture flow, see M1_VALIDATION_GUIDE.md.

Project Structure

MCPPythonDemo/
├── README.md
├── LICENSE.txt
├── pyproject.toml
├── .env.example
├── .vscode/
│   └── mcp.json
├── scripts/
│   └── smoke_test.py
├── src/
│   └── mcp_linux_diag_server/
│       ├── __main__.py
│       ├── client.py
│       ├── http_config.py
│       ├── server.py
│       └── tools/
│           ├── log_snapshots.py
│           ├── proc_snapshots.py
│           ├── processes.py
│           └── system_info.py
├── tests/
│   ├── http_harness.py
│   ├── test_client.py
│   ├── test_m1_smoke.py
│   ├── test_m2_smoke.py
│   ├── test_m3_smoke.py
│   ├── test_m4_http.py
│   ├── test_log_snapshots.py
│   ├── test_processes.py
│   └── test_system_info.py

Requirements

  • Python 3.12+
  • mcp[cli]
  • Azure OpenAI only if you want to run the lecture chat client

Milestones

Milestone 1 - Minimal diagnostics tool over stdio plus lecture chat client
Milestone 2 - Process inspection
Milestone 3 - Log snapshot resources and prompts
Milestone 4 - HTTP transport and security
Milestone 5 - Elicitation-backed kill_process
Milestone 6 - Sampling-assisted Linux diagnostics
Milestone 7 - Roots and proc/sys snapshots

License

MIT. See LICENSE.txt.

Resources

推荐服务器

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 多个工具。

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

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

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

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

官方
精选
TypeScript
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 模型以安全和受控的方式获取实时的网络信息。

官方
精选