search_entity
Retrieve a Wikidata entity ID by submitting a specific query, enabling quick identification of unique entities within the Wikidata database.
Instructions
Search for a Wikidata entity ID by its query.
Args:
query (str): The query to search for. The query should be unambiguous enough to uniquely identify the entity.
Returns:
str: The Wikidata entity ID corresponding to the given query."
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- src/server.py:38-49 (handler)The handler function for the 'search_entity' tool. It is registered via the @server.tool() decorator and implements the core logic by delegating to the search_wikidata helper. Includes type hints (schema) for input 'query: str' and output 'str', plus detailed docstring.@server.tool() async def search_entity(query: str) -> str: """ Search for a Wikidata entity ID by its query. Args: query (str): The query to search for. The query should be unambiguous enough to uniquely identify the entity. Returns: str: The Wikidata entity ID corresponding to the given query." """ return await search_wikidata(query, is_entity=True)
- src/server.py:12-35 (helper)Supporting helper function that performs the HTTP request to Wikidata API to search for entity or property IDs based on the query.async def search_wikidata(query: str, is_entity: bool = True) -> str: """ Search for a Wikidata item or property ID by its query. """ params = { "action": "query", "list": "search", "srsearch": query, "srnamespace": 0 if is_entity else 120, "srlimit": 1, # TODO: add a parameter to limit the number of results? "srqiprofile": "classic_noboostlinks" if is_entity else "classic", "srwhat": "text", "format": "json", } async with httpx.AsyncClient() as client: response = await client.get(WIKIDATA_URL, headers=HEADER, params=params) response.raise_for_status() try: title = response.json()["query"]["search"][0]["title"] title = title.split(":")[-1] return title except KeyError: return "No results found. Consider changing the search term."