get_ip_hostname
Retrieve the hostname associated with an IP address or get the current system's hostname when no IP is specified.
Instructions
Get just the hostname for an IP address.
Args: ip: IP address to lookup. If None, returns current hostname.
Returns: Hostname.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ip | No |
Implementation Reference
- src/mcp_ipinfo/server.py:498-517 (handler)The handler function for the 'get_ip_hostname' tool, decorated with @mcp.tool() for registration. It fetches the hostname for a given IP address (or current IP if none provided) using the IPInfoClient, handling API errors appropriately.@mcp.tool() async def get_ip_hostname(ctx: Context[Any, Any, Any], ip: str | None = None) -> str: """Get just the hostname for an IP address. Args: ip: IP address to lookup. If None, returns current hostname. Returns: Hostname. """ client = get_client(ctx) try: if ip: return await client.get_hostname_by_ip(ip) else: return await client.get_current_hostname() except IPInfoAPIError as e: ctx.error(f"API error: {e.message}") raise
- src/mcp_ipinfo/server.py:498-498 (registration)The @mcp.tool() decorator registers the get_ip_hostname function as an MCP tool.@mcp.tool()
- src/mcp_ipinfo/server.py:29-38 (helper)Helper function to get or initialize the shared IPInfoClient instance, used by get_ip_hostname and other tools.def get_client(ctx: Context[Any, Any, Any]) -> IPInfoClient: """Get or create the API client instance.""" global _client if _client is None: api_token = os.environ.get("IPINFO_API_TOKEN") if not api_token: ctx.warning("IPINFO_API_TOKEN is not set - some features may be limited") _client = IPInfoClient(api_token=api_token) return _client