get_ip_country
Retrieve the two-letter country code for any IP address using IP geolocation. Specify an IP to get its country or omit to identify your current location.
Instructions
Get just the country code for an IP address.
Args: ip: IP address to lookup. If None, returns current country.
Returns: Two-letter country code (ISO-3166).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ip | No |
Implementation Reference
- src/mcp_ipinfo/server.py:393-412 (handler)The main handler function for the 'get_ip_country' tool. It is registered via the @mcp.tool() decorator. The function retrieves the country code for a given IP address (or the current IP if none provided) using the IPInfoClient, handling API errors appropriately.@mcp.tool() async def get_ip_country(ctx: Context[Any, Any, Any], ip: str | None = None) -> str: """Get just the country code for an IP address. Args: ip: IP address to lookup. If None, returns current country. Returns: Two-letter country code (ISO-3166). """ client = get_client(ctx) try: if ip: return await client.get_country_by_ip(ip) else: return await client.get_current_country() except IPInfoAPIError as e: ctx.error(f"API error: {e.message}") raise