get_ip_city
Retrieve the city location for any IP address. When no IP is specified, it returns the city of your current connection. This tool provides quick geolocation data for IP analysis.
Instructions
Get just the city for an IP address.
Args: ip: IP address to lookup. If None, returns current city.
Returns: City name.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ip | No |
Implementation Reference
- src/mcp_ipinfo/server.py:372-390 (handler)The MCP tool handler for 'get_ip_city'. Registers the tool via @mcp.tool() decorator and implements the logic by using get_client() to fetch IP city via IPInfoClient, handling both specific IP and current IP.@mcp.tool() async def get_ip_city(ctx: Context[Any, Any, Any], ip: str | None = None) -> str: """Get just the city for an IP address. Args: ip: IP address to lookup. If None, returns current city. Returns: City name. """ client = get_client(ctx) try: if ip: return await client.get_city_by_ip(ip) else: return await client.get_current_city() except IPInfoAPIError as e: ctx.error(f"API error: {e.message}") raise
- src/mcp_ipinfo/server.py:29-37 (helper)Helper function used by get_ip_city to lazily initialize and retrieve the shared IPInfoClient instance.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
- src/mcp_ipinfo/api_client.py:343-346 (helper)Core helper method in IPInfoClient that performs the actual API request to retrieve city for a specific IP address.async def get_city_by_ip(self, ip: str) -> str: """Get city for an IP.""" data = await self._request("GET", f"/{ip}/city") return str(data.get("result", ""))
- src/mcp_ipinfo/api_client.py:337-341 (helper)Core helper method in IPInfoClient that performs the API request to retrieve city for the current IP.async def get_current_city(self) -> str: """Get current city.""" data = await self._request("GET", "/city") return str(data.get("result", ""))
- src/mcp_ipinfo/server.py:372-372 (registration)The @mcp.tool() decorator registers the get_ip_city function as an MCP tool.@mcp.tool()