QuestionQ23

AI in Automation

An engineer needs to give an AI agent reverse-DNS capability so it can map IP addresses to hostnames when summarizing incidents. The engineer must implement a FastMCP tool that resolves PTR records with socket.gethostbyaddr and expose it through stdio for MCP clients. Construct the artifact by dragging the code snippets into the boxes. Not all options are used.

Drag & Drop
from mcp.server.fastmcp import 
import socket

mcp = FastMCP("dns-tools")

@mcp.()
def reverse_dns(ip: str) -> str:
    """Return primary hostname for an IP address (PTR)."""
    try:
        name, _, _ = socket.gethostbyaddr(ip)
        return name
    except Exception as e:
        return f"lookup failed: {e}"

if __name__ == "__main__":
    mcp.(="stdio")
Explanation

FastMCP is the server class, @mcp.tool() registers the function as an MCP tool, and mcp.run(transport="stdio") starts the server using the standard-input/output transport required by local MCP clients. socket.gethostbyaddr(ip) performs the reverse hostname lookup used for PTR resolution.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!