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.
npx -y @QuentinCody/wikidata-sparql-mcp-serverOverview
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):
# set configuration (example)
# run
Run with Docker:
# build
# run with environment vars
Common environment variables
| Variable | Default | Description |
|---|---|---|
| SPARQL_ENDPOINT | https://query.wikidata.org/sparql | Wikidata SPARQL endpoint URL |
| PORT | 8080 | HTTP listen port |
| CACHE_TTL | 3600 | Cache time-to-live in seconds |
| MAX_RESULTS | 50 | Max rows returned per query |
| LOG_LEVEL | info | Server 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
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):
Returned context can include English label, description, notable properties (instance of, occupation) and a short list of related items for disambiguation.
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.
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.
Data validation and provenance checks
- Fetch claim values and their references to validate facts surfaced by models or to supply provenance alongside generated content.
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:
Adjust templates and formatting in the repository to match the exact MCP consumer