Urgent.News

What's breaking now, across thousands of outlets.

Health & Medicine

How to Build a MCP Server in Python for Linux Network Diagnostics

LLMs can explain Linux networking concepts well enough, but out of the box they have no visibility into a live machine. They cannot see your network interfaces, tell you if a WireGuard tunnel has an MTU mismatch or check if an adapter is dropping packets. Anthropic's Model Context Protocol (MCP) is a practical way to bridge that. It gives you a standard interface for exposing local or remote…

To build an MCP server in Python for Linux network diagnostics, you'll first need to set up a Python 3.12+ environment with uv installed. Then, create a new project directory, install the necessary packages including the mcp SDK, FastAPI, Uvicorn, Pydantic, and psutil. Next, define typed telemetry schemas using Pydantic v2 to structure the output of network interface metrics such as IP addresses, MTU, and packet counters.

In your main.py file, import the required modules, set up FastAPI with a title and version, and create Pydantic models for InterfaceStats and SystemNetworkReport. The InterfaceStats model will hold information about each network interface including its name, whether it's up, MTU, bytes sent and received, dropped packets, anomaly detection status, and a diagnostic note. The SystemNetworkReport model will contain a list of InterfaceStats objects and a healthy flag.

The next step is to implement the logic to inspect Linux interfaces. Use psutil.net_if_stats() to get interface statistics like the name, MTU, and whether the interface is up. Use psutil.net_io_counters() to get inbound and outbound packet counters for each interface. Implement two checks: flag any interface with more than 100 total dropped packets, and flag virtual tunnel interfaces (tun* or wg*) with an MTU above 1420.

Append the diagnostic results to a list of InterfaceStats objects and return a SystemNetworkReport containing the list of interface stats and the overall system health.

Finally, use the mcp.tool() decorator to expose the collect_interface_diagnostics() function to both MCP clients and FastAPI, allowing clients to retrieve the network diagnostics in a structured format. This MCP server will provide visibility into live Linux network interfaces, helping diagnose common network issues such as MTU mismatches and packet drops.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Health & Medicine

Why Royalty Pharma Investors Could Be the Quiet Winners From Revolution Medicines' Groundbreaking Pancreatic Cancer Drug

Revolution Medicines has a big drug making headlines, and Royalty Pharma gets a bite of the action without doing anything at all.

  • Royalty Pharma invested in Revolution Medicines for potential royalties.
  • Revolution Medicines' pancreatic cancer drug daraxonraib received FDA approval.
  • Royalty Pharma offers stable, diversified revenue stream with 35 approved patents.

More from Sunday 8 November →