ping
Test host reachability and measure round-trip times to diagnose network connectivity issues.
Instructions
Ping a host and return reachability and round-trip times.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | ||
| count | No | ||
| timeout | No |
Implementation Reference
- src/mcp_nettools/server.py:19-35 (handler)The ping tool handler: decorated with @mcp.tool(), executes the system ping command via subprocess, returning reachability status and output.
@mcp.tool() def ping(host: str, count: int = 4, timeout: int = 5) -> dict: """Ping a host and return reachability and round-trip times.""" try: result = subprocess.run( ["ping", "-c", str(count), "-W", str(timeout), host], capture_output=True, text=True, timeout=timeout * count + 5, ) return { "host": host, "reachable": result.returncode == 0, "output": result.stdout, } except Exception as e: return {"error": str(e), "tool": "ping", "host": host} - src/mcp_nettools/server.py:19-19 (registration)Registration: The @mcp.tool() decorator on the ping function registers it as an MCP tool.
@mcp.tool() - src/mcp_nettools/server.py:20-20 (schema)Input schema: The function signature defines typed parameters (host: str, count: int, timeout: int) with defaults.
def ping(host: str, count: int = 4, timeout: int = 5) -> dict: - src/mcp_nettools/server.py:35-36 (helper)Error handling: returns a dict with error info and tool name for any exception during ping execution.
return {"error": str(e), "tool": "ping", "host": host}