get_coordinates
Retrieve geographic coordinates of a Wikipedia article by providing the article title. Use this tool to extract location data directly from Wikipedia for enhanced context in applications.
Instructions
Get the coordinates of a Wikipedia article.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes |
Implementation Reference
- wikipedia_mcp/server.py:158-163 (handler)MCP tool handler for get_coordinates, registers the tool and delegates execution to WikipediaClient.get_coordinates(title).@server.tool() def get_coordinates(title: str) -> Dict[str, Any]: """Get the coordinates of a Wikipedia article.""" logger.info(f"Tool: Getting coordinates for: {title}") coordinates = wikipedia_client.get_coordinates(title) return coordinates
- Core helper function in WikipediaClient that implements the logic to fetch coordinates from Wikipedia API using prop=coordinates, processes the response, handles errors and non-existent pages.def get_coordinates(self, title: str) -> Dict[str, Any]: """Get the coordinates of a Wikipedia article. Args: title: The title of the Wikipedia article. Returns: A dictionary containing the coordinates information. """ params = { "action": "query", "format": "json", "prop": "coordinates", "titles": title, } # Add variant parameter if needed params = self._add_variant_to_params(params) try: response = requests.get(self.api_url, headers=self._get_request_headers(), params=params) response.raise_for_status() data = response.json() pages = data.get("query", {}).get("pages", {}) if not pages: return { "title": title, "coordinates": None, "exists": False, "error": "No page found", } # Get the first (and typically only) page page_data = next(iter(pages.values())) # Check if page exists (pageid > 0 means page exists) if page_data.get("pageid", -1) < 0: return { "title": title, "coordinates": None, "exists": False, "error": "Page does not exist", } coordinates = page_data.get("coordinates", []) if not coordinates: return { "title": page_data.get("title", title), "pageid": page_data.get("pageid"), "coordinates": None, "exists": True, "error": None, "message": "No coordinates available for this article", } # Process coordinates - typically there's one primary coordinate processed_coordinates = [] for coord in coordinates: processed_coordinates.append( { "latitude": coord.get("lat"), "longitude": coord.get("lon"), "primary": coord.get("primary", False), "globe": coord.get("globe", "earth"), "type": coord.get("type", ""), "name": coord.get("name", ""), "region": coord.get("region", ""), "country": coord.get("country", ""), } ) return { "title": page_data.get("title", title), "pageid": page_data.get("pageid"), "coordinates": processed_coordinates, "exists": True, "error": None, } except Exception as e: logger.error(f"Error getting coordinates for Wikipedia article: {e}") return { "title": title, "coordinates": None, "exists": False, "error": str(e), }
- wikipedia_mcp/server.py:158-163 (registration)Registration of the get_coordinates tool via @server.tool() decorator in the FastMCP server.@server.tool() def get_coordinates(title: str) -> Dict[str, Any]: """Get the coordinates of a Wikipedia article.""" logger.info(f"Tool: Getting coordinates for: {title}") coordinates = wikipedia_client.get_coordinates(title) return coordinates