ai newsMi., 21. Okt.
EilmeldungMake AI Project Warnings Traceable—and Retire Them When Facts Change

How to Turn Claude & Cursor into a Safe Linux Sysadmin Assistant with an MCP Server in Python

How to Turn Claude & Cursor into a Safe Linux Sysadmin Assistant with an MCP Server in Python Autonomous AI assistants in Claude Desktop , Cursor , Windsurf , and Antigravity are capable of parsing stack traces, diagnosing compilation errors, and suggesting Linux system configurations. However, whe

dev.toMittwoch 04:43 · 21.10.4 Min. Lesezeit
Bild: dev.to

Autonomous AI assistants in Claude Desktop, Cursor, Windsurf, and Antigravity are capable of parsing stack traces, diagnosing compilation errors, and suggesting Linux system configurations. However, when developers grant raw shell execution tools to an AI assistant, they constantly encounter three crippling failure modes:

The standardized solution is Model Context Protocol (MCP), Anthropic's open protocol for connecting AI models to local tools and system resources safely.

In this tutorial, we will build SysFlow MCP in Python: a safe, non-blocking sysadmin assistant with hard timeout controls, non-interactive environment enforcement, and user-level systemd auditing.

[ AI Agent: Claude / Cursor / Antigravity ] │ JSON-RPC 2.0 (stdio) ▼ [ SysFlow MCP Server ] │ [ Non-Interactive Environment ] ├─ PAGER=cat ├─ GIT_PAGER=cat ├─ DEBIAN_FRONTEND=noninteractive └─ TERM=dumb │ [ Hard Timeout Control ] └─ Auto-kills process tree on timeout │ ┌────────────────┼────────────────┐ ▼ ▼ ▼ [ System Resources ] [ Bounded Logs ] [ Systemd --user ] (CPU / RAM / Disk) (Seek from EOF) (Services / Timers) Enter fullscreen mode Exit fullscreen mode 1. Defeating Terminal Pager Locks The single most common reason AI terminal agents lock up is the default behavior of Linux command-line tools invoking pagers. We can eliminate this by injecting non-interactive environment flags into every subprocess:

import os import subprocess def run_safe_command ( command: str, timeout_sec: int = 30 ) -> dict: env = os. environ. copy () env [ " PAGER " ] = " cat " env [ " GIT_PAGER " ] = " cat " env [ " SYSTEMD_PAGER " ] = " cat " env [ " DEBIAN_FRONTEND " ] = " noninteractive " env [ " TERM " ] = " dumb " try: res = subprocess. run ( command, shell = True, env = env, capture_output = True, text = True, timeout = timeout_sec ) return { " success ": res. returncode == 0, " stdout ": res. stdout [: 20000 ], " stderr ": res. stderr [: 10000 ] } except subprocess. TimeoutExpired: return { " success ": False, " error ": f " Task timed out after { timeout_sec } s and was terminated. " } Enter fullscreen mode Exit fullscreen mode 2.

Safe, Bounded Log Tailing (Zero Memory Overload) Rather than opening a 2GB log file into RAM, we seek backwards from the end of the file, slice the last N lines, and filter with regex:

import os import re def tail_recent_logs ( filepath: str, max_lines: int = 50, pattern: str = None ) -> list [ str ]: file_size = os. path. getsize ( filepath ) with open ( filepath, " r ", encoding = " utf-8 ", errors = " replace " ) as f: if file_size > 100 * 1024: f. seek ( max ( 0, file_size - ( max_lines * 500 ))) f. readline () # Discard partial line lines = [ l. strip () for l in f. readlines ()[ - max_lines:]] if pattern: regex = re. compile ( pattern, re. IGNORECASE ) lines = [ l for l in lines if regex. search ( l )] return lines Enter fullscreen mode Exit fullscreen mode 3. Auditing User-Level Systemd Daemons AI agents should not have unrestricted sudo access to root-level system daemons. Instead, SysFlow inspects systemd --user units:

import subprocess def list_failed_services () -> list [ str ]: cmd = [ " systemctl ", " --user ", " list-units ", " --type=service ", " --state=failed ", " --no-pager ", " --no-legend " ] res = subprocess. run ( cmd, capture_output = True, text = True, timeout = 5 ) return [ line. split ()[ 0 ]. replace ( " ● ", "" ). strip () for line in res. stdout. strip (). split ( " \n " ) if line ] Enter fullscreen mode Exit fullscreen mode 4. Configuring Claude Desktop & Cursor Connect the server in your claude_desktop_config.json:

{ "mcpServers": { "sysflow": { "command": "python", "args": [ "-m", "sysflow.server" ], "cwd": "/path/to/sysflow-mcp" } } } Enter fullscreen mode Exit fullscreen mode Now you can instruct Claude:

"Inspect my system CPU and memory load, check if any user-level systemd background services have failed, and tail the last 30 lines of my project log."

When orchestrating background automations, build tasks, and internal containers, the Enterprise Pro edition includes:

Quelle: dev.to (dev.to). Original öffnen