WI

Wikidata SPARQL MCP Server for Semantic Data Access

Query Wikidata via an MCP server to access structured knowledge, entity relationships and semantic SPARQL data for research and analysis.

Quick Install
npx -y @QuentinCody/wikidata-sparql-mcp-server

Overview

This MCP (Model Context Protocol) server provides a lightweight bridge between large language models and Wikidata’s structured knowledge by exposing SPARQL query results as retrievable context blocks. Instead of embedding raw SPARQL calls inside your application or LLM prompts, the server executes queries against the Wikidata Query Service, formats results, and serves them via an MCP-compatible endpoint so models or tools can request entity-centric context on demand.

This arrangement is useful for research, semantic search, knowledge augmentation and any workflow that needs up-to-date, structured facts (labels, descriptions, statements, and relationships) from Wikidata. By centralizing query logic, caching, and formatting, the server simplifies integration with LLMs and downstream analyzers while avoiding direct SPARQL handling inside each client.

Features

  • Query Wikidata Query Service (Wikidata SPARQL) and return structured results as MCP context blocks
  • Predefined SPARQL templates for common entity lookups (labels, descriptions, claims, relationships)
  • Caching layer to reduce repeated requests to the public SPARQL endpoint
  • Simple REST API compatible with MCP-style clients
  • Docker image and local development support (uvicorn)
  • Configurable pagination, timeouts and result limits
  • Optional rate limiting and request logging for production deployments

Installation / Configuration

Clone the repository and run locally or in Docker.

Clone and run locally (Python + uvicorn):

git clone https://github.com/QuentinCody/wikidata-sparql-mcp-server.git
cd wikidata-sparql-mcp-server
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# set configuration (example)
export SPARQL_ENDPOINT="https://query.wikidata.org/sparql"
export PORT=8080
export CACHE_TTL=3600

# run
uvicorn app:app --host 0.0.0.0 --port ${PORT}

Run with Docker:

# build
docker build -t wikidata-mcp-server .

# run with environment vars
docker run -p 8080:8080 \
  -e SPARQL_ENDPOINT="https://query.wikidata.org/sparql" \
  -e CACHE_TTL=3600 \
  wikidata-mcp-server

Common environment variables

VariableDefaultDescription
SPARQL_ENDPOINThttps://query.wikidata.org/sparqlWikidata SPARQL endpoint URL
PORT8080HTTP listen port
CACHE_TTL3600Cache time-to-live in seconds
MAX_RESULTS50Max rows returned per query
LOG_LEVELinfoServer logging level

Adjust these values for heavier workloads or private deployments.

Available Resources

  • SPARQL query templates: included in the repo under templates/ — use or extend for custom entity views
  • OpenAPI / Swagger: if enabled, visit /docs or /openapi.json when the server is running to inspect endpoints
  • Dockerfile and example compose file for quick deployments
  • Example client snippets (curl, Python) in examples/ to get started faster
  • GitHub repository: https://github.com/QuentinCody/wikidata-sparql-mcp-server

Use Cases

  1. Augment LLM prompts with verified entity facts

    • Problem: An LLM formulates text about an entity but lacks structured facts or up-to-date relationships.
    • Solution: Request the MCP server for the entity’s label, description, and top claims; include those blocks in the prompt as authoritative context.

    Example (cURL):

    curl -X POST "http://localhost:8080/mcp/context" \
      -H "Content-Type: application/json" \
      -d '{"entity":"Q42","view":"entity_basic"}'
    

    Returned context can include English label, description, notable properties (instance of, occupation) and a short list of related items for disambiguation.

  2. Semantic search and entity-aware retrieval

    • Use the server to produce structured context for candidate entities returned by a text search pipeline, enabling more precise ranking or relationship-based filtering.
  3. Knowledge graph exploration and visualization

    • Pull statements and relationships for a set of entities and feed them into a graph visualizer. The MCP server’s formatted outputs simplify transformation to nodes/edges.
  4. Data validation and provenance checks

    • Fetch claim values and their references to validate facts surfaced by models or to supply provenance alongside generated content.
  5. Research and analytics

    • Batch requests to gather properties or counts (e.g., number of instances for a class) for statistical analysis; use cached results to avoid overloading the public SPARQL endpoint.

Example response structure

A typical MCP-style response (simplified) contains a list of context blocks with metadata:

{
  "source": "wikidata",
  "entity": "Q42",
  "contexts": [
    {
      "id": "Q42-basic",
      "type": "entity_summary",
      "language": "en",
      "content": "Douglas Adams — English author best known for 'The Hitchhiker’s Guide to the Galaxy'..."
    },
    {
      "id": "Q42-claims",
      "type": "claims",
      "content": [
        {"property": "P31", "propertyLabel": "instance of", "value": "Q5 (human)"},
        {"property": "P106", "propertyLabel": "occupation", "value": "Q36180 (writer)"}
      ]
    }
  ]
}

Adjust templates and formatting in the repository to match the exact MCP consumer

Tags:search