locate_ip
Find geographic location details including province, city, district, and coordinates for any IP address using Amap's geolocation service.
Instructions
获取用户的 IP 地址定位信息,返回省市区经纬度等信息。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ip | No | 用户的ip地址 |
Implementation Reference
- src/build_mcp/services/server.py:59-80 (handler)MCP tool handler for 'locate_ip'. Includes registration decorator, input schema via Annotated Field, docstring, logging, delegation to GdSDK.locate_ip, error handling, and ApiResponse wrapping. Note: potential bug on line 74 missing 'return'.@mcp.tool(name="locate_ip", description="获取用户的 IP 地址定位信息,返回省市区经纬度等信息。") async def locate_ip(ip: Annotated[Optional[str], Field(description="用户的ip地址")] = None) -> ApiResponse: """ 根据 IP 地址定位位置。 Args: ip (str): 要定位的 IP 地址。 Returns: dict: 包含定位结果的字典。 """ logger.info(f"Locating IP: {ip}") try: result = await sdk.locate_ip(ip) if not result: ApiResponse.fail("定位结果为空,请检查日志,系统异常请检查相关日志,日志默认路径为/var/log/build_mcp。") logger.info(f"Locate IP result: {result}") return ApiResponse.ok(data=result, meta={"ip": ip}) except Exception as e: logger.error(f"Error locating IP {ip}: {e}") return ApiResponse.fail(str(e))
- Input schema and type annotation for the 'locate_ip' tool parameter using Pydantic's Annotated and Field.async def locate_ip(ip: Annotated[Optional[str], Field(description="用户的ip地址")] = None) -> ApiResponse:
- src/build_mcp/services/server.py:59-59 (registration)Registration of the 'locate_ip' tool using the @mcp.tool decorator with name and description.@mcp.tool(name="locate_ip", description="获取用户的 IP 地址定位信息,返回省市区经纬度等信息。")
- Core helper implementation of IP location in GdSDK class. Constructs API request to Amap's /v3/ip endpoint, uses shared _request_with_retry for HTTP call with retries, checks status, and returns result or None.async def locate_ip(self, ip: str = None) -> Any | None: """ IP定位接口 https://lbs.amap.com/api/webservice/guide/api/ipconfig Args: ip (str, optional): 要查询的 IP,若为空,则使用请求方公网 IP。 Returns: dict: 定位结果,若失败则返回 None。 """ url = f"{self.base_url}/v3/ip" params = { "key": self.api_key, } if ip: params["ip"] = ip result = await self._request_with_retry( method="GET", url=url, params=params ) if result and result.get("status") == "1": return result else: self.logger.error(f"IP定位失败: {result}") return None