Skip to main content
Glama
zzaebok

Wikidata MCP Server

by zzaebok

execute_sparql

Execute SPARQL queries on Wikidata to retrieve structured data in JSON format, enabling data extraction and analysis from the knowledge base.

Instructions

Execute a SPARQL query on Wikidata.

You may assume the following prefixes:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>

Args:
    sparql_query (str): The SPARQL query to execute.

Returns:
    str: The JSON-formatted result of the SPARQL query execution. If there are no results, an empty JSON object will be returned.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sparql_queryYes

Implementation Reference

  • Handler function decorated with @server.tool(), implementing the execution of SPARQL queries on Wikidata Query Service, sending the query via HTTP GET and returning JSON-formatted results. Serves as both handler and registration.
    @server.tool()
    async def execute_sparql(sparql_query: str) -> str:
        """
        Execute a SPARQL query on Wikidata.
    
        You may assume the following prefixes:
        PREFIX wd: <http://www.wikidata.org/entity/>
        PREFIX wdt: <http://www.wikidata.org/prop/direct/>
        PREFIX p: <http://www.wikidata.org/prop/>
        PREFIX ps: <http://www.wikidata.org/prop/statement/>
    
        Args:
            sparql_query (str): The SPARQL query to execute.
    
        Returns:
            str: The JSON-formatted result of the SPARQL query execution. If there are no results, an empty JSON object will be returned.
        """
        url = "https://query.wikidata.org/sparql"
        async with httpx.AsyncClient() as client:
            response = await client.get(
                url, params={"query": sparql_query, "format": "json"}
            )
        response.raise_for_status()
        result = response.json()["results"]["bindings"]
        return json.dumps(result)

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

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