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)
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It describes the return format (JSON-formatted result) and edge case behavior (empty JSON object for no results), which is valuable. However, it doesn't mention rate limits, authentication requirements, timeout behavior, or query complexity constraints that would be important for a query execution tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is efficiently structured with a clear purpose statement, helpful prefix information, and well-organized parameter/return documentation. Every sentence serves a distinct purpose with zero wasted words, making it easy to parse and understand quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a single-parameter query execution tool with no annotations or output schema, the description provides good coverage of purpose, parameter meaning, and return behavior. It could be more complete by addressing authentication, rate limits, or error handling, but it covers the essential aspects well given the tool's complexity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for the single parameter, the description fully compensates by clearly explaining what the sparql_query parameter should contain. It provides context about assumed prefixes and specifies it's a SPARQL query for Wikidata, adding significant meaning beyond what the bare schema provides.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Execute a SPARQL query') and target resource ('on Wikidata'), distinguishing it from sibling tools like get_metadata or search_entity. It provides a complete verb+resource+scope statement that leaves no ambiguity about what the tool does.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides implicit usage guidance by specifying the target (Wikidata) and listing assumed prefixes, which helps understand when this tool is appropriate. However, it doesn't explicitly state when to use this versus alternatives like search_entity or get_properties, nor does it mention any prerequisites or exclusions for SPARQL query execution.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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

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