Skip to main content
Glama

get_coordinates

Retrieve geographic coordinates for any Wikipedia article by providing its title, enabling location-based data extraction from the platform.

Instructions

Get the coordinates of a Wikipedia article.

Returns a dictionary containing coordinate information.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYes

Implementation Reference

  • Core handler logic for fetching coordinates from Wikipedia API using prop=coordinates query.
    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),
            }
  • MCP tool registration via @server.tool() decorator and thin wrapper handler that delegates to WikipediaClient.get_coordinates.
    @server.tool()
    def get_coordinates(title: str) -> Dict[str, Any]:
        """
        Get the coordinates of a Wikipedia article.
    
        Returns a dictionary containing coordinate information.
        """
        logger.info(f"Tool: Getting coordinates for: {title}")
        coordinates = wikipedia_client.get_coordinates(title)
        return coordinates
  • Optional caching decorator applied to get_coordinates method when enable_cache=True.
    self.get_coordinates = functools.lru_cache(maxsize=128)(self.get_coordinates)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Rudra-ravi/wikipedia-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server