使用说明
systemd监控mcp
MCP服务器,用于通过JSON-RPC监控Linux服务器。
功能(MVP)
GET /health公共卫生终点。GET /.well-known/mcp公共MCP发现端点。POST /mcpMCP JSON-RPC端点(受承载令牌保护)。initialize接受现代协议版本(包括2025-03-26)并优雅地进行谈判。- MCP工具:
list_services,list_timers,list_logs. - MCP资源:
resource://services/snapshot,resource://services/failed,resource://logs/recent. - 使用承载令牌进行身份验证
MCP_API_TOKEN.
MCP工具功能
list_services:列表*.service可选单元scope,state,name_contains,limit,以及summary.list_timers:列表*.timer可选单元scope,name_contains,state,limit,sort,order,overdue_only,include_persistent,以及summary.list_logs:列出带有必填项的日志start_utc/end_utc可选scope,priority,unit,exclude_units,grep,order,limit,allow_large_window,以及summary.
scope 支持 system|user|both 默认为 system 对于所有三个列表工具。
配置
注: 强烈建议在反向代理(例如Nginx、HAProxy、Envoy)后面运行此服务 它负责TLS终止并限制网络访问。
不要把这个暴露在互联网上!
看 安全和威胁模型 对于经过身份验证的客户端和失控的代理 威胁边界、攻击场景、控制和剩余风险。服务器允许只读, 非秘密监控披露,仅接受来自令牌持有客户端的拒绝服务风险, 但它不得允许持久的主机/工作负载修改或故意泄露秘密。因为 它使用普通HTTP和静态承载令牌,在流量穿过不受信任的网络时使用TLS和网络访问控制。
| 变量 | 必填 | 默认 | 描述 |
|---|---|---|---|
MCP_API_TOKEN |
是 | - | 静态API令牌(最少16个字符)。 |
BIND_ADDR |
没有 | 127.0.0.1 |
绑定地址。 |
BIND_PORT |
没有 | 8080 |
绑定端口 |
跑
export MCP_API_TOKEN="a-secure-token-at-least-16-chars"
# optional:
# export BIND_ADDR="127.0.0.1"
# export BIND_PORT="8080"
cargo run
API示例
健康
curl -s http://127.0.0.1:8080/health
MCP初始化
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","clientInfo":{"name":"example-client","version":"1.0.0"},"capabilities":{}}}' \
http://127.0.0.1:8080/mcp
MCP工具/清单
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' \
http://127.0.0.1:8080/mcp
MCP工具/呼叫列表_服务
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"list_services","arguments":{"state":"failed","name_contains":"sshd","limit":200}}}' \
http://127.0.0.1:8080/mcp
MCP工具/调用列表计时器
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"list_timers","arguments":{"scope":"both","sort":"next","order":"asc","limit":200}}}' \
http://127.0.0.1:8080/mcp
MCP工具/调用列表_logs
list_logs 对可选过滤器要求严格:
- 省略
priority当你不想要优先级阈值时。有效值为0通过7,或别名,例如err,warning,info,以及debug.priority不是正则表达式字段,因此以下值.*返回JSON-RPC-32602随着invalid_priority. - 省略
unit当你不想要一个单位过滤器时。不发送unit: "";空单元名称返回JSON-RPC-32602随着invalid_unit. - 使用
grep用于消息文本过滤。纯字符串是子字符串过滤器,例如正则表达式精简模式/timeout|failed/. - 保持
start_utc和end_utc因为RFC3339 UTC字符串以结尾Z。超过7天的Windows需要allow_large_window: true. exclude_units空时可以省略。如果存在,每个条目都必须是有效的单位名称。
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"list_logs","arguments":{"scope":"both","priority":"err","unit":"sshd_service","exclude_units":["cron.service"],"grep":"/timeout|failed/","order":"desc","start_utc":"2026-02-27T00:00:00Z","end_utc":"2026-02-27T01:00:00Z","limit":200}}}' \
http://127.0.0.1:8080/mcp
对于原始错误报告中的输入形状,等效的有效请求为:
{
"allow_large_window": true,
"end_utc": "2026-05-17T23:59:59Z",
"grep": "error",
"limit": 20,
"order": "desc",
"scope": "system",
"start_utc": "2026-05-14T00:00:00Z",
"summary": true
}
省略的字段是有意的: priority: ".*" 无效,因为优先级只接受日志记录的严重性阈值,以及 unit: "" 无效,因为提供的单位筛选器必须是非空的单位标识符。
MCP工具/呼叫摘要模式
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"list_services","arguments":{"summary":true}}}' \
http://127.0.0.1:8080/mcp
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"list_timers","arguments":{"scope":"both","summary":true}}}' \
http://127.0.0.1:8080/mcp
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"list_logs","arguments":{"scope":"both","start_utc":"2026-02-27T00:00:00Z","end_utc":"2026-02-27T01:00:00Z","summary":true}}}' \
http://127.0.0.1:8080/mcp
MCP工具/调用无效作用域(验证示例)
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":10,"method":"tools/call","params":{"name":"list_services","arguments":{"scope":"global"}}}' \
http://127.0.0.1:8080/mcp
预期:JSON-RPC错误 -32602 具有稳定的数据代码 invalid_scope.
MCP资源/读取失败服务快照
curl -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MCP_API_TOKEN" \
-d '{"jsonrpc":"2.0","id":5,"method":"resources/read","params":{"uri":"resource://services/failed"}}' \
http://127.0.0.1:8080/mcp
验证
在切换或释放之前使用此序列:
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test
备注
list_logs需要UTC RFC3339时间戳Z后缀为start_utc和end_utc.- 时间窗口超过7天需要
allow_large_window=true. - 定时器和服务工具是只读的,不会改变systemd状态。