Skip to main content
Glama
joelgombin

MCP Wikidata Server

by joelgombin

get_entity

Retrieve detailed information about Wikidata entities using entity IDs, with options to specify language, properties, and output format.

Instructions

Get detailed information about a Wikidata entity

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
entity_idYesWikidata entity ID (Q123, P456)
languageNoLanguage code (default: en)en
propertiesNoSpecific properties to include
simplifiedNoReturn simplified format (default: false)

Implementation Reference

  • Main handler function that executes the Wikidata API request to retrieve entity details using wbgetentities action, handles optional properties and simplification.
    async def get_entity(
        self,
        entity_id: str,
        language: str = "en",
        properties: Optional[List[str]] = None,
        simplified: bool = False
    ) -> Dict[str, Any]:
        params = {
            "action": "wbgetentities",
            "ids": entity_id,
            "languages": language,
            "format": "json",
        }
    
        if properties:
            params["props"] = "|".join(properties)
    
        data = await self._make_request(self.config.wikibase_api_url, params)
        
        if "entities" not in data or entity_id not in data["entities"]:
            raise ValueError(f"Entity {entity_id} not found")
    
        entity_data = data["entities"][entity_id]
        
        if simplified:
            return self._simplify_entity(entity_data, language)
        
        return {"entity": entity_data}
  • Helper function to simplify the entity data structure for easier consumption, extracting labels, descriptions, and property values.
    def _simplify_entity(self, entity_data: Dict[str, Any], language: str) -> Dict[str, Any]:
        entity = {
            "id": entity_data.get("id"),
            "labels": {},
            "descriptions": {},
            "properties": {}
        }
    
        if "labels" in entity_data:
            entity["labels"] = {
                lang: label["value"] 
                for lang, label in entity_data["labels"].items()
            }
    
        if "descriptions" in entity_data:
            entity["descriptions"] = {
                lang: desc["value"] 
                for lang, desc in entity_data["descriptions"].items()
            }
    
        if "claims" in entity_data:
            for prop_id, claims in entity_data["claims"].items():
                prop_values = []
                for claim in claims:
                    if "mainsnak" in claim and "datavalue" in claim["mainsnak"]:
                        value = claim["mainsnak"]["datavalue"]["value"]
                        if isinstance(value, dict) and "id" in value:
                            prop_values.append({
                                "value": value["id"],
                                "label": value.get("label", value["id"])
                            })
                        else:
                            prop_values.append({"value": str(value)})
                
                if prop_values:
                    entity["properties"][prop_id] = prop_values
    
        return {"entity": entity}
  • Registration of the 'get_entity' tool in the MCP tools list, including description and input schema.
    Tool(
        name="get_entity",
        description="Get detailed information about a Wikidata entity",
        inputSchema={
            "type": "object",
            "properties": {
                "entity_id": {
                    "type": "string",
                    "description": "Wikidata entity ID (Q123, P456)"
                },
                "language": {
                    "type": "string",
                    "description": "Language code (default: en)",
                    "default": "en"
                },
                "properties": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Specific properties to include"
                },
                "simplified": {
                    "type": "boolean",
                    "description": "Return simplified format (default: false)",
                    "default": False
                }
            },
            "required": ["entity_id"]
        }
    ),
  • Input schema definition for the 'get_entity' tool, specifying parameters like entity_id (required), language, properties, and simplified.
    inputSchema={
        "type": "object",
        "properties": {
            "entity_id": {
                "type": "string",
                "description": "Wikidata entity ID (Q123, P456)"
            },
            "language": {
                "type": "string",
                "description": "Language code (default: en)",
                "default": "en"
            },
            "properties": {
                "type": "array",
                "items": {"type": "string"},
                "description": "Specific properties to include"
            },
            "simplified": {
                "type": "boolean",
                "description": "Return simplified format (default: false)",
                "default": False
            }
        },
        "required": ["entity_id"]
    }
  • Dispatcher in call_tool method that invokes the client.get_entity implementation based on tool name.
    elif name == "get_entity":
        result = await self.client.get_entity(**arguments)

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

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/joelgombin/mcp-wikidata'

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