This article covers Set Up an MCP Server So in detail. AI assistants are excellent at reasoning but blind to your infrastructure: they cannot see your disk usage, your failed services or your firewall rules — unless you give them tools. That is exactly what an MCP server does. The Model Context Protocol (MCP) is an open standard that lets AI applications call external tools in a safe, structured way. In this guide you will build a small MCP server in Python that exposes read-only server diagnostics, and connect an AI assistant to it.
How an MCP Server Works – Set Up an MCP Server So
MCP has three parts:
- Host: the AI application you interact with (for example Claude Desktop, or an MCP-capable editor).
- Client: the connector inside the host that speaks the protocol.
- MCP server: your program, which offers tools (functions the model can call), resources (data it can read) and prompts.
When you ask “why is the disk full?”, the model decides to call your disk_usage tool, your MCP server runs the check, and the answer comes back grounded in real data instead of guesses. Communication happens over stdio or HTTP using JSON-RPC messages.
Prerequisites – Set Up an MCP Server So
- A Linux machine (server or workstation) with Python 3.10+.
- An MCP-capable AI application to connect with.
- Optional: a local LLM setup such as Ollama for the assistant side.
- Basic Python knowledge.
Safety first: an MCP server executes commands on a real machine. Start with read-only, allowlisted tools. Never expose a generic “run any shell command” tool — that is remote code execution with extra steps.
Step 1: Install the MCP Python SDK
sudo apt install python3-venv -y
mkdir -p ~/mcp-server && cd ~/mcp-server
python3 -m venv .venv
source .venv/bin/activate
pip install "mcp[cli]"
Step 2: Write a Read-Only MCP Server for Diagnostics
Using FastMCP from the official SDK, each tool is just a decorated Python function. Notice the design: fixed commands, no user-controlled input reaching the shell.
# server.py
import subprocess
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("sysadmin-tools")
def run(cmd: list[str]) -> str:
out = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
return (out.stdout + out.stderr)[:4000]
@mcp.tool()
def disk_usage() -> str:
"""Show filesystem usage for all mounted filesystems."""
return run(["df", "-h"])
@mcp.tool()
def failed_services() -> str:
"""List failed systemd units."""
return run(["systemctl", "--failed", "--no-pager"])
@mcp.tool()
def memory_status() -> str:
"""Show memory and swap usage."""
return run(["free", "-h"])
@mcp.tool()
def top_processes() -> str:
"""Show the top 10 processes by memory usage."""
return run(["ps", "aux", "--sort=-%mem"])
@mcp.tool()
def recent_errors(minutes: int = 30) -> str:
"""Show error-priority journal entries from the last N minutes (max 120)."""
minutes = max(1, min(minutes, 120))
return run(["journalctl", "-p", "err", "--since", f"{minutes} minutes ago", "--no-pager"])
if __name__ == "__main__":
mcp.run(transport="stdio")
Test it with the MCP Inspector before connecting any AI app:
mcp dev server.py

Step 3: Connect Your AI Application to the MCP Server
Most hosts read a JSON configuration listing the servers to launch. Add an entry pointing at your script:
{
"mcpServers": {
"sysadmin-tools": {
"command": "/home/youruser/mcp-server/.venv/bin/python",
"args": ["/home/youruser/mcp-server/server.py"]
}
}
}
Restart the host application and ask: “Check this machine: is anything wrong?” The assistant will call failed_services, disk_usage and recent_errors, then summarize — the same workflow as the automation patterns in our AI agents guide, but interactive and under your control.
Step 4: Harden the MCP Server
- Run it as an unprivileged user with only the sudo rights it truly needs (ideally none).
- Allowlist everything: fixed commands, validated numeric ranges, no string interpolation into shell commands — use argument lists, never
shell=True. - Truncate output (the 4,000-character cap above) so a huge log cannot flood the model’s context.
- Add timeouts to every call so a hung command cannot block the assistant.
- Log tool calls to a file, so you can audit exactly what the AI asked your server to do.
- Keep write actions separate: if you later add a “restart service” tool, put it in a different server you only enable during maintenance windows.
Troubleshooting
The host does not see the tools. Check the JSON config path and use absolute paths for the Python binary and script — hosts do not inherit your shell’s PATH or virtualenv.
The server starts then exits. Run python server.py manually in a terminal; import errors and tracebacks appear there, not in the host’s UI.
Tool calls time out. A command is slow (journalctl over huge ranges is the usual suspect). Narrow the query, lower the default window, and keep the 15-second subprocess timeout.
The model never calls the tools. Tool descriptions matter — write them like documentation: what the tool returns, when to use it, and its limits.
Conclusion
An MCP server turns an AI assistant from a chatbot into a supervised operator’s apprentice: it can look at real system state through tools you defined, with limits you enforced. Start read-only, allowlist every command, log every call, and expand slowly. It is the cleanest architecture available today for connecting LLMs to real infrastructure — and a foundation you can reuse across every AI client your team adopts.