Skip to main content
Glama

get_relations

Extract incoming, outgoing, or all relations for a Wikidata entity, filtered by specific properties, with customizable result limits.

Instructions

Get relations of a Wikidata entity

Input Schema

NameRequiredDescriptionDefault
entity_idYesWikidata entity ID
limitNoMaximum number of relations
property_filterNoFilter by specific properties
relation_typeNoType of relations to retrieveoutgoing

Input Schema (JSON Schema)

{ "properties": { "entity_id": { "description": "Wikidata entity ID", "type": "string" }, "limit": { "default": 20, "description": "Maximum number of relations", "maximum": 100, "type": "integer" }, "property_filter": { "description": "Filter by specific properties", "items": { "type": "string" }, "type": "array" }, "relation_type": { "default": "outgoing", "description": "Type of relations to retrieve", "enum": [ "incoming", "outgoing", "all" ], "type": "string" } }, "required": [ "entity_id" ], "type": "object" }

Implementation Reference

  • The main handler function that implements the get_relations tool. It constructs a SPARQL query based on relation_type (outgoing or incoming), executes it via sparql_query, processes the results into a structured list of relations, and returns them.
    async def get_relations( self, entity_id: str, relation_type: str = "outgoing", property_filter: Optional[List[str]] = None, limit: int = 20 ) -> Dict[str, Any]: if relation_type == "outgoing": query = f""" SELECT ?property ?propertyLabel ?target ?targetLabel WHERE {{ wd:{entity_id} ?property ?target . ?prop wikibase:directClaim ?property . SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en" . }} }} LIMIT {limit} """ elif relation_type == "incoming": query = f""" SELECT ?property ?propertyLabel ?source ?sourceLabel WHERE {{ ?source ?property wd:{entity_id} . ?prop wikibase:directClaim ?property . SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en" . }} }} LIMIT {limit} """ else: raise ValueError(f"Invalid relation_type: {relation_type}") result = await self.sparql_query(query) relations = [] for binding in result.get("results", {}).get("bindings", []): relation = { "property": binding.get("property", {}).get("value", "").split("/")[-1], "property_label": binding.get("propertyLabel", {}).get("value", ""), "direction": relation_type } if relation_type == "outgoing": relation["target"] = { "id": binding.get("target", {}).get("value", "").split("/")[-1], "label": binding.get("targetLabel", {}).get("value", "") } else: relation["source"] = { "id": binding.get("source", {}).get("value", "").split("/")[-1], "label": binding.get("sourceLabel", {}).get("value", "") } relations.append(relation) return {"relations": relations}
  • Registration of the get_relations tool in get_tool_definitions(), including the tool name, description, and input schema.
    name="get_relations", description="Get relations of a Wikidata entity", inputSchema={ "type": "object", "properties": { "entity_id": { "type": "string", "description": "Wikidata entity ID" }, "relation_type": { "type": "string", "description": "Type of relations to retrieve", "enum": ["incoming", "outgoing", "all"], "default": "outgoing" }, "property_filter": { "type": "array", "items": {"type": "string"}, "description": "Filter by specific properties" }, "limit": { "type": "integer", "description": "Maximum number of relations", "default": 20, "maximum": 100 } }, "required": ["entity_id"] } ),
  • Dispatch logic in the call_tool method that calls the WikidataClient's get_relations handler when the tool name matches.
    elif name == "get_relations": result = await self.client.get_relations(**arguments) elif name == "find_by_property": result = await self.client.find_by_property(**arguments)
  • Input schema definition for the get_relations tool, specifying parameters like entity_id, relation_type, property_filter, and limit.
    inputSchema={ "type": "object", "properties": { "entity_id": { "type": "string", "description": "Wikidata entity ID" }, "relation_type": { "type": "string", "description": "Type of relations to retrieve", "enum": ["incoming", "outgoing", "all"], "default": "outgoing" }, "property_filter": { "type": "array", "items": {"type": "string"}, "description": "Filter by specific properties" }, "limit": { "type": "integer", "description": "Maximum number of relations", "default": 20, "maximum": 100 } }, "required": ["entity_id"] }

Other Tools

Related 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